微信第三方小程序代開發

微信申請第三方以後能夠獲取受權方的不少權限,主要的是生碼和待開發,生碼的第三方受權以前已經寫了一篇文章,最近作了小程序待開發,總結一下寫下來供你們參考html

注意事項:若是在調試過程當中返回了錯誤碼請到小程序代開發api頁面查看,json

      小程序代開發使用的域名是你申請第三方時候填寫的域名,小程序

     小程序代碼模板最多隻有50個,能夠刪除而後從新添加。api

準備工做:服務器

  申請微信第三方而且權限那邊要選上代開發,第三方申請成功以後就是準備小程序了,須要兩個小程序,一個做爲小程序代碼庫,一個做爲用戶測試用,須要在第三方受權。微信

  添加小程序代碼庫: 在第三方那邊將小程序添加爲開發小程序,而後該小程序就成爲了第三方的開發小程序,以後該小程序提交的代碼都會存入第三方草稿箱,你能夠選擇版本添加爲模板,一個第三方最             多隻能有50個模板。app

代開發流程:curl

  post請求公共方法,與微信服務器交互用工具

  代碼以下post

 1    protected function curl_post( $curlHttp, $postdata ) {
 2         $ch = curl_init(); //用curl發送數據給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請求公共方法,與微信服務器交互用

  代碼以下

 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     }

  獲取受權方api調用拼成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     }

  首先是開發一套小程序而且上傳,以後再第三方里邊把該版本設置成模板,這個時候你就用了模板id(用於代碼指定用)

  經過調用微信接口,給用戶小程序指定小程序代碼

  代碼以下

 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         // 我的信息我給清除了,空字符部分請本身補充
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( '代碼指定錯誤' );
71             return;
72         }
73 
74         $this->success( '操做成功' );
75         return;
76     }

  指定代碼以後就是查看功能是否正常了,因此就要調用微信接口獲取體驗二維碼掃碼體驗,

  代碼以下

 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( '獲取受權accessToken錯誤' );
11             return;
12         }
13 
14         $params = array(
15             'access_token' => $accessToken
16         );
17         $result = $this->buildRequestForm( $params, 'GET', 'https://api.weixin.qq.com/wxa/get_qrcode?access_token='.$accessToken, true );
18         echo $result;
19         exit;
20     }

  若是受權用戶沒有體驗權限則掃碼以後不能進行小程序功能體驗,這個時候就須要你經過微信接口將用戶設置爲體驗者了,這一步能夠在小程序平臺用戶管理裏邊操做,爲了提升逼格,你可能夠經過微       信接口進行體驗者的添加和刪除,添加的時候須要被添加者微信確認

  代碼以下

 1 public function bindTester() {
 2         $appId = input( 'app_id', '' );
 3         $wxNumber = input( 'wx_number', '' );
 4         if ( empty( $appId ) ) {
 5             $this->error( appid不能爲空 );
 6             return;
 7         }
 8         if ( empty( $wxNumber ) ) {
 9             $this->error( 微信號不能爲空 );
10             return;
11         }
12 
13         $accessToken = $this->getAccessToken( $appId );
14         if ( empty( $accessToken ) ) {
15             $this->error( '獲取受權accessToken錯誤' );
16             return;
17         }
18         $params = array(
19             'wechatid' => $wxNumber
20         );
21         $result = $this->curl_post( 'https://api.weixin.qq.com/wxa/bind_tester?access_token='.$accessToken, json_encode( $params ) );
22         print_r($result);
23         exit;
24         return;
25     }
26 
27 public function unBindTester() {
28         $appId = input( 'app_id', '' );
29         $wxNumber = input( 'wx_number', '' );
30         if ( empty( $appId ) ) {
31             $this->error( appid不能爲空 );
32             return;
33         }
34         if ( empty( $wxNumber ) ) {
35             $this->error( 微信號不能爲空 );
36             return;
37         }
38 
39         $accessToken = $this->getAccessToken( $appId );
40         if ( empty( $accessToken ) ) {
41             $this->error( '獲取受權accessToken錯誤' );
42             return;
43         }
44         $params = array(
45             'wechatid' => $wxNumber
46         );
47         $result = $this->curl_post( 'https://api.weixin.qq.com/wxa/unbind_tester?access_token='.$accessToken, json_encode( $params ) );
48         print_r($result);
49         exit;
50         return;
51     }

  若是體驗功能有問題則從新調整小程序代碼邏輯而後上傳以後設置爲模板,若是沒有問題則將小程序代碼提交審覈,可是提交審覈的時候須要指定category,因此須要調用微信接口查看

  若是受權用戶沒有設置的話,須要對方進入小程序平臺,在填寫小程序信息的地方添加服務條目

  代碼以下

 1 public function getCategory() {
 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( '獲取受權accessToken錯誤' );
11             return;
12         }
13 
14         $params = array(
15             'access_token' => $accessToken
16         );
17         $result = $this->buildRequestForm( $params, 'GET', 'https://api.weixin.qq.com/wxa/get_category?access_token='.$accessToken, true );
18 
19         echo $result;
20         exit;
21     }

  拿到服務條目以後就是提交代碼審覈了

  代碼以下

 1  public function submitAudit() {
 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( '獲取受權accessToken錯誤' );
11             return;
12         }
13 
14         $params = array(
15             'item_list' => array(
16                     array(
17                         'address' => 'pages/index/index',
18                         'tag' => 'IT科技',
19                         'first_class' => 'IT科技',
20                         'second_class' => '硬件與設備',
21                         'title' => '生成二維碼'
22                     ),
23                     array(
24                         'address' => 'pages/media/media',
25                         'tag' => '工具',
26                         'first_class' => '工具',
27                         'second_class' => '辦公',
28                         'title' => '多媒體上傳'
29                     )
30             )
31         );
32         $result = $this->curl_post( 'https://api.weixin.qq.com/wxa/submit_audit?access_token='.$accessToken, json_encode( $params, JSON_UNESCAPED_UNICODE ) );
33 
34         echo'<pre>';
35         print_r($result);
36         exit;
37         $this->success( '操做成功' );
38         return;
39     }

  提交審覈以後,微信服務器會返回一個審覈id,你能夠經過該審覈id查詢審覈狀態

  當審覈經過以後,微信會給你第三方註冊的回調地址推送一個審覈結果

  代碼以下

 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  public function release (){
 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( '獲取受權accessToken錯誤' );
11             return;
12         }
13         $result = $this->curl_post( 'https://api.weixin.qq.com/wxa/release?access_token='.$accessToken, '{}' );
14         print_r($result);
15         exit;
16         return;
17     }

就這樣,小程序代開發就完成了,邏輯很簡單,代碼也沒難度,本文章的代碼僅供你們參考,若是有問題請評論指出,我儘可能補充。

本文章爲原創文章,若是轉載請標明出處 http://www.sui-xinlu.com/hbyzs/p/7060521.html。

相關文章
相關標籤/搜索