微信申請(qǐng)第三方之后可以獲取授權(quán)方的很多權(quán)限,主要的是生碼和待開發(fā),生碼的第三方授權(quán)之前已經(jīng)寫了一篇文章,最近做了小程序待開發(fā),總結(jié)一下寫下來供大家參考
注意事項(xiàng):如果在調(diào)試過程中返回了錯(cuò)誤碼請(qǐng)到小程序代開發(fā)api頁面查看,
小程序代開發(fā)使用的域名是你申請(qǐng)第三方時(shí)候填寫的域名,
小程序代碼模板最多只有50個(gè),可以刪除然后重新添加。
準(zhǔn)備工作:
申請(qǐng)微信第三方并且權(quán)限那邊要選上代開發(fā),第三方申請(qǐng)成功之后就是準(zhǔn)備小程序了,需要兩個(gè)小程序,一個(gè)作為小程序代碼庫,一個(gè)作為用戶測試用,需要在第三方授權(quán)。
添加小程序代碼庫: 在第三方那邊將小程序添加為開發(fā)小程序,然后該小程序就成為了第三方的開發(fā)小程序,之后該小程序提交的代碼都會(huì)存入第三方草稿箱,你可以選擇版本添加為模板,一個(gè)第三方最 多只能有50個(gè)模板。
代開發(fā)流程:
post請(qǐng)求公共方法,與微信服務(wù)器交互用
代碼如下
1 protected function curl_post( $curlHttp, $postdata ) {
2 $ch = curl_init(); //用curl發(fā)送數(shù)據(jù)給api
3 curl_setopt( $ch, CURLOPT_POST, true );
4 curl_setopt( $ch, CURLOPT_POST, true );
5 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
6 curl_setopt( $ch, CURLOPT_URL, $curlHttp );
7 curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata );
8 curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
9 curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
10
11 $response = curl_exec( $ch );
12 curl_close( $ch );
13 $result = json_decode( $response, true );
14 return $result;
15 }
get請(qǐng)求公共方法,與微信服務(wù)器交互用 代碼如下
1 protected function buildRequestForm( array $param, $method, $target='',$jump=false) {
2 $sHtml = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><form id='autoSubmit' action='".$target."' method='".$method."'>";
3
4 if ( !empty( $param ) ) {
5 foreach( $param as $key => $value ) {
6 $sHtml.= "<input type='hidden' name='".$key."' value='".urldecode($value)."'/>";
7 }
8 }
9 $sHtml .= "</form>";
10
11 if($jump) $sHtml = $sHtml."<script>document.getElementById(\"autoSubmit\").submit();</script>";
12
13 return $sHtml;
14 }
獲取授權(quán)方api調(diào)用拼成access_token公共方法 代碼如下:
1 protectd function getAccessToken( $appId ) {
2 $accessToken = '';
3
4 if ( empty( $appId ) ) {
5 return $accessToken;
6 }
7
8 // 中間的邏輯自己填充
9
10 return $accessToken;
11 }
首先是開發(fā)一套小程序并且上傳,之后再第三方里邊把該版本設(shè)置成模板,這個(gè)時(shí)候你就用了模板id(用于代碼指定用)
通過調(diào)用微信接口,給用戶小程序指定小程序代碼
代碼如下
1 public function commitCode() {
2 $appId = input( 'app_id', '' );
3 $descript = input( 'descript', '測試代碼指定' );
4 $version = input( 'version', 'V.1.0' );
5 $templateId = input( 'template_id', 1 );
6 if ( empty( $appId ) ) {
7 $this->error( appid不能為空 );
8 return;
9 }
10
11 if ( empty( $templateId ) && ( $templateId != 0 ) ) {
12 $this->error( '模板id不能為空' );
13 return;
14 }
15
16 $accessToken = $this->getAccessToken( $appId );
17
18 // 個(gè)人信息我給清除了,空字符部分請(qǐng)自己補(bǔ)充
19 $extJson = array(
20 'extAppid' => $appId,
21 'ext' => array(
22 'attr1' => 'value1'
23 ),
24 'extPages' => array(
25 'pages/index/index' => array(
26 'navigationBarTitleText' => ''
27 ),
28 'pages/media/media' => array(
29 'navigationBarTitleText' => ''
30 )
31 ),
32 'pages' => array(
33 'pages/index/index',
34 'pages/media/media'
35 ),
36 'window' => array(
37 'backgroundColor' => '#f8f8f8',
38 'navigationBarTextStyle' => 'white',
39 "navigationBarTitleText" => "",
40 'navigationBarBackgroundColor' => '#2b3b48'
41 ),
42 'tabBar' => array(
43 'list' => array(
44 array(
45 'text' => '',
46 'pagePath' => 'pages/index/index',
47 ),
48 array(
49 'text' => '',
50 'pagePath' => 'pages/media/media',
51 )
52 )
53 ),
54 'networkTimeout' => array(
55 'request' => 10000,
56 'uploadFile' => 10000,
57 'downloadFile' => 10000,
58 'connectSocket' => 10000
59 )
60 );
61
62 $params = array(
63 'template_id' => $templateId,
64 'user_version' => $version,
65 'user_desc' => $descript,
66 'ext_json' => json_encode( $extJson, JSON_UNESCAPED_UNICODE )
67 );
68 $result = $this->curl_post( 'https://api.weixin.qq.com/wxa/commit?access_token='.$accessToken, json_encode( $params, JSON_UNESCAPED_UNICODE ) );
69 if ( empty( $result ) || !empty( $result['errcode'] ) ) {
70 $this->error( '代碼指定錯(cuò)誤' );
71 return;
72 }
73
74 $this->success( '操作成功' );
75 return;
76 }
指定代碼之后就是查看功能是否正常了,所以就要調(diào)用微信接口獲取體驗(yàn)二維碼掃碼體驗(yàn),
代碼如下
1 public function getExpCode() {
2 $appId = input( 'app_id', '' );
3 if ( empty( $appId ) ) {
4 $this->error( appid不能為空 );
5 return;
6 }
7
8 $accessToken = $this->getAccessToken( $appId );
9 if ( empty( $accessToken ) ) {
10 $this->error( '獲取授權(quán)accessToken錯(cuò)誤' );
11 return;
12 }
13
14 $params = array(
15 'access_token' => $accessToken
16 );