100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > thinkphp5.1+easywechat4 微信开放平台开发

thinkphp5.1+easywechat4 微信开放平台开发

时间:2018-08-16 23:44:56

相关推荐

thinkphp5.1+easywechat4 微信开放平台开发

需求描述

1. 当前商城(uid标识)授权第三方开发平台.

2. 网页授权成功后跳转到另一个商城项目链接并带上当前微信用户信息和微信初始化验证签名.

第三方平台授权

安装easywechat4

$ composer require overtrue/wechat:~4.0 -vvv

引入

use EasyWeChat\Factory;

创建一个跳转到微信扫二维码授权页面

/*** 开发平台授权跳转** @return void*/public function accessView(){// $uid = Request()->route('uid' , 0);$url = '/public/index/wxopen/config?uid=' . $uid;$this->assign('url' , $url);return $this->fetch();}

跳转方法(为什么我不写到上一个方法呢 因为微信要求同一个地址)

/*** 开发平台跳转授权扫码页** @return void*/public function config(){$uid = Request()->get('uid' , 0);$config = ['app_id' => '开放平台第三方平台 APPID','secret' => '开放平台第三方平台 Secret','token' => '开放平台第三方平台 Token','aes_key' => '开放平台第三方平台 AES Key'];$openPlatform = Factory::openPlatform($config);$url = $openPlatform->getPreAuthorizationUrl('/public/index/wxopen/wxcallback?uid=' . $uid);$this->redirect($url);}

授权回调(注意:扫码确认授权后他第一次回调不会带uid参数)

引入

use EasyWeChat\OpenPlatform\Server\Guard;

/*** 开发平台授权回调** @return void*/public function wxcallback(){// 这个表是记录授权成功的//$Wxpublic = new Wxpublic;// 这个表是记录授权成功后传过来所属uid商城绑定appid//$ShopConfig = new ShopConfig;$get = Request()->param();$config = ['app_id' => '开放平台第三方平台 APPID','secret' => '开放平台第三方平台 Secret','token' => '开放平台第三方平台 Token','aes_key' => '开放平台第三方平台 AES Key'];$openPlatform = Factory::openPlatform($config);$server = $openPlatform->server;// 处理授权成功事件-第一次回调// 闭包方法!里面调用外面的方法请在use里面填写$server->push(function ($message) use ($openPlatform /*, $Wxpublic*/) {$authCode = $message['AuthorizationCode'];$res= $openPlatform->handleAuthorize($authCode);if($res['authorization_info']['authorizer_refresh_token']){//授权成功记录到数据库//$Wxpublic->insert(['appid' => $res['authorization_info']['authorizer_appid'] , 'createtime' => time()]);}}, Guard::EVENT_AUTHORIZED);// 处理授权取消事件-第一次回调// 闭包方法!里面调用外面的方法请在use里面填写$server->push(function ($message) use(/*$Wxpublic , $ShopConfig*/) {//处理数据库逻辑//$Wxpublic::appid($message['AppId'])->delete();//$ShopConfig::appid($message['AppId'])->update(['token' => '']);}, Guard::EVENT_UNAUTHORIZED);// 第二次回调会带一个授权code和自定义参数商城id(uid)if(isset($get['auth_code']) && isset($get['uid'])){$res= $openPlatform->handleAuthorize($get['auth_code']);$appid = $res['authorization_info']['authorizer_appid'];//数据库逻辑//$isConfig = $Wxpublic::appid($appid)->count();//if($isConfig){//$add = $ShopConfig->where('uid' , $get['uid'])->update(['token' => $appid]);//}}return $server->serve();}

第三方平台 网页授权&微信JSSDK初始化签名生成

/*** 网页授权调起** @return void*/public function htmlAccess(){$appid = Request()->get('appid' , 0);$config = ['app_id' => '开放平台第三方平台 APPID','secret' => '开放平台第三方平台 Secret','token' => '开放平台第三方平台 Token','aes_key' => '开放平台第三方平台 AES Key'];$openPlatform = Factory::openPlatform($config);$data = $openPlatform->getAuthorizer($appid);$appid = $data['authorization_info']['authorizer_appid'];$refreshToken = $data['authorization_info']['authorizer_refresh_token'];$officialAccount = $openPlatform->officialAccount($appid , $refreshToken);$oauth = $officialAccount->oauth;// 回调授权地址$url= "/public/index/wxopen/callbackOpenid";$response = $officialAccount->oauth->scopes(['snsapi_userinfo'])->redirect($url)->send();}

网页授权回调方法

/*** 网页授权回调** @return void*/public function callbackOpenid(){$appid = Request()->get('appid' , null);$config = ['app_id' => '开放平台第三方平台 APPID','secret' => '开放平台第三方平台 Secret','token' => '开放平台第三方平台 Token','aes_key' => '开放平台第三方平台 AES Key'];$openPlatform = Factory::openPlatform($config);$data = $openPlatform->getAuthorizer($appid);$appid = $data['authorization_info']['authorizer_appid'];$refreshToken = $data['authorization_info']['authorizer_refresh_token'];// 获取微信用户信息 如openid nickname等信息$officialAccount = $openPlatform->officialAccount($appid , $refreshToken);$oauth = $officialAccount->oauth;$user = $oauth->user();// 处理wxconfig初始化JSSDK$officialAccount->jssdk->setUrl('/');$wxconfig = $officialAccount->jssdk->buildConfig(['chooseWXPay'], $debug = true, $beta = false, $json = true);$ShopConfig = new ShopConfig;$shopInfo = $ShopConfig::appid($appid)->find();// 注意 这里我是带参数跳转到其他TP5项目里面再用缓存处理一下$url = '/public/wxoauthCallback?data=' . json_encode($user->toArray()) . '&token=' . $shopInfo['id'] . '&wxconfig=' . $wxconfig;$this->redirect($url);}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。