微信公眾號(hào)三方平臺(tái)開(kāi)發(fā)【生成授權(quán)頁(yè)】

之前我們講過(guò),在獲取到預(yù)授權(quán)碼之后,我們需要在自己的網(wǎng)站中設(shè)置授權(quán)頁(yè)入口(如下圖),從而引導(dǎo)微信公眾號(hào)管理員進(jìn)入到授權(quán)頁(yè),對(duì)第三方平臺(tái)進(jìn)行微信公眾號(hào)的托管授權(quán)。


授權(quán)頁(yè)引導(dǎo)頁(yè)

對(duì)于微信授權(quán)頁(yè),簡(jiǎn)單的說(shuō)就是一個(gè)帶有規(guī)定參數(shù)的URL,其中包括第三方平臺(tái)的appid、預(yù)授權(quán)碼以及回調(diào)URL,通過(guò)訪問(wèn)這個(gè)URL,各個(gè)參數(shù)正確無(wú)誤,就會(huì)進(jìn)入到授權(quán)頁(yè),如:


授權(quán)頁(yè)

生成授權(quán)頁(yè)URL,首先我們需要獲取得到“預(yù)授權(quán)碼”:

$pre_auth_code = $this->get_pre_auth_code ();
if ($pre_auth_code == false) {
  $res ['msg'] = '獲取pre_auth_code失??!';
  return $res;
}

其次還需要封裝好“回調(diào)URL”,其中“BASE_URL”為網(wǎng)站域名:

$callback = BASE_URL.U('Wechat/Wechat/after_auth');

然后,組裝授權(quán)頁(yè)URL:

$jumpURL = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=第三方平臺(tái)appid&pre_auth_code='.$pre_auth_code.'&redirect_uri='.$callback;

這里,我們對(duì)授權(quán)頁(yè)的生成創(chuàng)建一個(gè)可供調(diào)用的方法,在需要生成的授權(quán)頁(yè)URL的頁(yè)面進(jìn)行調(diào)用,如:

$tips = A('Wechat/Wechat')
$res = $tips->tips();
if (! $res ['status']) {
    $this->error ( $res ['msg'] );
    exit ();
}
$this->assign ( 'jumpURL', $res ['jumpURL'] );
完整代碼:
1)授權(quán)引導(dǎo)頁(yè)調(diào)用
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)授權(quán)頁(yè)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=第三方平臺(tái)appid&pre_auth_code='.$pre_auth_code.'&redirect_uri='.$callback;
    $res ['status'] = true;
    $res ['jumpURL'] = $jumpURL;
    return $res;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容