首页 > 编程语言 >第三方平台 之代开发小程序

第三方平台 之代开发小程序

时间:2023-10-27 14:34:41浏览次数:32  
标签:之代 authorizer 程序 ret access token https appid 第三方

一、服务商代开发小程序

文档:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/product/how_to_dev.html

第三方平台 之代开发小程序_CMS

二、extAppid 的开发调试

文档:https://developers.weixin.qq.com/miniprogram/dev/devtools/ext.html

ext.json文件

{
    "extEnable": true,
    "extAppid": "", //模板小程序appid
    "directCommit": false,
    "ext": {
        "uniacid": 10001  //多平台标识
    }
}

通过uni.getExtConfigSync()获取ext数据

三、代小程序实现业务

<?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('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 : 域名地址。只接收一维数组。

     * */

    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;

        }

    }

    /*

     * 设置小程序业务域名,无需加https前缀,但域名必须可以通过https访问

     * @params string / array $domains : 域名地址。只接收一维数组。

     * */

    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("设置小程序业务域名失败,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微信公众号营销小程序开发")

    {

        $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;

        }

    }

    /*

     * 小程序审核撤回

     * 单个帐号每天审核撤回次数最多不超过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 = '{"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("查询指定版本的审核状态操作失败,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 : '';

            Db::name('wxminiprogram_audit')->where(['appid'=>$this->authorizer_appid,'auditid'=>$ret->auditid])->update([

                'status'=>$ret->status,

                'reason'=>$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 = '{}';

        $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 = '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);

    }

}

 



标签:之代,authorizer,程序,ret,access,token,https,appid,第三方
From: https://blog.51cto.com/u_15309652/8056201

相关文章

  • e4a开发了银行回执单生成器app,电子版都可以,分享源码和程序
    闲着没事,朋友之间装逼娱乐经常能用到,还好之前我还学了半年的e4a开发,虽然现在e4a已经被专卖了,但是软件正常可以用,但是启动的时候会有版权,然后这个APP也是用e4a进行开发的,就是内置了很多回执单图片,然后前台设置了很多编辑框,你只需要输入参数它会自动数据指定内容的回执单,我先把设计......
  • 使用 AppDomain.CurrentDomain.GetAssemblies() 始终读取不到某一个程序集
     AppDomain.CurrentDomain.GetAssemblies() 只会获取到已加载到当前域的程序集。可以先将所有程序集加载之后再进行读取:DependencyContext.Default.RuntimeLibraries.Where(o=>o.Name.StartsWith("Yuji.")).Select(o=>Assembly.Load(newAssemblyName(o.Name))).ToArray()......
  • 自动化测试-友好的第三方库
    自动化测试脚本开发中,总是会遇到各种数据处理,例如MOCK、URL处理、JSON数据处理、结果断言等,也会遇到所采用的测试框架不能满足当前需求,这些问题都需要我们自己动手解决。在强大的IT世界,我们遇到的百分之九十八问题,前辈们都遇到过并且给出了解决方案,有的无私前辈将其整理并开源,这些......
  • 通过requests库使用HTTP编写的爬虫程序
    使用Python的requests库可以方便地编写HTTP爬虫程序。以下是一个使用requests库的示例:importrequests#发送HTTPGET请求response=requests.get("http://example.com")#检查响应状态码ifresponse.status_code==200:#获取响应内容html=response.text......
  • Web 应用程序中进行多线程处理-Web Workers
    1、什么是WebWorkers?WebWorkersAPI是一组用于创建并在后台运行脚本的接口,以便在Web应用程序中进行多线程处理。它使得可以将一些耗时的计算任务放在单独的线程中执行,从而避免阻塞主线程,提高了应用程序的响应性能。2、使用方式以下是WebWorkersAPI中常用的接口和方法:Worke......
  • 微信小程序获取用户名和头像方式以及使文本可复制方法
    1.微信小程序获取微信昵称和头像在微信小程序之前的版本可以通过wx.getUserInfo和wx.getUserProfile来获取微信头像和昵称。2022年11月8日24时之后上述两个接口均被微信小程序进行回收。本来以为通过一些其他方式也可以获取到微信头像和昵称,比如设置button组件的open-type为getU......
  • uniapp之微信小程序 支付
    一、注册微信支付商户号(由上级或法人注册)注册链接:https://pay.weixin.qq.com/index.php/apply/applyment_home/guide_normal#none,如图注意:需要由主管及更上级领导进行注册,成为公司收款账户(企业注册需要材料:营业执照、对公银行账户信......
  • 指定文件夹内删下划线程序(输入数字为需要保留的下划线)
    importosdefrename_files_in_directory(directory_path,underscore_input):try:#如果是范围输入,则解析范围的结束数字if'-'inunderscore_input:start,end=map(int,underscore_input.split('-'))underscore_count......
  • 如何才能从程序员到架构师?
    1引言小团队一般10人左右,其中常常是技术最牛的人做架构师(或TL)。所以,架构师在广大码农中的占比大概平均不到10%。而架构师也可以分为初级、中级、高级三档,江湖上真正高水平的软件架构师就更少了。所以,大部分(超过九成的)码农干上许多年,还是做不了架构师,这是什么原因造成的呢?2......
  • #yyds干货盘点# LeetCode程序员面试金典:生命游戏
    题目根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机。给定一个包含m×n个格子的面板,每一个格子都可以看成是一个细胞。每个细胞都具有一个初始状态:1即为活细胞(live),或0即为死细胞(dead)。每个细胞与其八个相邻位置(水平,垂......