日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

详解微信开放平台第三方平台代小程序开发业务基本接口调用逻辑

發(fā)布時(shí)間:2024/6/30 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 详解微信开放平台第三方平台代小程序开发业务基本接口调用逻辑 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

詳解微信第三方小程序代開(kāi)發(fā)

微信申請(qǐng)第三方之后可以獲取授權(quán)方的很多權(quán)限,主要的是生碼和待開(kāi)發(fā),生碼的第三方授權(quán)之前已經(jīng)寫了一篇文章,最近做了小程序待開(kāi)發(fā),總結(jié)一下寫下來(lái)供大家參考

由百牛信息技術(shù)bainiu.ltd整理發(fā)布于博客園

注意事項(xiàng):如果在調(diào)試過(guò)程中返回了錯(cuò)誤碼請(qǐng)到小程序代開(kāi)發(fā)api頁(yè)面查看,

    ? 小程序代開(kāi)發(fā)使用的域名是你申請(qǐng)第三方時(shí)候填寫的域名,

     小程序代碼模板最多只有50個(gè),可以刪除然后重新添加。

準(zhǔn)備工作:

  申請(qǐng)微信第三方并且權(quán)限那邊要選上代開(kāi)發(fā),第三方申請(qǐng)成功之后就是準(zhǔn)備小程序了,需要兩個(gè)小程序,一個(gè)作為小程序代碼庫(kù),一個(gè)作為用戶測(cè)試用,需要在第三方授權(quán)。

  添加小程序代碼庫(kù): 在第三方那邊將小程序添加為開(kāi)發(fā)小程序,然后該小程序就成為了第三方的開(kāi)發(fā)小程序,之后該小程序提交的代碼都會(huì)存入第三方草稿箱,你可以選擇版本添加為模板,一個(gè)第三方最???????????? 多只能有50個(gè)模板。

代開(kāi)發(fā)流程:

  post請(qǐng)求公共方法,與微信服務(wù)器交互用

  代碼如下

?
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發(fā)送數(shù)據(jù)給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請(qǐng)求公共方法,與微信服務(wù)器交互用

  代碼如下

?
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; ??}

  獲取授權(quán)方api調(diào)用拼成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; ??}

  首先是開(kāi)發(fā)一套小程序并且上傳,之后再第三方里邊把該版本設(shè)置成模板,這個(gè)時(shí)候你就用了模板id(用于代碼指定用)

  通過(guò)調(diào)用微信接口,給用戶小程序指定小程序代碼

  代碼如下

?
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', '測(cè)試代碼指定' ); ????$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 ); ????// 個(gè)人信息我給清除了,空字符部分請(qǐng)自己補(bǔ)充 ????$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( '代碼指定錯(cuò)誤' ); ??????return; ????} ????$this->success( '操作成功' ); ????return; ??}

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

  代碼如下

?
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( '獲取授權(quán)accessToken錯(cuò)誤' ); ??????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; ??}

  如果授權(quán)用戶沒(méi)有體驗(yàn)權(quán)限則掃碼之后不能進(jìn)行小程序功能體驗(yàn),這個(gè)時(shí)候就需要你通過(guò)微信接口將用戶設(shè)置為體驗(yàn)者了,這一步可以在小程序平臺(tái)用戶管理里邊操作,為了提高逼格,你可可以通過(guò)微?????? 信接口進(jìn)行體驗(yàn)者的添加和刪除,添加的時(shí)候需要被添加者微信確認(rèn)

  代碼如下

?
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( 微信號(hào)不能為空 ); ??????return; ????} ????$accessToken = $this->getAccessToken( $appId ); ????if ( empty( $accessToken ) ) { ??????$this->error( '獲取授權(quán)accessToken錯(cuò)誤' ); ??????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( 微信號(hào)不能為空 ); ??????return; ????} ????$accessToken = $this->getAccessToken( $appId ); ????if ( empty( $accessToken ) ) { ??????$this->error( '獲取授權(quán)accessToken錯(cuò)誤' ); ??????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; ??}

  如果體驗(yàn)功能有問(wèn)題則重新調(diào)整小程序代碼邏輯然后上傳之后設(shè)置為模板,如果沒(méi)有問(wèn)題則將小程序代碼提交審核,但是提交審核的時(shí)候需要指定category,所以需要調(diào)用微信接口查看

  如果授權(quán)用戶沒(méi)有設(shè)置的話,需要對(duì)方進(jìn)入小程序平臺(tái),在填寫小程序信息的地方添加服務(wù)條目

  代碼如下

?
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( '獲取授權(quán)accessToken錯(cuò)誤' ); ??????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; ??}

  拿到服務(wù)條目之后就是提交代碼審核了

  代碼如下

?
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( '獲取授權(quán)accessToken錯(cuò)誤' ); ??????return; ????} ????$params = array( ??????'item_list' => array( ??????????array( ????????????'address' => 'pages/index/index', ????????????'tag' => 'IT科技', ????????????'first_class' => 'IT科技', ????????????'second_class' => '硬件與設(shè)備', ????????????'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; ??}

  提交審核之后,微信服務(wù)器會(huì)返回一個(gè)審核id,你可以通過(guò)該審核id查詢審核狀態(tài)

  當(dāng)審核通過(guò)之后,微信會(huì)給你第三方注冊(cè)的回調(diào)地址推送一個(gè)審核結(jié)果

  代碼如下

?
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( '獲取授權(quán)accessToken錯(cuò)誤' ); ??????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; ??}

  當(dāng)小程序?qū)徍送ㄟ^(guò)了接下來(lái)就是小程序發(fā)布了

  代碼如下

?
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( '獲取授權(quán)accessToken錯(cuò)誤' ); ??????return; ????} ????$result = $this->curl_post( 'https://api.weixin.qq.com/wxa/release?access_token='.$accessToken, '{}' ); ????print_r($result); ????exit; ????return; ??}

就這樣,小程序代開(kāi)發(fā)就完成了,邏輯很簡(jiǎn)單,代碼也沒(méi)難度,本文章的代碼僅供大家參考,如果有問(wèn)題請(qǐng)?jiān)u論指出,我盡量補(bǔ)充。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

轉(zhuǎn)載于:https://www.cnblogs.com/bainiu/p/8026751.html

總結(jié)

以上是生活随笔為你收集整理的详解微信开放平台第三方平台代小程序开发业务基本接口调用逻辑的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。