zoukankan      html  css  js  c++  java
  • 详解微信开放平台第三方平台代小程序开发业务基本接口调用逻辑

    详解微信第三方小程序代开发

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

    由百牛信息技术bainiu.ltd整理发布于博客园

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

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

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

    准备工作:

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

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

    代开发流程:

      post请求公共方法,与微信服务器交互用

      代码如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    protected function curl_post( $curlHttp, $postdata ) {
        $ch = curl_init(); //用curl发送数据给api
        curl_setopt( $ch, CURLOPT_POST, true );
        curl_setopt( $ch, CURLOPT_POST, true );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_URL, $curlHttp );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
     
        $response = curl_exec( $ch );
        curl_close( $ch );
        $result = json_decode( $response, true );
        return $result;
      }

      get请求公共方法,与微信服务器交互用

      代码如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    protected function buildRequestForm( array $param, $method, $target='',$jump=false) {
        $sHtml = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><form id='autoSubmit' action='".$target."' method='".$method."'>";
     
        if ( !empty( $param ) ) {
          foreach( $param as $key => $value ) {
            $sHtml.= "<input type='hidden' name='".$key."' value='".urldecode($value)."'/>";
          }
        }
        $sHtml .= "</form>";
     
        if($jump) $sHtml = $sHtml."<script>document.getElementById("autoSubmit").submit();</script>";
     
        return $sHtml;
      }

      获取授权方api调用拼成access_token公共方法

      代码如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    protectd function getAccessToken( $appId ) {
        $accessToken = '';
     
        if ( empty( $appId ) ) {
          return $accessToken;
        }
         
        // 中间的逻辑自己填充
     
        return $accessToken;
      }

      首先是开发一套小程序并且上传,之后再第三方里边把该版本设置成模板,这个时候你就用了模板id(用于代码指定用)

      通过调用微信接口,给用户小程序指定小程序代码

      代码如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    public function commitCode() {
        $appId = input( 'app_id', '' );
        $descript = input( 'descript', '测试代码指定' );
        $version = input( 'version', 'V.1.0' );
        $templateId = input( 'template_id', 1 );
        if ( empty( $appId ) ) {
          $this->error( appid不能为空 );
          return;
        }
     
        if ( empty( $templateId ) && ( $templateId != 0 ) ) {
          $this->error( '模板id不能为空' );
          return;
        }
     
        $accessToken = $this->getAccessToken( $appId );
     
        // 个人信息我给清除了,空字符部分请自己补充
        $extJson = array(
          'extAppid' => $appId,
          'ext' => array(
            'attr1' => 'value1'
          ),
          'extPages' => array(
              'pages/index/index' => array(
                'navigationBarTitleText'  => ''
              ),
              'pages/media/media' => array(
                'navigationBarTitleText'  => ''
              )
          ),
          'pages' => array(
              'pages/index/index',
              'pages/media/media'
          ),
          'window' => array(
              'backgroundColor'      => '#f8f8f8',
              'navigationBarTextStyle'  => 'white',
              "navigationBarTitleText"  => "",
              'navigationBarBackgroundColor' => '#2b3b48'
          ),
          'tabBar' => array(
            'list' => array(
              array(
                'text'   => '',
                'pagePath' => 'pages/index/index',
              ),
              array(
                'text'   => '',
                'pagePath' => 'pages/media/media',
              )
            )
          ),
          'networkTimeout' => array(
              'request'    => 10000,
              'uploadFile'  => 10000,
              'downloadFile' => 10000,
              'connectSocket' => 10000
          )
        );
     
        $params = array(
          'template_id'  => $templateId,
          'user_version' => $version,
          'user_desc'   => $descript,
          'ext_json'   => json_encode( $extJson, JSON_UNESCAPED_UNICODE )
        );
        $result = $this->curl_post( 'https://api.weixin.qq.com/wxa/commit?access_token='.$accessToken, json_encode( $params, JSON_UNESCAPED_UNICODE ) );
        if ( empty( $result ) || !empty( $result['errcode'] ) ) {
          $this->error( '代码指定错误' );
          return;
        }
     
        $this->success( '操作成功' );
        return;
      }

      指定代码之后就是查看功能是否正常了,所以就要调用微信接口获取体验二维码扫码体验,

      代码如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    public function getExpCode() {
        $appId = input( 'app_id', '' );
        if ( empty( $appId ) ) {
          $this->error( appid不能为空 );
          return;
        }
     
        $accessToken = $this->getAccessToken( $appId );
        if ( empty( $accessToken ) ) {
          $this->error( '获取授权accessToken错误' );
          return;
        }
     
        $params = array(
          'access_token' => $accessToken
        );
        $result = $this->buildRequestForm( $params, 'GET', 'https://api.weixin.qq.com/wxa/get_qrcode?access_token='.$accessToken, true );
        echo $result;
        exit;
      }

      如果授权用户没有体验权限则扫码之后不能进行小程序功能体验,这个时候就需要你通过微信接口将用户设置为体验者了,这一步可以在小程序平台用户管理里边操作,为了提高逼格,你可可以通过微       信接口进行体验者的添加和删除,添加的时候需要被添加者微信确认

      代码如下

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

      如果体验功能有问题则重新调整小程序代码逻辑然后上传之后设置为模板,如果没有问题则将小程序代码提交审核,但是提交审核的时候需要指定category,所以需要调用微信接口查看

      如果授权用户没有设置的话,需要对方进入小程序平台,在填写小程序信息的地方添加服务条目

      代码如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    public function getCategory() {
        $appId = input( 'app_id', '' );
        if ( empty( $appId ) ) {
          $this->error( appid不能为空 );
          return;
        }
     
        $accessToken = $this->getAccessToken( $appId );
        if ( empty( $accessToken ) ) {
          $this->error( '获取授权accessToken错误' );
          return;
        }
     
        $params = array(
          'access_token' => $accessToken
        );
        $result = $this->buildRequestForm( $params, 'GET', 'https://api.weixin.qq.com/wxa/get_category?access_token='.$accessToken, true );
     
        echo $result;
        exit;
      }

      拿到服务条目之后就是提交代码审核了

      代码如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    public function submitAudit() {
        $appId = input( 'app_id', '' );
        if ( empty( $appId ) ) {
          $this->error( appid不能为空 );
          return;
        }
     
        $accessToken = $this->getAccessToken( $appId );
        if ( empty( $accessToken ) ) {
          $this->error( '获取授权accessToken错误' );
          return;
        }
     
        $params = array(
          'item_list' => array(
              array(
                'address' => 'pages/index/index',
                'tag' => 'IT科技',
                'first_class' => 'IT科技',
                'second_class' => '硬件与设备',
                'title' => '生成二维码'
              ),
              array(
                'address' => 'pages/media/media',
                'tag' => '工具',
                'first_class' => '工具',
                'second_class' => '办公',
                'title' => '多媒体上传'
              )
          )
        );
        $result = $this->curl_post( 'https://api.weixin.qq.com/wxa/submit_audit?access_token='.$accessToken, json_encode( $params, JSON_UNESCAPED_UNICODE ) );
     
        echo'<pre>';
        print_r($result);
        exit;
        $this->success( '操作成功' );
        return;
      }

      提交审核之后,微信服务器会返回一个审核id,你可以通过该审核id查询审核状态

      当审核通过之后,微信会给你第三方注册的回调地址推送一个审核结果

      代码如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    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
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public function release (){
        $appId = input( 'app_id', '' );
        if ( empty( $appId ) ) {
          $this->error( appid不能为空 );
          return;
        }
     
        $accessToken = $this->getAccessToken( $appId );
        if ( empty( $accessToken ) ) {
          $this->error( '获取授权accessToken错误' );
          return;
        }
        $result = $this->curl_post( 'https://api.weixin.qq.com/wxa/release?access_token='.$accessToken, '{}' );
        print_r($result);
        exit;
        return;
      }

    就这样,小程序代开发就完成了,逻辑很简单,代码也没难度,本文章的代码仅供大家参考,如果有问题请评论指出,我尽量补充。

    感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

  • 相关阅读:
    瀑布流 jquery。
    LeetCode: Validate Binary Search Tree [098]
    【WebService】使用JDK开发WebService(二)
    【WebService】WebService基础知识(一)
    【Apache】Apache服务的基本概念(二)
    【Apache】Apache服务的安装(一)
    【Linux】DNS服务-BIND从服务器、缓存服务器及转发服务器配置(三)
    【Linux】DNS服务-BIND基础配置(二)
    【Linux】DNS基础(一)
    【Web】Nginx 反向代理与负载均衡
  • 原文地址:https://www.cnblogs.com/bainiu/p/8026751.html
Copyright © 2011-2022 走看看