namespace app\index\controller;
use app\admin\model\User;
use think\Controller;
use think\Db;
class Index extends Controller
{
public function initialize (){
//parent::__construct();
//parent::initialize();
if(session('openid')==""||session('openid')==null){
$get=input('get.');
$whereAB['id']=1;
$wechatRes=Db::name('weixin')->where($whereAB)->find();
$appid=$wechatRes['appid'];
$appsecret=$wechatRes['appsecret'];
if (!isset($get['code'])) {
//生成state 通过session验证
$state = self::getState(25, 32);
session('state',$state);
$redirect_uri = urlencode ('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$getCodeUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='. $appid .'&redirect_uri=' . $redirect_uri . '&response_type=code&scope=snsapi_userinfo&state=' . $state . '#wechat_redirect';
$this->redirect($getCodeUrl);
}else{
if ($get['state'] === session('state')){
$getAccessTokenUrl ='https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $get['code'] . '&grant_type=authorization_code';
$data = json_decode(self::curlRequest($getAccessTokenUrl), true);
if (isset($data['openid'])) {
//获取用户信息
$getUserInfoUrl = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $data['access_token'] . '&openid=' . $data['openid'] . '&lang=zh_CN';
$user_info = json_decode(self::curlRequest($getUserInfoUrl), true);
session('openid',$user_info['openid']);
session('nickname',$user_info['nickname']);
session('headimgurl',$user_info['headimgurl']);
//判断数据库是否有用户
$whereAA['openid']=session('openid');
$member=Db::name('user')->where($whereAA)->find();
$dataaa['openid']=session('openid');
$dataaa['nickname']=session('nickname');
$dataaa['headimgurl']=session('headimgurl');
if(empty($member)){
Db::name('user')->insert($dataaa);
}else{
if($member['nickname']==null||$member['nickname']==""||$member['nickname']!=session('nickname')){
Db::name('user')->where($whereAA)->update($dataaa);
}
if($member['headimgurl']==null||$member['headimgurl']==""||$member['headimgurl']!=session('headimgurl')){
Db::name('user')->where($whereAA)->update($dataaa);
}
}
}
}
}
}
}
private static function getState($minLength, $maxLength)
{
$length = mt_rand($minLength, $maxLength);
$letters = '1234567890bcdfghjklmnpqrstvwxyz';
$vowels = 'aeiou';
$code = '';
for ($i = 0; $i < $length; ++$i) {
if ($i % 2 && mt_rand(0, 10) > 2 || !($i % 2) && mt_rand(0, 10) > 9) {
$code .= $vowels[mt_rand(0, 4)];
} else {
$code .= $letters[mt_rand(0, 20)];
}
}
return $code;
}
public function curlRequest($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
//+-----------------------------------------------------------------------------------------
public function index() //首页
{
//用户信息(ID、头像、昵称)
$where['openid']=session('openid');
$userRes=Db::name('user')->where($where)->find();
$user['user_id']=$userRes['id'];
$user['nickname']=session('nickname');
$user['headimgurl']=session('headimgurl');
$this->assign('userData',$user);
return $this->fetch();
}
}
?>