需求说明当前商城(uid标识)授权第三方开发平台。网页授权成功后,跳转到另一个商城项目链接,带上当前微信用户信息和微信初始化验证签名。第三方平台授权安装easywechat4$composerrequireovertrue/wechat:~4.0-vvvreferenceuseEasyWeChat\Factory;创建微信扫描二维码授权页面跳转/***开发平台授权跳转*@returnvoid*/publicfunctionaccessView(){//$uid=Request()->route('uid',0);$url='http://qgcloud.capsui.com/public/index/wxopen/config?uid='.$uid;$this->assign('url',$url);return$this->fetch();}跳转方法(为什么我没有写前面的方法,因为微信要求同一个地址)/***开发平台跳转到授权扫描页面**@returnvoid*/publicfunction配置(){$uid=Request()->get('uid',0);$config=['app_id'=>'开放平台第三方平台APPID','secret'=>'开放平台第三方平台Secret','token'=>'开放平台第三方平台Token','aes_key'=>'开放平台第三方平台AESKey'];$openPlatform=Factory::openPlatform($config);$url=$openPlatform->getPreAuthorizationUrl('http://qgcloud.capsui.com/public/index/wxopen/wxcallback?uid='.$uid);$this->redirect($url);}授权回调(注意:扫码确认授权后,第一次回调不会带uid参数,)介绍使用EasyWeChat\OpenPlatform\Server\Guard;/***开发平台授权回调**@returnvoid*/publicfunctionwxcallback(){//这张表记录授权成功//$Wxpublic=newWxpublic;//这个表是为了记录授权成功,传给uid商城绑定appid//$ShopConfig=newShopConfig;$get=Request()->param();$config=['app_id'=>'开放平台第三方平台APPID','secret'=>'开放平台第三方平台Secret','token'=>'开放平台第三方平台Token','aes_key'=>'开放平台第三方平台AESKey'];$openPlatform=Factory::openPlatform($config);$server=$openPlatform->server;//处理授权成功事件-Firstcallback//关闭方法!请填写$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()]);}},警卫::EVENT_AUTHORIZED);//处理授权取消事件-Firstcallback//关闭方法!请填写$server->push(function($message)use(/*$Wxpublic,$ShopConfig*/){//处理数据库逻辑//$Wxpublic::appid($message['AppId'])->delete();//$ShopConfig::appid($message['AppId'])->update(['token'=>'']);},Guard::EVENT_UNAUTHORIZED);//第二个回调会带来授权码和自定义参数mallid(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]);//}}返回$server->serve();}第三方平台网页授权&微信JSDK初始化签名生成/***网页授权调用**@returnvoid*/publicfunctionhtmlAccess(){$appid=Request()->get('appid',0);$config=['app_id'=>'开放平台第三方平台APPID','secret'=>'开放平台第三方平台Secret','token'=>'开放平台第三方平台Token','aes_key'=>'开放平台第三方平台AESKey'];$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="http://qgcloud.capsui.com/public/index/wxopen/callbackOpenid";$response=$officialAccount->oauth->scopes(['snsapi_userinfo'])->redirect($url)->send();}网页授权回调方法/***网页授权回调**@returnvoid*/publicfunctioncallbackOpenid(){$appid=Request()->get('appid',null);$config=['app_id'=>'开放平台第三方平台APPID','secret'=>'开放平台第三方平台Secret','token'=>'开放平台第三方平台Token','aes_key'=>'开放平台第三方平台AESKey'];$openPlatform=Factory::openPlatform($config);$data=$openPlatform->getAuthorizer($appid);$appid=$data['authorization_info']['authorizer_appid'];$refreshToken=$data['authorization_info']['authorizer_refresh_token'];//获取微信用户信息如openid昵称等信息$officialAccount=$openPlatform->officialAccount($appid,$refreshToken);$oauth=$officialAccount->oauth;$user=$oauth->user();//处理wxconfig初始化JSSDK$officialAccount->jssdk->setUrl('http://quguoshop.capsui.com/');$wxconfig=$officialAccount->jssdk->buildConfig(['chooseWXPay'],$debug=true,$beta=false,$json=真);$ShopConfig=newShopConfig;$shopInfo=$ShopConfig::appid($appid)->find();//注意这里我是跳转到其他带参数的TP5项目,然后使用缓存处理$url='http://quguoshop.capsui.com/public/wxoauthCallback?data='.json_encode($user->toArray())。'&令牌='。$shopInfo['id'].'&wxconfig='.$wxconfig;$this->redirect($url);}
