小程序授权第三方平台

2023-11-16

<?php

namespace app\common\lib\built;

use app\platform\model\AppletGrant;

/**
 * 微信小程序类文件
 * 公开属性
 * 微信小程序类文件返回统一格式['status'=>'0失败  1成功','message'=>'提示语','data'=>'结果']
 * Class WxApplet
 * [
 * release 发布已通过审核的小程序
 * bindUser 绑定微信用户为体验者
 * modifyDomain 设置服务器域名
 * auditStatus 查询指定发布审核单的审核状态
 * latestAuditStatus 查询最新一次提交的审核状态
 * undoCodeAudit 小程序审核撤回
 * submitAudit 提交审核
 * commit 上传小程序代码并生成体验版
 * getQRCode 获取体验版二维码
 * getCategory 获取审核时可填写的类目信息
 * getPage 获取已上传的代码的页面列表
 * privacySetting 配置小程序用户隐私保护指引
 * setWebViewDomain 设置小程序业务域名
 * getAccessToken 获取或刷新接口调用令牌
 * ]
 * @package app\common\lib\built
 */
class WxApplet
{
    public $platform_applet_config; //平台小程序配置信息
    protected $platform_id; //平台id
    protected $request;

    public function __construct($platform_id)
    {
        $where = [
            ['platform_id', '=', $platform_id]
        ];
        $fields = [
            'authorizer_appid',
            'authorizer_access_token',
            'access_token_time',
            'authorizer_refresh_token',
            'user_desc',
            'tag'
        ];
        $AppletGrantModel = new AppletGrant();
        $this->platform_applet_config = $AppletGrantModel->getFindField($where, $fields);
        if (empty($this->platform_applet_config)) {
            echo json_encode(callBack('error', 'param_error', '小程序未授权'), JSON_UNESCAPED_UNICODE);
            die();
        }
        $this->platform_id = $platform_id;
        $this->request = new ThirdRequest();
        $this->getAccessToken(); //获取令牌
    }


    //发布已通过审核的小程序
    public function release()
    {
        $url = "https://api.weixin.qq.com/wxa/release?access_token=" . $this->platform_applet_config['authorizer_access_token'];
        $result = $this->request->curlPostRaw($url, "{}");
        $result = json_decode($result, true);
        if ($result['errcode'] != 0) {
            return callBack('error', 'param_error', '发布小程序失败' . $result['errmsg']);
        }
        return callBack('success', 'success', '发布成功');
    }


    /**
     * 绑定微信用户为体验者
     * @param $wx_number  微信号
     * @return array
     */
    public function bindUser($wx_number)
    {
        $url = "https://api.weixin.qq.com/wxa/bind_tester?access_token=" . $this->platform_applet_config['authorizer_access_token'];
        $data = [
            'wechatid' => $wx_number,
        ];
        $result = $this->request->curlPostRaw($url, json_encode($data));
        $result = json_decode($result, true);
        if ($result['errcode'] != 0) {
            return callBack('error', 'param_error', '绑定失败' . $result['errmsg'], $result);
        }
        return callBack('success', 'success', '绑定成功');
    }


    /**
     * 设置服务器域名
     * @param $domain_name
     * @return array
     */
    public function modifyDomain($domain_name)
    {
        $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token=" . $this->platform_applet_config['authorizer_access_token'];
        $data = [
            "action" => "add",
            "requestdomain" => ["https://" . $domain_name],
            "uploaddomain" => ["https://" . $domain_name],
            "downloaddomain" => ["https://" . $domain_name],
        ];
        $result = $this->request->curlPostRaw($url, json_encode($data, JSON_UNESCAPED_SLASHES));
        $result = json_decode($result, true);
        if ($result['errcode'] != 0 && $result['errcode'] != 85017) {
            return callBack('error', 'param_error', '设置服务器域名失败' . $result['errmsg']);
        }
        return callBack('success', 'success', '设置服务器域名成功');
    }


    //查询指定发布审核单的审核状态
    public function auditStatus($audit_id)
    {
        $data = [
            'auditid' => $audit_id,
        ];
        $url = "https://api.weixin.qq.com/wxa/get_auditstatus?access_token=" . $this->platform_applet_config['authorizer_access_token'];
        $result = $this->request->curlPostRaw($url, json_encode($data));
        $result = json_decode($result, true);
        if ($result['errcode'] != 0) {
            return callBack('error', 'param_error', '指定版本审核状态查询失败' . $result['errmsg']);
        }
        return callBack('success', 'success', '查询成功', $result);
    }


    //查询最新一次提交的审核状态
    public function latestAuditStatus()
    {
        $url = "https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=" . $this->platform_applet_config['authorizer_access_token'];
        $result = $this->request->curlGet($url);
        $result = json_decode($result, true);
        if ($result['errcode'] != 0) {
            return callBack('error', 'param_error', '查询最新一次提交的审核状态失败' . $result['errmsg']);
        }
        return callBack('success', 'success', '查询成功', $result);
    }


    //小程序审核撤回
    public function undoCodeAudit()
    {
        $url = "https://api.weixin.qq.com/wxa/undocodeaudit?access_token=" . $this->platform_applet_config['authorizer_access_token'];
        $result = $this->request->curlGet($url);
        $result = json_decode($result, true);
        if ($result['errcode'] != 0) {
            return callBack('error', 'param_error', '撤销审核失败' . $result['errmsg'], $result);
        } else {
            return callBack('success', 'success', '撤销审核成功');
        }
    }


    /**
     * 提交审核
     * @param $data
     * @return array
     */
    public function submitAudit($data)
    {
        $url = 'https://api.weixin.qq.com/wxa/submit_audit?access_token=' . $this->platform_applet_config['authorizer_access_token'];
        $result = $this->request->curlPostRaw($url, json_encode($data, JSON_UNESCAPED_UNICODE));
        $result = json_decode($result, true);
        if ($result['errcode'] != 0) {
            return callBack('error', 'param_error', '提交审核失败');
        }
        return callBack('success', 'success', '提交审核成功', $result['auditid']);
    }


    /**
     * 上传小程序代码并生成体验版
     * @param $template_id 模板id
     * @return array
     */
    public function commit($template_id)
    {
        $domain = 'https://' . config('super.domain_name');
        $platform_id = $this->platform_id;
        $authorizer_appid = $this->platform_applet_config['authorizer_appid'];
        $data = [
            'template_id' => $template_id,
            "user_version" => "V" . $template_id,
            'ext_json' => "{
                \"extEnable\":true,
                \"extAppid\": \"$authorizer_appid\",
                \"ext\":{\"platform_id\":\"$platform_id\",\"appid\":\"$authorizer_appid\",\"domain\":\"$domain\"}
                }",
            "user_desc" => $this->platform_applet_config['user_desc'],
        ];
        $url = "https://api.weixin.qq.com/wxa/commit?access_token=" . $this->platform_applet_config['authorizer_access_token'];
        $result = $this->request->curlPostRaw($url, json_encode($data, JSON_UNESCAPED_UNICODE));
        $result = json_decode($result, true);
        if ($result['errcode'] != 0) {
            return callBack('error', 'param_error', '上传失败', $result);
        }
        return callBack('success', 'success', '上传成功', $result);
    }


    //获取体验版二维码
    public function getQRCode()
    {
        $url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token=" . $this->platform_applet_config['authorizer_access_token'];
        $result = $this->request->curlGet($url);
        if (($result['errcode'] ?? 0) != 0) {
            return callBack('error', 'param_error', '获取体验版二维码失败', $result);
        }
        return callBack('success', 'success', '获取成功', $result);
    }


    //获取审核时可填写的类目信息
    public function getCategory()
    {
        $url = "https://api.weixin.qq.com/wxa/get_category?access_token=" . $this->platform_applet_config['authorizer_access_token'];
        $result = $this->request->curlGet($url);
        $result = json_decode($result, true);
        if ($result['errcode'] != 0) {
            return callBack('error', 'param_error', '获取授权小程序帐号的可选类目:' . $result['errmsg'], $result);
        } else {
            return callBack('success', 'success', '获取成功', $result['category_list']);
        }
    }


    //获取已上传的代码的页面列表
    public function getPage()
    {
        $url = 'https://api.weixin.qq.com/wxa/get_page?access_token=' . $this->platform_applet_config['authorizer_access_token'];
        $result = $this->request->curlGet($url);
        $result = json_decode($result, true);
        if ($result['errcode'] != 0) {
            return callBack('error', 'param_error', '获取小程序的第三方提交代码的页面配置接口错误' . $result['errmsg'], $result);
        } else {
            return callBack('success', 'success', '获取成功', $result['page_list']);
        }
    }


    /**
     * 配置小程序用户隐私保护指引
     * @param $param
     * [
     * owner_setting 收集方(开发者)信息配置
     * setting_list 要收集的用户信息配置,可选择的用户信息类型参考下方详情
     * ]
     * @return array
     */
    public function privacySetting($param)
    {
        $url = 'https://api.weixin.qq.com/cgi-bin/component/setprivacysetting?access_token=' . $this->platform_applet_config['authorizer_access_token'];
        $param = [
            'owner_setting' => $param['owner_setting'],
            'setting_list' => $param['setting_list']
        ];
        $result = $this->request->curlPostRaw($url, json_encode($param));
        $result = json_decode($result, true);
        if ($result['errcode'] != 0) {
            return callBack('error', 'param_error', '设置失败' . $result['errmsg']);
        }
        return callBack('success', 'success', '设置成功');
    }


    //设置小程序业务域名
    public function setWebViewDomain($domain_name)
    {
        $url = "https://api.weixin.qq.com/wxa/setwebviewdomain?access_token=" . $this->platform_applet_config['authorizer_access_token'];
        $data = [
            'action' => 'add',
            'webviewdomain' => ['https://' . $domain_name]
        ];
        $data = json_encode($data);
        $result = $this->request->curlPostRaw($url, $data);
        $result = json_decode($result, true);
        if ($result['errcode'] != 0) {
            return callBack('error', 'param_error', '小程序业务域名设置失败');
        }
        return callBack('success', 'success', '设置成功');
    }


    //获取或刷新接口调用令牌
    public function getAccessToken()
    {
        if ($this->platform_applet_config['access_token_time'] < time() || empty($this->platform_applet_config['authorizer_access_token'])) {
            //令牌过期获取新令牌
            //获取开放平台接口调用令牌
            $WxOpenPlatformClass = new WxOpenPlatform();
            $url = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=' . $WxOpenPlatformClass->open_platform_config['component_access_token'];
            $data = [
                'component_appid' => $WxOpenPlatformClass->open_platform_config['component_appid'],
                'authorizer_appid' => $this->platform_applet_config['authorizer_appid'],
                'authorizer_refresh_token' => $this->platform_applet_config['authorizer_refresh_token']
            ];
            $data = json_encode($data);
            $result = $this->request->curlPostRaw($url, $data);
            file_put_contents('./z2.txt', $result);
            $result = json_decode($result, true);
            if (empty($result['authorizer_access_token'])) {
                echo json_encode(callBack('error', 'param_error', '调用微信接口令牌刷新失败' . $result['errmsg']), JSON_UNESCAPED_UNICODE);
                die();
            }
            $content = [
                'authorizer_access_token' => $result['authorizer_access_token'],
                'authorizer_refresh_token' => $result['authorizer_refresh_token'],
                'access_token_time' => time() + $result['expires_in'] - 200
            ];
            $where = [
                ['platform_id', '=', $this->platform_id]
            ];
            $AppletGrantModel = new AppletGrant();
            $AppletGrantModel->updateInfo($where, $content);
            $this->platform_applet_config['authorizer_access_token'] = $result['authorizer_access_token'];
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

小程序授权第三方平台 的相关文章

  • 如何使用 PHP 正确添加跨站请求伪造 (CSRF) 令牌

    我正在尝试为我网站上的表单添加一些安全性 其中一个表单使用 AJAX 另一个表单是简单的 联系我们 表单 我正在尝试添加 CSRF 令牌 我遇到的问题是令牌有时只显示在 HTML 值 中 其余时间 该值为空 这是我在 AJAX 表单上使用的
  • PHP MySql 百分比

    我的问题是关于百分比 我不是专家 所以我会尽力以更好的方式进行解释 我的 mysql 服务器中有一个表 假设有 700 条记录 如下所示 Name country language Birth Lucy UK EN 1980 Mari Ca
  • 简单的 PHP 条件帮助: if($Var1 = in list($List) and $Cond2) - 这可能吗?

    这是一个可能的功能吗 我需要检查一个变量是否存在于我需要检查的变量列表中 并且 cond2 是否为 true 例如 if row name 1 2 3 Cond2 doThis 它对我不起作用 我在复制粘贴中更改的只是我的列表和变量名称 i
  • 通过 Ajax 加载内容时,WORDPRESS 音频播放器未加载,MediaElement.js 未应用

    我正在创建一个 WordPress 主题 当我使用 ajax 加载内容时 它不会将 MediaElements js 应用于我的音频播放器 因此不会显示音频 我认为这是因为 MediaElement js 加载了 wp footer 并且此
  • 如何使用 phpunit 运行单个测试方法?

    我正在努力运行一个名为testSaveAndDrop在文件中escalation EscalationGroupTest php with phpunit 我尝试了以下组合 phpunit EscalationGroupTest escal
  •  
    标记内删除

    我制作了简单的 BBCode 脚本 一切正常 但后来我使用了一个 javascript 库来美化我的代码 pre pre 现在我面临的唯一问题是 br 每行代码后面的标签 pre pre tags 所以问题是我怎样才能删除 br 标记哪些在
  • PHP MySQL 使用选项/选择 HTML 表单标签进行多重搜索查询

    我正在尝试使用两个搜索字段设置基本的 MySQL LIKE 搜索 我不想拥有它 所以它有多个可选搜索字段 例如if isset POST city isset POST name 我不知道如何用 HTML 来做到这一点
  • 生成大随机数 php [重复]

    这个问题在这里已经有答案了 我想使用 PHP 生成一个包含 75 个字符的数字 我到处寻找 但一无所获 除了这个 http dailycoding com tools RandomNumber aspx http dailycoding c
  • 检查 PHP 中“@”字符后面的单词

    我现在正在制作一个新闻和评论系统 但是我已经在一个部分上停留了一段时间了 我希望用户能够在 Twitter 上引用其他玩家的风格 例如 用户名 该脚本看起来像这样 不是真正的 PHP 只是想象脚本 3 string I loved the
  • 如何使用 php 发送服务器错误响应?

    一旦用户点击删除按钮我的 jQuery 脚本要求服务器删除所选项目 现在我想要我的php发送成功或错误响应的脚本 是否有可能触发错误回调万一该项目无法删除 Thanks 我的 jQuery 代码 ajax type post url myA
  • 如何使用 php 下载/打印页面的特定部分

    我有一个 HTML 页面如下 Lorem Ipsum is simply dummy text of the printing and typesetting industry Lorem Ipsum has been the indust
  • Symfony2 Assetic 和 Less Sourcemaps

    我不确定如何破解 assetic less 过滤器以输出源映射文件 我这里指的是LessFilterhttps github com kriswallsmith assetic blob master src Assetic Filter
  • 学说迁移后备

    我们正在使用原则迁移 当迁移包含多个操作并且其中一个操作失败时 通常会出现问题 例如 如果迁移添加了 5 个外键 其中第 5 个失败 而字段长度不同 则修复字段错误并重新生成迁移不会not修复整个问题 而现在出现一个与 4 个密钥已存在有关
  • 交换关联数组中的两个项目

    Example arr array apple gt sweet grapefruit gt bitter pear gt tasty banana gt yellow 我想调换一下柚子和梨的位置 这样数组就变成了 arr array ap
  • 在 PHP 中撤销 Google 访问令牌

    正如标题所示 我想以编程方式撤销授予的访问令牌 即在 PHP 中 我发现这个他们的网站 https developers google com identity protocols OAuth2WebServer tokenrevoke 但
  • 覆盖控制器 Symfony 3.4/4.0

    我目前正在尝试覆盖 FOSUserBundle 中的控制器 在新的文档中 https symfony com doc 3 4 bundles override html https symfony com doc 3 4 bundles o
  • 表单提交后如何保留选择字段中的选定值?

    我有一个用于将票证上传到数据库的主页 我有一个选择字段 我想保留用户在提交表单之前选择的值 但它没有发生 这是我选择字段的代码
  • Laravel 中只向登录用户显示按钮

    如果我以 John 身份登录 如何才能只显示 John 的红色按钮而不显示 Susan 的红色按钮 测试系统环境 Win10 Laravel5 4 Mysql5 7 19 table class table table responsive
  • 为什么 Composer 降级了我的包?

    php composer phar update这样做了 删除了 2 3 0 软件包并安装了整个 2 2 5 Zend Framework php composer phar update Loading composer reposito
  • post php mysql 的拆分关键字

    我有一个表存储帖子 ID 它的标签如下 Post id Tags 1 keyword1 keyword2 keyword3 我想循环遍历该表中的每一行并执行以下操作 将关键字1 关键字2 关键字3放入新表中 word id word val

随机推荐

  • python实现分页

    使用python实现分页功能 当我们有大量数据需要展示时 需要对数据进行分页展示 这时就用到了分页功能 分页使得数据更好的展示给用户 当访问页码数大于总页码数的时候 展示第一页内容 import math content name aa a
  • 基于STM8的ADC0832采集及蓝牙通信系统

    为了方便大家学习 工程已经打包上传 http download csdn net detail devintt 8029389 最近在淘宝逛的时候发现了一款单片机 STM8 相比之前一直使用的也是8位的AVR相比 感觉STM8更为强大 芯片
  • 【计算机网络】

    1 1 1 发出一条邮件 其OSI七层模型的传输过程 参考1 参考2
  • Python学习笔记 数据类型

    要学习一门语言 首先要看看它的数据类型 由于Python是一门动态语言 所以Python的变量在声明时不需要类型 只要写出名称即可 这里所用的Python版本是目前最新的Python 3 6 1 数字类型 整数 在Python中只有一种整数
  • 机器学习集成模型ML ens学习——多层模型集成(一)

    集成模型就是把多个模型融合在一起使用 通过构建一层层的模型体系 最终得到不同模型的预测结果 首先安装 pip install mlens 案例 import pandas as pd from sklearn model selection
  • vue中逗号的使用方法_Vuex 的使用方法

    Vuex 的使用 安装 npm i vuex save 然后创建一个store import Vue from vue import Vuex from vuex Vue use Vuex export default new Vuex S
  • java 反射泛型工具类, 获取类的泛型类型

    直接上代码 public class ParameterizedTypeUtils 根据索引获取泛型实例类 param type 类型 param index return java lang Class
  • aixos ajax,使用axios如何实现ajax请求(详细教程)

    之前给大家介绍了jQuery利用最优雅的方式写ajax请求的相关内容 这篇文章主要给大家介绍了关于axios进阶实践之利用最优雅的方式写ajax请求的相关资料 文中通过示例代码介绍的非常详细 对大家的学习或者工作具有一定的参考学习价值 需要
  • Linux grep 显示前后几行的信息

    grep 后面带上 A B C 参数可以多显示几行内容 grep A 5 可以显示匹配内容以及后面的5行内容 grep B 5 可以显示匹配内容以及前面的5行内容 grep C 5 可以显示匹配内容以及前后面的5行内容 cat size t
  • QGIS 使用矢量数据(shp)批量裁剪栅格数据(tif)

    前提描述 有一种情况是一个矢量数据有多个元素 比如A省有11个市 那么我用A省的矢量去裁剪栅格的时候 想直接输出11个市域独立的栅格结果 也就是输出11个栅格数据 这个在ArcGIS Pro和Arcmap目前还不能直接做到 用Python肯
  • 线程的三种实现方式

    一 继承Thread类 二 实现Runable接口 对于1 2两种方法应该都会知道的 即使在上java相关的课程中老师一般都会介绍这两种实现的方法 所以这里不赘述了 直接进入第三种第四种的实现方式 三 实现Callable接口 packag
  • 【项目设计】高并发内存池(三)[CentralCache的实现]

    C 学习历程 入门 博客主页 一起去看日落吗 持续分享博主的C 学习历程 博主的能力有限 出现错误希望大家不吝赐教 分享给大家一句我很喜欢的话 也许你现在做的事情 暂时看不到成果 但不要忘记 树 成长之前也要扎根 也要在漫长的时光 中沉淀养
  • QT中学习Opengl---(纹理基本加载opengl写法)

    前言 本文的代码是 LearnOpenGL 中对应代码 这里提供学习 大家喜欢的可去官方网站去看看 LearnOpenGL CNhttps learnopengl cn readthedocs io zh latest 本章为加载纹理 qt
  • 单链表 和 顺序表 实现的 通讯录

    通讯录 前言 通讯录 通讯录的逻辑 通讯录存储结构的选择 顺序表 数组 在堆区开辟的空间 柔性数组 单链表 单链表实现通讯录 进入页面设计 代码效果 看起来比较有那味了 通讯录属性的设计 主函数的设计 AddContact ShowCont
  • mysql内置函数,时间戳与时间类型互转

    from unixtime time stamp gt 将时间戳转换为日期 unix timestamp date gt 将指定的日期或者日期字符串转换为时间戳
  • 域格模组的模拟语音、PCM、I2S

    域格模组的模拟语音 PCM I2S 文章目录 域格模组的模拟语音 PCM I2S 一 域格模块里的音频功能 二 常见音频 传输 接口 1 介绍 2 一目了然的协议时序 I2S协议时序 PCM协议 二 模块中使用音频功能 一 域格模块里的音频
  • 基于MATLAB的车牌识别

    目录 车牌识别系统的介绍与展示 车牌定位 1 灰度处理 2 边缘检测 车牌分割 1 图像腐蚀 2 图像平滑 3 移除对象 4 图像切割 车牌识别 1 灰度处理 2 直方图均衡化 3 二值化 4 中值滤波 5 字符识别 车牌识别系统的介绍与展
  • 如何启动单节点zookeeper以及注意事项

    1 安装jdk 因为zookeeper是基于java进行开发的 2 将zookeeper解压之后 再zookeeper目录下conf目录下有一个zoo sample cfg 将它改名成zoo cfg 因为zookeeper启动时自动加载的是
  • QT获取各种平台系统版本信息之QSysInfo

    一 头文件 include
  • 小程序授权第三方平台