微信公众号三方平台开发【生成授权页】

之前我们讲过,在获取到预授权码之后,我们需要在自己的网站中设置授权页入口(如下图),从而引导微信公众号管理员进入到授权页,对第三方平台进行微信公众号的托管授权。
授权页引导页

对于微信授权页,简单的说就是一个带有规定参数的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;
}

猜你喜欢

转载自blog.csdn.net/joelingwei/article/details/72876777