之前我们讲过,在获取到预授权码之后,我们需要在自己的网站中设置授权页入口(如下图),从而引导微信公众号管理员进入到授权页,对第三方平台进行微信公众号的托管授权。
对于微信授权页,简单的说就是一个带有规定参数的URL,其中包括第三方平台的appid、预授权码以及回调URL,通过访问这个URL,各个参数正确无误,就会进入到授权页,如:
生成授权页URL,首先我们需要获取得到“预授权码”:
$pre_auth_code =$this->get_pre_auth_code ();
if ($pre_auth_code == false) {
$res ['msg'] = '获取pre_auth_code失败!';
return $res;
}
其次还需要封装好“回调URL”,其中“BASE_URL”为网站域名:
$callback = BASE_URL.U('Wechat/Wechat/after_auth');
然后,组装授权页URL:
$jumpURL ='https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=第三方平台appid&pre_auth_code='.$pre_auth_code.'&redirect_uri='.$callback;
这里,我们对授权页的生成创建一个可供调用的方法,在需要生成的授权页URL的页面进行调用,如:
$tips = A('Wechat/Wechat')
$res = $tips->tips();
if (! $res ['status']) {
$this->error( $res ['msg'] );
exit();
}
$this->assign ( 'jumpURL', $res['jumpURL'] );
完整代码:
1)授权引导页调用
Public function index(){
$tips= A('Wechat/Wechat')
$res =$tips->tips();
if (! $res['status']) {
$this->error ( $res ['msg'] );
exit ();
}
$this->assign( 'jumpURL', $res ['jumpURL'] );
$this->display():
}
2)授权页URL生成方法
public function tips(){
$res['status'] = false;
$pre_auth_code= $this->get_pre_auth_code ();
if($pre_auth_code == false) {
$res['msg'] = '获取pre_auth_code失败!';
return$res;
}
$callback= BASE_URL.U('Wechat/Wechat/after_auth');
$jumpURL= 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=第三方平台appid&pre_auth_code='.$pre_auth_code.'&redirect_uri='.$callback;
$res['status'] = true;
$res['jumpURL'] = $jumpURL;
return$res;
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有