• <label id="pxtpz"><meter id="pxtpz"></meter></label>
      1. <span id="pxtpz"><optgroup id="pxtpz"></optgroup></span>

        當前位置:雨林木風下載站 > 網絡軟件教程 > 詳細頁面

        終于整理出來了,用微信第3方平臺授權小程序業(yè)務

        終于整理出來了,用微信第3方平臺授權小程序業(yè)務

        更新時間:2025-09-20 文章作者:未知 信息來源:網絡 閱讀次數(shù):

        微信(WeChat)是騰訊公司于2011年1月21日推出的一個為智能終端提供即時通訊服務的免費應用程序,由張小龍所帶領的騰訊廣州研發(fā)中心產品團隊打造 [2] 。微信支持跨通信運營商、跨操作系統(tǒng)平臺...
        微信(WeChat)是騰訊公司于2011年1月21日推出的一個為智能終端提供即時通訊服務的免費應用程序,由張小龍所帶領的騰訊廣州研發(fā)中心產品團隊打造 [2] 。微信支持跨通信運營商、跨操作系統(tǒng)平臺通過網絡快速發(fā)送免費(需消耗少量網絡流量)語音短信、視頻、圖片和文字,同時,也可以使用通過共享流媒體內容的資料和基于位置的社交插件“搖一搖”、“漂流瓶”、“朋友圈”、”公眾平臺“、”語音記事本“等服務插件。
        本文章是自己編寫的,用微信第三方平臺開發(fā)實現(xiàn)小程序業(yè)務,代碼很全,每一步都有詳細介紹,供大家學習參考。

        第一步:申請微信開放平臺帳號并創(chuàng)建第三方平臺

        2236665639-5b238482e677f_articlex.png

        636996152-5b2384a750438_articlex.png

        1610749029-5b2386c9e68e0_articlex.png

        253370562-5b23877febf0c_articlex.png

        第二步:公眾號/小程序授權給第三方平臺

        <?php
        /*
        *    微信第三方平臺授權流程
        */
        namespace app\home\controller;
        class Weixin extends Common
        {
            private $appid = 'wx3e******165c';            //第三方平臺應用appid
            private $appsecret = '13e**********d039';     //第三方平臺應用appsecret
            private $token = 'ePF58******Q2Ae';           //第三方平臺應用token(消息校驗Token)
            private $encodingAesKey = 'bzH***FCamD';      //第三方平臺應用Key(消息加解密Key)
            private $component_ticket= 'ticket@**xv-g';   //微信后臺推送的ticket,用于獲取第三方平臺接口調用憑據(jù)
            
            /*
            * 掃碼授權,注意此URL必須放置在頁面當中用戶點擊進行跳轉,不能通過程序跳轉,否則將出現(xiàn)“請確認授權入口頁所在域名,與授權后回調頁所在域名相同....”錯誤
            * @params string $redirect_uri : 掃碼成功后的回調地址
            * @params int $auth_type : 授權類型,1公眾號,2小程序,3公眾號/小程序同時展現(xiàn)。不傳參數(shù)默認都展示    
            */
            public function startAuth($redirect_uri,$auth_type = 3)
            {
                $url = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=".$this->appid."&pre_auth_code=".$this->get_pre_auth_code()."&redirect_uri=".urlencode($redirect_uri)."&auth_type=".$auth_type;
                return $url;
            }
            
            /*
            * 獲取第三方平臺access_token
            * 注意,此值應保存,代碼這里沒保存
            */
            private function get_component_access_token()
            {
                $url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
                $data = '{
                    "component_appid":"'.$this->appid.'" ,
                    "component_appsecret": "'.$this->appsecret.'",
                    "component_verify_ticket": "'.$this->component_ticket.'"
                }';
                $ret = json_decode($this->https_post($url,$data));
                if($ret->errcode == 0) {
                    return $ret->component_access_token;
                } else {
                    return $ret->errcode;
                }
            }
            /*
            *  第三方平臺方獲取預授權碼pre_auth_code
            */
            private function get_pre_auth_code()
            {
                $url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=".$this->get_component_access_token();
                $data = '{"component_appid":"'.$this->appid.'"}';
                $ret = json_decode($this->https_post($url,$data));
                if($ret->errcode == 0) {
                    return $ret->pre_auth_code;
                } else {
                    return $ret->errcode;
                }
            }
            
            /*
            * 發(fā)起POST網絡提交
            * @params string $url : 網絡地址
            * @params json $data : 發(fā)送的json格式數(shù)據(jù)
            */
            private function https_post($url,$data)
            {
                $curl = curl_init();
                curl_setopt($curl, CURLOPT_URL, $url);
                if (!empty($data)){
                    curl_setopt($curl, CURLOPT_POST, 1);
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                }
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                $output = curl_exec($curl);
                curl_close($curl);
                return $output;
            }
             /*
            * 發(fā)起GET網絡提交
            * @params string $url : 網絡地址
            */
            private function https_get($url)
            {
                $curl = curl_init();
                curl_setopt($curl, CURLOPT_URL, $url);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); 
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); 
                curl_setopt($curl, CURLOPT_HEADER, FALSE) ; 
                curl_setopt($curl, CURLOPT_TIMEOUT,60);
                if (curl_errno($curl)) {
                    return 'Errno'.curl_error($curl);
                }
                else{$result=curl_exec($curl);}
                curl_close($curl);
                return $result;
            }
        }
        <?php
        /*
        *    接收微信官方推送的ticket值以及取消授權等操作
        */
        namespace app\home\controller;
        use think\Db;
        class Openoauth extends Common
        {
            private $appid = 'wx3e******165c';            //第三方平臺應用appid
            private $appsecret = '13e**********d039';     //第三方平臺應用appsecret
            private $token = 'ePF58******Q2Ae';           //第三方平臺應用token(消息校驗Token)
            private $encodingAesKey = 'bzH***FCamD';      //第三方平臺應用Key(消息加解密Key)
            private $component_ticket= 'ticket@**xv-g';   //微信后臺推送的ticket,用于獲取第三方平臺接口調用憑據(jù)
            /*
            *    接收微信官方推送的消息(每10分鐘1次)
            *    這里需要引入微信官方提供的加解密碼示例包
            *    官方文檔:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318479&token=&lang=zh_CN
            *    示例包下載:https://wximg.gtimg.com/shake_tv/mpwiki/cryptoDemo.zip
            */
            public function index()
            {
                $encryptMsg = file_get_contents("php://input");
                $xml_tree = new \DOMDocument();
                $xml_tree->loadXML($encryptMsg);
                $xml_array = $xml_tree->getElementsByTagName("Encrypt");
                $encrypt = $xml_array->item(0)->nodeValue;
                require_once('wxBizMsgCrypt.php');
                $Prpcrypt = new \Prpcrypt($this->encodingAesKey);
                $postData = $Prpcrypt->decrypt($encrypt, $this->appid);
                if ($postData[0] != 0) {
                    return $postData[0];
                } else {
                    $msg = $postData[1];
                    $xml = new \DOMDocument();
                    $xml->loadXML($msg);
                    $array_a = $xml->getElementsByTagName("InfoType");
                    $infoType = $array_a->item(0)->nodeValue;
                    if ($infoType == "unauthorized") {
                        //取消公眾號/小程序授權
                        $array_b = $xml->getElementsByTagName("AuthorizerAppid");
                        $AuthorizerAppid = $array_b->item(0)->nodeValue;    //公眾號/小程序appid
                        $where = array("type" => 1, "appid" => $AuthorizerAppid);
                        $save = array("authorizer_access_token" => "", "authorizer_refresh_token" => "", "authorizer_expires" => 0);
                        Db::name("wxuser")->where($where)->update($save);   //公眾號取消授權
                        Db::name("wxminiprograms")->where('authorizer_appid',$AuthorizerAppid)->update($save);   //小程序取消授權
                    } else if ($infoType == "component_verify_ticket") {
                        //微信官方推送的ticket值
                        $array_e = $xml->getElementsByTagName("ComponentVerifyTicket");
                        $component_verify_ticket = $array_e->item(0)->nodeValue;
                        if (Db::name("weixin_account")->where(array("type" => 1))->update(array("component_verify_ticket" => $component_verify_ticket, "date_time" => time()))) {
                            $this->updateAccessToken($component_verify_ticket);
                            echo "success";
                        }
                    }
                }
            }
            
            /*
             * 更新component_access_token
             * @params string $component_verify_ticket
             * */
            private function updateAccessToken($component_verify_ticket)
            {
                $weixin_account = Db::name('weixin_account')->where(['type'=>1])->field('id,appId,appSecret,component_access_token,token_expires')->find();
                if($weixin_account['token_expires'] <= time() ) {
                    $apiUrl = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
                    $data = '{"component_appid":"'.$weixin_account['appId'].'" ,"component_appsecret": "'.$weixin_account['appSecret'].'","component_verify_ticket": "'.$component_verify_ticket.'"}';
                    $json = json_decode(_request($apiUrl,$data));
                    if(isset($json->component_access_token)) {
                        Db::name('weixin_account')->where(['id'=>$weixin_account['id']])->update(['component_access_token'=>$json->component_access_token,'token_expires'=>time()+7200]);
                    }
                }
            }
        }
        <?php
        /*
        *    代小程序實現(xiàn)業(yè)務
        */
        namespace app\home\model;
        use think\Model;
        use think\Db;
        use think\Cache;
        class Miniprogram extends Model
        {
            private $thirdAppId;        //開放平臺appid
            private $encodingAesKey;    //開放平臺encodingAesKey
            private $thirdToken;        //開放平臺token
            private $thirdAccessToken;  //開放平臺access_token
        
            private $authorizer_appid;
            private  $authorizer_access_token;
            private  $authorizer_refresh_token;
        
            public function __construct($appid)
            {
                $weixin_account = Db::name('weixin_account')->where(['type' => 1])->field('token,encodingAesKey,appId,component_access_token')->find();
                if ($weixin_account) {
                    $this->thirdAppId = $weixin_account['appId'];
                    $this->encodingAesKey = $weixin_account['encodingAesKey'];
                    $this->thirdToken = $weixin_account['token'];
                    $this->thirdAccessToken = $weixin_account['component_access_token'];
        
                    $miniprogram = Db::name('wxminiprograms')->where('authorizer_appid',$appid)
                        ->field('authorizer_access_token,authorizer_refresh_token,authorizer_expires')->find();
                    if($miniprogram){
                        $this->authorizer_appid = $appid;
                        if(time() > $miniprogram['authorizer_expires']){
                            $miniapp = $this->update_authorizer_access_token($appid,$miniprogram['authorizer_refresh_token']);
                            if($miniapp) {
                                $this->authorizer_access_token = $miniapp->authorizer_access_token;
                                $this->authorizer_refresh_token = $miniapp->authorizer_refresh_token;
                            } else {
                                $this->errorLog("更新小程序access_token失敗,appid:".$this->authorizer_appid,'');
                                exit;
                            }
                        } else {
                            $this->authorizer_access_token = $miniprogram['authorizer_access_token'];
                            $this->authorizer_refresh_token = $miniprogram['authorizer_refresh_token'];
                        }
        
                    } else {
                        $this->errorLog("小程序不存在,appid:".$this->authorizer_appid,'');
                        exit;
                    }
                } else {
                    $this->errorLog("請增加微信第三方公眾號平臺賬戶信息",'');
                    exit;
                }
            }
        
            /*
             * 設置小程序服務器地址,無需加https前綴,但域名必須可以通過https訪問
             * @params string / array $domains : 域名地址。只接收一維數(shù)組。
             * */
            public  function setServerDomain($domain = 'test.moh.cc')
            {
                $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token=".$this->authorizer_access_token;
                if(is_array($domain)) {
                    $https = ''; $wss = '';
                    foreach ($domain as $key => $value) {
                        $https .= '"https://'.$value.'",';
                        $wss .= '"wss://'.$value.'",';
                    }
                    $https = rtrim($https,',');
                    $wss = rtrim($wss,',');
                    $data = '{
                        "action":"add",
                        "requestdomain":['.$https.'],
                        "wsrequestdomain":['.$wss.'],
                        "uploaddomain":['.$https.'],
                        "downloaddomain":['.$https.']
                    }';
                } else {
                    $data = '{
                        "action":"add",
                        "requestdomain":"https://'.$domain.'",
                        "wsrequestdomain":"wss://'.$domain.'",
                        "uploaddomain":"https://'.$domain.'",
                        "downloaddomain":"https://'.$domain.'"
                    }';
                }
                $ret = json_decode(https_post($url,$data));
                if($ret->errcode == 0) {
                    return true;
                } else {
                    $this->errorLog("設置小程序服務器地址失敗,appid:".$this->authorizer_appid,$ret);
                    return false;
                }
            }
            /*
             * 設置小程序業(yè)務域名,無需加https前綴,但域名必須可以通過https訪問
             * @params string / array $domains : 域名地址。只接收一維數(shù)組。
             * */
            public function setBusinessDomain($domain = 'test.moh.cc')
            {
                $url = "https://api.weixin.qq.com/wxa/setwebviewdomain?access_token=".$this->authorizer_access_token;
                if(is_array($domain)) {
                    $https = '';
                    foreach ($domain as $key => $value) {
                        $https .= '"https://'.$value.'",';
                    }
                    $https = rtrim($https,',');
                    $data = '{
                        "action":"add",
                        "webviewdomain":['.$https.']
                    }';
                } else {
                    $data = '{
                        "action":"add",
                        "webviewdomain":"https://'.$domain.'"
                    }';
                }
        
                $ret = json_decode(https_post($url,$data));
                if($ret->errcode == 0) {
                    return true;
                } else {
                    $this->errorLog("設置小程序業(yè)務域名失敗,appid:".$this->authorizer_appid,$ret);
                    return false;
                }
            }
            /*
             * 成員管理,綁定小程序體驗者
             * @params string $wechatid : 體驗者的微信號
             * */
            public function bindMember($wechatid)
            {
                $url = "https://api.weixin.qq.com/wxa/bind_tester?access_token=".$this->authorizer_access_token;
                $data = '{"wechatid":"'.$wechatid.'"}';
                $ret = json_decode(https_post($url,$data));
                if($ret->errcode == 0) {
                    return true;
                } else {
                    $this->errorLog("綁定小程序體驗者操作失敗,appid:".$this->authorizer_appid,$ret);
                    return false;
                }
            }
            /*
             * 成員管理,解綁定小程序體驗者
             * @params string $wechatid : 體驗者的微信號
             * */
            public function unBindMember($wechatid)
            {
                $url = "https://api.weixin.qq.com/wxa/unbind_tester?access_token=".$this->authorizer_access_token;
                $data = '{"wechatid":"'.$wechatid.'"}';
                $ret = json_decode(https_post($url,$data));
                if($ret->errcode == 0) {
                    return true;
                } else {
                    $this->errorLog("解綁定小程序體驗者操作失敗,appid:".$this->authorizer_appid,$ret);
                    return false;
                }
            }
            /*
            * 成員管理,獲取小程序體驗者列表
            * */
            public function listMember()
            {
                $url = "https://api.weixin.qq.com/wxa/memberauth?access_token=".$this->authorizer_access_token;
                $data = '{"action":"get_experiencer"}';
                $ret = json_decode(https_post($url,$data));
                if($ret->errcode == 0) {
                    return $ret->members;
                } else {
                    $this->errorLog("獲取小程序體驗者列表操作失敗,appid:".$this->authorizer_appid,$ret);
                    return false;
                }
            }
            /*
             * 為授權的小程序帳號上傳小程序代碼
             * @params int $template_id : 模板ID
             * @params json $ext_json : 小程序配置文件,json格式
             * @params string $user_version : 代碼版本號
             * @params string $user_desc : 代碼描述
             * */
            public function uploadCode($template_id = 1, $user_version = 'v1.0.0', $user_desc = "魔盒CMS小程序模板庫")
            {
                $ext_json = json_encode('{"extEnable": true,"extAppid": "wx572****bfb","ext":{"appid": "'.$this->authorizer_appid.'"}}');
                $url = "https://api.weixin.qq.com/wxa/commit?access_token=".$this->authorizer_access_token;
                $data = '{"template_id":"'.$template_id.'","ext_json":'.$ext_json.',"user_version":"'.$user_version.'","user_desc":"'.$user_desc.'"}';
                $ret = json_decode(https_post($url,$data));
                if($ret->errcode == 0) {
                    return true;
                } else {
                    $this->errorLog("為授權的小程序帳號上傳小程序代碼操作失敗,appid:".$this->authorizer_appid,$ret);
                    return false;
                }
            }
            /*
             * 獲取體驗小程序的體驗二維碼
             * @params string $path :   指定體驗版二維碼跳轉到某個具體頁面
             * */
            public function getExpVersion($path = '')
            {
                if($path){
                    $url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token=".$this->authorizer_access_token."&path=".urlencode($path);
                } else {
                    $url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token=".$this->authorizer_access_token;
                }
                $ret = json_decode(https_get($url));
                if($ret->errcode) {
                    $this->errorLog("獲取體驗小程序的體驗二維碼操作失敗,appid:".$this->authorizer_appid,$ret);
                    return false;
                } else {
                    return $url;
                }
            }
            /*
             * 提交審核
             * @params string $tag : 小程序標簽,多個標簽以空格分開
             * @params strint $title : 小程序頁面標題,長度不超過32
             * */
            public function submitReview($tag = "魔盒CMS 微信投票 微網站 微信商城" ,$title = "魔盒CMS微信公眾號營銷小程序開發(fā)")
            {
                $first_class = '';$second_class = '';$first_id = 0;$second_id = 0;
                $address = "pages/index/index";
                $category = $this->getCategory();
                if(!empty($category)) {
                    $first_class = $category[0]->first_class ? $category[0]->first_class : '' ;
                    $second_class = $category[0]->second_class ? $category[0]->second_class : '';
                    $first_id = $category[0]->first_id ? $category[0]->first_id : 0;
                    $second_id = $category[0]->second_id ? $category[0]->second_id : 0;
                }
                $getpage = $this->getPage();
                if(!empty($getpage) && isset($getpage[0])) {
                    $address = $getpage[0];
                }
                $url = "https://api.weixin.qq.com/wxa/submit_audit?access_token=".$this->authorizer_access_token;
                $data = '{
                        "item_list":[{
                            "address":"'.$address.'",
                            "tag":"'.$tag.'",
                            "title":"'.$title.'",
                            "first_class":"'.$first_class.'",
                            "second_class":"'.$second_class.'",
                            "first_id":"'.$first_id.'",
                            "second_id":"'.$second_id.'"
                        }]
                    }';
                $ret = json_decode(https_post($url,$data));
                if($ret->errcode == 0) {
                    Db::name('wxminiprogram_audit')->insert([
                        'appid'=>$this->authorizer_appid,
                        'auditid'=>$ret->auditid,
                        'create_time'=>date('Y-m-d H:i:s')
                    ]);
                    return true;
                } else {
                    $this->errorLog("小程序提交審核操作失敗,appid:".$this->authorizer_appid,$ret);
                    return false;
                }
            }
            /*
             * 小程序審核撤回
             * 單個帳號每天審核撤回次數(shù)最多不超過1次,一個月不超過10次。
             * */
            public function unDoCodeAudit()
            {
                $url = "https://api.weixin.qq.com/wxa/undocodeaudit?access_token=".$this->authorizer_access_token;
                $ret = json_decode(https_get($url));
                if($ret->errcode == 0) {
                    return true;
                } else {
                    $this->errorLog("小程序審核撤回操作失敗,appid:".$this->authorizer_appid,$ret);
                    return false;
                }
            }
            /*
             * 查詢指定版本的審核狀態(tài)
             * @params string $auditid : 提交審核時獲得的審核id
             * */
            public function getAuditStatus($auditid)
            {
                $url = "https://api.weixin.qq.com/wxa/get_auditstatus?access_token=".$this->authorizer_access_token;
                $data = '{"auditid":"'.$auditid.'"}';
                $ret = json_decode(https_post($url,$data));
                if($ret->errcode == 0) {
                    $reason = $ret->reason ? $ret->reason : '';
                    Db::name('wxminiprogram_audit')->where(['appid'=>$this->authorizer_appid,'auditid'=>$auditid])->update([
                        'status'=>$ret->status,
                        'reason'=>$reason
                    ]);
                    return true;
                } else {
                    $this->errorLog("查詢指定版本的審核狀態(tài)操作失敗,appid:".$this->authorizer_appid,$ret);
                    return false;
                }
            }
            /*
             * 查詢最新一次提交的審核狀態(tài)
             * */
            public function getLastAudit()
            {
                $url = "https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=".$this->authorizer_access_token;
                $ret = json_decode(https_get($url));
                if($ret->errcode == 0) {
                    $reason = $ret->reason ? $ret->reason : '';
                    Db::name('wxminiprogram_audit')->where(['appid'=>$this->authorizer_appid,'auditid'=>$ret->auditid])->update([
                        'status'=>$ret->status,
                        'reason'=>$reason
                    ]);
                    return $ret->auditid;
                } else {
                    $this->errorLog("查詢最新一次提交的審核狀態(tài)操作失敗,appid:".$this->authorizer_appid,$ret);
                    return false;
                }
            }
            /*
             * 發(fā)布已通過審核的小程序
             * */
            public function release()
            {
                $url = "https://api.weixin.qq.com/wxa/release?access_token=".$this->authorizer_access_token;
                $data = '{}';
                $ret = json_decode(https_post($url,$data));
                if($ret->errcode == 0) {
                    return true;
                } else {
                    $this->errorLog("發(fā)布已通過審核的小程序操作失敗,appid:".$this->authorizer_appid,$ret);
                    return $ret->errcode;
                }
            }
            /*
             * 獲取授權小程序帳號的可選類目
             * */
            private function getCategory()
            {
                $url = "https://api.weixin.qq.com/wxa/get_category?access_token=".$this->authorizer_access_token;
                $ret = json_decode(https_get($url));
                if($ret->errcode == 0) {
                    return $ret->category_list;
                } else {
                    $this->errorLog("獲取授權小程序帳號的可選類目操作失敗,appid:".$this->authorizer_appid,$ret);
                    return false;
                }
            }
            /*
             * 獲取小程序的第三方提交代碼的頁面配置
             * */
            private function getPage()
            {
                $url = "https://api.weixin.qq.com/wxa/get_page?access_token=".$this->authorizer_access_token;
                $ret = json_decode(https_get($url));
                if($ret->errcode == 0) {
                    return $ret->page_list;
                } else {
                    $this->errorLog("獲取小程序的第三方提交代碼的頁面配置失敗,appid:".$this->authorizer_appid,$ret);
                    return false;
                }
            }
            /*
            * 更新授權小程序的authorizer_access_token
            * @params string $appid : 小程序appid
            * @params string $refresh_token : 小程序authorizer_refresh_token
            * */
            private function update_authorizer_access_token($appid,$refresh_token)
            {
                $url = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=' . $this->thirdAccessToken;
                $data = '{"component_appid":"' . $this->thirdAppId . '","authorizer_appid":"' . $appid . '","authorizer_refresh_token":"' . $refresh_token . '"}';
                $ret = json_decode(https_post($url, $data));
                if (isset($ret->authorizer_access_token)) {
                    Db::name('wxminiprograms')->where(['authorizer_appid' => $appid])->update(['authorizer_access_token' => $ret->authorizer_access_token, 'authorizer_expires' => (time() + 7200), 'authorizer_refresh_token' => $ret->authorizer_refresh_token]);
                    return $ret;
                } else {
                    $this->errorLog("更新授權小程序的authorizer_access_token操作失敗,appid:".$appid,$ret);
                    return null;
                }
            }
        
            private function errorLog($msg,$ret)
            {
                file_put_contents(ROOT_PATH . 'runtime/error/miniprogram.log', "[" . date('Y-m-d H:i:s') . "] ".$msg."," .json_encode($ret).PHP_EOL, FILE_APPEND);
            }
        }
        <?php
        //代小程序實現(xiàn)業(yè)務示例包
        namespace app\user\controller;
        use app\home\model\Miniprogram;
        use think\Db;
        class Wxminiprogram extends Pub
        {
            public $appid = 'wx57****1bfb';    //需要實現(xiàn)業(yè)務小程序appid
            public function index()
            {
                return view();
            }
            public function doAction()
            {
                if(request()->isPost()) {
                    $action = input('action');
                    $mini = new Miniprogram($this->appid);
                    if($action == 'auth') {
                        //小程序授權
                        echo '<script>alert("已授權");history.back();</script>';
                    } elseif($action == 'setServerDomain') {
                        //設置小程序服務器域名地址
                        if($mini->setServerDomain()){
                            echo '<script>alert("設置小程序服務器域名操作成功");history.back();</script>';
                        } else {
                            echo '<script>alert("設置小程序服務器域名操作失敗或已設置,請查看日志");history.back();</script>';
                        }
                    }  elseif($action == 'setBusinessDomain') {
                        //設置業(yè)務域名
                        if($mini->setBusinessDomain()){
                            echo '<script>alert("設置小程序業(yè)務域名操作成功");history.back();</script>';
                        } else {
                            echo '<script>alert("設置小程序業(yè)務域名操作失敗或已設置,請查看日志");history.back();</script>';
                        }
                    }  elseif($action == 'bind') {
                        //綁定小程序體驗者
                        $wechatid = input('wechatid');
                        if($wechatid) {
                            if($mini->bindMember($wechatid)){
                                echo '<script>alert("綁定小程序體驗者操作成功");history.back();</script>';
                            } else {
                                echo '<script>alert("綁定小程序體驗者操作失敗,請查看日志");history.back();</script>';
                            }
                        } else {
                            echo '<script>alert("請輸入微信號");history.back();</script>';
                        }
        
                    }  elseif($action == 'uploadCode') {
                        //上傳小程序代碼
                        if($mini->uploadCode(2)){
                            echo '<script>alert("上傳小程序代碼操作成功");history.back();</script>';
                        } else {
                            echo '<script>alert("上傳小程序代碼操作失敗,請查看日志");history.back();</script>';
                        }
                    }  elseif($action == 'getExpVersion') {
                        //獲取體驗小程序的體驗二維碼
                        $qrcode = $mini->getExpVersion();
                        if($qrcode){
                            echo '<script>window.location.href="'.$qrcode.'";</script>';
                        } else {
                            echo '<script>alert("獲取體驗小程序的體驗二維碼操作失敗");history.back();</script>';
                        }
                    } elseif($action == 'review') {
                        //提交審核
                        $auditid = Db::name('wxminiprogram_audit')->where(['appid'=>$this->appid,'status'=>['neq',0]])->order('create_time','desc')->value('auditid');
                        if($auditid){
                            echo '<script>alert("有待處理的版本,請先處理該版本相關事項再提交新的審核。審核ID:'.$auditid.'");history.back();</script>';
                        } else {
                            if($mini->submitReview()){
                                echo '<script>alert("小程序提交審核操作成功");history.back();</script>';
                            } else {
                                echo '<script>alert("小程序提交審核操作失敗,請查看日志");history.back();</script>';
                            }
                        }
                    } elseif($action == 'getAudit') {
                        //查詢指定版本的審核狀態(tài)
                        $auditid = input('auditid');
                        if($auditid) {
                            if($mini->getAuditStatus($auditid)){
                                $audit = Db::name('wxminiprogram_audit')->where(['appid'=>$this->appid,'auditid'=>$auditid])->field('status,reason')->find();
                                if($audit['status'] == 0) {
                                    echo '<script>alert("該版本審核已通過");history.back();</script>';
                                } elseif($audit['status'] == 1) {
                                    echo '<script>alert("該版本審核失敗,原因:'.$audit['reason'].'");history.back();</script>';
                                } elseif($audit['status'] == 2) {
                                    echo '<script>alert("該版本小程序正在審核中......");history.back();</script>';
                                } else {
                                    echo '<script>alert("未知狀態(tài)......");history.back();</script>';
                                }
                            } else {
                                echo '<script>alert("查詢指定版本的審核狀態(tài)操作失敗,請查看日志");history.back();</script>';
                            }
                        } else {
                            echo '<script>alert("請輸入要查詢的審核ID");history.back();</script>';
                        }
                    } elseif($action == 'lastAudit') {
                        //查詢最新一次提交的審核狀態(tài)
                        $auditid = $mini->getLastAudit();
                        if($auditid){
                            $audit = Db::name('wxminiprogram_audit')->where(['appid'=>$this->appid,'auditid'=>$auditid])->field('status,reason')->find();
                            if($audit['status'] == 0) {
                                echo '<script>alert("審核已通過");history.back();</script>';
                            } elseif($audit['status'] == 1) {
                                echo '<script>alert("審核失敗,原因:'.$audit['reason'].'");history.back();</script>';
                            } elseif($audit['status'] == 2) {
                                echo '<script>alert("小程序正在審核中......");history.back();</script>';
                            } else {
                                echo '<script>alert("未知狀態(tài)......");history.back();</script>';
                            }
                        }else {
                            echo '<script>alert("查詢最新一次提交的審核狀態(tài)操作失敗,請查看日志");history.back();</script>';
                        }
                    } elseif($action == 'release') {
                        //發(fā)布已通過審核的小程序
                        $auditid = Db::name('wxminiprogram_audit')->where(['appid'=>$this->appid,'status'=>['neq',0]])->order('create_time','desc')->value('auditid');
                        if($auditid){
                            echo '<script>alert("有待處理的版本,請先處理該版本相關事項再發(fā)布版本。審核ID:'.$auditid.'");history.back();</script>';
                        } else {
                            $errcode = $mini->release();
                            if($errcode){
                                echo '<script>alert("已發(fā)版");history.back();</script>';
                            } else {
                                echo '<script>alert("發(fā)版失敗,錯誤代碼:'.$errcode.'");history.back();</script>';
                            }
                        }
                    }
                }
            }
        }

        wxminiprograms數(shù)據(jù)表,保存已授權小程序的基本信息及授權相關信息(authorizer_access_token/authorizer_refresh_token)這兩個值很重要,代小程序實現(xiàn)業(yè)務基本上是通過這兩個值來實現(xiàn)

        -- Adminer 4.6.2 MySQL dump
        
        SET NAMES utf8;
        SET time_zone = '+00:00';
        SET foreign_key_checks = 0;
        SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
        
        DROP TABLE IF EXISTS `wxminiprograms`;
        CREATE TABLE `wxminiprograms` (
          `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
          `uid` int(10) unsigned NOT NULL COMMENT '用戶ID',
          `nick_name` varchar(45) DEFAULT NULL COMMENT '微信小程序名稱',
          `alias` varchar(45) DEFAULT NULL COMMENT '別名',
          `token` varchar(45) DEFAULT NULL COMMENT '平臺生成的token值',
          `head_img` varchar(255) DEFAULT NULL COMMENT '微信小程序頭像',
          `verify_type_info` tinyint(1) DEFAULT NULL COMMENT '授權方認證類型,-1代表未認證,0代表微信認證',
          `is_show` tinyint(1) DEFAULT '0' COMMENT '是否顯示,0顯示,1隱藏',
          `user_name` varchar(45) DEFAULT NULL COMMENT '原始ID',
          `qrcode_url` varchar(255) DEFAULT NULL COMMENT '二維碼圖片的URL',
          `business_info` varchar(255) DEFAULT NULL COMMENT 'json格式。用以了解以下功能的開通狀況(0代表未開通,1代表已開通): open_store:是否開通微信門店功能 open_scan:是否開通微信掃商品功能 open_pay:是否開通微信支付功能 open_card:是否開通微信卡券功能 open_shake:是否開通微信搖一搖功能',
          `idc` int(10) unsigned DEFAULT NULL COMMENT 'idc',
          `principal_name` varchar(45) DEFAULT NULL COMMENT '小程序的主體名稱',
          `signature` varchar(255) DEFAULT NULL COMMENT '帳號介紹',
          `miniprograminfo` varchar(255) DEFAULT NULL COMMENT 'json格式。判斷是否為小程序類型授權,包含network小程序已設置的各個服務器域名',
          `func_info` longtext COMMENT 'json格式。權限集列表,ID為17到19時分別代表: 17.帳號管理權限 18.開發(fā)管理權限 19.客服消息管理權限 請注意: 1)該字段的返回不會考慮小程序是否具備該權限集的權限(因為可能部分具備)。',
          `authorizer_appid` varchar(45) DEFAULT NULL COMMENT '小程序appid',
          `authorizer_access_token` varchar(255) DEFAULT NULL COMMENT '授權方接口調用憑據(jù)(在授權的公眾號或小程序具備API權限時,才有此返回值),也簡稱為令牌',
          `authorizer_expires` int(10) unsigned DEFAULT NULL COMMENT 'refresh有效期',
          `authorizer_refresh_token` varchar(255) DEFAULT NULL COMMENT '接口調用憑據(jù)刷新令牌',
          `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '授權時間',
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='微信小程序授權列表';
        
        
        -- 2018-07-25 09:32:49

        wxminiprogram_audit數(shù)據(jù)表,保存提交審核的小程序

        -- Adminer 4.6.2 MySQL dump
        
        SET NAMES utf8;
        SET time_zone = '+00:00';
        SET foreign_key_checks = 0;
        SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
        
        DROP TABLE IF EXISTS `wxminiprogram_audit`;
        CREATE TABLE `wxminiprogram_audit` (
          `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
          `appid` varchar(45) NOT NULL COMMENT '小程序appid',
          `auditid` varchar(45) NOT NULL COMMENT '審核編號',
          `status` tinyint(1) unsigned NOT NULL DEFAULT '3' COMMENT '審核狀態(tài),其中0為審核成功,1為審核失敗,2為審核中,3已提交審核',
          `reason` varchar(255) DEFAULT NULL COMMENT '當status=1,審核被拒絕時,返回的拒絕原因',
          `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '提交審核時間',
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='微信小程序提交審核的小程序';
        
        
        -- 2018-07-25 09:35:07

        3839345434-5b28cd7a43128_articlex.png

        2715287353-5b239018f1482_articlex.pngx

        相關推薦:

        微信開發(fā)公眾號平臺視頻教程

        微信公眾平臺開發(fā)者文檔

        PHP微信公眾平臺開發(fā)視頻教程

        以上就是終于整理出來了,用微信第三方平臺授權小程序業(yè)務的詳細內容,更多請關注php中文網其它相關文章!


        微信提供公眾平臺、朋友圈、消息推送等功能,用戶可以通過“搖一搖”、“搜索號碼”、“附近的人”、掃二維碼方式添加好友和關注公眾平臺,同時微信將內容分享給好友以及將用戶看到的精彩內容分享到微信朋友圈。

        溫馨提示:喜歡本站的話,請收藏一下本站!

        本類教程下載

        系統(tǒng)下載排行

        主站蜘蛛池模板: 免费视频专区一国产盗摄| 亚洲五月六月丁香激情| 精品亚洲综合在线第一区| 亚洲AV乱码一区二区三区林ゆな| 亚洲导航深夜福利| 亚洲AV无码一区二区三区性色 | 久久国产亚洲观看| 亚洲欧美成人一区二区三区| 日韩一级片免费观看| 性色午夜视频免费男人的天堂| 午夜成年女人毛片免费观看| 不卡一卡二卡三亚洲| 亚洲国产午夜精品理论片| 男性gay黄免费网站| 91大神免费观看| 亚洲欧洲自拍拍偷精品 美利坚 | 成人最新午夜免费视频| 国产亚洲美日韩AV中文字幕无码成人 | 久久夜色精品国产亚洲| 久久夜色精品国产噜噜亚洲a| 国产A∨免费精品视频| 91在线视频免费播放| 亚洲国产精品福利片在线观看| 亚洲精品女同中文字幕| 99久9在线|免费| 亚洲国产成人精品91久久久| 亚洲成aⅴ人片在线影院八| 亚洲免费视频一区二区三区| 成人啪精品视频免费网站| 久久久久亚洲AV片无码| 最好2018中文免费视频| 天天看片天天爽_免费播放| 亚洲av色影在线| 中文字幕免费在线看线人动作大片 | 免费高清小黄站在线观看| 亚洲一区二区三区日本久久九| 一级做a爰黑人又硬又粗免费看51社区国产精品视 | 国产视频精品免费视频| 国产男女猛烈无遮挡免费视频网站| 亚洲一区二区电影| 国产麻豆一精品一AV一免费 |