【移動(dòng)應(yīng)用開發(fā)技術(shù)】如何使用微信第三方平臺(tái)授權(quán)小程序業(yè)務(wù)_第1頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】如何使用微信第三方平臺(tái)授權(quán)小程序業(yè)務(wù)_第2頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】如何使用微信第三方平臺(tái)授權(quán)小程序業(yè)務(wù)_第3頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】如何使用微信第三方平臺(tái)授權(quán)小程序業(yè)務(wù)_第4頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】如何使用微信第三方平臺(tái)授權(quán)小程序業(yè)務(wù)_第5頁(yè)
已閱讀5頁(yè),還剩27頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

【移動(dòng)應(yīng)用開發(fā)技術(shù)】如何使用微信第三方平臺(tái)授權(quán)小程序業(yè)務(wù)

這篇文章主要為大家展示了“如何使用微信第三方平臺(tái)授權(quán)小程序業(yè)務(wù)”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓在下帶領(lǐng)大家一起研究并學(xué)習(xí)一下“如何使用微信第三方平臺(tái)授權(quán)小程序業(yè)務(wù)”這篇文章吧。第一步:申請(qǐng)微信開放平臺(tái)帳號(hào)并創(chuàng)建第三方平臺(tái)第二步:公眾號(hào)/小程序授權(quán)給第三方平臺(tái)<?php

/*

*

微信第三方平臺(tái)授權(quán)流程

*/

namespace

app\home\controller;

class

Weixin

extends

Common

{

private

$appid

=

'wx3e******165c';

//第三方平臺(tái)應(yīng)用appid

private

$appsecret

=

'13e**********d039';

//第三方平臺(tái)應(yīng)用appsecret

private

$token

=

'ePF58******Q2Ae';

//第三方平臺(tái)應(yīng)用token(消息校驗(yàn)Token)

private

$encodingAesKey

=

'bzH***FCamD';

//第三方平臺(tái)應(yīng)用Key(消息加解密Key)

private

$component_ticket=

'ticket@**xv-g';

//微信后臺(tái)推送的ticket,用于獲取第三方平臺(tái)接口調(diào)用憑據(jù)

/*

*

掃碼授權(quán),注意此URL必須放置在頁(yè)面當(dāng)中用戶點(diǎn)擊進(jìn)行跳轉(zhuǎn),不能通過(guò)程序跳轉(zhuǎn),否則將出現(xiàn)“請(qǐng)確認(rèn)授權(quán)入口頁(yè)所在域名,與授權(quán)后回調(diào)頁(yè)所在域名相同”錯(cuò)誤

*

@params

string

$redirect_uri

:

掃碼成功后的回調(diào)地址

*

@params

int

$auth_type

:

授權(quán)類型,1公眾號(hào),2小程序,3公眾號(hào)/小程序同時(shí)展現(xiàn)。不傳參數(shù)默認(rèn)都展示

*/

public

function

startAuth($redirect_uri,$auth_type

=

3)

{

$url

=

"/cgi-bin/componentloginpage?component_appid=".$this->appid."&pre_auth_code=".$this->get_pre_auth_code()."&redirect_uri=".urlencode($redirect_uri)."&auth_type=".$auth_type;

return

$url;

}

/*

*

獲取第三方平臺(tái)access_token

*

注意,此值應(yīng)保存,代碼這里沒(méi)保存

*/

private

function

get_component_access_token()

{

$url

=

"/cgi-bin/component/api_component_token";

$data

=

'{

"component_appid":"'.$this->appid.'"

,

"component_appsecret":

"'.$this->appsecret.'",

"component_verify_ticket":

"'.$this->component_ticket.'"

}';

$ret

=

json_decode($this->https_post($url,$data));

if($ret->errcode

==

0)

{

return

$ret->component_access_token;

}

else

{

return

$ret->errcode;

}

}

/*

*

第三方平臺(tái)方獲取預(yù)授權(quán)碼pre_auth_code

*/

private

function

get_pre_auth_code()

{

$url

=

"/cgi-bin/component/api_create_preauthcode?component_access_token=".$this->get_component_access_token();

$data

=

'{"component_appid":"'.$this->appid.'"}';

$ret

=

json_decode($this->https_post($url,$data));

if($ret->errcode

==

0)

{

return

$ret->pre_auth_code;

}

else

{

return

$ret->errcode;

}

}

/*

*

發(fā)起POST網(wǎng)絡(luò)提交

*

@params

string

$url

:

網(wǎng)絡(luò)地址

*

@params

json

$data

發(fā)送的json格式數(shù)據(jù)

*/

private

function

https_post($url,$data)

{

$curl

=

curl_init();

curl_setopt($curl,

CURLOPT_URL,

$url);

if

(!empty($data)){

curl_setopt($curl,

CURLOPT_POST,

1);

curl_setopt($curl,

CURLOPT_POSTFIELDS,

$data);

}

curl_setopt($curl,

CURLOPT_RETURNTRANSFER,

1);

$output

=

curl_exec($curl);

curl_close($curl);

return

$output;

}

/*

*

發(fā)起GET網(wǎng)絡(luò)提交

*

@params

string

$url

:

網(wǎng)絡(luò)地址

*/

private

function

https_get($url)

{

$curl

=

curl_init();

curl_setopt($curl,

CURLOPT_URL,

$url);

curl_setopt($curl,

CURLOPT_RETURNTRANSFER,

TRUE);

curl_setopt($curl,

CURLOPT_SSL_VERIFYPEER,

FALSE);

curl_setopt($curl,

CURLOPT_SSL_VERIFYHOST,

FALSE);

curl_setopt($curl,

CURLOPT_HEADER,

FALSE)

;

curl_setopt($curl,

CURLOPT_TIMEOUT,60);

if

(curl_errno($curl))

{

return

'Errno'.curl_error($curl);

}

else{$result=curl_exec($curl);}

curl_close($curl);

return

$result;

}

}<?php

/*

*

接收微信官方推送的ticket值以及取消授權(quán)等操作

*/

namespace

app\home\controller;

use

think\Db;

class

Openoauth

extends

Common

{

private

$appid

=

'wx3e******165c';

//第三方平臺(tái)應(yīng)用appid

private

$appsecret

=

'13e**********d039';

//第三方平臺(tái)應(yīng)用appsecret

private

$token

=

'ePF58******Q2Ae';

//第三方平臺(tái)應(yīng)用token(消息校驗(yàn)Token)

private

$encodingAesKey

=

'bzH***FCamD';

//第三方平臺(tái)應(yīng)用Key(消息加解密Key)

private

$component_ticket=

'ticket@**xv-g';

//微信后臺(tái)推送的ticket,用于獲取第三方平臺(tái)接口調(diào)用憑據(jù)

/*

*

接收微信官方推送的消息(每10分鐘1次)

*

這里需要引入微信官方提供的加解密碼示例包

*

官方文檔:/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318479&token=&lang=zh_CN

*

示例包下載:/shake_tv/mpwiki/cryptoDemo.zip

*/

public

function

index()

{

$encryptMsg

=

file_get_contents("php://input");

$xml_tree

=

new

\DOMDocument();

$xml_tree->loadXML($encryptMsg);

$xml_array

=

$xml_tree->getElementsByTagName("Encrypt");

$encrypt

=

$xml_array->item(0)->nodeValue;

require_once('wxBizMsgCrypt.php');

$Prpcrypt

=

new

\Prpcrypt($this->encodingAesKey);

$postData

=

$Prpcrypt->decrypt($encrypt,

$this->appid);

if

($postData[0]

!=

0)

{

return

$postData[0];

}

else

{

$msg

=

$postData[1];

$xml

=

new

\DOMDocument();

$xml->loadXML($msg);

$array_a

=

$xml->getElementsByTagName("InfoType");

$infoType

=

$array_a->item(0)->nodeValue;

if

($infoType

==

"unauthorized")

{

//取消公眾號(hào)/小程序授權(quán)

$array_b

=

$xml->getElementsByTagName("AuthorizerAppid");

$AuthorizerAppid

=

$array_b->item(0)->nodeValue;

//公眾號(hào)/小程序appid

$where

=

array("type"

=>

1,

"appid"

=>

$AuthorizerAppid);

$save

=

array("authorizer_access_token"

=>

"",

"authorizer_refresh_token"

=>

"",

"authorizer_expires"

=>

0);

Db::name("wxuser")->where($where)->update($save);

//公眾號(hào)取消授權(quán)

Db::name("wxminiprograms")->where('authorizer_appid',$AuthorizerAppid)->update($save);

//小程序取消授權(quán)

}

else

if

($infoType

==

"component_verify_ticket")

{

//微信官方推送的ticket值

$array_e

=

$xml->getElementsByTagName("ComponentVerifyTicket");

$component_verify_ticket

=

$array_e->item(0)->nodeValue;

if

(Db::name("weixin_account")->where(array("type"

=>

1))->update(array("component_verify_ticket"

=>

$component_verify_ticket,

"date_time"

=>

time())))

{

$this->updateAccessToken($component_verify_ticket);

echo

"success";

}

}

}

}

/*

*

更新component_access_token

*

@params

string

$component_verify_ticket

*

*/

private

function

updateAccessToken($component_verify_ticket)

{

$weixin_account

=

Db::name('weixin_account')->where(['type'=>1])->field('id,appId,appSecret,component_access_token,token_expires')->find();

if($weixin_account['token_expires']

<=

time()

)

{

$apiUrl

=

'/cgi-bin/component/api_component_token';

$data

=

'{"component_appid":"'.$weixin_account['appId'].'"

,"component_appsecret":

"'.$weixin_account['appSecret'].'","component_verify_ticket":

"'.$component_verify_ticket.'"}';

$json

=

json_decode(_request($apiUrl,$data));

if(isset($json->component_access_token))

{

Db::name('weixin_account')->where(['id'=>$weixin_account['id']])->update(['component_access_token'=>$json->component_access_token,'token_expires'=>time()+7200]);

}

}

}

}<?php

/*

*

代小程序?qū)崿F(xiàn)業(yè)務(wù)

*/

namespace

app\home\model;

use

think\Model;

use

think\Db;

use

think\Cache;

class

Miniprogram

extends

Model

{

private

$thirdAppId;

//開放平臺(tái)appid

private

$encodingAesKey;

//開放平臺(tái)encodingAesKey

private

$thirdToken;

//開放平臺(tái)token

private

$thirdAccessToken;

//開放平臺(tái)access_token

private

$authorizer_appid;

private

$authorizer_access_token;

private

$authorizer_refresh_token;

public

function

__construct($appid)

{

$weixin_account

=

Db::name('weixin_account')->where(['type'

=>

1])->field('token,encodingAesKey,appId,component_access_token')->find();

if

($weixin_account)

{

$this->thirdAppId

=

$weixin_account['appId'];

$this->encodingAesKey

=

$weixin_account['encodingAesKey'];

$this->thirdToken

=

$weixin_account['token'];

$this->thirdAccessToken

=

$weixin_account['component_access_token'];

$miniprogram

=

Db::name('wxminiprograms')->where('authorizer_appid',$appid)

->field('authorizer_access_token,authorizer_refresh_token,authorizer_expires')->find();

if($miniprogram){

$this->authorizer_appid

=

$appid;

if(time()

>

$miniprogram['authorizer_expires']){

$miniapp

=

$this->update_authorizer_access_token($appid,$miniprogram['authorizer_refresh_token']);

if($miniapp)

{

$this->authorizer_access_token

=

$miniapp->authorizer_access_token;

$this->authorizer_refresh_token

=

$miniapp->authorizer_refresh_token;

}

else

{

$this->errorLog("更新小程序access_token失敗,appid:".$this->authorizer_appid,'');

exit;

}

}

else

{

$this->authorizer_access_token

=

$miniprogram['authorizer_access_token'];

$this->authorizer_refresh_token

=

$miniprogram['authorizer_refresh_token'];

}

}

else

{

$this->errorLog("小程序不存在,appid:".$this->authorizer_appid,'');

exit;

}

}

else

{

$this->errorLog("請(qǐng)?jiān)黾游⑿诺谌焦娞?hào)平臺(tái)賬戶信息",'');

exit;

}

}

/*

*

設(shè)置小程序服務(wù)器地址,無(wú)需加https前綴,但域名必須可以通過(guò)https訪問(wèn)

*

@params

string

/

array

$domains

:

域名地址。只接收一維數(shù)組。

*

*/

public

function

setServerDomain($domain

=

'test.moh.cc')

{

$url

=

"/wxa/modify_domain?access_token=".$this->authorizer_access_token;

if(is_array($domain))

{

$https

=

'';

$wss

=

'';

foreach

($domain

as

$key

=>

$value)

{

$https

.=

'"https://'.$value.'",';

$wss

.=

'"wss://'.$value.'",';

}

$https

=

rtrim($https,',');

$wss

=

rtrim($wss,',');

$data

=

'{

"action":"add",

"requestdomain":['.$https.'],

"wsrequestdomain":['.$wss.'],

"uploaddomain":['.$https.'],

"downloaddomain":['.$https.']

}';

}

else

{

$data

=

'{

"action":"add",

"requestdomain":"https://'.$domain.'",

"wsrequestdomain":"wss://'.$domain.'",

"uploaddomain":"https://'.$domain.'",

"downloaddomain":"https://'.$domain.'"

}';

}

$ret

=

json_decode(https_post($url,$data));

if($ret->errcode

==

0)

{

return

true;

}

else

{

$this->errorLog("設(shè)置小程序服務(wù)器地址失敗,appid:".$this->authorizer_appid,$ret);

return

false;

}

}

/*

*

設(shè)置小程序業(yè)務(wù)域名,無(wú)需加https前綴,但域名必須可以通過(guò)https訪問(wèn)

*

@params

string

/

array

$domains

:

域名地址。只接收一維數(shù)組。

*

*/

public

function

setBusinessDomain($domain

=

'test.moh.cc')

{

$url

=

"/wxa/setwebviewdomain?access_token=".$this->authorizer_access_token;

if(is_array($domain))

{

$https

=

'';

foreach

($domain

as

$key

=>

$value)

{

$https

.=

'"https://'.$value.'",';

}

$https

=

rtrim($https,',');

$data

=

'{

"action":"add",

"webviewdomain":['.$https.']

}';

}

else

{

$data

=

'{

"action":"add",

"webviewdomain":"https://'.$domain.'"

}';

}

$ret

=

json_decode(https_post($url,$data));

if($ret->errcode

==

0)

{

return

true;

}

else

{

$this->errorLog("設(shè)置小程序業(yè)務(wù)域名失敗,appid:".$this->authorizer_appid,$ret);

return

false;

}

}

/*

*

成員管理,綁定小程序體驗(yàn)者

*

@params

string

$wechatid

:

體驗(yàn)者的微信號(hào)

*

*/

public

function

bindMember($wechatid)

{

$url

=

"/wxa/bind_tester?access_token=".$this->authorizer_access_token;

$data

=

'{"wechatid":"'.$wechatid.'"}';

$ret

=

json_decode(https_post($url,$data));

if($ret->errcode

==

0)

{

return

true;

}

else

{

$this->errorLog("綁定小程序體驗(yàn)者操作失敗,appid:".$this->authorizer_appid,$ret);

return

false;

}

}

/*

*

成員管理,解綁定小程序體驗(yàn)者

*

@params

string

$wechatid

:

體驗(yàn)者的微信號(hào)

*

*/

public

function

unBindMember($wechatid)

{

$url

=

"/wxa/unbind_tester?access_token=".$this->authorizer_access_token;

$data

=

'{"wechatid":"'.$wechatid.'"}';

$ret

=

json_decode(https_post($url,$data));

if($ret->errcode

==

0)

{

return

true;

}

else

{

$this->errorLog("解綁定小程序體驗(yàn)者操作失敗,appid:".$this->authorizer_appid,$ret);

return

false;

}

}

/*

*

成員管理,獲取小程序體驗(yàn)者列表

*

*/

public

function

listMember()

{

$url

=

"/wxa/memberauth?access_token=".$this->authorizer_access_token;

$data

=

'{"action":"get_experiencer"}';

$ret

=

json_decode(https_post($url,$data));

if($ret->errcode

==

0)

{

return

$ret->members;

}

else

{

$this->errorLog("獲取小程序體驗(yàn)者列表操作失敗,appid:".$this->authorizer_appid,$ret);

return

false;

}

}

/*

*

為授權(quán)的小程序帳號(hào)上傳小程序代碼

*

@params

int

$template_id

:

模板ID

*

@params

json

$ext_json

:

小程序配置文件,json格式

*

@params

string

$user_version

:

代碼版本號(hào)

*

@params

string

$user_desc

:

代碼描述

*

*/

public

function

uploadCode($template_id

=

1,

$user_version

=

'v1.0.0',

$user_desc

=

"魔盒CMS小程序模板庫(kù)")

{

$ext_json

=

json_encode('{"extEnable":

true,"extAppid":

"wx572****bfb","ext":{"appid":

"'.$this->authorizer_appid.'"}}');

$url

=

"/wxa/commit?access_token=".$this->authorizer_access_token;

$data

=

'{"template_id":"'.$template_id.'","ext_json":'.$ext_json.',"user_version":"'.$user_version.'","user_desc":"'.$user_desc.'"}';

$ret

=

json_decode(https_post($url,$data));

if($ret->errcode

==

0)

{

return

true;

}

else

{

$this->errorLog("為授權(quán)的小程序帳號(hào)上傳小程序代碼操作失敗,appid:".$this->authorizer_appid,$ret);

return

false;

}

}

/*

*

獲取體驗(yàn)小程序的體驗(yàn)二維碼

*

@params

string

$path

:

指定體驗(yàn)版二維碼跳轉(zhuǎn)到某個(gè)具體頁(yè)面

*

*/

public

function

getExpVersion($path

=

'')

{

if($path){

$url

=

"/wxa/get_qrcode?access_token=".$this->authorizer_access_token."&path=".urlencode($path);

}

else

{

$url

=

"/wxa/get_qrcode?access_token=".$this->authorizer_access_token;

}

$ret

=

json_decode(https_get($url));

if($ret->errcode)

{

$this->errorLog("獲取體驗(yàn)小程序的體驗(yàn)二維碼操作失敗,appid:".$this->authorizer_appid,$ret);

return

false;

}

else

{

return

$url;

}

}

/*

*

提交審核

*

@params

string

$tag

:

小程序標(biāo)簽,多個(gè)標(biāo)簽以空格分開

*

@params

strint

$title

:

小程序頁(yè)面標(biāo)題,長(zhǎng)度不超過(guò)32

*

*/

public

function

submitReview($tag

=

"魔盒CMS

微信投票

微網(wǎng)站

微信商城"

,$title

=

"魔盒CMS微信公眾號(hào)營(yíng)銷小程序開發(fā)")

{

$first_class

=

'';$second_class

=

'';$first_id

=

0;$second_id

=

0;

$address

=

"pages/index/index";

$category

=

$this->getCategory();

if(!empty($category))

{

$first_class

=

$category[0]->first_class

?

$category[0]->first_class

:

''

;

$second_class

=

$category[0]->second_class

?

$category[0]->second_class

:

'';

$first_id

=

$category[0]->first_id

?

$category[0]->first_id

:

0;

$second_id

=

$category[0]->second_id

?

$category[0]->second_id

:

0;

}

$getpage

=

$this->getPage();

if(!empty($getpage)

&&

isset($getpage[0]))

{

$address

=

$getpage[0];

}

$url

=

"/wxa/submit_audit?access_token=".$this->authorizer_access_token;

$data

=

'{

"item_list":[{

"address":"'.$address.'",

"tag":"'.$tag.'",

"title":"'.$title.'",

"first_class":"'.$first_class.'",

"second_class":"'.$second_class.'",

"first_id":"'.$first_id.'",

"second_id":"'.$second_id.'"

}]

}';

$ret

=

json_decode(https_post($url,$data));

if($ret->errcode

==

0)

{

Db::name('wxminiprogram_audit')->insert([

'appid'=>$this->authorizer_appid,

'auditid'=>$ret->auditid,

'create_time'=>date('Y-m-d

H:i:s')

]);

return

true;

}

else

{

$this->errorLog("小程序提交審核操作失敗,appid:".$this->authorizer_appid,$ret);

return

false;

}

}

/*

*

小程序?qū)徍顺坊?/p>

*

單個(gè)帳號(hào)每天審核撤回次數(shù)最多不超過(guò)1次,一個(gè)月不超過(guò)10次。

*

*/

public

function

unDoCodeAudit()

{

$url

=

"/wxa/undocodeaudit?access_token=".$this->authorizer_access_token;

$ret

=

json_decode(https_get($url));

if($ret->errcode

==

0)

{

return

true;

}

else

{

$this->errorLog("小程序?qū)徍顺坊夭僮魇?,appid:".$this->authorizer_appid,$ret);

return

false;

}

}

/*

*

查詢指定版本的審核狀態(tài)

*

@params

string

$auditid

:

提交審核時(shí)獲得的審核id

*

*/

public

function

getAuditStatus($auditid)

{

$url

=

"/wxa/get_auditstatus?access_token=".$this->authorizer_access_token;

$data

=

'{"auditid":"'.$auditid.'"}';

$ret

=

json_decode(https_post($url,$data));

if($ret->errcode

==

0)

{

$reason

=

$ret->reason

?

$ret->reason

:

'';

Db::name('wxminiprogram_audit')->where(['appid'=>$this->authorizer_appid,'auditid'=>$auditid])->update([

'status'=>$ret->status,

'reason'=>$reason

]);

return

true;

}

else

{

$this->errorLog("查詢指定版本的審核狀態(tài)操作失敗,appid:".$this->authorizer_appid,$ret);

return

false;

}

}

/*

*

查詢最新一次提交的審核狀態(tài)

*

*/

public

function

getLastAudit()

{

$url

=

"/wxa/get_latest_auditstatus?access_token=".$this->authorizer_access_token;

$ret

=

json_decode(https_get($url));

if($ret->errcode

==

0)

{

$reason

=

$ret->reason

?

$ret->reason

:

'';

Db::name('wxminiprogram_audit')->where(['appid'=>$this->authorizer_appid,'auditid'=>$ret->auditid])->update([

'status'=>$ret->status,

'reason'=>$reason

]);

return

$ret->auditid;

}

else

{

$this->errorLog("查詢最新一次提交的審核狀態(tài)操作失敗,appid:".$this->authorizer_appid,$ret);

return

false;

}

}

/*

*

發(fā)布已通過(guò)審核的小程序

*

*/

public

function

release()

{

$url

=

"/wxa/release?access_token=".$this->authorizer_access_token;

$data

=

'{}';

$ret

=

json_decode(https_post($url,$data));

if($ret->errcode

==

0)

{

return

true;

}

else

{

$this->errorLog("發(fā)布已通過(guò)審核的小程序操作失敗,appid:".$this->authorizer_appid,$ret);

return

$ret->errcode;

}

}

/*

*

獲取授權(quán)小程序帳號(hào)的可選類目

*

*/

private

function

getCategory()

{

$url

=

"/wxa/get_category?access_token=".$this->authorizer_access_token;

$ret

=

json_decode(https_get($url));

if($ret->errcode

==

0)

{

return

$ret->category_list;

}

else

{

$this->errorLog("獲取授權(quán)小程序帳號(hào)的可選類目操作失敗,appid:".$this->authorizer_appid,$ret);

return

false;

}

}

/*

*

獲取小程序的第三方提交代碼的頁(yè)面配置

*

*/

private

function

getPage()

{

$url

=

"/wxa/get_page?access_token=".$this->authorizer_access_token;

$ret

=

json_decode(https_get($url));

if($ret->errcode

==

0)

{

return

$ret->page_list;

}

else

{

$this->errorLog("獲取小程序的第三方提交代碼的頁(yè)面配置失敗,appid:".$this->authorizer_appid,$ret);

return

false;

}

}

/*

*

更新授權(quán)小程序的authorizer_access_token

*

@params

string

$appid

:

小程序appid

*

@params

string

$refresh_token

:

小程序authorizer_refresh_token

*

*/

private

function

update_authorizer_access_token($appid,$refresh_token)

{

$url

=

'/cgi-bin/component/api_authorizer_token?component_access_token='

.

$this->thirdAccessToken;

$data

=

'{"component_appid":"'

.

$this->thirdAppId

.

'","authorizer_appid":"'

.

$appid

.

'","authorizer_refresh_token":"'

.

$refresh_token

.

'"}';

$ret

=

json_decode(https_post($url,

$data));

if

(isset($ret->authorizer_access_token))

{

Db::name('wxminiprograms')->where(['authorizer_appid'

=>

$appid])->update(['authorizer_access_token'

=>

$ret->authorizer_access_token,

'authorizer_expires'

=>

(time()

+

7200),

'authorizer_refresh_token'

=>

$ret->authorizer_refresh_token]);

return

$ret;

}

else

{

$this->errorLog("更新授權(quán)小程序的authorizer_access_token操作失敗,appid:".$appid,$ret);

return

null;

}

}

private

function

errorLog($msg,$ret)

{

file_put_contents(ROOT_PATH

.

'runtime/error/miniprogram.log',

"["

.

date('Y-m-d

H:i:s')

.

"]

".$msg.","

.json_encode($ret).PHP_EOL,

FILE_APPEND);

}

}<?php

//代小程序?qū)崿F(xiàn)業(yè)務(wù)示例包

namespace

app\user\controller;

use

app\home\model\Miniprogram;

use

think\Db;

class

Wxminiprogram

extends

Pub

{

public

$appid

=

'wx57****1bfb';

//需要實(shí)現(xiàn)業(yè)務(wù)小程序appid

public

function

index()

{

return

view();

}

public

function

doAction()

{

if(request()->isPost())

{

$action

=

input('action');

$mini

=

new

Miniprogram($this->appid);

if($action

==

'auth')

{

//小程序授權(quán)

echo

'<script>alert("已授權(quán)");history.back();</script>';

}

elseif($action

==

'setServerDomain')

{

//設(shè)置小程序服務(wù)器域名地址

if($mini->setServerDomain()){

echo

'<script>alert("設(shè)置小程序服務(wù)器域名操作成功");history.back();</script>';

}

else

{

echo

'<script>alert("設(shè)置小程序服務(wù)器域名操作失敗或已設(shè)置,請(qǐng)查看日志");history.back();</script>';

}

}

elseif($action

==

'setBusinessDomain')

{

//設(shè)置業(yè)務(wù)域名

if($mini->setBusinessDomain()){

echo

'<script>alert("設(shè)置小程序業(yè)務(wù)域名操作成功");history.back();</script>';

}

else

{

echo

'<script>alert("設(shè)置小程序業(yè)務(wù)域名操作失敗或已設(shè)置,請(qǐng)查看日志");history.back();</script>';

}

}

elseif($action

==

'bind')

{

//綁定小程序體驗(yàn)者

$wechatid

=

input('wechatid');

if($wechatid)

{

if($mini->bindMember($wechatid)){

echo

'<script>alert("綁定小程序體驗(yàn)者操作成功");history.back();</script>';

}

else

{

echo

'<script>alert("綁定小程序體驗(yàn)者操作失敗,請(qǐng)查看日志");history.back();</script>';

}

}

else

{

echo

'<script>alert("請(qǐng)輸入微信號(hào)");history.back();</script>';

}

}

elseif($action

==

'uploadCode')

{

//上傳小程序代碼

if($mini->uploadCode(2)){

echo

'<script>alert("上傳小程序代碼操作成功");history.back();</script>';

}

else

{

echo

'<script>alert("上傳小程序代碼操作失敗,請(qǐng)查看日志");history.back();</script>';

}

}

elseif($action

==

'getExpVersion')

{

//獲取體驗(yàn)小程序的體驗(yàn)二維碼

$qrcode

=

$mini->getExpVersion();

if($qrcode){

echo

'<script>window.location.href="'.$qrcode.'";</script>';

}

else

{

echo

'<script>alert("獲取體驗(yàn)小程序的體驗(yàn)二維碼操作失敗");history.back();</script>';

}

}

elseif($action

==

'review')

{

//提交審核

$auditid

=

Db::name('wxminiprogram_audit')->where(['appid'=>$this->appid,'status'=>['neq',0]])->order('create_time','desc')->value('auditid');

if($auditid){

echo

'<script>alert("有待處理的版本,請(qǐng)先處理該版本相關(guān)事項(xiàng)再提交新的審核。審核ID:'.$auditid.'");history.back();</script>';

}

else

{

if($mini->submitReview()){

echo

'<script>alert("小程序提交審核操作成功");history.back();</script>';

}

else

{

echo

'<script>alert("小程序提交審核操作失敗,請(qǐng)查看日志");history.back();</script>';

}

}

}

elseif($action

==

'getAudit')

{

//查詢指定版本的審核狀態(tài)

$auditid

=

input('auditid');

if($auditid)

{

if($mini->getAuditStatus($auditid)){

$audit

=

Db::name('wxminiprogram_audit')->where(['appid'=>$this->appid,'auditid'=>$auditid])->field('status,reason')->find();

if($audit['status']

==

0)

{

echo

'<script>alert("該版本審核已通過(guò)");history.back();</script>';

}

elseif($audit['status']

==

1)

{

echo

'<script>alert("該版本審核失敗,原因:'.$audit['reason'].'");history.back();</script>';

}

elseif($audit['status']

==

2)

{

echo

'<script>alert("該版本小程序正在審核中");history.back();</script>';

}

else

{

echo

'<script>alert("未知狀態(tài)");history.back();</script>';

}

}

else

{

echo

'<script>alert("查詢指定版本的審核狀態(tài)操作失敗,請(qǐng)查看日志");history.back();</script>';

}

}

else

{

echo

'<script>alert("請(qǐng)輸入要查詢的審核ID");history.back();</script>';

}

}

elseif($action

==

'lastAudit')

{

//查詢最新一次提交的審核狀態(tài)

$auditid

=

$mini->getLastAudit();

if($auditid){

$audit

=

Db::name('wxminiprogram_audit')->where(['appid'=>$this->appid,'auditid'=>$auditid])->field('status,reason')->find();

if($audit['status']

==

0)

{

echo

'<script>alert("審核已通過(guò)");history.back();</script>';

}

elseif($audit['status']

==

1)

{

echo

'<script>alert("審核失敗,原因:'.$audit['reason'].'");history.back();</script>';

}

elseif($audit['status']

==

2)

{

echo

'<script>alert("小程序正在審核中");history.back();</script>';

}

else

{

echo

'<script>alert("未知狀態(tài)");history.back();</script>';

}

}else

{

echo

'<script>alert("查詢最新一次提交的審核狀態(tài)操作失敗,請(qǐng)查看日志");history.back();</script>';

}

}

elseif($action

==

'release')

{

//發(fā)布已通過(guò)審核的小程序

$auditid

=

Db::name('wxminiprogram_audit')->where(['appid'=>$this->appid,'status'=>['neq',0]])->order('create_time','desc')->value('auditid');

if($auditid){

echo

'<script>alert("有待處理的版本,請(qǐng)先處理該版本相關(guān)事項(xiàng)再發(fā)布版本。審核ID:'.$auditid.'");history.back();</script>';

}

else

{

$errcode

=

$mini->release();

if($errcode){

echo

'<script>alert("已發(fā)版");history.back();</script>';

}

else

{

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論