先上代码
1 /** 2 * =========获取微信用户详细信息===================== 3 **/ 4 public function wechatLogin(){ 5 $param = input('param.'); 6 7 //-------配置 8 $AppID = 'wxb42ad51501111111'; 9 $AppSecret = '8b023a4ec3bfb58ecaf1b96b91111111'; 10 11 12 $my_redirect_url = input('param.my_redirect_url'); 13 $my_redirect_url = base64_encode($my_redirect_url); 14 $callback = 'http://www.j.cn'.url('ydyl/wechatCllBack',['my_redirect_url'=>$my_redirect_url]); 15 16 17 //微信登录 18 //---生成唯一随机串防CSRF攻击 19 $state = md5(uniqid(rand(), TRUE)); 20 $_SESSION["wx_state"] = $state; //存到SESSION snsapi_userinfo snsapi_base 21 $callback = urlencode($callback); 22 $wxurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$AppID."&redirect_uri=".$callback."&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect"; 23 24 header("Location: $wxurl"); 25 26 } 27 //微信登录回调函数 28 public function wechatCllBack() { 29 30 31 $AppID = 'wxb42ad51501111111'; 32 $AppSecret = '8b023a4ec3bfb58ecaf1b96b91111111'; 33 34 35 $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$AppID.'&secret='.$AppSecret.'&code='.$_GET['code'].'&grant_type=authorization_code'; 36 $ch = curl_init(); 37 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 38 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 39 curl_setopt($ch, CURLOPT_URL, $url); 40 $json = curl_exec($ch); 41 curl_close($ch); 42 $arr=json_decode($json,1); 43 //得到 access_token 与 openid 44 45 $url='https://api.weixin.qq.com/sns/userinfo?access_token='.$arr['access_token'].'&openid='.$arr['openid'].'&lang=zh_CN'; 46 $ch = curl_init(); 47 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 48 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 49 curl_setopt($ch, CURLOPT_URL, $url); 50 $json = curl_exec($ch); 51 curl_close($ch); 52 $arr=json_decode($json,1); 53 $this->_login_handle($arr, input('param.my_redirect_url')); 54 55 } 56 private function _login_handle($user_info, $redirect_url){ 57 58 $user_oauth = Db('ydyl_weixin_user')->where(array("openid"=>$user_info['openid']))->find(); 59 if(empty($user_oauth)){ 60 /*二【如果用户不存在】 61 * @1添加该用户到users 62 */ 63 $nickname = base64_encode($user_info['nickname']); 64 $new_user_data = array( 65 'openid' => $user_info['openid'], 66 'nickname' => $nickname, //名称 67 'sex' => $user_info['sex'],//性别 68 'city' => $user_info['city'],//城市 69 'province' => $user_info['province'],//省份 70 'country' => $user_info['country'], //国家 71 'headimgurl' => $user_info['headimgurl'],//头像 72 'create_time' => date("Y-m-d H:i:s"), 73 ); 74 $new_user_data['id'] = Db('ydyl_weixin_user')->insertGetId($new_user_data); 75 $weixin_user_data = Db('ydyl_weixin_user')->where('id',$new_user_data['id'])->find(); 76 $user_oauth = $weixin_user_data; 77 } 78 session('ydyl_weixin_user',$user_oauth); 79 $redirect_url = 'http://'.base64_decode($redirect_url); 80 header("Location: $redirect_url"); 81 82 }
首先要说明,我都是使用URL请求跳转该方法,ajax请求试验过几次,都没成功.(我太弱了)
script里的代码也贴出来好了
<script> $.post('http://www.juplus.cn/vdma/index/ydyl/is_Login', {}, function(data) { console.log(data) var code_ = data.code if(!code_){ window.location.href = 'http://www.j.cn/vdma/index/ydyl/wechatLogin&my_redirect_url=www.j.cn/br/index.html' } }); </script>
微信的授权登录一般分三步
第一步:
请求授权
①获取需要跳转的URL,详见代码$my_redirect_url
②$callback是回调函数的路径,记得把$my_redirect_url传递下去($callback的路径是根据项目路径来得,因人而异,我的是tp5框架,所以这样写)
第二步:
执行回调函数并存数据库(基本是固定套路,不用改,复制过去就行,最后调用_login_handle()方法时,第二个实参就是跳转URL)
①存数据库时改改表明就可以了,字段名不用改.
)
第三步:
跳转到初始请求页面
差不多就这样了
有写错的请私信我,谢谢