詳解微信第三方小程序代開發html
微信申請第三方以後能夠獲取受權方的不少權限,主要的是生碼和待開發,生碼的第三方受權以前已經寫了一篇文章,最近作了小程序待開發,總結一下寫下來供你們參考java
由百牛信息技術bainiu.ltd整理髮佈於博客園json
注意事項:若是在調試過程當中返回了錯誤碼請到小程序代開發api頁面查看,小程序
小程序代開發使用的域名是你申請第三方時候填寫的域名,api
小程序代碼模板最多隻有50個,能夠刪除而後從新添加。服務器
準備工做:微信
申請微信第三方而且權限那邊要選上代開發,第三方申請成功以後就是準備小程序了,須要兩個小程序,一個做爲小程序代碼庫,一個做爲用戶測試用,須要在第三方受權。app
添加小程序代碼庫: 在第三方那邊將小程序添加爲開發小程序,而後該小程序就成爲了第三方的開發小程序,以後該小程序提交的代碼都會存入第三方草稿箱,你能夠選擇版本添加爲模板,一個第三方最 多隻能有50個模板。curl
代開發流程:工具
post請求公共方法,與微信服務器交互用
代碼以下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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;
}
|
get請求公共方法,與微信服務器交互用
代碼以下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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;
}
|
獲取受權方api調用拼成access_token公共方法
代碼以下
1
2
3
4
5
6
7
8
9
10
11
|
protectd function getAccessToken( $appId ) {
$accessToken =
''
;
if
( empty( $appId ) ) {
return
$accessToken;
}
// 中間的邏輯本身填充
return
$accessToken;
}
|
首先是開發一套小程序而且上傳,以後再第三方里邊把該版本設置成模板,這個時候你就用了模板id(用於代碼指定用)
經過調用微信接口,給用戶小程序指定小程序代碼
代碼以下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
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
;
}
|
指定代碼以後就是查看功能是否正常了,因此就要調用微信接口獲取體驗二維碼掃碼體驗,
代碼以下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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;
}
|
若是受權用戶沒有體驗權限則掃碼以後不能進行小程序功能體驗,這個時候就須要你經過微信接口將用戶設置爲體驗者了,這一步能夠在小程序平臺用戶管理裏邊操做,爲了提升逼格,你可能夠經過微 信接口進行體驗者的添加和刪除,添加的時候須要被添加者微信確認
代碼以下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
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
;
}
|
若是體驗功能有問題則從新調整小程序代碼邏輯而後上傳以後設置爲模板,若是沒有問題則將小程序代碼提交審覈,可是提交審覈的時候須要指定category,因此須要調用微信接口查看
若是受權用戶沒有設置的話,須要對方進入小程序平臺,在填寫小程序信息的地方添加服務條目
代碼以下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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;
}
|
拿到服務條目以後就是提交代碼審覈了
代碼以下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
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
;
}
|
提交審覈以後,微信服務器會返回一個審覈id,你能夠經過該審覈id查詢審覈狀態
當審覈經過以後,微信會給你第三方註冊的回調地址推送一個審覈結果
代碼以下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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
;
}
|
當小程序審覈經過了接下來就是小程序發佈了
代碼以下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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
;
}
|
就這樣,小程序代開發就完成了,邏輯很簡單,代碼也沒難度,本文章的代碼僅供你們參考,若是有問題請評論指出,我儘可能補充。
感謝閱讀,但願能幫助到你們,謝謝你們對本站的支持!