首页 > 微信小程序 > 微信开发 > 终于整理出来了,用微信第三方平台授权小程序业务

终于整理出来了,用微信第三方平台授权小程序业务

php是最好的语言
发布: 2018-07-25 14:16:05
原创
41282 人浏览过

本文章是自己编写的,用微信第三方平台开发实现小程序业务,代码很全,每一步都有详细介绍,供大家学习参考。

第一步:申请微信开放平台帐号并创建第三方平台

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 = &#39;wx3e******165c&#39;;            //第三方平台应用appid
    private $appsecret = &#39;13e**********d039&#39;;     //第三方平台应用appsecret
    private $token = &#39;ePF58******Q2Ae&#39;;           //第三方平台应用token(消息校验Token)
    private $encodingAesKey = &#39;bzH***FCamD&#39;;      //第三方平台应用Key(消息加解密Key)
    private $component_ticket= &#39;ticket@**xv-g&#39;;   //微信后台推送的ticket,用于获取第三方平台接口调用凭据
    
    /*
    * 扫码授权,注意此URL必须放置在页面当中用户点击进行跳转,不能通过程序跳转,否则将出现“请确认授权入口页所在域名,与授权后回调页所在域名相同....”错误
    * @params string $redirect_uri : 扫码成功后的回调地址
    * @params int $auth_type : 授权类型,1公众号,2小程序,3公众号/小程序同时展现。不传参数默认都展示    
    */
    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 = &#39;{
            "component_appid":"&#39;.$this->appid.&#39;" ,
            "component_appsecret": "&#39;.$this->appsecret.&#39;",
            "component_verify_ticket": "&#39;.$this->component_ticket.&#39;"
        }&#39;;
        $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 = &#39;{"component_appid":"&#39;.$this->appid.&#39;"}&#39;;
        $ret = json_decode($this->https_post($url,$data));
        if($ret->errcode == 0) {
            return $ret->pre_auth_code;
        } else {
            return $ret->errcode;
        }
    }
    
    /*
    * 发起POST网络提交
    * @params string $url : 网络地址
    * @params json $data : 发送的json格式数据
    */
    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;
    }
     /*
    * 发起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 &#39;Errno&#39;.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 = &#39;wx3e******165c&#39;;            //第三方平台应用appid
    private $appsecret = &#39;13e**********d039&#39;;     //第三方平台应用appsecret
    private $token = &#39;ePF58******Q2Ae&#39;;           //第三方平台应用token(消息校验Token)
    private $encodingAesKey = &#39;bzH***FCamD&#39;;      //第三方平台应用Key(消息加解密Key)
    private $component_ticket= &#39;ticket@**xv-g&#39;;   //微信后台推送的ticket,用于获取第三方平台接口调用凭据
    /*
    *    接收微信官方推送的消息(每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(&#39;wxBizMsgCrypt.php&#39;);
        $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(&#39;authorizer_appid&#39;,$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(&#39;weixin_account&#39;)->where([&#39;type&#39;=>1])->field(&#39;id,appId,appSecret,component_access_token,token_expires&#39;)->find();
        if($weixin_account[&#39;token_expires&#39;] <= time() ) {
            $apiUrl = &#39;https://api.weixin.qq.com/cgi-bin/component/api_component_token&#39;;
            $data = &#39;{"component_appid":"&#39;.$weixin_account[&#39;appId&#39;].&#39;" ,"component_appsecret": "&#39;.$weixin_account[&#39;appSecret&#39;].&#39;","component_verify_ticket": "&#39;.$component_verify_ticket.&#39;"}&#39;;
            $json = json_decode(_request($apiUrl,$data));
            if(isset($json->component_access_token)) {
                Db::name(&#39;weixin_account&#39;)->where([&#39;id&#39;=>$weixin_account[&#39;id&#39;]])->update([&#39;component_access_token&#39;=>$json->component_access_token,&#39;token_expires&#39;=>time()+7200]);
            }
        }
    }
}
登录后复制
<?php
/*
*    代小程序实现业务
*/
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(&#39;weixin_account&#39;)->where([&#39;type&#39; => 1])->field(&#39;token,encodingAesKey,appId,component_access_token&#39;)->find();
        if ($weixin_account) {
            $this->thirdAppId = $weixin_account[&#39;appId&#39;];
            $this->encodingAesKey = $weixin_account[&#39;encodingAesKey&#39;];
            $this->thirdToken = $weixin_account[&#39;token&#39;];
            $this->thirdAccessToken = $weixin_account[&#39;component_access_token&#39;];

            $miniprogram = Db::name(&#39;wxminiprograms&#39;)->where(&#39;authorizer_appid&#39;,$appid)
                ->field(&#39;authorizer_access_token,authorizer_refresh_token,authorizer_expires&#39;)->find();
            if($miniprogram){
                $this->authorizer_appid = $appid;
                if(time() > $miniprogram[&#39;authorizer_expires&#39;]){
                    $miniapp = $this->update_authorizer_access_token($appid,$miniprogram[&#39;authorizer_refresh_token&#39;]);
                    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,&#39;&#39;);
                        exit;
                    }
                } else {
                    $this->authorizer_access_token = $miniprogram[&#39;authorizer_access_token&#39;];
                    $this->authorizer_refresh_token = $miniprogram[&#39;authorizer_refresh_token&#39;];
                }

            } else {
                $this->errorLog("小程序不存在,appid:".$this->authorizer_appid,&#39;&#39;);
                exit;
            }
        } else {
            $this->errorLog("请增加微信第三方公众号平台账户信息",&#39;&#39;);
            exit;
        }
    }

    /*
     * 设置小程序服务器地址,无需加https前缀,但域名必须可以通过https访问
     * @params string / array $domains : 域名地址。只接收一维数组。
     * */
    public  function setServerDomain($domain = &#39;test.moh.cc&#39;)
    {
        $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token=".$this->authorizer_access_token;
        if(is_array($domain)) {
            $https = &#39;&#39;; $wss = &#39;&#39;;
            foreach ($domain as $key => $value) {
                $https .= &#39;"https://&#39;.$value.&#39;",&#39;;
                $wss .= &#39;"wss://&#39;.$value.&#39;",&#39;;
            }
            $https = rtrim($https,&#39;,&#39;);
            $wss = rtrim($wss,&#39;,&#39;);
            $data = &#39;{
                "action":"add",
                "requestdomain":[&#39;.$https.&#39;],
                "wsrequestdomain":[&#39;.$wss.&#39;],
                "uploaddomain":[&#39;.$https.&#39;],
                "downloaddomain":[&#39;.$https.&#39;]
            }&#39;;
        } else {
            $data = &#39;{
                "action":"add",
                "requestdomain":"https://&#39;.$domain.&#39;",
                "wsrequestdomain":"wss://&#39;.$domain.&#39;",
                "uploaddomain":"https://&#39;.$domain.&#39;",
                "downloaddomain":"https://&#39;.$domain.&#39;"
            }&#39;;
        }
        $ret = json_decode(https_post($url,$data));
        if($ret->errcode == 0) {
            return true;
        } else {
            $this->errorLog("设置小程序服务器地址失败,appid:".$this->authorizer_appid,$ret);
            return false;
        }
    }
    /*
     * 设置小程序业务域名,无需加https前缀,但域名必须可以通过https访问
     * @params string / array $domains : 域名地址。只接收一维数组。
     * */
    public function setBusinessDomain($domain = &#39;test.moh.cc&#39;)
    {
        $url = "https://api.weixin.qq.com/wxa/setwebviewdomain?access_token=".$this->authorizer_access_token;
        if(is_array($domain)) {
            $https = &#39;&#39;;
            foreach ($domain as $key => $value) {
                $https .= &#39;"https://&#39;.$value.&#39;",&#39;;
            }
            $https = rtrim($https,&#39;,&#39;);
            $data = &#39;{
                "action":"add",
                "webviewdomain":[&#39;.$https.&#39;]
            }&#39;;
        } else {
            $data = &#39;{
                "action":"add",
                "webviewdomain":"https://&#39;.$domain.&#39;"
            }&#39;;
        }

        $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 bindMember($wechatid)
    {
        $url = "https://api.weixin.qq.com/wxa/bind_tester?access_token=".$this->authorizer_access_token;
        $data = &#39;{"wechatid":"&#39;.$wechatid.&#39;"}&#39;;
        $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 = &#39;{"wechatid":"&#39;.$wechatid.&#39;"}&#39;;
        $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 = &#39;{"action":"get_experiencer"}&#39;;
        $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 = &#39;v1.0.0&#39;, $user_desc = "魔盒CMS小程序模板库")
    {
        $ext_json = json_encode(&#39;{"extEnable": true,"extAppid": "wx572****bfb","ext":{"appid": "&#39;.$this->authorizer_appid.&#39;"}}&#39;);
        $url = "https://api.weixin.qq.com/wxa/commit?access_token=".$this->authorizer_access_token;
        $data = &#39;{"template_id":"&#39;.$template_id.&#39;","ext_json":&#39;.$ext_json.&#39;,"user_version":"&#39;.$user_version.&#39;","user_desc":"&#39;.$user_desc.&#39;"}&#39;;
        $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 = &#39;&#39;)
    {
        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微信公众号营销小程序开发")
    {
        $first_class = &#39;&#39;;$second_class = &#39;&#39;;$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 : &#39;&#39; ;
            $second_class = $category[0]->second_class ? $category[0]->second_class : &#39;&#39;;
            $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 = &#39;{
                "item_list":[{
                    "address":"&#39;.$address.&#39;",
                    "tag":"&#39;.$tag.&#39;",
                    "title":"&#39;.$title.&#39;",
                    "first_class":"&#39;.$first_class.&#39;",
                    "second_class":"&#39;.$second_class.&#39;",
                    "first_id":"&#39;.$first_id.&#39;",
                    "second_id":"&#39;.$second_id.&#39;"
                }]
            }&#39;;
        $ret = json_decode(https_post($url,$data));
        if($ret->errcode == 0) {
            Db::name(&#39;wxminiprogram_audit&#39;)->insert([
                &#39;appid&#39;=>$this->authorizer_appid,
                &#39;auditid&#39;=>$ret->auditid,
                &#39;create_time&#39;=>date(&#39;Y-m-d H:i:s&#39;)
            ]);
            return true;
        } else {
            $this->errorLog("小程序提交审核操作失败,appid:".$this->authorizer_appid,$ret);
            return false;
        }
    }
    /*
     * 小程序审核撤回
     * 单个帐号每天审核撤回次数最多不超过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;
        }
    }
    /*
     * 查询指定版本的审核状态
     * @params string $auditid : 提交审核时获得的审核id
     * */
    public function getAuditStatus($auditid)
    {
        $url = "https://api.weixin.qq.com/wxa/get_auditstatus?access_token=".$this->authorizer_access_token;
        $data = &#39;{"auditid":"&#39;.$auditid.&#39;"}&#39;;
        $ret = json_decode(https_post($url,$data));
        if($ret->errcode == 0) {
            $reason = $ret->reason ? $ret->reason : &#39;&#39;;
            Db::name(&#39;wxminiprogram_audit&#39;)->where([&#39;appid&#39;=>$this->authorizer_appid,&#39;auditid&#39;=>$auditid])->update([
                &#39;status&#39;=>$ret->status,
                &#39;reason&#39;=>$reason
            ]);
            return true;
        } else {
            $this->errorLog("查询指定版本的审核状态操作失败,appid:".$this->authorizer_appid,$ret);
            return false;
        }
    }
    /*
     * 查询最新一次提交的审核状态
     * */
    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 : &#39;&#39;;
            Db::name(&#39;wxminiprogram_audit&#39;)->where([&#39;appid&#39;=>$this->authorizer_appid,&#39;auditid&#39;=>$ret->auditid])->update([
                &#39;status&#39;=>$ret->status,
                &#39;reason&#39;=>$reason
            ]);
            return $ret->auditid;
        } else {
            $this->errorLog("查询最新一次提交的审核状态操作失败,appid:".$this->authorizer_appid,$ret);
            return false;
        }
    }
    /*
     * 发布已通过审核的小程序
     * */
    public function release()
    {
        $url = "https://api.weixin.qq.com/wxa/release?access_token=".$this->authorizer_access_token;
        $data = &#39;{}&#39;;
        $ret = json_decode(https_post($url,$data));
        if($ret->errcode == 0) {
            return true;
        } else {
            $this->errorLog("发布已通过审核的小程序操作失败,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 = &#39;https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=&#39; . $this->thirdAccessToken;
        $data = &#39;{"component_appid":"&#39; . $this->thirdAppId . &#39;","authorizer_appid":"&#39; . $appid . &#39;","authorizer_refresh_token":"&#39; . $refresh_token . &#39;"}&#39;;
        $ret = json_decode(https_post($url, $data));
        if (isset($ret->authorizer_access_token)) {
            Db::name(&#39;wxminiprograms&#39;)->where([&#39;authorizer_appid&#39; => $appid])->update([&#39;authorizer_access_token&#39; => $ret->authorizer_access_token, &#39;authorizer_expires&#39; => (time() + 7200), &#39;authorizer_refresh_token&#39; => $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 . &#39;runtime/error/miniprogram.log&#39;, "[" . date(&#39;Y-m-d H:i:s&#39;) . "] ".$msg."," .json_encode($ret).PHP_EOL, FILE_APPEND);
    }
}
登录后复制
<?php
//代小程序实现业务示例包
namespace app\user\controller;
use app\home\model\Miniprogram;
use think\Db;
class Wxminiprogram extends Pub
{
    public $appid = &#39;wx57****1bfb&#39;;    //需要实现业务小程序appid
    public function index()
    {
        return view();
    }
    public function doAction()
    {
        if(request()->isPost()) {
            $action = input(&#39;action&#39;);
            $mini = new Miniprogram($this->appid);
            if($action == &#39;auth&#39;) {
                //小程序授权
                echo &#39;<script>alert("已授权");history.back();</script>&#39;;
            } elseif($action == &#39;setServerDomain&#39;) {
                //设置小程序服务器域名地址
                if($mini->setServerDomain()){
                    echo &#39;<script>alert("设置小程序服务器域名操作成功");history.back();</script>&#39;;
                } else {
                    echo &#39;<script>alert("设置小程序服务器域名操作失败或已设置,请查看日志");history.back();</script>&#39;;
                }
            }  elseif($action == &#39;setBusinessDomain&#39;) {
                //设置业务域名
                if($mini->setBusinessDomain()){
                    echo &#39;<script>alert("设置小程序业务域名操作成功");history.back();</script>&#39;;
                } else {
                    echo &#39;<script>alert("设置小程序业务域名操作失败或已设置,请查看日志");history.back();</script>&#39;;
                }
            }  elseif($action == &#39;bind&#39;) {
                //绑定小程序体验者
                $wechatid = input(&#39;wechatid&#39;);
                if($wechatid) {
                    if($mini->bindMember($wechatid)){
                        echo &#39;<script>alert("绑定小程序体验者操作成功");history.back();</script>&#39;;
                    } else {
                        echo &#39;<script>alert("绑定小程序体验者操作失败,请查看日志");history.back();</script>&#39;;
                    }
                } else {
                    echo &#39;<script>alert("请输入微信号");history.back();</script>&#39;;
                }

            }  elseif($action == &#39;uploadCode&#39;) {
                //上传小程序代码
                if($mini->uploadCode(2)){
                    echo &#39;<script>alert("上传小程序代码操作成功");history.back();</script>&#39;;
                } else {
                    echo &#39;<script>alert("上传小程序代码操作失败,请查看日志");history.back();</script>&#39;;
                }
            }  elseif($action == &#39;getExpVersion&#39;) {
                //获取体验小程序的体验二维码
                $qrcode = $mini->getExpVersion();
                if($qrcode){
                    echo &#39;<script>window.location.href="&#39;.$qrcode.&#39;";</script>&#39;;
                } else {
                    echo &#39;<script>alert("获取体验小程序的体验二维码操作失败");history.back();</script>&#39;;
                }
            } elseif($action == &#39;review&#39;) {
                //提交审核
                $auditid = Db::name(&#39;wxminiprogram_audit&#39;)->where([&#39;appid&#39;=>$this->appid,&#39;status&#39;=>[&#39;neq&#39;,0]])->order(&#39;create_time&#39;,&#39;desc&#39;)->value(&#39;auditid&#39;);
                if($auditid){
                    echo &#39;<script>alert("有待处理的版本,请先处理该版本相关事项再提交新的审核。审核ID:&#39;.$auditid.&#39;");history.back();</script>&#39;;
                } else {
                    if($mini->submitReview()){
                        echo &#39;<script>alert("小程序提交审核操作成功");history.back();</script>&#39;;
                    } else {
                        echo &#39;<script>alert("小程序提交审核操作失败,请查看日志");history.back();</script>&#39;;
                    }
                }
            } elseif($action == &#39;getAudit&#39;) {
                //查询指定版本的审核状态
                $auditid = input(&#39;auditid&#39;);
                if($auditid) {
                    if($mini->getAuditStatus($auditid)){
                        $audit = Db::name(&#39;wxminiprogram_audit&#39;)->where([&#39;appid&#39;=>$this->appid,&#39;auditid&#39;=>$auditid])->field(&#39;status,reason&#39;)->find();
                        if($audit[&#39;status&#39;] == 0) {
                            echo &#39;<script>alert("该版本审核已通过");history.back();</script>&#39;;
                        } elseif($audit[&#39;status&#39;] == 1) {
                            echo &#39;<script>alert("该版本审核失败,原因:&#39;.$audit[&#39;reason&#39;].&#39;");history.back();</script>&#39;;
                        } elseif($audit[&#39;status&#39;] == 2) {
                            echo &#39;<script>alert("该版本小程序正在审核中......");history.back();</script>&#39;;
                        } else {
                            echo &#39;<script>alert("未知状态......");history.back();</script>&#39;;
                        }
                    } else {
                        echo &#39;<script>alert("查询指定版本的审核状态操作失败,请查看日志");history.back();</script>&#39;;
                    }
                } else {
                    echo &#39;<script>alert("请输入要查询的审核ID");history.back();</script>&#39;;
                }
            } elseif($action == &#39;lastAudit&#39;) {
                //查询最新一次提交的审核状态
                $auditid = $mini->getLastAudit();
                if($auditid){
                    $audit = Db::name(&#39;wxminiprogram_audit&#39;)->where([&#39;appid&#39;=>$this->appid,&#39;auditid&#39;=>$auditid])->field(&#39;status,reason&#39;)->find();
                    if($audit[&#39;status&#39;] == 0) {
                        echo &#39;<script>alert("审核已通过");history.back();</script>&#39;;
                    } elseif($audit[&#39;status&#39;] == 1) {
                        echo &#39;<script>alert("审核失败,原因:&#39;.$audit[&#39;reason&#39;].&#39;");history.back();</script>&#39;;
                    } elseif($audit[&#39;status&#39;] == 2) {
                        echo &#39;<script>alert("小程序正在审核中......");history.back();</script>&#39;;
                    } else {
                        echo &#39;<script>alert("未知状态......");history.back();</script>&#39;;
                    }
                }else {
                    echo &#39;<script>alert("查询最新一次提交的审核状态操作失败,请查看日志");history.back();</script>&#39;;
                }
            } elseif($action == &#39;release&#39;) {
                //发布已通过审核的小程序
                $auditid = Db::name(&#39;wxminiprogram_audit&#39;)->where([&#39;appid&#39;=>$this->appid,&#39;status&#39;=>[&#39;neq&#39;,0]])->order(&#39;create_time&#39;,&#39;desc&#39;)->value(&#39;auditid&#39;);
                if($auditid){
                    echo &#39;<script>alert("有待处理的版本,请先处理该版本相关事项再发布版本。审核ID:&#39;.$auditid.&#39;");history.back();</script>&#39;;
                } else {
                    $errcode = $mini->release();
                    if($errcode){
                        echo &#39;<script>alert("已发版");history.back();</script>&#39;;
                    } else {
                        echo &#39;<script>alert("发版失败,错误代码:&#39;.$errcode.&#39;");history.back();</script>&#39;;
                    }
                }
            }
        }
    }
}
登录后复制

wxminiprograms数据表,保存已授权小程序的基本信息及授权相关信息(authorizer_access_token/authorizer_refresh_token)这两个值很重要,代小程序实现业务基本上是通过这两个值来实现

-- Adminer 4.6.2 MySQL dump

SET NAMES utf8;
SET time_zone = &#39;+00:00&#39;;
SET foreign_key_checks = 0;
SET sql_mode = &#39;NO_AUTO_VALUE_ON_ZERO&#39;;

DROP TABLE IF EXISTS `wxminiprograms`;
CREATE TABLE `wxminiprograms` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT &#39;ID&#39;,
  `uid` int(10) unsigned NOT NULL COMMENT &#39;用户ID&#39;,
  `nick_name` varchar(45) DEFAULT NULL COMMENT &#39;微信小程序名称&#39;,
  `alias` varchar(45) DEFAULT NULL COMMENT &#39;别名&#39;,
  `token` varchar(45) DEFAULT NULL COMMENT &#39;平台生成的token值&#39;,
  `head_img` varchar(255) DEFAULT NULL COMMENT &#39;微信小程序头像&#39;,
  `verify_type_info` tinyint(1) DEFAULT NULL COMMENT &#39;授权方认证类型,-1代表未认证,0代表微信认证&#39;,
  `is_show` tinyint(1) DEFAULT &#39;0&#39; COMMENT &#39;是否显示,0显示,1隐藏&#39;,
  `user_name` varchar(45) DEFAULT NULL COMMENT &#39;原始ID&#39;,
  `qrcode_url` varchar(255) DEFAULT NULL COMMENT &#39;二维码图片的URL&#39;,
  `business_info` varchar(255) DEFAULT NULL COMMENT &#39;json格式。用以了解以下功能的开通状况(0代表未开通,1代表已开通): open_store:是否开通微信门店功能 open_scan:是否开通微信扫商品功能 open_pay:是否开通微信支付功能 open_card:是否开通微信卡券功能 open_shake:是否开通微信摇一摇功能&#39;,
  `idc` int(10) unsigned DEFAULT NULL COMMENT &#39;idc&#39;,
  `principal_name` varchar(45) DEFAULT NULL COMMENT &#39;小程序的主体名称&#39;,
  `signature` varchar(255) DEFAULT NULL COMMENT &#39;帐号介绍&#39;,
  `miniprograminfo` varchar(255) DEFAULT NULL COMMENT &#39;json格式。判断是否为小程序类型授权,包含network小程序已设置的各个服务器域名&#39;,
  `func_info` longtext COMMENT &#39;json格式。权限集列表,ID为17到19时分别代表: 17.帐号管理权限 18.开发管理权限 19.客服消息管理权限 请注意: 1)该字段的返回不会考虑小程序是否具备该权限集的权限(因为可能部分具备)。&#39;,
  `authorizer_appid` varchar(45) DEFAULT NULL COMMENT &#39;小程序appid&#39;,
  `authorizer_access_token` varchar(255) DEFAULT NULL COMMENT &#39;授权方接口调用凭据(在授权的公众号或小程序具备API权限时,才有此返回值),也简称为令牌&#39;,
  `authorizer_expires` int(10) unsigned DEFAULT NULL COMMENT &#39;refresh有效期&#39;,
  `authorizer_refresh_token` varchar(255) DEFAULT NULL COMMENT &#39;接口调用凭据刷新令牌&#39;,
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT &#39;授权时间&#39;,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=&#39;微信小程序授权列表&#39;;


-- 2018-07-25 09:32:49
登录后复制

wxminiprogram_audit数据表,保存提交审核的小程序

-- Adminer 4.6.2 MySQL dump

SET NAMES utf8;
SET time_zone = &#39;+00:00&#39;;
SET foreign_key_checks = 0;
SET sql_mode = &#39;NO_AUTO_VALUE_ON_ZERO&#39;;

DROP TABLE IF EXISTS `wxminiprogram_audit`;
CREATE TABLE `wxminiprogram_audit` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT &#39;ID&#39;,
  `appid` varchar(45) NOT NULL COMMENT &#39;小程序appid&#39;,
  `auditid` varchar(45) NOT NULL COMMENT &#39;审核编号&#39;,
  `status` tinyint(1) unsigned NOT NULL DEFAULT &#39;3&#39; COMMENT &#39;审核状态,其中0为审核成功,1为审核失败,2为审核中,3已提交审核&#39;,
  `reason` varchar(255) DEFAULT NULL COMMENT &#39;当status=1,审核被拒绝时,返回的拒绝原因&#39;,
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT &#39;提交审核时间&#39;,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=&#39;微信小程序提交审核的小程序&#39;;


-- 2018-07-25 09:35:07
登录后复制

3839345434-5b28cd7a43128_articlex.png

2715287353-5b239018f1482_articlex.pngx

相关推荐:

微信开发公众号平台视频教程

微信公众平台开发者文档

PHP微信公众平台开发视频教程

以上是终于整理出来了,用微信第三方平台授权小程序业务的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板