• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

微信第三方小程序代开发

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

注意事项:如果在调试过程中返回了错误码请到小程序代开发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         );
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。

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
如何快速开发一套微信商城小程序? - 想她发布时间:2022-07-18
下一篇:
微信小程序 错误代码 列表发布时间:2022-07-18
热门推荐
    热门话题
    阅读排行榜

    扫描微信二维码

    查看手机版网站

    随时了解更新最新资讯

    139-2527-9053

    在线客服(服务时间 9:00~18:00)

    在线QQ客服
    地址:深圳市南山区西丽大学城创智工业园
    电邮:jeky_zhao#qq.com
    移动电话:139-2527-9053

    Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap