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 hema\wechat\Driver as Wechat;
use app\common\model\User as UserModel;
use app\common\model\Setting as SettingModel;
use app\common\model\food\ShopClerk as ShopClerkModel;
/**
* 生成数字验证码
*/
@ -21,11 +23,11 @@ if (!function_exists('is_json')) {
* 生成数字验证码
*/
if (!function_exists('get_captcha')) {
function get_captcha($length=4)
function get_captcha($length = 4)
{
$str = '';
for($n=0;$n<$length;$n++){
$str .= (string)rand(0,9);
for ($n = 0; $n < $length; $n++) {
$str .= (string)rand(0, 9);
}
return $str;
}
@ -36,12 +38,12 @@ if (!function_exists('get_captcha')) {
if (!function_exists('hema_json')) {
function hema_json($data)
{
return json_encode($data,JSON_UNESCAPED_UNICODE);
return json_encode($data, JSON_UNESCAPED_UNICODE);
}
}
/**
* 验证手机号是否正确
*/
* 验证手机号是否正确
*/
if (!function_exists('is_phone')) {
function is_phone($phone)
{
@ -88,6 +90,7 @@ function base_url()
}
return $baseUrl;
}
/**
* 获取当前域名
*/
@ -96,6 +99,7 @@ function domain()
$request = Request::instance();
return $request->host();
}
/**
* 获取当前uploads目录访问地址
*/
@ -103,6 +107,7 @@ function uploads_url()
{
return base_url() . 'uploads/';
}
/**
* 获取当前的应用名称
*/
@ -110,6 +115,7 @@ function app_name()
{
return app('http')->getName();
}
/**
* 获取web根目录
*/
@ -122,6 +128,7 @@ function web_path()
}
return $webPath;
}
/**
* 获取当前url的子目录路径
*/
@ -135,32 +142,34 @@ function root_url()
}
return $rootUrl;
}
/**
* 清空缓存目录
*/
function deldir($path)
{
//如果是目录则继续
if(is_dir($path)){
if (is_dir($path)) {
//扫描一个文件夹内的所有文件夹和文件并返回数组
$p = scandir($path);
foreach($p as $val){
foreach ($p as $val) {
//排除目录中的.和..
if($val !="." && $val !=".."){
if ($val != "." && $val != "..") {
//如果是目录则递归子目录,继续操作
if(is_dir($path.$val)){
if (is_dir($path . $val)) {
//子目录中操作删除文件夹和文件
deldir($path.$val.'/');
deldir($path . $val . '/');
//目录清空后删除空文件夹
@rmdir($path.$val.'/');
}else{
@rmdir($path . $val . '/');
} else {
//如果是文件直接删除
unlink($path.$val);
unlink($path . $val);
}
}
}
}
}
/**
* 下划线转驼峰
*/
@ -169,6 +178,7 @@ function camelize($uncamelized_words, $separator = '_')
$uncamelized_words = $separator . str_replace($separator, " ", strtolower($uncamelized_words));
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));
}
/**
* 隐藏手机号中间四位 13012345678 -> 130****5678
* @param string $phone 手机号
@ -290,12 +301,12 @@ if (!function_exists('convert_left_slash')) {
}
/**
* 将xml转为array
*/
*/
if (!function_exists('_xml_to_arr')) {
function _xml_to_arr($xml)
{
$res = @simplexml_load_string ( $xml,NULL, LIBXML_NOCDATA );
$res = json_decode ( json_encode ( $res), true );
$res = @simplexml_load_string($xml, NULL, LIBXML_NOCDATA);
$res = json_decode(json_encode($res), true);
return $res;
}
}
@ -320,6 +331,7 @@ function array_merge_multiple($array1, $array2)
}
return $data;
}
/**
* 判断是否为自定义索引数组
*/
@ -336,13 +348,13 @@ if (!function_exists('is_assoc')) {
if (!function_exists('arr_sort')) {
function arr_sort(array $data, string $field, string $order = 'desc')
{
foreach($data as $val){
$key_arrays[]=$val[$field];
foreach ($data as $val) {
$key_arrays[] = $val[$field];
}
if($order == 'desc'){
array_multisort($key_arrays,SORT_DESC,SORT_NUMERIC,$data);
}else{
array_multisort($key_arrays,SORT_ASC,SORT_NUMERIC,$data);
if ($order == 'desc') {
array_multisort($key_arrays, SORT_DESC, SORT_NUMERIC, $data);
} else {
array_multisort($key_arrays, SORT_ASC, SORT_NUMERIC, $data);
}
return $data;
}
@ -365,9 +377,9 @@ if (!function_exists('array_sort')) {
}
reset($key_value);
foreach ($key_value as $k => $v) {
if(is_int($k)){
if (is_int($k)) {
$new_array[] = $arr[$k];
}else{
} else {
$new_array[$k] = $arr[$k];
}
}
@ -380,26 +392,23 @@ if (!function_exists('array_sort')) {
*keyid 需要判断是否重复的项目
*/
if (!function_exists('array_repeat')) {
function array_repeat($array,$keyid)
function array_repeat($array, $keyid)
{
$array =array_values($array);
$array = array_values($array);
//提取需要判断的项目变成一维数组
$a = array_tq($array,$keyid);
$a = array_tq($array, $keyid);
//去除一维数组重复值
$a =array_unique($a);
$a = array_unique($a);
//提取二维数组项目值
foreach($array[0] AS$key=>$value)
{
$akey[] =$key;
foreach ($array[0] AS $key => $value) {
$akey[] = $key;
}
//重新拼接二维数组
foreach($akey AS$key=>$value)
{
$b = array_tq($array,$value);
foreach($a AS$key2=>$value2)
{
$c[$key2][$value] =$b[$key2];
foreach ($akey AS $key => $value) {
$b = array_tq($array, $value);
foreach ($a AS $key2 => $value2) {
$c[$key2][$value] = $b[$key2];
}
}
return $c;
@ -407,10 +416,9 @@ if (!function_exists('array_repeat')) {
}
//提取二维数组项目
if (!function_exists('array_tq')) {
function array_tq($array, $aval="")
{
foreach($array AS $key => $value)
function array_tq($array, $aval = "")
{
foreach ($array AS $key => $value) {
$result[] = $value[$aval];
}
return $result;
@ -424,19 +432,19 @@ if (!function_exists('array_tq')) {
* @return [type] [description]
*/
if (!function_exists('http_post')) {
function http_post($url,$data = array(),$headers=array())
function http_post($url, $data = array(), $headers = array())
{
$curl = curl_init();
if( count($headers) >= 1 ){
if (count($headers) >= 1) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (count($data) >= 1 AND !isset($data['media'])){
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
}elseif(count($data) == 0){
if (count($data) >= 1 AND !isset($data['media'])) {
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
} elseif (count($data) == 0) {
$data = '{}';
}
curl_setopt($curl, CURLOPT_POST, 1);
@ -515,10 +523,10 @@ if (!function_exists('get_guid_v4')) {
* 商户管理员 - 账户资金变动提醒 - 公众号
*/
if (!function_exists('sand_account_change_msg')) {
function sand_account_change_msg($title,$pay,$surplus,$user_id,$applet_id = 0)
function sand_account_change_msg($title, $pay, $surplus, $user_id, $applet_id = 0)
{
$webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号
if(empty($webtplmsg['balance'])){
$webtplmsg = SettingModel::getItem('webtplmsg', $applet_id); //获取模板消息编号
if (empty($webtplmsg['balance'])) {
return true;
}
$foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序
@ -539,15 +547,15 @@ if (!function_exists('sand_account_change_msg')) {
'color' => '#173177'
],
'keyword2' => [
'value' => '¥'.$pay.'元',//交易金额
'value' => '¥' . $pay . '元',//交易金额
'color' => '#173177'
],
'keyword3' => [
'value' => date('Y/m/d H:i:s',time()),//交易时间
'value' => date('Y/m/d H:i:s', time()),//交易时间
'color' => '#173177'
],
'keyword4' => [
'value' => '¥'.$surplus.'元',//账户余额
'value' => '¥' . $surplus . '元',//账户余额
'color' => '#173177'
],
'remark' => [
@ -556,7 +564,7 @@ if (!function_exists('sand_account_change_msg')) {
]
]
];
return send_wechat_msg($msg,$user_id);
return send_wechat_msg($msg, $user_id);
}
}
@ -564,10 +572,10 @@ if (!function_exists('sand_account_change_msg')) {
* 商户管理员 - 申请受理通知 - 公众号
*/
if (!function_exists('sand_apply_msg')) {
function sand_apply_msg($name,$title,$user_id,$applet_id = 0)
function sand_apply_msg($name, $title, $user_id, $applet_id = 0)
{
$webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号
if(empty($webtplmsg['apply'])){
$webtplmsg = SettingModel::getItem('webtplmsg', $applet_id); //获取模板消息编号
if (empty($webtplmsg['apply'])) {
return true;
}
$foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序
@ -592,7 +600,7 @@ if (!function_exists('sand_apply_msg')) {
'color' => '#173177'
],
'keyword3' => [
'value' => date('Y/m/d H:i:s',time()),//时间
'value' => date('Y/m/d H:i:s', time()),//时间
'color' => '#173177'
],
'remark' => [
@ -601,7 +609,7 @@ if (!function_exists('sand_apply_msg')) {
]
]
];
return send_wechat_msg($msg,$user_id);
return send_wechat_msg($msg, $user_id);
}
}
@ -609,10 +617,10 @@ if (!function_exists('sand_apply_msg')) {
* 商户管理员 - 申请状态更新通知 - 公众号
*/
if (!function_exists('sand_examine_msg')) {
function sand_examine_msg($id,$title,$status,$user_id,$applet_id=0)
function sand_examine_msg($id, $title, $status, $user_id, $applet_id = 0)
{
$webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号
if(empty($webtplmsg['examine'])){
$webtplmsg = SettingModel::getItem('webtplmsg', $applet_id); //获取模板消息编号
if (empty($webtplmsg['examine'])) {
return true;
}
$foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序
@ -637,7 +645,7 @@ if (!function_exists('sand_examine_msg')) {
'color' => '#173177'
],
'keyword3' => [
'value' => date('Y/m/d H:i:s',time()),//申请时间
'value' => date('Y/m/d H:i:s', time()),//申请时间
'color' => '#173177'
],
'keyword4' => [
@ -650,17 +658,17 @@ if (!function_exists('sand_examine_msg')) {
]
]
];
return send_wechat_msg($msg,$user_id);
return send_wechat_msg($msg, $user_id);
}
}
/**
* 商户管理员 - 试用申请成功通知 - 公众号
*/
if (!function_exists('sand_testing_msg')) {
function sand_testing_msg($title,$time,$user_id,$applet_id = 0)
function sand_testing_msg($title, $time, $user_id, $applet_id = 0)
{
$webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号
if(empty($webtplmsg['testing'])){
$webtplmsg = SettingModel::getItem('webtplmsg', $applet_id); //获取模板消息编号
if (empty($webtplmsg['testing'])) {
return true;
}
$foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序
@ -681,7 +689,7 @@ if (!function_exists('sand_testing_msg')) {
'color' => '#173177'
],
'keyword2' => [
'value' => date('Y/m/d H:i:s',$time),//有效期至
'value' => date('Y/m/d H:i:s', $time),//有效期至
'color' => '#173177'
],
'remark' => [
@ -690,17 +698,17 @@ if (!function_exists('sand_testing_msg')) {
]
]
];
return send_wechat_msg($msg,$user_id);
return send_wechat_msg($msg, $user_id);
}
}
/**
* 商户骑手 - 抢单提醒 - 公众号
*/
if (!function_exists('sand_grab_msg')) {
function sand_grab_msg($order,$applet_id = 0)
function sand_grab_msg($order, $applet_id = 0)
{
$webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号
if(empty($webtplmsg['grab'])){
$webtplmsg = SettingModel::getItem('webtplmsg', $applet_id); //获取模板消息编号
if (empty($webtplmsg['grab'])) {
return true;
}
$foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序
@ -739,10 +747,10 @@ if (!function_exists('sand_grab_msg')) {
]
];
$model = new ShopClerkModel;
$clerk = $model->getAll($order['shop_id'],30);
for($n=0;$n<sizeof($clerk);$n++){
if($clerk[$n]['user_id'] > 0){
send_wechat_msg($msg,$clerk[$n]['user_id']);
$clerk = $model->getAll($order['shop_id'], 30);
for ($n = 0; $n < sizeof($clerk); $n++) {
if ($clerk[$n]['user_id'] > 0) {
send_wechat_msg($msg, $clerk[$n]['user_id']);
}
}
}
@ -751,10 +759,10 @@ if (!function_exists('sand_grab_msg')) {
* 商户店长 - 退款申请通知 - 公众号
*/
if (!function_exists('sand_refund_msg')) {
function sand_refund_msg($order,$applet_id = 0)
function sand_refund_msg($order, $applet_id = 0)
{
$webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号
if(empty($webtplmsg['refund'])){
$webtplmsg = SettingModel::getItem('webtplmsg', $applet_id); //获取模板消息编号
if (empty($webtplmsg['refund'])) {
return true;
}
$foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序
@ -779,11 +787,11 @@ if (!function_exists('sand_refund_msg')) {
'color' => '#173177'
],
'keyword3' => [
'value' => '¥'.$order['refund_price'].'元',//退款金额
'value' => '¥' . $order['refund_price'] . '元',//退款金额
'color' => '#173177'
],
'keyword4' => [
'value' => date('Y/m/d H:i:s',time()),//退款时间
'value' => date('Y/m/d H:i:s', time()),//退款时间
'color' => '#173177'
],
'remark' => [
@ -793,10 +801,10 @@ if (!function_exists('sand_refund_msg')) {
]
];
$model = new ShopClerkModel;
$clerk = $model->getAll($order['shop_id'],20);
for($n=0;$n<sizeof($clerk);$n++){
if($clerk[$n]['user_id'] > 0){
send_wechat_msg($msg,$clerk[$n]['user_id']);
$clerk = $model->getAll($order['shop_id'], 20);
for ($n = 0; $n < sizeof($clerk); $n++) {
if ($clerk[$n]['user_id'] > 0) {
send_wechat_msg($msg, $clerk[$n]['user_id']);
}
}
}
@ -805,18 +813,18 @@ if (!function_exists('sand_refund_msg')) {
* 商户店长 新订单提醒模板消息 - 公众号
*/
if (!function_exists('sand_new_order_msg')) {
function sand_new_order_msg($order,$applet_id = 0)
function sand_new_order_msg($order, $applet_id = 0)
{
$webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号
if(empty($webtplmsg['new_order'])){
$webtplmsg = SettingModel::getItem('webtplmsg', $applet_id); //获取模板消息编号
if (empty($webtplmsg['new_order'])) {
return true;
}
$foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序
if($order['order_mode']['value']==30){
if ($order['order_mode']['value'] == 30) {
$title = '自取';
}elseif($order['order_mode']['value']==20){
} elseif ($order['order_mode']['value'] == 20) {
$title = '外卖';
}else{
} else {
$title = '堂食';
}
$msg = [
@ -828,7 +836,7 @@ if (!function_exists('sand_new_order_msg')) {
],
'data' => [
'first' => [
'value' => '收到一个新的'.$title.'订单',
'value' => '收到一个新的' . $title . '订单',
'color' => '#173177'
],
'keyword1' => [
@ -836,7 +844,7 @@ if (!function_exists('sand_new_order_msg')) {
'color' => '#173177'
],
'keyword2' => [
'value' => '¥'.$order['pay_price'].'元',
'value' => '¥' . $order['pay_price'] . '元',
'color' => '#173177'
],
'keyword3' => [
@ -850,10 +858,10 @@ if (!function_exists('sand_new_order_msg')) {
]
];
$model = new ShopClerkModel;
$clerk = $model->getAll($order['shop_id'],20);
for($n=0;$n<sizeof($clerk);$n++){
if($clerk[$n]['user_id'] > 0){
send_wechat_msg($msg,$clerk[$n]['user_id']);
$clerk = $model->getAll($order['shop_id'], 20);
for ($n = 0; $n < sizeof($clerk); $n++) {
if ($clerk[$n]['user_id'] > 0) {
send_wechat_msg($msg, $clerk[$n]['user_id']);
}
}
}
@ -862,12 +870,12 @@ if (!function_exists('sand_new_order_msg')) {
* 发布模板消息 - 公众号
*/
if (!function_exists('send_wechat_msg')) {
function send_wechat_msg($msg, $user_id, $applet_id=0)
function send_wechat_msg($msg, $user_id, $applet_id = 0)
{
$user = UserModel::get($user_id);
$msg['touser'] = $user['open_id'];
$wx = new Wechat;
$wx->sendWechatMsg($applet_id,$msg);
$wx->sendWechatMsg($applet_id, $msg);
return true;
}
}
@ -934,7 +942,7 @@ function write_log($values, $dir)
if (is_array($values))
$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 {
// 文件路径
$filePath = $dir . '/logs/';

File diff suppressed because it is too large Load Diff

View File

@ -7,9 +7,9 @@ use app\applet\model\User as UserModel;
use app\common\model\Setting;
use app\common\model\Applet;
use app\common\model\Wechat;
use Requests;
use think\facade\Cache;
use hema\Http;
use think\facade\Db;
class Index
{
@ -29,62 +29,65 @@ class Index
];
}
public function index()
{
if (isset($_GET['echostr'])) {
echo $_GET["echostr"];
exit;
}
//接收微信推送数据
$nonce = empty ($_GET ['nonce']) ? "" : trim($_GET ['nonce']);
$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');
$data = file_get_contents('php://input');
write_log($encryptMsg, __DIR__);
die('success');
$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') {
// $data = simplexml_load_string($data, "SimpleXMLElement", LIBXML_NOCDATA);
if (!empty($data)) {
$data = json_decode($data, true);
if (!empty($data)) {
//首次关注
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;
if ($wechat_user = $wx->getWechatUserInfo($data['FromUserName'], 10001)) {
//用户操作
$model = new UserModel;
if ($user = $model->subscribe($wechat_user, $this->wechat['applet_id'])) {
$access_token = $wx->getAccessToken(10001);
$url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $access_token;
$queryarr = [
'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');
}
}
}
}