欢迎光临,了解微信小程序开发,就上易用通!

微信小程序第三方代开发

发布:2018-04-21 08:56浏览: 来源:网络 作者:cola

微信申请第三方之后可以获取授权方的很多权限,主要的是生码和待开发,生码的第三方授权之前已经写了一篇文章,最近做了小程序待开发,总结一下写下来供大家参考

注意事项:如果在调试过程中返回了错误码请到小程序代开发api页面查看,

     小程序代开发使用的域名是你申请第三方时候填写的域名,

     小程序代码模板最多只有50个,可以删除然后重新添加。

准备工作:

  申请微信第三方并且权限那边要选上代开发,第三方申请成功之后就是准备小程序了,需要两个小程序,一个作为小程序代码库,一个作为用户测试用,需要在第三方授权。

  添加小程序代码库: 在第三方那边将小程序添加为开发小程序,然后该小程序就成为了第三方的开发小程序,之后该小程序提交的代码都会存入第三方草稿箱,你可以选择版本添加为模板,一个第三方最 多只能有50个模板。

代开发流程:

  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         );





免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。