This commit is contained in:
尖叫 2023-12-07 09:59:32 +08:00
parent 36b4c5eac5
commit f55e4a9550
3 changed files with 823 additions and 785 deletions

View File

@ -2,11 +2,13 @@
/** /**
* 应用公共函数库文件 * 应用公共函数库文件
*/ */
use think\facade\Request; use think\facade\Request;
use hema\wechat\Driver as Wechat; use hema\wechat\Driver as Wechat;
use app\common\model\User as UserModel; use app\common\model\User as UserModel;
use app\common\model\Setting as SettingModel; use app\common\model\Setting as SettingModel;
use app\common\model\food\ShopClerk as ShopClerkModel; use app\common\model\food\ShopClerk as ShopClerkModel;
/** /**
* 生成数字验证码 * 生成数字验证码
*/ */
@ -88,6 +90,7 @@ function base_url()
} }
return $baseUrl; return $baseUrl;
} }
/** /**
* 获取当前域名 * 获取当前域名
*/ */
@ -96,6 +99,7 @@ function domain()
$request = Request::instance(); $request = Request::instance();
return $request->host(); return $request->host();
} }
/** /**
* 获取当前uploads目录访问地址 * 获取当前uploads目录访问地址
*/ */
@ -103,6 +107,7 @@ function uploads_url()
{ {
return base_url() . 'uploads/'; return base_url() . 'uploads/';
} }
/** /**
* 获取当前的应用名称 * 获取当前的应用名称
*/ */
@ -110,6 +115,7 @@ function app_name()
{ {
return app('http')->getName(); return app('http')->getName();
} }
/** /**
* 获取web根目录 * 获取web根目录
*/ */
@ -122,6 +128,7 @@ function web_path()
} }
return $webPath; return $webPath;
} }
/** /**
* 获取当前url的子目录路径 * 获取当前url的子目录路径
*/ */
@ -135,6 +142,7 @@ function root_url()
} }
return $rootUrl; return $rootUrl;
} }
/** /**
* 清空缓存目录 * 清空缓存目录
*/ */
@ -161,6 +169,7 @@ function deldir($path)
} }
} }
} }
/** /**
* 下划线转驼峰 * 下划线转驼峰
*/ */
@ -169,6 +178,7 @@ function camelize($uncamelized_words, $separator = '_')
$uncamelized_words = $separator . str_replace($separator, " ", strtolower($uncamelized_words)); $uncamelized_words = $separator . str_replace($separator, " ", strtolower($uncamelized_words));
return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator); return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator);
} }
/** /**
* 驼峰转下划线 * 驼峰转下划线
*/ */
@ -176,6 +186,7 @@ function uncamelize($camelCaps, $separator = '_')
{ {
return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps)); return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
} }
/** /**
* 隐藏手机号中间四位 13012345678 -> 130****5678 * 隐藏手机号中间四位 13012345678 -> 130****5678
* @param string $phone 手机号 * @param string $phone 手机号
@ -320,6 +331,7 @@ function array_merge_multiple($array1, $array2)
} }
return $data; return $data;
} }
/** /**
* 判断是否为自定义索引数组 * 判断是否为自定义索引数组
*/ */
@ -389,16 +401,13 @@ if (!function_exists('array_repeat')) {
//去除一维数组重复值 //去除一维数组重复值
$a = array_unique($a); $a = array_unique($a);
//提取二维数组项目值 //提取二维数组项目值
foreach($array[0] AS$key=>$value) foreach ($array[0] AS $key => $value) {
{
$akey[] = $key; $akey[] = $key;
} }
//重新拼接二维数组 //重新拼接二维数组
foreach($akey AS$key=>$value) foreach ($akey AS $key => $value) {
{
$b = array_tq($array, $value); $b = array_tq($array, $value);
foreach($a AS$key2=>$value2) foreach ($a AS $key2 => $value2) {
{
$c[$key2][$value] = $b[$key2]; $c[$key2][$value] = $b[$key2];
} }
} }
@ -409,8 +418,7 @@ if (!function_exists('array_repeat')) {
if (!function_exists('array_tq')) { if (!function_exists('array_tq')) {
function array_tq($array, $aval = "") function array_tq($array, $aval = "")
{ {
foreach($array AS $key => $value) foreach ($array AS $key => $value) {
{
$result[] = $value[$aval]; $result[] = $value[$aval];
} }
return $result; return $result;
@ -934,7 +942,7 @@ function write_log($values, $dir)
if (is_array($values)) if (is_array($values))
$values = print_r($values, true); $values = print_r($values, true);
// 日志内容 // 日志内容
$content = '[' . date('Y-m-d H:i:s') . ']' . PHP_EOL . $values . PHP_EOL . PHP_EOL; $content = '[' . date('Y-m-d H:i:s') . ']' . PHP_EOL . json_encode($values, 256) . PHP_EOL . PHP_EOL;
try { try {
// 文件路径 // 文件路径
$filePath = $dir . '/logs/'; $filePath = $dir . '/logs/';

View File

@ -1,4 +1,5 @@
<?php <?php
namespace hema\wechat; namespace hema\wechat;
use app\common\model\Setting; use app\common\model\Setting;
@ -13,6 +14,7 @@ class Driver
private $isp; //是否为服务商调用 private $isp; //是否为服务商调用
private $config; private $config;
private $error; private $error;
/** /**
* 构造函数 * 构造函数
*/ */
@ -61,9 +63,9 @@ class Driver
return $result; return $result;
} }
//* ************************即将废除 - 开始************************* //* ************************即将废除 - 开始*************************
/** /**
* 获取session_key - 服务商版 * 获取session_key - 服务商版
*/ */
public function getComponentSessionKey(string $code, int $applet_id) public function getComponentSessionKey(string $code, int $applet_id)
{ {
@ -87,7 +89,6 @@ class Driver
//* ************************即将废除 - 结束************************* //* ************************即将废除 - 结束*************************
/************ 小程序直播 (权限集 id 为52**************/ /************ 小程序直播 (权限集 id 为52**************/
/** /**
* 申请开通直播 * 申请开通直播
@ -137,6 +138,7 @@ class Driver
} }
return []; return [];
} }
/** /**
* 将草稿添加到模板库 * 将草稿添加到模板库
* $template_type 模板类型 0=普通模板 1=标准模板 * $template_type 模板类型 0=普通模板 1=标准模板
@ -151,6 +153,7 @@ class Driver
]; ];
return $this->result(Http::post($url, hema_json($queryarr))); return $this->result(Http::post($url, hema_json($queryarr)));
} }
/** /**
* 获取草稿箱列表 * 获取草稿箱列表
*/ */
@ -207,6 +210,7 @@ class Driver
]; ];
return $this->result(Http::post($url, hema_json($queryarr))); return $this->result(Http::post($url, hema_json($queryarr)));
} }
/** /**
* 获取体验版二维码 * 获取体验版二维码
*/ */
@ -224,6 +228,7 @@ class Driver
file_put_contents('./' . $path . '/test_code_' . $applet_id . '.png', $result); file_put_contents('./' . $path . '/test_code_' . $applet_id . '.png', $result);
return '/' . $path . '/test_code_' . $applet_id . '.png'; return '/' . $path . '/test_code_' . $applet_id . '.png';
} }
/** /**
* 提交代码审核 * 提交代码审核
*/ */
@ -247,6 +252,7 @@ class Driver
} }
return $result; return $result;
} }
/** /**
* 查询审核单状态 * 查询审核单状态
*/ */
@ -259,6 +265,7 @@ class Driver
]; ];
return $this->result(Http::post($url, json_encode($queryarr))); return $this->result(Http::post($url, json_encode($queryarr)));
} }
/** /**
* 撤回代码审核 * 撤回代码审核
* 单个帐号每天审核撤回次数最多不超过 5 每天的额度从0点开始生效一个月不超过 10 * 单个帐号每天审核撤回次数最多不超过 5 每天的额度从0点开始生效一个月不超过 10
@ -269,6 +276,7 @@ class Driver
$url = 'https://api.weixin.qq.com/wxa/undocodeaudit?access_token=' . $access_token; $url = 'https://api.weixin.qq.com/wxa/undocodeaudit?access_token=' . $access_token;
return $this->result(Http::get($url)); return $this->result(Http::get($url));
} }
/** /**
* 发布已通过审核的小程序 * 发布已通过审核的小程序
*/ */
@ -278,6 +286,7 @@ class Driver
$url = 'https://api.weixin.qq.com/wxa/release?access_token=' . $access_token; $url = 'https://api.weixin.qq.com/wxa/release?access_token=' . $access_token;
return $this->result(Http::post($url, '{}')); return $this->result(Http::post($url, '{}'));
} }
/** /**
* 小程序版本回退 * 小程序版本回退
*/ */
@ -287,6 +296,7 @@ class Driver
$url = 'https://api.weixin.qq.com/wxa/revertcoderelease?app_version=' . $version . '&access_token=' . $access_token; $url = 'https://api.weixin.qq.com/wxa/revertcoderelease?app_version=' . $version . '&access_token=' . $access_token;
return $this->result(Http::get($url)); return $this->result(Http::get($url));
} }
/** /**
* 获取可回退的小程序版本 * 获取可回退的小程序版本
*/ */
@ -296,6 +306,7 @@ class Driver
$url = 'https://api.weixin.qq.com/wxa/revertcoderelease?action=get_history_version&access_token=' . $access_token; $url = 'https://api.weixin.qq.com/wxa/revertcoderelease?action=get_history_version&access_token=' . $access_token;
return $this->result(Http::get($url)); return $this->result(Http::get($url));
} }
/** /**
* 设置小程序服务状态 * 设置小程序服务状态
*/ */
@ -308,6 +319,7 @@ class Driver
$url = 'https://api.weixin.qq.com/wxa/change_visitstatus?access_token=' . $access_token; $url = 'https://api.weixin.qq.com/wxa/change_visitstatus?access_token=' . $access_token;
return $this->result(Http::post($url, hema_json($queryarr))); return $this->result(Http::post($url, hema_json($queryarr)));
} }
/** /**
* 查询小程序服务状态 * 查询小程序服务状态
*/ */
@ -317,6 +329,7 @@ class Driver
$url = 'https://api.weixin.qq.com/wxa/getvisitstatus?access_token=' . $access_token; $url = 'https://api.weixin.qq.com/wxa/getvisitstatus?access_token=' . $access_token;
return $this->result(Http::post($url, '{}')); return $this->result(Http::post($url, '{}'));
} }
/** /**
* 设置最低基础库版本 * 设置最低基础库版本
*/ */
@ -329,6 +342,7 @@ class Driver
$url = 'https://api.weixin.qq.com/cgi-bin/wxopen/setweappsupportversion?access_token=' . $access_token; $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/setweappsupportversion?access_token=' . $access_token;
return $this->result(Http::post($url, hema_json($queryarr))); return $this->result(Http::post($url, hema_json($queryarr)));
} }
/** /**
* 查询服务商审核额度 * 查询服务商审核额度
*/ */
@ -338,6 +352,7 @@ class Driver
$url = 'https://api.weixin.qq.com/wxa/queryquota?access_token=' . $access_token; $url = 'https://api.weixin.qq.com/wxa/queryquota?access_token=' . $access_token;
return $this->result(Http::get($url)); return $this->result(Http::get($url));
} }
/** /**
* 加急代码审核 * 加急代码审核
*/ */
@ -350,6 +365,7 @@ class Driver
$url = 'https://api.weixin.qq.com/wxa/speedupaudit?access_token=' . $access_token; $url = 'https://api.weixin.qq.com/wxa/speedupaudit?access_token=' . $access_token;
return $this->result(Http::post($url, hema_json($queryarr))); return $this->result(Http::post($url, hema_json($queryarr)));
} }
/** /**
* 查询小程序版本信息 * 查询小程序版本信息
*/ */
@ -359,6 +375,7 @@ class Driver
$url = 'https://api.weixin.qq.com/wxa/getversioninfo?access_token=' . $access_token; $url = 'https://api.weixin.qq.com/wxa/getversioninfo?access_token=' . $access_token;
return $this->result(Http::post($url, '{}')); return $this->result(Http::post($url, '{}'));
} }
/** /**
* 查询最新一次审核单状态 * 查询最新一次审核单状态
*/ */
@ -368,6 +385,7 @@ class Driver
$url = 'https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=' . $access_token; $url = 'https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=' . $access_token;
return $this->result(Http::get($url)); return $this->result(Http::get($url));
} }
/** /**
* 获取隐私接口检测结果 * 获取隐私接口检测结果
*/ */
@ -429,6 +447,7 @@ class Driver
$result = json_decode(Http::post($url, $queryarr), true); $result = json_decode(Http::post($url, $queryarr), true);
return $result; return $result;
} }
/** /**
* 增加或修改二维码规则 * 增加或修改二维码规则
*/ */
@ -454,6 +473,7 @@ class Driver
rmdirs($path);//添加成功,删除效验文件 rmdirs($path);//添加成功,删除效验文件
return $result; return $result;
} }
/** /**
* 发布已设置的二维码规则 * 发布已设置的二维码规则
*/ */
@ -471,6 +491,7 @@ class Driver
]; ];
return $this->result(Http::post($url, hema_json($queryarr))); return $this->result(Http::post($url, hema_json($queryarr)));
} }
/** /**
* 删除已设置的二维码规则 * 删除已设置的二维码规则
*/ */
@ -488,6 +509,7 @@ class Driver
$url = 'https://api.weixin.qq.com/cgi-bin/wxopen/qrcodejumpdelete?access_token=' . $access_token; $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/qrcodejumpdelete?access_token=' . $access_token;
return $this->result(Http::post($url, hema_json($queryarr))); return $this->result(Http::post($url, hema_json($queryarr)));
} }
/** /**
* 获取校验文件名称及内容 * 获取校验文件名称及内容
*/ */
@ -518,6 +540,7 @@ class Driver
$result = json_decode(Http::get($url), true); $result = json_decode(Http::get($url), true);
return $result; return $result;
} }
/** /**
* 申请地理位置接口 * 申请地理位置接口
*/ */
@ -542,6 +565,7 @@ class Driver
Http::post($url, json_encode($queryarr)); Http::post($url, json_encode($queryarr));
return true; return true;
} }
/** /**
* 获取不限制的小程序码 * 获取不限制的小程序码
*/ */
@ -561,6 +585,7 @@ class Driver
//获取的二维码数据存储到指定的文件 //获取的二维码数据存储到指定的文件
return $file_path; return $file_path;
} }
/** /**
* 公众号用户登录 - 获取用户资料 * 公众号用户登录 - 获取用户资料
*/ */
@ -582,10 +607,12 @@ class Driver
} }
return $ticket; return $ticket;
} }
/** /**
* 查询昵称设置状态 * 查询昵称设置状态
*/ */
public function querynickname(){ public function querynickname()
{
$audit_id = '454738159'; $audit_id = '454738159';
$access_token = getAccessToken(); $access_token = getAccessToken();
$url = 'https://api.weixin.qq.com/wxa/api_wxa_querynickname?access_token=' . $access_token; $url = 'https://api.weixin.qq.com/wxa/api_wxa_querynickname?access_token=' . $access_token;
@ -603,6 +630,7 @@ class Driver
$url = 'https://api.weixin.qq.com/wxa/getshowwxaitem?access_token=' . $access_token; $url = 'https://api.weixin.qq.com/wxa/getshowwxaitem?access_token=' . $access_token;
return curl($url); return curl($url);
} }
/** /**
* code换取token - 微信扫码登录 * code换取token - 微信扫码登录
*/ */
@ -797,6 +825,7 @@ class Driver
]; ];
return $this->result(Http::post($url, hema_json($queryarr))); return $this->result(Http::post($url, hema_json($queryarr)));
} }
/** /**
* 设置功能介绍 * 设置功能介绍
*/ */
@ -829,6 +858,7 @@ class Driver
$queryarr = ['nick_name' => $nick_name]; $queryarr = ['nick_name' => $nick_name];
return $this->result(Http::post($url, hema_json($queryarr))); return $this->result(Http::post($url, hema_json($queryarr)));
} }
/** /**
* 设置小程序昵称 * 设置小程序昵称
*/ */
@ -988,6 +1018,7 @@ class Driver
} }
return $list; return $list;
} }
/** /**
* 附近小程序 - 申请附近地点类目 * 附近小程序 - 申请附近地点类目
*/ */
@ -997,6 +1028,7 @@ class Driver
$url = 'https://api.weixin.qq.com/wxa/nearbyapplycategory?access_token=' . $access_token; $url = 'https://api.weixin.qq.com/wxa/nearbyapplycategory?access_token=' . $access_token;
$this->result(Http::post($url, hema_json($queryarr))); $this->result(Http::post($url, hema_json($queryarr)));
} }
/** /**
* 附近小程序 - 拉取门店小程序类目 * 附近小程序 - 拉取门店小程序类目
*/ */
@ -1038,6 +1070,7 @@ class Driver
} }
return 'code' . $result['errcode'] . 'msg' . $result['errmsg'];//获取失败 return 'code' . $result['errcode'] . 'msg' . $result['errmsg'];//获取失败
} }
/** /**
* 附近小程序 - 获取附近门店列表 * 附近小程序 - 获取附近门店列表
*/ */
@ -1048,6 +1081,7 @@ class Driver
$result = json_decode(Http::get($url), true); $result = json_decode(Http::get($url), true);
return $result; return $result;
} }
/** /**
* 附近小程序 - 添加附近门店 * 附近小程序 - 添加附近门店
*/ */
@ -1058,6 +1092,7 @@ class Driver
$url = 'https://api.weixin.qq.com/wxa/addnearbypoi?access_token=' . $access_token; $url = 'https://api.weixin.qq.com/wxa/addnearbypoi?access_token=' . $access_token;
return $this->result(Http::post($url, hema_json($queryarr))); return $this->result(Http::post($url, hema_json($queryarr)));
} }
/** /**
* 附近小程序 - 删除附近门店 * 附近小程序 - 删除附近门店
*/ */
@ -1118,6 +1153,7 @@ class Driver
]; ];
return json_decode(Http::post($url, hema_json($queryarr)), true); return json_decode(Http::post($url, hema_json($queryarr)), true);
} }
/** /**
* 添加体验用户 * 添加体验用户
*/ */
@ -1130,6 +1166,7 @@ class Driver
]; ];
return $this->result(Http::post($url, hema_json($queryarr))); return $this->result(Http::post($url, hema_json($queryarr)));
} }
/** /**
* 删除体验用户 * 删除体验用户
*/ */
@ -1164,6 +1201,7 @@ class Driver
} }
return $result['url']; return $result['url'];
} }
/** /**
* 使用授权码获取授权信息 * 使用授权码获取授权信息
*/ */
@ -1202,6 +1240,7 @@ class Driver
$this->error = 'code' . $result['errcode'] . ' msg' . $result['errmsg']; $this->error = 'code' . $result['errcode'] . ' msg' . $result['errmsg'];
return false; return false;
} }
/************ 公众号 - 生成二维码 (帐号管理-生成带参数的二维码)**************/ /************ 公众号 - 生成二维码 (帐号管理-生成带参数的二维码)**************/
public function qrcodeCreate($applet_id = 0, $action_name = 'QR_STR_SCENE', $scene = 'login', $expire_seconds = 7200) public function qrcodeCreate($applet_id = 0, $action_name = 'QR_STR_SCENE', $scene = 'login', $expire_seconds = 7200)
@ -1276,6 +1315,7 @@ class Driver
} }
return $result; return $result;
} }
/** /**
* 公众号获取粉丝列表 * 公众号获取粉丝列表
*/ */
@ -1286,6 +1326,7 @@ class Driver
$result = json_decode(Http::get($url), true); $result = json_decode(Http::get($url), true);
return $result; return $result;
} }
/** /**
* 上传图文消息内的图片 - 到微信端(永久素材) - 公众号端 * 上传图文消息内的图片 - 到微信端(永久素材) - 公众号端
* 图片仅支持jpg/png格式大小必须在1MB以下 * 图片仅支持jpg/png格式大小必须在1MB以下
@ -1306,6 +1347,7 @@ class Driver
} }
return $img; return $img;
} }
/** /**
* 上传素材文件 - 到微信端 * 上传素材文件 - 到微信端
*/ */
@ -1333,6 +1375,7 @@ class Driver
$result = json_decode(http_post($url, $queryarr), true); $result = json_decode(http_post($url, $queryarr), true);
return $result; //$result['media_id']; return $result; //$result['media_id'];
} }
/** /**
* 删除素材文件 - 到微信端 * 删除素材文件 - 到微信端
*/ */
@ -1412,6 +1455,7 @@ class Driver
]; ];
return json_decode(Http::post($url, json_encode($queryarr)), true); return json_decode(Http::post($url, json_encode($queryarr)), true);
} }
/** /**
* 根据OpenID列表群发公众号信息 * 根据OpenID列表群发公众号信息
*/ */
@ -1485,6 +1529,7 @@ class Driver
$result = json_decode(Http::post($url, json_encode($queryarr)), true); $result = json_decode(Http::post($url, json_encode($queryarr)), true);
return $result; return $result;
} }
/** /**
* 删除公众号群发记录 * 删除公众号群发记录
*/ */
@ -1496,6 +1541,7 @@ class Driver
$result = json_decode(Http::post($url, json_encode($queryarr)), true); $result = json_decode(Http::post($url, json_encode($queryarr)), true);
return $result; return $result;
} }
/** /**
* 向公众号发布预览消息 * 向公众号发布预览消息
*/ */
@ -1568,6 +1614,7 @@ class Driver
$result = json_decode(Http::post($url, json_encode($queryarr)), true); $result = json_decode(Http::post($url, json_encode($queryarr)), true);
return $result; return $result;
} }
/** /**
* 公众号自定义菜单 - 同步到微信端 * 公众号自定义菜单 - 同步到微信端
*/ */
@ -1581,6 +1628,7 @@ class Driver
$result = json_decode(Http::post($url, hema_json($queryarr)), true); $result = json_decode(Http::post($url, hema_json($queryarr)), true);
return $result; return $result;
} }
/** /**
* 发布模板消息 - 公众号 * 发布模板消息 - 公众号
*/ */
@ -1721,23 +1769,21 @@ class Driver
$this->error = '参数错误'; $this->error = '参数错误';
return false; return false;
} }
/** /**
* 获取令牌 - 开放平台 * 获取令牌 - 开放平台
* $type 请求类型 1小程序2公众号 * $type 请求类型 1小程序2公众号
*/ */
function getAccessToken(int $applet_id = 0, int $type = 1) function getAccessToken(int $applet_id = 0, int $type = 1)
{ {
$config = $this->config; //获取第三方配置
$access_token = ''; $access_token = '';
if($type==1){ if ($applet = Applet::getApplet(['applet_id' => $applet_id, 'status' => 1])) {
if($applet = Applet::getApplet([ 'applet_id' => $applet_id,'status' => 1 ]))
{
$access_token = $applet['access_token']; $access_token = $applet['access_token'];
if (empty($access_token)) { if (empty($access_token)) {
if (time() > $applet['expires_in']) {
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3668302906e3894f&secret=3ef948febd085fb89908f22b4a79c52f'; $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3668302906e3894f&secret=3ef948febd085fb89908f22b4a79c52f';
$result = json_decode(Http::get($url), true); $result = json_decode(Http::get($url), true);
@ -1747,38 +1793,15 @@ class Driver
$applet->expires_in = time() + 3600;//2个小时候过期这里设置1小时获取一次 $applet->expires_in = time() + 3600;//2个小时候过期这里设置1小时获取一次
$applet->save();//保存最新的令牌access_token和过期时间 $applet->save();//保存最新的令牌access_token和过期时间
} }
}
}
} else { } else {
//公众号
if($wechat = Wechat::getWechat([
'applet_id' => $applet_id,
'status' => 1
])){
$access_token=$wechat['access_token'];
if($wechat['expires_in'] < time()){
//重新获取
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token='.$config['component_access_token'];
$queryarr = [
'component_appid' => $config['app_id'],
'authorizer_appid' => $wechat['app_id'],
'authorizer_refresh_token' => $wechat['authorizer_refresh_token']
];
$result = json_decode(Http::post($url, json_encode($queryarr)),true);
if(isset($result['authorizer_access_token'])){
$access_token = $result['authorizer_access_token'];
$wechat->access_token = $result['authorizer_access_token'];
$wechat->expires_in = time()+3600; //2个小时候过期这里设置1小时获取一次
$wechat->save();//保存最新的令牌access_token和过期时间
}
}
}
}
return $access_token; return $access_token;
} }
}
}
return $access_token;
}
/** /**
* 请求数据验证 * 请求数据验证
**/ **/
@ -1791,6 +1814,7 @@ class Driver
} }
return $result; return $result;
} }
public function getError() public function getError()
{ {
return $this->error; return $this->error;
@ -1807,6 +1831,7 @@ class Driver
$url = 'https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token=' . $access_token; $url = 'https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token=' . $access_token;
return $this->result(Http::get($url)); return $this->result(Http::get($url));
} }
/** /**
* 删除帐号下的模板 * 删除帐号下的模板
*/ */
@ -1819,6 +1844,7 @@ class Driver
]; ];
return $this->result(Http::post($url, hema_json($queryarr), [], ['content-type: application/json'])); return $this->result(Http::post($url, hema_json($queryarr), [], ['content-type: application/json']));
} }
/** /**
* 添加帐号下的模板 * 添加帐号下的模板
*/ */
@ -1833,6 +1859,7 @@ class Driver
]; ];
return $this->result(Http::post($url, hema_json($queryarr), [], ['content-type: application/json'])); return $this->result(Http::post($url, hema_json($queryarr), [], ['content-type: application/json']));
} }
/** /**
* 发送订阅消息 * 发送订阅消息
*/ */

View File

@ -7,9 +7,9 @@ use app\applet\model\User as UserModel;
use app\common\model\Setting; use app\common\model\Setting;
use app\common\model\Applet; use app\common\model\Applet;
use app\common\model\Wechat; use app\common\model\Wechat;
use Requests;
use think\facade\Cache; use think\facade\Cache;
use hema\Http; use hema\Http;
use think\facade\Db;
class Index class Index
{ {
@ -29,62 +29,65 @@ class Index
]; ];
} }
public function index() public function index()
{ {
if (isset($_GET['echostr'])) { if (isset($_GET['echostr'])) {
echo $_GET["echostr"]; echo $_GET["echostr"];
exit; exit;
} }
//接收微信推送数据 //接收微信推送数据
$nonce = empty ($_GET ['nonce']) ? "" : trim($_GET ['nonce']); $data = file_get_contents('php://input');
$signature = empty ($_GET['signature']) ? "" : trim($_GET ['signature']);
$timeStamp = empty ($_GET ['timestamp']) ? "" : trim($_GET ['timestamp']);
$msg_signature = empty ($_GET['msg_signature']) ? "" : trim($_GET ['msg_signature']);
$encryptMsg = file_get_contents('php://input');
write_log($encryptMsg, __DIR__); // $data = simplexml_load_string($data, "SimpleXMLElement", LIBXML_NOCDATA);
if (!empty($data)) {
die('success'); $data = json_decode($data, true);
if (!empty($data)) {
//首次关注
$pc = new WxBizMsgCrypt();//创建解密类
$msg = '';
$errCode = $pc->decryptMsg($msg_signature, $timeStamp, $nonce, $encryptMsg, $msg);
if ($errCode == 0) {
$data = _xml_to_arr($msg); //XML转换为数组
//首次关注公众号
if ($data['MsgType'] == 'event') {
if ($data['Event'] == 'subscribe') { if ($data['Event'] == 'subscribe') {
$user = Db::name('food_user')->where('open_id', $data['FromUserName'])->value('is_new');
if ($user !== 1) {
//不是新关注
die('success');
} else {
//插入一条新用户数据
$data = [
'open_id' => $data['FromUserName'],
'platform' => 10,
'applet_id' => 10001,
'create_time' => time()
];
Db::name('food_user')->insert($data);
//发送公众号消息
$wx = new Driver; $wx = new Driver;
if ($wechat_user = $wx->getWechatUserInfo($data['FromUserName'], 10001)) { $access_token = $wx->getAccessToken(10001);
//用户操作 $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $access_token;
$model = new UserModel; $queryarr = [
if ($user = $model->subscribe($wechat_user, $this->wechat['applet_id'])) { 'msgtype' => 'news',
'news' => [
'articles' => [
'title' => '您有一张新的优惠券,请及时领取',
'picurl' => '',
'url' => 'https://www.baidu.com',
'description' => '黄辛一新人优惠,首次关注赠送优惠券'
]
]
];
$queryarr['touser'] = $data['FromUserName'];
Http::post($url, hema_json($queryarr));
} }
} else {
//返回文本提醒
$wx->sendServiceMsg([
'type' => 'text',
'content' => $wx->getError()
], $data['FromUserName'], $this->wechat['applet_id']);
}
//是否设置了关注回复
if ($subscribe = SettingModel::getItem('subscribe', $this->wechat['applet_id'])) {
$this->replyMsg($subscribe, $data);//回复信息
}
die('success'); die('success');
} }
} }
} }
} }