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

前端之家收集整理的这篇文章主要介绍了详解微信第三方小程序代开发前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

@H_404_3@

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

注意事项:

如果在调试过程中返回了错误码请到小程序代开发api页面查看,@H_404_3@

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

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

准备工作:

@H_404_3@

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

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

代开发流程:

@H_404_3@

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

  代码如下@H_404_3@

$response = curl_exec( $ch ); curl_close( $ch ); $result = json_decode( $response,true ); return $result;

}

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

  代码如下@H_404_3@

";
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公共方法@H_404_3@

  代码如下@H_404_3@

if ( empty( $appId ) ) { return $accessToken; } // 中间的逻辑自己填充 return $accessToken;

}

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

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

  代码如下@H_404_3@

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( '<a href="https://www.jb51.cc/tag/daima/" target="_blank" class="keywords">代码</a>指定<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>' );
  return;
}

$this->success( '操作成功' );
return;

}

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

  代码如下@H_404_3@

error( appid不能为空 ); return; }
$accessToken = $this->getAccessToken( $appId );
if ( empty( $accessToken ) ) {
  $this->error( '<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>授权accessToken<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>' );
  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;

}

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

  代码如下@H_404_3@

error( appid不能为空 ); return; } if ( empty( $wxNumber ) ) { $this->error( 微信号不能为空 ); return; }
$accessToken = $this->getAccessToken( $appId );
if ( empty( $accessToken ) ) {
  $this->error( '<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>授权accessToken<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>' );
  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','' );
if ( empty( $appId ) ) {
$this->error( appid不能为空 );
return;
}
if ( empty( $wxNumber ) ) {
$this->error( 微信号不能为空 );
return;
}

$accessToken = $this->getAccessToken( $appId );
if ( empty( $accessToken ) ) {
  $this->error( '<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>授权accessToken<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>' );
  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,所以需要调用微信接口查看@H_404_3@

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

  代码如下@H_404_3@

error( appid不能为空 ); return; }
$accessToken = $this->getAccessToken( $appId );
if ( empty( $accessToken ) ) {
  $this->error( '<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>授权accessToken<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>' );
  return;
}

$params = array(
  'item_list' => array(
      array(
        'address' => 'pages/index/index','tag' => 'IT科技','first_class' => 'IT科技','second_class' => '硬件与设备','title' => '<a href="https://www.jb51.cc/tag/shengcheng/" target="_blank" class="keywords">生成</a><a href="https://www.jb51.cc/tag/erweima/" target="_blank" class="keywords">二维码</a>'
      ),array(
        'address' => 'pages/media/media','tag' => '工具','first_class' => '工具','second_class' => '办公','title' => '多媒体<a href="https://www.jb51.cc/tag/shangchuan/" target="_blank" class="keywords">上传</a>'
      )
  )
);
$result = $this->curl_post( 'https://api.weixin.qq.com/wxa/submit_audit?access_token='.$accessToken,JSON_UNESCAPED_UNICODE ) );

echo'<pre>';
print_r($result);
exit;
$this->success( '操作成功' );
return;

}

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

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

  代码如下@H_404_3@

error( appid不能为空 ); return; }
$accessToken = $this->getAccessToken( $appId );
if ( empty( $accessToken ) ) {
  $this->error( '<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>授权accessToken<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>' );
  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;

}

  当小程序审核通过了接下来就是小程序发布了@H_404_3@

  代码如下@H_404_3@

error( appid不能为空 ); return; }
$accessToken = $this->getAccessToken( $appId );
if ( empty( $accessToken ) ) {
  $this->error( '<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>授权accessToken<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>' );
  return;
}
$result = $this->curl_post( 'https://api.weixin.qq.com/wxa/release?access_token='.$accessToken,'{}' );
print_r($result);
exit;
return;

}

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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!@H_404_3@ 原文链接:https://www.f2er.com/js/38389.html

猜你在找的JavaScript相关文章