小程序模板網(wǎng)

微信小程序第三方代開發(fā)

發(fā)布時間:2018-04-21 08:56 所屬欄目:小程序開發(fā)教程
 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請求公共方法,與微信服務(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è)置成模板,這個時候你就用了模板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         // 個人信息我給清除了,空字符部分請自己補(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( '代碼指定錯誤' );
71             return;
72         }
73 
74         $this->success( '操作成功' );
75         return;
76     }

  指定代碼之后就是查看功能是否正常了,所以就要調(diào)用微信接口獲取體驗二維碼掃碼體驗,

  代碼如下

 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錯誤' );
11             return;
12         }
13 
14         $params = array(
15             'access_token' => $accessToken
16         );


易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開源 碼云倉庫:starfork
本文地址:http://22321a.com/wxmini/doc/course/23818.html 復(fù)制鏈接 如需定制請聯(lián)系易優(yōu)客服咨詢:800182392 點擊咨詢
QQ在線咨詢