詳細介紹微信第三方小程序代開發
微信申請第三方之後可以獲取授權方的很多權限,主要的是生碼和待開發,生碼的第三方授權之前已經寫了一篇文章,最近做了小程序待開發,總結一下寫下來供大家參考
需要注意:如果在調試過程中返回了錯誤碼請到小程序代開發api頁面查看,
小程序代開發使用的域名是你申請第三方時候填寫的域名,
小程序代碼模板最多隻有50個,可以刪除然後重新添加。
準備工作:
申請微信第三方並且權限那邊要選上代開發,第三方申請成功之後就是準備小程序了,需要兩個小程序,一個作爲小程序代碼庫,一個作爲用戶測試用,需要在第三方授權。
添加小程序代碼庫: 在第三方那邊將小程序添加爲開發小程序,然後該小程序就成爲了第三方的開發小程序,之後該小程序提交的代碼都會存入第三方草稿箱,你可以選擇版本添加爲模板,一個第三方最 多只能有50個模板。
代開發流程:
post請求公共方法,與微信服務器交互用
代碼如下
protected
function curl_post( $curlHttp, $postdata ) {
$ch = curl_init();
//用curl發送數據給api
curl_setopt( $ch, CURLOPT_POST,
true
);
curl_setopt( $ch, CURLOPT_POST,
true
);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER,
true
);
curl_setopt( $ch, CURLOPT_URL, $curlHttp );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
$response = curl_exec( $ch );
curl_close( $ch );
$result = json_decode( $response,
true
);
return
$result;
}
代碼如下
protected
function buildRequestForm( array $param, $method, $target=
''
,$jump=
false
)
{
$sHtml =
"<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><form id='autoSubmit' action='"
.$target.
"'
method='"
.$method.
"'>"
;
if
( !empty( $param ) ) {
foreach( $param as $key => $value ) {
$sHtml.=
"<input type='hidden' name='"
.$key.
"' value='"
.urldecode($value).
"'/>"
;
}
}
$sHtml .=
"</form>"
;
if
($jump) $sHtml = $sHtml.
"<script>document.getElementById(\"autoSubmit\").submit();</script>"
;
return
$sHtml;
}
代碼如下
protectd function getAccessToken( $appId ) {
$accessToken =
''
;
if
( empty( $appId ) ) {
return
$accessToken;
}
// 中間的邏輯自己填充
return
$accessToken;
}
通過調用微信接口,給用戶小程序指定小程序代碼
代碼如下
public
function commitCode() {
$appId = input(
'app_id'
,
''
);
$descript = input(
'descript'
,
'測試代碼指定'
);
$version = input(
'version'
,
'V.1.0'
);
$templateId = input(
'template_id'
,
1
);
if
( empty( $appId ) ) {
$
this
->error( appid不能爲空 );
return
;
}
if
( empty( $templateId ) && ( $templateId !=
0
) ) {
$
this
->error(
'模板id不能爲空'
);
return
;
}
$accessToken = $
this
->getAccessToken( $appId );
// 個人信息我給清除了,空字符部分請自己補充
$extJson = array(
'extAppid'
=> $appId,
'ext'
=> array(
'attr1'
=>
'value1'
),
'extPages'
=> array(
'pages/index/index'
=> array(
'navigationBarTitleText'
=>
''
),
'pages/media/media'
=> array(
'navigationBarTitleText'
=>
''
)
),
'pages'
=> array(
'pages/index/index'
,
'pages/media/media'
),
'window'
=> array(
'backgroundColor'
=>
'#f8f8f8'
,
'navigationBarTextStyle'
=>
'white'
,
"navigationBarTitleText"
=>
""
,
'navigationBarBackgroundColor'
=>
'#2b3b48'
),
'tabBar'
=> array(
'list'
=> array(
array(
'text'
=>
''
,
'pagePath'
=>
'pages/index/index'
,
),
array(
'text'
=>
''
,
'pagePath'
=>
'pages/media/media'
,
)
)
),
'networkTimeout'
=> array(
'request'
=>
10000
,
'uploadFile'
=>
10000
,
'downloadFile'
=>
10000
,
'connectSocket'
=>
10000
)
);
$params = array(
'template_id'
=> $templateId,
'user_version'
=> $version,
'user_desc'
=> $descript,
'ext_json'
=> json_encode( $extJson, JSON_UNESCAPED_UNICODE )
);
$result = $
this
->curl_post(
'https://api.weixin.qq.com/wxa/commit?access_token='
.$accessToken,
json_encode( $params, JSON_UNESCAPED_UNICODE ) );
if
( empty( $result ) || !empty( $result[
'errcode'
] ) ) {
$
this
->error(
'代碼指定錯誤'
);
return
;
}
$
this
->success(
'操作成功'
);
return
;
}
代碼如下
public
function getExpCode() {
$appId = input(
'app_id'
,
''
);
if
( empty( $appId ) ) {
$
this
->error( appid不能爲空 );
return
;
}
$accessToken = $
this
->getAccessToken( $appId );
if
( empty( $accessToken ) ) {
$
this
->error(
'獲取授權accessToken錯誤'
);
return
;
}
$params = array(
'access_token'
=> $accessToken
);
$result = $
this
->buildRequestForm( $params,
'GET'
,
'https://api.weixin.qq.com/wxa/get_qrcode?access_token='
.$accessToken,
true
);
echo $result;
exit;
}
代碼如下
public
function bindTester() {
$appId = input(
'app_id'
,
''
);
$wxNumber = input(
'wx_number'
,
''
);
if
( empty( $appId ) ) {
$
this
->error( appid不能爲空 );
return
;
}
if
( empty( $wxNumber ) ) {
$
this
->error( 微信號不能爲空 );
return
;
}
$accessToken = $
this
->getAccessToken( $appId );
if
( empty( $accessToken ) ) {
$
this
->error(
'獲取授權accessToken錯誤'
);
return
;
}
$params = array(
'wechatid'
=> $wxNumber
);
$result = $
this
->curl_post(
'https://api.weixin.qq.com/wxa/bind_tester?access_token='
.$accessToken,
json_encode( $params ) );
print_r($result);
exit;
return
;
}
public
function unBindTester() {
$appId = input(
'app_id'
,
''
);
$wxNumber = input(
'wx_number'
,
''
);
if
( empty( $appId ) ) {
$
this
->error( appid不能爲空 );
return
;
}
if
( empty( $wxNumber ) ) {
$
this
->error( 微信號不能爲空 );
return
;
}
$accessToken = $
this
->getAccessToken( $appId );
if
( empty( $accessToken ) ) {
$
this
->error(
'獲取授權accessToken錯誤'
);
return
;
}
$params = array(
'wechatid'
=> $wxNumber
);
$result = $
this
->curl_post(
'https://api.weixin.qq.com/wxa/unbind_tester?access_token='
.$accessToken,
json_encode( $params ) );
print_r($result);
exit;
return
;
}
如果授權用戶沒有設置的話,需要對方進入小程序平臺,在填寫小程序信息的地方添加服務條目
代碼如下
public
function getCategory() {
$appId = input(
'app_id'
,
''
);
if
( empty( $appId ) ) {
$
this
->error( appid不能爲空 );
return
;
}
$accessToken = $
this
->getAccessToken( $appId );
if
( empty( $accessToken ) ) {
$
this
->error(
'獲取授權accessToken錯誤'
);
return
;
}
$params = array(
'access_token'
=> $accessToken
);
$result = $
this
->buildRequestForm( $params,
'GET'
,
'https://api.weixin.qq.com/wxa/get_category?access_token='
.$accessToken,
true
);
echo $result;
exit;
}
代碼如下
public
function submitAudit() {
$appId = input(
'app_id'
,
''
);
if
( empty( $appId ) ) {
$
this
->error( appid不能爲空 );
return
;
}
$accessToken = $
this
->getAccessToken( $appId );
if
( empty( $accessToken ) ) {
$
this
->error(
'獲取授權accessToken錯誤'
);
return
;
}
$params = array(
'item_list'
=> array(
array(
'address'
=>
'pages/index/index'
,
'tag'
=>
'IT科技'
,
'first_class'
=>
'IT科技'
,
'second_class'
=>
'硬件與設備'
,
'title'
=>
'生成二維碼'
),
array(
'address'
=>
'pages/media/media'
,
'tag'
=>
'工具'
,
'first_class'
=>
'工具'
,
'second_class'
=>
'辦公'
,
'title'
=>
'多媒體上傳'
)
)
);
$result = $
this
->curl_post(
'https://api.weixin.qq.com/wxa/submit_audit?access_token='
.$accessToken,
json_encode( $params, JSON_UNESCAPED_UNICODE ) );
echo
'<pre>'
;
print_r($result);
exit;
$
this
->success(
'操作成功'
);
return
;
}
當審覈通過之後,微信會給你第三方註冊的回調地址推送一個審覈結果
代碼如下
public
function getAuditStatus (){
$appId = input(
'app_id'
,
''
);
if
( empty( $appId ) ) {
$
this
->error( appid不能爲空 );
return
;
}
$accessToken = $
this
->getAccessToken( $appId );
if
( empty( $accessToken ) ) {
$
this
->error(
'獲取授權accessToken錯誤'
);
return
;
}
$params = array(
'auditid'
=>
12334
);
$result = $
this
->curl_post(
'https://api.weixin.qq.com/wxa/get_auditstatus?access_token='
.$accessToken,
json_encode( $params ) );
print_r($result);
exit;
return
;
}
代碼如下
public
function release (){
$appId = input(
'app_id'
,
''
);
if
( empty( $appId ) ) {
$
this
->error( appid不能爲空 );
return
;
}
$accessToken = $
this
->getAccessToken( $appId );
if
( empty( $accessToken ) ) {
$
this
->error(
'獲取授權accessToken錯誤'
);
return
;
}
$result = $
this
->curl_post(
'https://api.weixin.qq.com/wxa/release?access_token='
.$accessToken,
'{}'
);
print_r($result);
exit;
return
;
}
就這樣,小程序代開發就完成了,邏輯很簡單,代碼也沒難度,本文章的代碼僅供大家參考,如果有問題請評論指出,我儘量補充。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!