//获取code public function get_code(){ $this->redirect_uri='http://www.e-hl.cc/phone/phone.php?r=WxLogin/wx_login_index'; // $this->redirect_uri='https://www.baidu.com'; $this->redirect_uri=urlencode($this->redirect_uri); $this->url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appId.'&redirect_uri='.$this->redirect_uri.'&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect'; // $this->url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appId.'&redirect_uri='.$this->redirect_uri.'&response_type=code&scope=snsapi_base&state=123#wechat_redirect'; header("location:{$this->url}"); }
//获取token返回 public function wx_login_index(){ // $wx = new Lib\WXOfficeAccount(); $this->get_openId(); //通过code换取网页授权access_token ,openId // $this->get_token(); //获取access_token $this->get_one_user_msg_code(); //获取用户信息. $this->client = 'WX_OA'; // var_dump($this->user_msg); $this->union_id = $this->user_msg->unionid; $this->nick_name = $this->user_msg->nickname; $this->headimgurl = $this->user_msg->headimgurl; // var_dump($this->union_id, $this->nick_name,$this->headimgurl); $this->wx_login(); $_SESSION['token']=empty($this->token)?output_error('微信登陆错误!'):$this->token; // output_data($_SESSION['token']); setcookie('token',$_SESSION['token']); setcookie('key',$_SESSION['token']); header("location:http://www.e-hl.cc/wap/templates/E_index.html?a=".$_SESSION['token']);
}
#########################js代码#################################
window.onload=function () { if (!getCookie('key')) { // document.getElementById("alink").click(); if (isWeiXin()) { //是微信端 window.location.href='http://www.e-hl.cc/phone/phone.php?r=WxLogin/get_code/'; } } }
var k=getQueryString('a')?addCookie('key',getQueryString('a')):'';
先说说我遇到的问题把。首先授权的流程是可以了,但问题是,如果前端使用AJAX的话就没有反应,所以就只能使用window.location.href来请求我写的code接口,从而持行下面的代码,自动登陆注册,及返回token值,微信苹果端会一直死循环,所以我就直接授权获取完我设置的token后就把值带在地址中,然后前端就拿到KEY值,实现登陆