上期说过的微信授权完成后!终于可以开始正经地写接口API了,躺完坑后,现在舒服多了,直接封装了十几个接口!
就是这些了
我的代码是分 控制器 逻辑层和数据层的
首先是控制器
<?php
namespace app\common\controller;
use app\common\model\WxOpenModel;
class WxOpen
{
private $WC;
public function __construct()
{
$this->WC = new WxOpenModel();
}
##############授权绑定小程序#############
/**
* 获取令牌
*/
public function componentAccessToken()
{
$this->WC->componentAccessToken();
}
/**
* 获取预授权码
*/
public function preAuthCode()
{
//获取令牌
$this->WC->componentAccessToken();
//获取预授权码
$this->WC->preAuthCode();
}
/**
* 使用授权码获取授权信息 3
*/
public function authorizationInfo($authorization_code)
{
$this->WC->authorizationInfo($authorization_code);
}
/**
* 创建开放平台帐号并绑定公众号/小程序 4
*/
public function createAccount()
{
$this->WC->createAccount();
}
/**
* 将公众号/小程序绑定到开放平台帐号下 5
*/
public function bindingAccount()
{
$this->WC->bindingAccount();
}
##############开发小程序#############
/**
* 获取代码草稿列表
*/
public function draftList()
{
$this->componentAccessToken();
$this->WC->draftList();
}
/**
* 将草稿添加到代码模板库
*/
public function addDraftList($draft_id)
{
$this->WC->addDraftList($draft_id);
}
/**
* 获取代码模板列表
*/
public function templateList()
{
$this->WC->templateList();
}
/**
* 上传小程序代码
*/
public function upCode()
{
$this->WC->upCode();
}
/**
* 获取体验版二维码
*/
public function getCode()
{
$this->WC->getCode();
}
/**
* 提交审核
*/
public function applyCode()
{
$this->WC->applyCode();
}
/**
* 查询指定发布审核单的审核状态
*/
public function selectApply()
{
$this->WC->selectApply();
}
/**
* 查询最新一次提交的审核状态
*/
public function selectNewApply()
{
$this->WC->selectNewApply();
}
/**
* 发布已通过审核的小程序
*/
public function release()
{
$this->WC->release();
}
/**
* 版本回退
*/
public function rollbackCode()
{
$this->WC->rollbackCode();
}
/**
* 设置最低基础库版本
*/
public function setVersion()
{
$this->WC->setVersion();
}
}
逻辑层
<?php
namespace app\common\model;
use app\common\controller\Redis;
use app\common\service\WxOpenService;
class WxOpenModel
{
public function __construct()
{
new WxOpenService();
}
/**
* 令牌的获取是有限制的,每个令牌的有效期为 2 小时,在令牌快过期时(比如1小时50分),重新调用接口获取
* 获取令牌 1
* [AppId] =>
* [CreateTime] => 1594886696
* [InfoType] => component_verify_ticket
* [ComponentVerifyTicket] =>
*/
public function componentAccessToken()
{
//拿票据
$R = New Redis();
//先判断令牌有没有过期
$component = $R->redis_str_get('component_access_token');
if (($component['add_time'] + 6000) < time()) {
//未过期
WxOpenService::$component_access_token = $component['component_access_token'];
} else {
$encrypt = $R->redis_str_get('encrypt');
$option = [
"component_appid" => WxOpenService::$component_appid,
"component_appsecret" => WxOpenService::$component_appsecret,
"component_verify_ticket" => $encrypt['ComponentVerifyTicket']
];
$list = WxOpenService::RequestPostCurl(WxOpenService::$componentAccessTokenUrl, $option);
WxOpenService::$component_access_token = $list['component_access_token'];
$list['add_time'] = time();
$R->redis_str_set('component_access_token', $list);
}
}
/**
* 获取预授权码 2
*/
public function preAuthCode()
{
$option = [
"component_appid" => WxOpenService::$component_appid,
];
WxOpenService::$pre_auth_code = WxOpenService::RequestPostCurl(WxOpenService::$preAuthCodeUrl . WxOpenService::$component_access_token, $option);
}
/**
* 使用授权码获取授权信息 3
*/
public function authorizationInfo($authorization_code)
{
WxOpenService::$authorization_code = $authorization_code;
$option = [
"component_appid" => WxOpenService::$component_appid,
"authorization_code" => WxOpenService::$authorization_code,
];
WxOpenService::$authorization_info = WxOpenService::RequestPostCurl(WxOpenService::$authorizationInfoUrl . WxOpenService::$component_access_token, $option);
}
/**
* 创建开放平台帐号并绑定公众号/小程序 4
*/
public function createAccount()
{
$option = [
"appid" => WxOpenService::$authorization_info['authorizer_appid'],
];
$list = WxOpenService::RequestPostCurl(WxOpenService::$createAccountUrl . WxOpenService::$authorization_info['authorizer_access_token'], $option);
if ($list['errcode'] != 0) {
WxOpenService::log('创建开放平台帐号并绑定公众号/小程序失败');
}
WxOpenService::$open_appid = $list['open_appid'];
WxOpenService::log('创建开放平台帐号并绑定公众号/小程序成功');
}
/**
* 将公众号/小程序绑定到开放平台帐号下 5
*/
public function bindingAccount()
{
$option = [
"appid" => WxOpenService::$authorization_info['authorizer_appid'],
"open_appid" => WxOpenService::$open_appid,
];
$list = WxOpenService::RequestPostCurl(WxOpenService::$bindingAccountUrl . WxOpenService::$authorization_info['authorizer_access_token'], $option);
if ($list['errcode'] != 0) {
WxOpenService::log('将公众号/小程序绑定到开放平台帐号下失败');
}
WxOpenService::log($list);
WxOpenService::log('将公众号/小程序绑定到开放平台帐号下成功');
}
/**
* 获取代码草稿列表
*/
public function draftList()
{
$list = WxOpenService::RequestGetCurl(WxOpenService::$draftUrl . WxOpenService::$component_access_token);
if ($list['errcode'] != 0) {
WxOpenService::log('获取代码草稿列表失败');
output_errors('获取代码草稿列表失败');
}
output_data($list);
}
/**
* 将草稿添加到代码模板库
*/
public function addDraftList($draft_id)
{
$option = [
"draft_id" => $draft_id,
];
$list = WxOpenService::RequestPostCurl(WxOpenService::$addDraftUrl . WxOpenService::$component_access_token, $option);
if ($list['errcode'] != 0) {
WxOpenService::log('将草稿添加到代码模板库失败');
output_errors('添加失败');
}
output_data('添加成功');
}
/**
* 获取代码模板列表
*/
public function templateList()
{
$list = WxOpenService::RequestGetCurl(WxOpenService::$GetTemplateUrl . WxOpenService::$component_access_token);
if ($list['errcode'] != 0) {
WxOpenService::log('获取代码模板列表失败');
output_errors('获取代码模板列表失败');
}
output_data($list);
}
/**
* 上传小程序代码
*/
public function upCode()
{
WxOpenService::check(['template_id', 'ext_json', 'user_version', 'user_desc']);
$option = [
"template_id" => $_POST['template_id'],
"ext_json" => $_POST['ext_json'],
"user_version" => $_POST['user_version'],
"user_desc" => $_POST['user_desc'],
];
$list = WxOpenService::RequestPostCurl(WxOpenService::$upCodeUrl . WxOpenService::$component_access_token, $option);
if ($list['errcode'] != 0) {
WxOpenService::log('将上传小程序代码失败');
}
WxOpenService::log($list);
WxOpenService::log('上传小程序代码成功');
output_data('成功');
}
/**
* 获取体验版二维码
*/
public function getCode()
{
$path = '/pages/index/index';
$path = urlencode($path);
$list = WxOpenService::RequestGetCurl(WxOpenService::$getCodeUrl . WxOpenService::$component_access_token . '&path=' . $path);
WxOpenService::log('获取体验版二维码');
output_data($list);
}
/**
* 提交审核
*/
public function applyCode()
{
$option = [
];
$list = WxOpenService::RequestPostCurl(WxOpenService::$applyCodeUrl . WxOpenService::$component_access_token, $option);
if ($list['errcode'] != 0) {
WxOpenService::log('提交审核失败');
}
WxOpenService::log($list);
WxOpenService::log('提交审核成功');
output_data('成功');
}
/**
* 查询指定发布审核单的审核状态
*/
public function selectApply()
{
WxOpenService::check(['auditid']);
$option = [
'auditid' => $_POST['auditid']
];
$list = WxOpenService::RequestPostCurl(WxOpenService::$selectApplyUrl . WxOpenService::$component_access_token, $option);
if ($list['errcode'] != 0) {
WxOpenService::log('查询指定发布审核单的审核状态失败');
}
switch ($list['status']) {
case 0 :
$list['status_name'] = '审核成功';
break;
case 1 :
$list['status_name'] = '审核被拒绝';
break;
case 2 :
$list['status_name'] = '审核中';
break;
case 3 :
$list['status_name'] = '已撤回';
break;
case 4 :
$list['status_name'] = '审核延后';
break;
}
WxOpenService::log($list);
WxOpenService::log('查询指定发布审核单的审核状态成功');
output_data($list);
}
/**
* 查询最新一次提交的审核状态
*/
public function selectNewApply()
{
$list = WxOpenService::RequestGetCurl(WxOpenService::$selectNewApplyUrl . WxOpenService::$component_access_token);
if ($list['errcode'] != 0) {
WxOpenService::log('查询最新一次提交的审核状态失败');
}
switch ($list['status']) {
case 0 :
$list['status_name'] = '审核成功';
break;
case 1 :
$list['status_name'] = '审核被拒绝';
break;
case 2 :
$list['status_name'] = '审核中';
break;
case 3 :
$list['status_name'] = '已撤回';
break;
}
WxOpenService::log($list);
WxOpenService::log('查询最新一次提交的审核状态成功');
output_data($list);
}
/**
* 发布已通过审核的小程序
*/
public function release()
{
$option = [
];
$list = WxOpenService::RequestPostCurl(WxOpenService::$releaseUrl . WxOpenService::$component_access_token, $option);
if ($list['errcode'] != 0) {
WxOpenService::log('发布已通过审核的小程序失败');
}
WxOpenService::log($list);
WxOpenService::log('发布已通过审核的小程序成功');
output_data($list);
}
/**
* 版本回退
*/
public function rollbackCode()
{
$list = WxOpenService::RequestGetCurl(WxOpenService::$rollbackCodeUrl . WxOpenService::$component_access_token);
if ($list['errcode'] != 0) {
WxOpenService::log('版本回退失败');
}
WxOpenService::log($list);
WxOpenService::log('版本回退成功');
output_data($list);
}
/**
* 设置最低基础库版本
*/
public function setVersion()
{
WxOpenService::check(['version']);
$option = [
'version'=>$_POST['version']
];
$list = WxOpenService::RequestPostCurl(WxOpenService::$setVersion . WxOpenService::$component_access_token, $option);
if ($list['errcode'] != 0) {
WxOpenService::log('设置最低基础库版本失败');
}
WxOpenService::log($list);
WxOpenService::log('设置最低基础库版本成功');
output_data($list);
}
}
最后是数据层
<?php
namespace app\common\service;
use app\common\ApiCommon;
use app\pay\service\PayService;
use lib\Curl;
class WxOpenService extends ApiCommon
{
//开放平台appid
public static $component_appid='';
//开放平台appsecret
public static $component_appsecret='';
//令牌
public static $component_access_token;
//预授权码
public static $pre_auth_code;
//授权码
public static $authorization_code;
//获取授权信息
public static $authorization_info;
//所创建的开放平台帐号的 appid
public static $open_appid;
//代码草稿列表
public static $draft_list;
//代码模板列表
public static $template_list;
//令牌
public static $componentAccessTokenUrl='https://api.weixin.qq.com/cgi-bin/component/api_component_token';
//预授权码
public static $preAuthCodeUrl='https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=';
//预授权码获取授权码
public static $authorizationCodeUrl='https://mp.weixin.qq.com/cgi-bin/componentloginpage?';
//预授权码获取授权码 回调 URI
public static $redirectUrl='https://master.jiaju01.com/api/Wx/GetAuthorizationCode';
//使用授权码获取授权信息
public static $authorizationInfoUrl='https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=';
//创建开放平台帐号并绑定公众号/小程序
public static $createAccountUrl='https://api.weixin.qq.com/cgi-bin/open/create?access_token=';
//将公众号/小程序绑定到开放平台帐号下
public static $bindingAccountUrl='https://api.weixin.qq.com/cgi-bin/open/bind?access_token=';
//获取代码草稿列表
public static $draftUrl='https://api.weixin.qq.com/wxa/gettemplatedraftlist?access_token=';
//将草稿添加到代码模板库
public static $addDraftUrl='https://api.weixin.qq.com/wxa/gettemplatedraftlist?access_token=';
//获取代码模板列表
public static $GetTemplateUrl='https://api.weixin.qq.com/wxa/gettemplatelist?access_token=';
//上传小程序代码
public static $upCodeUrl='https://api.weixin.qq.com/wxa/commit?access_token=';
//获取体验版二维码
public static $getCodeUrl='https://api.weixin.qq.com/wxa/get_qrcode?access_token=';
//提交审核
public static $applyCodeUrl='https://api.weixin.qq.com/wxa/submit_audit?access_token=';
//查询指定发布审核单的审核状态
public static $selectApplyUrl='https://api.weixin.qq.com/wxa/get_auditstatus?access_token=';
//查询最新一次提交的审核状态
public static $selectNewApplyUrl='https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=';
//发布已通过审核的小程序
public static $releaseUrl='https://api.weixin.qq.com/wxa/release?access_token=';
//版本回退
public static $rollbackCodeUrl='https://api.weixin.qq.com/wxa/revertcoderelease?access_token=';
//设置最低基础库版本
public static $setVersion='https://api.weixin.qq.com/cgi-bin/wxopen/setweappsupportversion?access_token=';
public function __construct()
{
//初始化父类
parent::__construct();
}
/**
* URL请求(GET)
*/
public static function RequestGetCurl($url)
{
$curl = new Curl();
$curl->setUrl($url);
$list = $curl->execute(true);
$curl->close();
return $list;
}
/**
* URL请求(POST)
*/
public static function RequestPostCurl($url, $xmlData)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url); //定义表单提交地址
curl_setopt($curl, CURLOPT_POST, 1); //定义提交类型 1:POST ;0:GET 设置post方式提交
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, 'Content-type: text/xml');
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_HEADER, 0); //定义是否显示状态头 1:显示 ; 0:不显示
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //定义是否直接输出返回流
curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlData); //定义提交的数据,这里是XML文件
$returnData = curl_exec($curl);
return $returnData;
}
public static function log($msg){
if(WxOpenService::$authorization_info['authorizer_appid']){
WxOpenService::p('小程序:' . WxOpenService::$authorization_info['authorizer_appid'], false, 'wx');
}
WxOpenService::p($msg, false, 'wx');
WxOpenService::p(date('Y-m-d H:i:s',time()), false, 'wx');
}
}
文章浏览阅读1.6k次。安装配置gi、安装数据库软件、dbca建库见下:http://blog.csdn.net/kadwf123/article/details/784299611、检查集群节点及状态:[root@rac2 ~]# olsnodes -srac1 Activerac2 Activerac3 Activerac4 Active[root@rac2 ~]_12c查看crs状态
文章浏览阅读1.3w次,点赞45次,收藏99次。我个人用的是anaconda3的一个python集成环境,自带jupyter notebook,但在我打开jupyter notebook界面后,却找不到对应的虚拟环境,原来是jupyter notebook只是通用于下载anaconda时自带的环境,其他环境要想使用必须手动下载一些库:1.首先进入到自己创建的虚拟环境(pytorch是虚拟环境的名字)activate pytorch2.在该环境下下载这个库conda install ipykernelconda install nb__jupyter没有pytorch环境
文章浏览阅读5.2k次,点赞19次,收藏28次。选择scoop纯属意外,也是无奈,因为电脑用户被锁了管理员权限,所有exe安装程序都无法安装,只可以用绿色软件,最后被我发现scoop,省去了到处下载XXX绿色版的烦恼,当然scoop里需要管理员权限的软件也跟我无缘了(譬如everything)。推荐添加dorado这个bucket镜像,里面很多中文软件,但是部分国外的软件下载地址在github,可能无法下载。以上两个是官方bucket的国内镜像,所有软件建议优先从这里下载。上面可以看到很多bucket以及软件数。如果官网登陆不了可以试一下以下方式。_scoop-cn
文章浏览阅读4.5k次,点赞2次,收藏3次。首先要有一个color-picker组件 <el-color-picker v-model="headcolor"></el-color-picker>在data里面data() { return {headcolor: ’ #278add ’ //这里可以选择一个默认的颜色} }然后在你想要改变颜色的地方用v-bind绑定就好了,例如:这里的:sty..._vue el-color-picker
文章浏览阅读640次。基于芯片日益增长的问题,所以内核开发者们引入了新的方法,就是在内核中只保留函数,而数据则不包含,由用户(应用程序员)自己把数据按照规定的格式编写,并放在约定的地方,为了不占用过多的内存,还要求数据以根精简的方式编写。boot启动时,传参给内核,告诉内核设备树文件和kernel的位置,内核启动时根据地址去找到设备树文件,再利用专用的编译器去反编译dtb文件,将dtb还原成数据结构,以供驱动的函数去调用。firmware是三星的一个固件的设备信息,因为找不到固件,所以内核启动不成功。_exynos 4412 刷机
文章浏览阅读2w次,点赞24次,收藏42次。Linux系统配置jdkLinux学习教程,Linux入门教程(超详细)_linux配置jdk
文章浏览阅读3.3k次,点赞5次,收藏19次。xlabel('\delta');ylabel('AUC');具体符号的对照表参照下图:_matlab微米怎么输入
文章浏览阅读119次。顺序读写指的是按照文件中数据的顺序进行读取或写入。对于文本文件,可以使用fgets、fputs、fscanf、fprintf等函数进行顺序读写。在C语言中,对文件的操作通常涉及文件的打开、读写以及关闭。文件的打开使用fopen函数,而关闭则使用fclose函数。在C语言中,可以使用fread和fwrite函数进行二进制读写。 Biaoge 于2024-03-09 23:51发布 阅读量:7 ️文章类型:【 C语言程序设计 】在C语言中,用于打开文件的函数是____,用于关闭文件的函数是____。
文章浏览阅读3.4k次,点赞2次,收藏13次。跟随鼠标移动的粒子以grid(SOP)为partical(SOP)的资源模板,调整后连接【Geo组合+point spirit(MAT)】,在连接【feedback组合】适当调整。影响粒子动态的节点【metaball(SOP)+force(SOP)】添加mouse in(CHOP)鼠标位置到metaball的坐标,实现鼠标影响。..._touchdesigner怎么让一个模型跟着鼠标移动
文章浏览阅读178次。项目运行环境配置:Jdk1.8 + Tomcat7.0 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)。项目技术:Springboot + mybatis + Maven +mysql5.7或8.0+html+css+js等等组成,B/S模式 + Maven管理等等。环境需要1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。_基于java技术的停车场管理系统实现与设计
文章浏览阅读3.5k次。前言对于MediaPlayer播放器的源码分析内容相对来说比较多,会从Java-&amp;gt;Jni-&amp;gt;C/C++慢慢分析,后面会慢慢更新。另外,博客只作为自己学习记录的一种方式,对于其他的不过多的评论。MediaPlayerDemopublic class MainActivity extends AppCompatActivity implements SurfaceHolder.Cal..._android多媒体播放源码分析 时序图
文章浏览阅读2.4k次,点赞41次,收藏13次。java 数据结构与算法 ——快速排序法_快速排序法