静安手机网站建设,wordpress显示用户无效,晴天影视,如何学习网站建设app问题描述#xff1a; 今天通过ajax调用方式做微信静默登录#xff0c;发现本地可以跳转#xff0c;到线上地址死活都不跳转#xff0c;就像没起作用一般#xff0c;经许久排查发现#xff0c;是因为https和http域名的问题#xff0c;线上只配置了http域名#xff0…问题描述 今天通过ajax调用方式做微信静默登录发现本地可以跳转到线上地址死活都不跳转就像没起作用一般经许久排查发现是因为https和http域名的问题线上只配置了http域名所以导致https访问微信时无法实现跳转。
解决 1、配置https域名 2、换为http访问
---------------------------------------------------------------------------------------------------------------------------------
附ajax微信静默登录代码
梳理步骤
1静态页面调用
!DOCTYPE html
html langen
headmeta charsetUTF-8title测试微信静默登录/titlescript src/static/plugins/jquery-2.1.4.min.js/script
/head
bodydiv classlogin这是首页啦~~~
/div
script$(function(){$.ajax({url:/admin/system.Login/islogin,type:post,data:[],dataType:json,success:function (res) {if (res.code 200) {$(.login).html(登录成功);//alert(登录成功即将跳转到主页...);window.location.href /index;return false;} else {if(res.code 4000){// $(.login).html(a href res.data.url 跳转2res.data.url /a);$(.login).html(获取成功,即将跳转到授权页...);window.location.href res.data.url;return false;}$(.login).html(授权失败,即将跳转到登录页...);window.location.href /login;return false;}}});});
/script
/body
/html
2后端代码
第一步获取微信openid并缓存起来
接口1首页-判断用户是否已登录接口 public function islogin(){$type $_GET[type];$param[user_id] user_id(false);//获取用户登录信息$wx_openid Cache::get(wx_openid);//第一步 判断用户是否已经登录if($param[user_id]){return success($param,用户已登录);}//第二步 用户未登录 判断是否为微信内登录if(request()-isWx()) {//手机端微信//第三步 微信内用户静默授权 获取用户openid 1存在 自动登录 2不存在 跳转登录页面 去登录绑定if($wx_openid){//获取openid后的操作$model new UserModel();$userInfo $model-where(openid, $wx_openid)-find();if ($userInfo) {//已登录//设置登录成功缓存信息$loginInfo LoginService::setlogin($userInfo);return success($loginInfo, 登录成功);}}else{$url $this-getWxCode();return error(未登录,[url$url], 4000);}}return error(用户未登录);}
函数1获取微信code码方法 public function getWxCode(){//生成ticket$ticket uniqid(); // 生成唯一的ticketCache::set($ticket, , 300);//微信网页授权url$appId self::$appId;$redirect_uri urlencode(https://www.myweb.com/admin/system.Login/saveWeiXinOpenId);$scope snsapi_base;//默认静默登录if($type 2){$scope snsapi_userinfo;//授权登录}$baseUrl https://open.weixin.qq.com/connect/oauth2/authorize;$param ?appid . $appId . redirect_uri . $redirect_uri . response_typecodescope. $scope .state . $ticket . #wechat_redirect;$url $baseUrl . $param;return $url;}
函数2微信授权后回调页面处理函数
public function saveWeiXinOpenId(){$param $this-params([code/s ,state/s ,]);$code $param[code];$ticket $param[state];$bool Cache::has($ticket);if (!$bool) {// return [code4001,msg已过期];header(location:/login);//跳转去登录页exit;}$info WxLoginService::getWeiXinOpenId($code);if (isset($info[errcode]) || !$info[openid]) {//授权失败直接跳登录页 再次登录// return [code4002,msg获取微信OpenId错误,data$info];header(location:/login);exit;}Cache::set($ticket, json_encode($info), 300);Cache::set(wx_openid, $info[openid], 300);// session(wx_openid, $info[openid]); //这个方法不好使换上面的cache缓存吧原因暂不明// file_put_contents(openid-save.log, Cache::get(wx_openid).---.date(Y-m-d H:i:s).\n, FILE_APPEND);//获取openid后的操作$model new UserModel();$userInfo $model-where(openid, $info[openid])-find();if (empty($userInfo)) {//未绑定过直接去登录 然后绑定// return [code4003,msg手机号不存在];header(location:/login);exit;}//已绑定过直接访问首页header(location:/index);exit;}//获取微信openid方法WxLoginService::getWeiXinOpenId($code)public static function getWeiXinOpenId($code){$appId self::$appId;$appSecret self::$appSecret;$baseUrl https://api.weixin.qq.com/sns/oauth2/access_token;$param ?appid . $appId . secret . $appSecret . code . $code . grant_typeauthorization_code;$url $baseUrl . $param;$guzzHttp new Client();$response $guzzHttp-request(GET, $url, [verify false]);$json $response-getBody()-getContents();$data json_decode($json, true);return $data;}
第二步跳转到登录页面登录完成后在登录接口调用bindOpenId方法给用户绑定openid public function bindOpenId($phone,$openid){$model new UserModel();$userInfo $model-where(phone, $phone)-find();//这个不需要判断因为已经登录成功手机号肯定存在加强判断if (empty($userInfo)) {return error(手机号不存在);}if(!$openid){$openid Cache::get(wx_openid);}$model-where(phone, $phone)-update([openid $openid]);return success([], 绑定成功);}