From 6f762dc2499684eae1f3aa006ef8dc89814b800e Mon Sep 17 00:00:00 2001 From: test_service Date: Thu, 7 Dec 2023 09:58:44 +0800 Subject: [PATCH] 1 --- app/common.php | 1894 ++++++++--------- extend/hema/wechat/Index.php | 183 +- extend/hema/wechat/logs/20231206.log | 12 + .../eb/300fe116b3568a7e80a6c2812b1a38.php | 2 +- 4 files changed, 1053 insertions(+), 1038 deletions(-) create mode 100644 extend/hema/wechat/logs/20231206.log diff --git a/app/common.php b/app/common.php index 9e97169..fc22fc5 100755 --- a/app/common.php +++ b/app/common.php @@ -1,948 +1,948 @@ -scheme(); - // url子目录 - $rootUrl = root_url(); - // 拼接完整url - $baseUrl = "{$scheme}://" . $request->host() . $rootUrl; - } - return $baseUrl; -} -/** - * 获取当前域名 - */ -function domain() -{ - $request = Request::instance(); - return $request->host(); -} -/** - * 获取当前uploads目录访问地址 - */ -function uploads_url() -{ - return base_url() . 'uploads/'; -} -/** - * 获取当前的应用名称 - */ -function app_name() -{ - return app('http')->getName(); -} -/** - * 获取web根目录 - */ -function web_path() -{ - static $webPath = ''; - if (empty($webPath)) { - $request = Request::instance(); - $webPath = dirname($request->server('SCRIPT_FILENAME')) . DIRECTORY_SEPARATOR; - } - return $webPath; -} -/** - * 获取当前url的子目录路径 - */ -function root_url() -{ - static $rootUrl = ''; - if (empty($rootUrl)) { - $request = Request::instance(); - $subUrl = str_replace('\\', '/', dirname($request->baseFile())); - $rootUrl = $subUrl . ($subUrl === '/' ? '' : '/'); - } - return $rootUrl; -} -/** - * 清空缓存目录 - */ -function deldir($path) -{ - //如果是目录则继续 - if(is_dir($path)){ - //扫描一个文件夹内的所有文件夹和文件并返回数组 - $p = scandir($path); - foreach($p as $val){ - //排除目录中的.和.. - if($val !="." && $val !=".."){ - //如果是目录则递归子目录,继续操作 - if(is_dir($path.$val)){ - //子目录中操作删除文件夹和文件 - deldir($path.$val.'/'); - //目录清空后删除空文件夹 - @rmdir($path.$val.'/'); - }else{ - //如果是文件直接删除 - unlink($path.$val); - } - } - } - } -} -/** - * 下划线转驼峰 - */ -function camelize($uncamelized_words, $separator = '_') -{ - $uncamelized_words = $separator . str_replace($separator, " ", strtolower($uncamelized_words)); - return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator); -} -/** - * 驼峰转下划线 - */ -function uncamelize($camelCaps, $separator = '_') -{ - return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps)); -} -/** - * 隐藏手机号中间四位 13012345678 -> 130****5678 - * @param string $phone 手机号 - * @return string - */ -if (!function_exists('hide_phone')) { - function hide_phone(string $phone) - { - return substr_replace($phone, '****', 3, 4); - } -} -/** - * 隐藏姓名第二位 欧阳振华 -> 欧*振华 - * @param string $name = 姓名 - * @return string - */ -if (!function_exists('hide_name')) { - function hide_name(string $name) - { - return substr_replace($name, '*', 1, 1); - } -} -/** - * 隐藏敏感字符 - */ -if (!function_exists('substr_cut')) { - function substr_cut($value) - { - $strlen = mb_strlen($value, 'utf-8'); - if ($strlen <= 1) return $value; - $firstStr = mb_substr($value, 0, 1, 'utf-8'); - $lastStr = mb_substr($value, -1, 1, 'utf-8'); - return $strlen == 2 ? $firstStr . str_repeat('*', $strlen - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr; - } -} -/** - * 时间戳转换日期 - */ -if (!function_exists('format_time')) { - function format_time($timeStamp) - { - return date('Y-m-d H:i:s', $timeStamp); - } -} -/** - * 将时间戳转换为日期时间 - * @param int $time 时间戳 - * @param string $format 日期时间格式 - * @return string - */ -if (!function_exists('datetime')) { - function datetime($time, $format = 'Y-m-d H:i:s') - { - $time = is_numeric($time) ? $time : strtotime($time); - return date($format, $time); - } -} -/** - * 左侧填充0 - * @param $value - * @param $padLength - * @return string - */ -if (!function_exists('pad_left')) { - function pad_left($value, $padLength = 2) - { - return \str_pad($value, $padLength, "0", STR_PAD_LEFT); - } -} -/** - * 重写trim方法 (解决int类型过滤后后变为string类型) - * @param $str - * @return string - */ -if (!function_exists('my_trim')) { - function my_trim($str) - { - return is_string($str) ? trim($str) : $str; - } -} -/** - * 重写htmlspecialchars方法 (解决int类型过滤后后变为string类型) - * @param $string - * @return string - */ -if (!function_exists('my_htmlspecialchars')) { - function my_htmlspecialchars($string) - { - return is_string($string) ? htmlspecialchars($string) : $string; - } -} -/** - * 根据指定长度截取字符串 - */ -if (!function_exists('str_substr')) { - function str_substr($str, $length = 30) - { - if (strlen($str) > $length) { - $str = mb_substr($str, 0, $length); - } - return $str; - } -} -/** - * 文本左斜杠转换为右斜杠 - */ -if (!function_exists('convert_left_slash')) { - function convert_left_slash(string $string) - { - return str_replace('\\', '/', $string); - } -} -/** - * 将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 ); - return $res; - } -} -/** - * 多维数组合并 - */ -function array_merge_multiple($array1, $array2) -{ - $merge = $array1 + $array2; - $data = []; - foreach ($merge as $key => $val) { - if ( - isset($array1[$key]) - && is_array($array1[$key]) - && isset($array2[$key]) - && is_array($array2[$key]) - ) { - $data[$key] = array_merge_multiple($array1[$key], $array2[$key]); - } else { - $data[$key] = isset($array2[$key]) ? $array2[$key] : $array1[$key]; - } - } - return $data; -} -/** - * 判断是否为自定义索引数组 - */ -if (!function_exists('is_assoc')) { - function is_assoc(array $array) - { - if (empty($array)) return false; - return array_keys($array) !== range(0, count($array) - 1); - } -} -/** - * 多维数组按照指定字段升/降序排列 - */ -if (!function_exists('arr_sort')) { - function arr_sort(array $data, string $field, string $order = 'desc') - { - 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); - } - return $data; - } -} -/** - * 二维数组排序 - * $desc 是否从大到小排列 - */ -if (!function_exists('array_sort')) { - function array_sort($arr, $keys, $desc = false) - { - $key_value = $new_array = array(); - foreach ($arr as $k => $v) { - $key_value[$k] = $v[$keys]; - } - if ($desc) { - arsort($key_value);//降序 - } else { - asort($key_value);//升序 - } - reset($key_value); - foreach ($key_value as $k => $v) { - if(is_int($k)){ - $new_array[] = $arr[$k]; - }else{ - $new_array[$k] = $arr[$k]; - } - } - return $new_array; - } -} -//去除二维数组重复值,默认重复保留前面的值 -/* - *array 二维数组 - *keyid 需要判断是否重复的项目 -*/ -if (!function_exists('array_repeat')) { - function array_repeat($array,$keyid) - { - $array =array_values($array); - //提取需要判断的项目变成一维数组 - $a = array_tq($array,$keyid); - - //去除一维数组重复值 - $a =array_unique($a); - //提取二维数组项目值 - 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]; - } - } - return $c; - } -} -//提取二维数组项目 -if (!function_exists('array_tq')) { - function array_tq($array, $aval="") - { - foreach($array AS $key => $value) - { - $result[] = $value[$aval]; - } - return $result; - } -} -/** - * POST请求 - * @param [type] $url [description] - * @param array $data [description] - * @param array $headers [description] - * @return [type] [description] - */ -if (!function_exists('http_post')) { - function http_post($url,$data = array(),$headers=array()) - { - $curl = curl_init(); - 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){ - $data = '{}'; - } - curl_setopt($curl, CURLOPT_POST, 1); - curl_setopt($curl, CURLOPT_POSTFIELDS, $data); - - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - $output = curl_exec($curl); - $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); - curl_close($curl); - return $output; - } -} -/** - * array_column 兼容低版本php - * (PHP < 5.5.0) - * @param $array - * @param $columnKey - * @param null $indexKey - * @return array - */ -if (!function_exists('array_column')) { - function array_column($array, $columnKey, $indexKey = null) - { - $result = array(); - foreach ($array as $subArray) { - if (is_null($indexKey) && array_key_exists($columnKey, $subArray)) { - $result[] = is_object($subArray) ? $subArray->$columnKey : $subArray[$columnKey]; - } elseif (array_key_exists($indexKey, $subArray)) { - if (is_null($columnKey)) { - $index = is_object($subArray) ? $subArray->$indexKey : $subArray[$indexKey]; - $result[$index] = $subArray; - } elseif (array_key_exists($columnKey, $subArray)) { - $index = is_object($subArray) ? $subArray->$indexKey : $subArray[$indexKey]; - $result[$index] = is_object($subArray) ? $subArray->$columnKey : $subArray[$columnKey]; - } - } - } - return $result; - } -} -/** - * 获取全局唯一标识符 - */ -if (!function_exists('get_guid_v4')) { - function get_guid_v4($trim = true) - { - // Windows - if (function_exists('com_create_guid') === true) { - $charid = com_create_guid(); - return $trim == true ? trim($charid, '{}') : $charid; - } - // OSX/Linux - if (function_exists('openssl_random_pseudo_bytes') === true) { - $data = openssl_random_pseudo_bytes(16); - $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100 - $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10 - return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); - } - // Fallback (PHP 4.2+) - mt_srand((double)microtime() * 10000); - $charid = strtolower(md5(uniqid(rand(), true))); - $hyphen = chr(45); // "-" - $lbrace = $trim ? "" : chr(123); // "{" - $rbrace = $trim ? "" : chr(125); // "}" - $guidv4 = $lbrace . - substr($charid, 0, 8) . $hyphen . - substr($charid, 8, 4) . $hyphen . - substr($charid, 12, 4) . $hyphen . - substr($charid, 16, 4) . $hyphen . - substr($charid, 20, 12) . - $rbrace; - return $guidv4; - } -} -/** - * 商户管理员 - 账户资金变动提醒 - 公众号 - */ -if (!function_exists('sand_account_change_msg')) { - function sand_account_change_msg($title,$pay,$surplus,$user_id,$applet_id = 0) - { - $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 - if(empty($webtplmsg['balance'])){ - return true; - } - $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 - $msg = [ - 'touser' => '', - 'template_id' => trim($webtplmsg['balance']), - 'miniprogram' => [ - 'appid' => $foodhelp['app_id'], - 'pagepath' => 'pages/index/index' - ], - 'data' => [ - 'first' => [ - 'value' => '请您核实下列项目', - 'color' => '#173177' - ], - 'keyword1' => [ - 'value' => $title,//交易类型 - 'color' => '#173177' - ], - 'keyword2' => [ - 'value' => '¥'.$pay.'元',//交易金额 - 'color' => '#173177' - ], - 'keyword3' => [ - 'value' => date('Y/m/d H:i:s',time()),//交易时间 - 'color' => '#173177' - ], - 'keyword4' => [ - 'value' => '¥'.$surplus.'元',//账户余额 - 'color' => '#173177' - ], - 'remark' => [ - 'value' => '如有疑问请及时联系我们!', - 'color' => '#173177' - ] - ] - ]; - return send_wechat_msg($msg,$user_id); - } -} - -/** - * 商户管理员 - 申请受理通知 - 公众号 - */ -if (!function_exists('sand_apply_msg')) { - function sand_apply_msg($name,$title,$user_id,$applet_id = 0) - { - $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 - if(empty($webtplmsg['apply'])){ - return true; - } - $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 - $msg = [ - 'touser' => '', - 'template_id' => trim($webtplmsg['apply']), - 'miniprogram' => [ - 'appid' => $foodhelp['app_id'], - 'pagepath' => 'pages/index/index' - ], - 'data' => [ - 'first' => [ - 'value' => '您的申请已接收', - 'color' => '#173177' - ], - 'keyword1' => [ - 'value' => $name,//申请人 - 'color' => '#173177' - ], - 'keyword2' => [ - 'value' => $title,//类型 - 'color' => '#173177' - ], - 'keyword3' => [ - 'value' => date('Y/m/d H:i:s',time()),//时间 - 'color' => '#173177' - ], - 'remark' => [ - 'value' => '感谢您的支持!', - 'color' => '#173177' - ] - ] - ]; - return send_wechat_msg($msg,$user_id); - } -} - -/** - * 商户管理员 - 申请状态更新通知 - 公众号 - */ -if (!function_exists('sand_examine_msg')) { - function sand_examine_msg($id,$title,$status,$user_id,$applet_id=0) - { - $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 - if(empty($webtplmsg['examine'])){ - return true; - } - $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 - $msg = [ - 'touser' => '', - 'template_id' => trim($webtplmsg['examine']), - 'miniprogram' => [ - 'appid' => $foodhelp['app_id'], - 'pagepath' => 'pages/index/index' - ], - 'data' => [ - 'first' => [ - 'value' => '最新申请状态', - 'color' => '#173177' - ], - 'keyword1' => [ - 'value' => $title,//申请类型 - 'color' => '#173177' - ], - 'keyword2' => [ - 'value' => $id,//申请编号 - 'color' => '#173177' - ], - 'keyword3' => [ - 'value' => date('Y/m/d H:i:s',time()),//申请时间 - 'color' => '#173177' - ], - 'keyword4' => [ - 'value' => $status,//当前状态 - 'color' => '#173177' - ], - 'remark' => [ - 'value' => '请及时关注新的动态提醒。', - 'color' => '#173177' - ] - ] - ]; - return send_wechat_msg($msg,$user_id); - } -} -/** - * 商户管理员 - 试用申请成功通知 - 公众号 - */ -if (!function_exists('sand_testing_msg')) { - function sand_testing_msg($title,$time,$user_id,$applet_id = 0) - { - $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 - if(empty($webtplmsg['testing'])){ - return true; - } - $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 - $msg = [ - 'touser' => '', - 'template_id' => trim($webtplmsg['testing']), - 'miniprogram' => [ - 'appid' => $foodhelp['app_id'], - 'pagepath' => 'pages/index/index' - ], - 'data' => [ - 'first' => [ - 'value' => '恭喜您获得试用特权', - 'color' => '#173177' - ], - 'keyword1' => [ - 'value' => $title,//申请项目 - 'color' => '#173177' - ], - 'keyword2' => [ - 'value' => date('Y/m/d H:i:s',$time),//有效期至 - 'color' => '#173177' - ], - 'remark' => [ - 'value' => '感谢您的使用。', - 'color' => '#173177' - ] - ] - ]; - return send_wechat_msg($msg,$user_id); - } -} -/** - * 商户骑手 - 抢单提醒 - 公众号 - */ -if (!function_exists('sand_grab_msg')) { - function sand_grab_msg($order,$applet_id = 0) - { - $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 - if(empty($webtplmsg['grab'])){ - return true; - } - $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 - $msg = [ - 'touser' => '', - 'template_id' => trim($webtplmsg['grab']), - 'miniprogram' => [ - 'appid' => $foodhelp['app_id'], - 'pagepath' => 'pages/order/index' - ], - 'data' => [ - 'first' => [ - 'value' => '您有新订单可抢单', - 'color' => '#173177' - ], - 'keyword1' => [ - 'value' => $order['shop']['shop_name'],//商家名称 - 'color' => '#173177' - ], - 'keyword2' => [ - 'value' => $order['shop']['address'],//配送起点 - 'color' => '#173177' - ], - 'keyword3' => [ - 'value' => $order['address']['detail'],//配送终点 - 'color' => '#173177' - ], - 'keyword4' => [ - 'value' => $order['shop']['phone'],//商家电话 - 'color' => '#173177' - ], - 'remark' => [ - 'value' => '有疑问请联系商家,点击查看详情>>', - 'color' => '#173177' - ] - ] - ]; - $model = new ShopClerkModel; - $clerk = $model->getAll($order['shop_id'],30); - for($n=0;$n 0){ - send_wechat_msg($msg,$clerk[$n]['user_id']); - } - } - } -} -/** - * 商户店长 - 退款申请通知 - 公众号 - */ -if (!function_exists('sand_refund_msg')) { - function sand_refund_msg($order,$applet_id = 0) - { - $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 - if(empty($webtplmsg['refund'])){ - return true; - } - $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 - $msg = [ - 'touser' => '', - 'template_id' => trim($webtplmsg['refund']), - 'miniprogram' => [ - 'appid' => $foodhelp['app_id'], - 'pagepath' => 'pages/order/index' - ], - 'data' => [ - 'first' => [ - 'value' => '收到一条新退款, 请尽快受理', - 'color' => '#173177' - ], - 'keyword1' => [ - 'value' => $order['shop']['shop_name'],//门店 - 'color' => '#173177' - ], - 'keyword2' => [ - 'value' => $order['order_no'],//订单编号 - 'color' => '#173177' - ], - 'keyword3' => [ - 'value' => '¥'.$order['refund_price'].'元',//退款金额 - 'color' => '#173177' - ], - 'keyword4' => [ - 'value' => date('Y/m/d H:i:s',time()),//退款时间 - 'color' => '#173177' - ], - 'remark' => [ - 'value' => $order['refund_desc'], - 'color' => '#173177' - ] - ] - ]; - $model = new ShopClerkModel; - $clerk = $model->getAll($order['shop_id'],20); - for($n=0;$n 0){ - send_wechat_msg($msg,$clerk[$n]['user_id']); - } - } - } -} -/** - * 商户店长 新订单提醒模板消息 - 公众号 - */ -if (!function_exists('sand_new_order_msg')) { - function sand_new_order_msg($order,$applet_id = 0) - { - $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 - if(empty($webtplmsg['new_order'])){ - return true; - } - $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 - if($order['order_mode']['value']==30){ - $title = '自取'; - }elseif($order['order_mode']['value']==20){ - $title = '外卖'; - }else{ - $title = '堂食'; - } - $msg = [ - 'touser' => '', - 'template_id' => trim($webtplmsg['new_order']), - 'miniprogram' => [ - 'appid' => $foodhelp['app_id'], - 'pagepath' => 'pages/order/index' - ], - 'data' => [ - 'first' => [ - 'value' => '收到一个新的'.$title.'订单', - 'color' => '#173177' - ], - 'keyword1' => [ - 'value' => $order['order_no'], - 'color' => '#173177' - ], - 'keyword2' => [ - 'value' => '¥'.$order['pay_price'].'元', - 'color' => '#173177' - ], - 'keyword3' => [ - 'value' => $order['create_time'], - 'color' => '#173177' - ], - 'remark' => [ - 'value' => '请及时处理您的订单。', - 'color' => '#173177' - ] - ] - ]; - $model = new ShopClerkModel; - $clerk = $model->getAll($order['shop_id'],20); - for($n=0;$n 0){ - send_wechat_msg($msg,$clerk[$n]['user_id']); - } - } - } -} -/** - * 发布模板消息 - 公众号 - */ -if (!function_exists('send_wechat_msg')) { - 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); - return true; - } -} -if (!function_exists('copydirs')) { - /** - * 复制文件夹 - * @param string $source 源文件夹 - * @param string $dest 目标文件夹 - */ - function copydirs($source, $dest) - { - if (!is_dir($dest)) { - mkdir($dest, 0777, true); - } - foreach ( - $iterator = new RecursiveIteratorIterator( - new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), - RecursiveIteratorIterator::SELF_FIRST - ) as $item - ) { - if ($item->isDir()) { - $sontDir = $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName(); - - if (!is_dir($sontDir)) { - mkdir($sontDir, 0777, true); - } - } else { - copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); - } - } - } -} -if (!function_exists('rmdirs')) { - /** - * 删除文件夹 - * @param string $dirname 目录 - * @param bool $withself 是否删除自身 - * @return boolean - */ - function rmdirs($dirname, $withself = true) - { - if (!is_dir($dirname)) { - return false; - } - $files = new RecursiveIteratorIterator( - new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS), - RecursiveIteratorIterator::CHILD_FIRST - ); - foreach ($files as $fileinfo) { - $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink'); - $todo($fileinfo->getRealPath()); - } - if ($withself) { - @rmdir($dirname); - } - return true; - } -} -/** - * 写入日志 - */ -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; - try { - // 文件路径 - $filePath = $dir . '/logs/'; - // 路径不存在则创建 - !is_dir($filePath) && mkdir($filePath, 0777, true); - // 写入文件 - return file_put_contents($filePath . date('Ymd') . '.log', $content, FILE_APPEND); - } catch (\Exception $e) { - return false; - } +scheme(); + // url子目录 + $rootUrl = root_url(); + // 拼接完整url + $baseUrl = "{$scheme}://" . $request->host() . $rootUrl; + } + return $baseUrl; +} +/** + * 获取当前域名 + */ +function domain() +{ + $request = Request::instance(); + return $request->host(); +} +/** + * 获取当前uploads目录访问地址 + */ +function uploads_url() +{ + return base_url() . 'uploads/'; +} +/** + * 获取当前的应用名称 + */ +function app_name() +{ + return app('http')->getName(); +} +/** + * 获取web根目录 + */ +function web_path() +{ + static $webPath = ''; + if (empty($webPath)) { + $request = Request::instance(); + $webPath = dirname($request->server('SCRIPT_FILENAME')) . DIRECTORY_SEPARATOR; + } + return $webPath; +} +/** + * 获取当前url的子目录路径 + */ +function root_url() +{ + static $rootUrl = ''; + if (empty($rootUrl)) { + $request = Request::instance(); + $subUrl = str_replace('\\', '/', dirname($request->baseFile())); + $rootUrl = $subUrl . ($subUrl === '/' ? '' : '/'); + } + return $rootUrl; +} +/** + * 清空缓存目录 + */ +function deldir($path) +{ + //如果是目录则继续 + if(is_dir($path)){ + //扫描一个文件夹内的所有文件夹和文件并返回数组 + $p = scandir($path); + foreach($p as $val){ + //排除目录中的.和.. + if($val !="." && $val !=".."){ + //如果是目录则递归子目录,继续操作 + if(is_dir($path.$val)){ + //子目录中操作删除文件夹和文件 + deldir($path.$val.'/'); + //目录清空后删除空文件夹 + @rmdir($path.$val.'/'); + }else{ + //如果是文件直接删除 + unlink($path.$val); + } + } + } + } +} +/** + * 下划线转驼峰 + */ +function camelize($uncamelized_words, $separator = '_') +{ + $uncamelized_words = $separator . str_replace($separator, " ", strtolower($uncamelized_words)); + return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator); +} +/** + * 驼峰转下划线 + */ +function uncamelize($camelCaps, $separator = '_') +{ + return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps)); +} +/** + * 隐藏手机号中间四位 13012345678 -> 130****5678 + * @param string $phone 手机号 + * @return string + */ +if (!function_exists('hide_phone')) { + function hide_phone(string $phone) + { + return substr_replace($phone, '****', 3, 4); + } +} +/** + * 隐藏姓名第二位 欧阳振华 -> 欧*振华 + * @param string $name = 姓名 + * @return string + */ +if (!function_exists('hide_name')) { + function hide_name(string $name) + { + return substr_replace($name, '*', 1, 1); + } +} +/** + * 隐藏敏感字符 + */ +if (!function_exists('substr_cut')) { + function substr_cut($value) + { + $strlen = mb_strlen($value, 'utf-8'); + if ($strlen <= 1) return $value; + $firstStr = mb_substr($value, 0, 1, 'utf-8'); + $lastStr = mb_substr($value, -1, 1, 'utf-8'); + return $strlen == 2 ? $firstStr . str_repeat('*', $strlen - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr; + } +} +/** + * 时间戳转换日期 + */ +if (!function_exists('format_time')) { + function format_time($timeStamp) + { + return date('Y-m-d H:i:s', $timeStamp); + } +} +/** + * 将时间戳转换为日期时间 + * @param int $time 时间戳 + * @param string $format 日期时间格式 + * @return string + */ +if (!function_exists('datetime')) { + function datetime($time, $format = 'Y-m-d H:i:s') + { + $time = is_numeric($time) ? $time : strtotime($time); + return date($format, $time); + } +} +/** + * 左侧填充0 + * @param $value + * @param $padLength + * @return string + */ +if (!function_exists('pad_left')) { + function pad_left($value, $padLength = 2) + { + return \str_pad($value, $padLength, "0", STR_PAD_LEFT); + } +} +/** + * 重写trim方法 (解决int类型过滤后后变为string类型) + * @param $str + * @return string + */ +if (!function_exists('my_trim')) { + function my_trim($str) + { + return is_string($str) ? trim($str) : $str; + } +} +/** + * 重写htmlspecialchars方法 (解决int类型过滤后后变为string类型) + * @param $string + * @return string + */ +if (!function_exists('my_htmlspecialchars')) { + function my_htmlspecialchars($string) + { + return is_string($string) ? htmlspecialchars($string) : $string; + } +} +/** + * 根据指定长度截取字符串 + */ +if (!function_exists('str_substr')) { + function str_substr($str, $length = 30) + { + if (strlen($str) > $length) { + $str = mb_substr($str, 0, $length); + } + return $str; + } +} +/** + * 文本左斜杠转换为右斜杠 + */ +if (!function_exists('convert_left_slash')) { + function convert_left_slash(string $string) + { + return str_replace('\\', '/', $string); + } +} +/** + * 将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 ); + return $res; + } +} +/** + * 多维数组合并 + */ +function array_merge_multiple($array1, $array2) +{ + $merge = $array1 + $array2; + $data = []; + foreach ($merge as $key => $val) { + if ( + isset($array1[$key]) + && is_array($array1[$key]) + && isset($array2[$key]) + && is_array($array2[$key]) + ) { + $data[$key] = array_merge_multiple($array1[$key], $array2[$key]); + } else { + $data[$key] = isset($array2[$key]) ? $array2[$key] : $array1[$key]; + } + } + return $data; +} +/** + * 判断是否为自定义索引数组 + */ +if (!function_exists('is_assoc')) { + function is_assoc(array $array) + { + if (empty($array)) return false; + return array_keys($array) !== range(0, count($array) - 1); + } +} +/** + * 多维数组按照指定字段升/降序排列 + */ +if (!function_exists('arr_sort')) { + function arr_sort(array $data, string $field, string $order = 'desc') + { + 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); + } + return $data; + } +} +/** + * 二维数组排序 + * $desc 是否从大到小排列 + */ +if (!function_exists('array_sort')) { + function array_sort($arr, $keys, $desc = false) + { + $key_value = $new_array = array(); + foreach ($arr as $k => $v) { + $key_value[$k] = $v[$keys]; + } + if ($desc) { + arsort($key_value);//降序 + } else { + asort($key_value);//升序 + } + reset($key_value); + foreach ($key_value as $k => $v) { + if(is_int($k)){ + $new_array[] = $arr[$k]; + }else{ + $new_array[$k] = $arr[$k]; + } + } + return $new_array; + } +} +//去除二维数组重复值,默认重复保留前面的值 +/* + *array 二维数组 + *keyid 需要判断是否重复的项目 +*/ +if (!function_exists('array_repeat')) { + function array_repeat($array,$keyid) + { + $array =array_values($array); + //提取需要判断的项目变成一维数组 + $a = array_tq($array,$keyid); + + //去除一维数组重复值 + $a =array_unique($a); + //提取二维数组项目值 + 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]; + } + } + return $c; + } +} +//提取二维数组项目 +if (!function_exists('array_tq')) { + function array_tq($array, $aval="") + { + foreach($array AS $key => $value) + { + $result[] = $value[$aval]; + } + return $result; + } +} +/** + * POST请求 + * @param [type] $url [description] + * @param array $data [description] + * @param array $headers [description] + * @return [type] [description] + */ +if (!function_exists('http_post')) { + function http_post($url,$data = array(),$headers=array()) + { + $curl = curl_init(); + 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){ + $data = '{}'; + } + curl_setopt($curl, CURLOPT_POST, 1); + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); + + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + $output = curl_exec($curl); + $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); + curl_close($curl); + return $output; + } +} +/** + * array_column 兼容低版本php + * (PHP < 5.5.0) + * @param $array + * @param $columnKey + * @param null $indexKey + * @return array + */ +if (!function_exists('array_column')) { + function array_column($array, $columnKey, $indexKey = null) + { + $result = array(); + foreach ($array as $subArray) { + if (is_null($indexKey) && array_key_exists($columnKey, $subArray)) { + $result[] = is_object($subArray) ? $subArray->$columnKey : $subArray[$columnKey]; + } elseif (array_key_exists($indexKey, $subArray)) { + if (is_null($columnKey)) { + $index = is_object($subArray) ? $subArray->$indexKey : $subArray[$indexKey]; + $result[$index] = $subArray; + } elseif (array_key_exists($columnKey, $subArray)) { + $index = is_object($subArray) ? $subArray->$indexKey : $subArray[$indexKey]; + $result[$index] = is_object($subArray) ? $subArray->$columnKey : $subArray[$columnKey]; + } + } + } + return $result; + } +} +/** + * 获取全局唯一标识符 + */ +if (!function_exists('get_guid_v4')) { + function get_guid_v4($trim = true) + { + // Windows + if (function_exists('com_create_guid') === true) { + $charid = com_create_guid(); + return $trim == true ? trim($charid, '{}') : $charid; + } + // OSX/Linux + if (function_exists('openssl_random_pseudo_bytes') === true) { + $data = openssl_random_pseudo_bytes(16); + $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100 + $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10 + return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); + } + // Fallback (PHP 4.2+) + mt_srand((double)microtime() * 10000); + $charid = strtolower(md5(uniqid(rand(), true))); + $hyphen = chr(45); // "-" + $lbrace = $trim ? "" : chr(123); // "{" + $rbrace = $trim ? "" : chr(125); // "}" + $guidv4 = $lbrace . + substr($charid, 0, 8) . $hyphen . + substr($charid, 8, 4) . $hyphen . + substr($charid, 12, 4) . $hyphen . + substr($charid, 16, 4) . $hyphen . + substr($charid, 20, 12) . + $rbrace; + return $guidv4; + } +} +/** + * 商户管理员 - 账户资金变动提醒 - 公众号 + */ +if (!function_exists('sand_account_change_msg')) { + function sand_account_change_msg($title,$pay,$surplus,$user_id,$applet_id = 0) + { + $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 + if(empty($webtplmsg['balance'])){ + return true; + } + $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 + $msg = [ + 'touser' => '', + 'template_id' => trim($webtplmsg['balance']), + 'miniprogram' => [ + 'appid' => $foodhelp['app_id'], + 'pagepath' => 'pages/index/index' + ], + 'data' => [ + 'first' => [ + 'value' => '请您核实下列项目', + 'color' => '#173177' + ], + 'keyword1' => [ + 'value' => $title,//交易类型 + 'color' => '#173177' + ], + 'keyword2' => [ + 'value' => '¥'.$pay.'元',//交易金额 + 'color' => '#173177' + ], + 'keyword3' => [ + 'value' => date('Y/m/d H:i:s',time()),//交易时间 + 'color' => '#173177' + ], + 'keyword4' => [ + 'value' => '¥'.$surplus.'元',//账户余额 + 'color' => '#173177' + ], + 'remark' => [ + 'value' => '如有疑问请及时联系我们!', + 'color' => '#173177' + ] + ] + ]; + return send_wechat_msg($msg,$user_id); + } +} + +/** + * 商户管理员 - 申请受理通知 - 公众号 + */ +if (!function_exists('sand_apply_msg')) { + function sand_apply_msg($name,$title,$user_id,$applet_id = 0) + { + $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 + if(empty($webtplmsg['apply'])){ + return true; + } + $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 + $msg = [ + 'touser' => '', + 'template_id' => trim($webtplmsg['apply']), + 'miniprogram' => [ + 'appid' => $foodhelp['app_id'], + 'pagepath' => 'pages/index/index' + ], + 'data' => [ + 'first' => [ + 'value' => '您的申请已接收', + 'color' => '#173177' + ], + 'keyword1' => [ + 'value' => $name,//申请人 + 'color' => '#173177' + ], + 'keyword2' => [ + 'value' => $title,//类型 + 'color' => '#173177' + ], + 'keyword3' => [ + 'value' => date('Y/m/d H:i:s',time()),//时间 + 'color' => '#173177' + ], + 'remark' => [ + 'value' => '感谢您的支持!', + 'color' => '#173177' + ] + ] + ]; + return send_wechat_msg($msg,$user_id); + } +} + +/** + * 商户管理员 - 申请状态更新通知 - 公众号 + */ +if (!function_exists('sand_examine_msg')) { + function sand_examine_msg($id,$title,$status,$user_id,$applet_id=0) + { + $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 + if(empty($webtplmsg['examine'])){ + return true; + } + $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 + $msg = [ + 'touser' => '', + 'template_id' => trim($webtplmsg['examine']), + 'miniprogram' => [ + 'appid' => $foodhelp['app_id'], + 'pagepath' => 'pages/index/index' + ], + 'data' => [ + 'first' => [ + 'value' => '最新申请状态', + 'color' => '#173177' + ], + 'keyword1' => [ + 'value' => $title,//申请类型 + 'color' => '#173177' + ], + 'keyword2' => [ + 'value' => $id,//申请编号 + 'color' => '#173177' + ], + 'keyword3' => [ + 'value' => date('Y/m/d H:i:s',time()),//申请时间 + 'color' => '#173177' + ], + 'keyword4' => [ + 'value' => $status,//当前状态 + 'color' => '#173177' + ], + 'remark' => [ + 'value' => '请及时关注新的动态提醒。', + 'color' => '#173177' + ] + ] + ]; + return send_wechat_msg($msg,$user_id); + } +} +/** + * 商户管理员 - 试用申请成功通知 - 公众号 + */ +if (!function_exists('sand_testing_msg')) { + function sand_testing_msg($title,$time,$user_id,$applet_id = 0) + { + $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 + if(empty($webtplmsg['testing'])){ + return true; + } + $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 + $msg = [ + 'touser' => '', + 'template_id' => trim($webtplmsg['testing']), + 'miniprogram' => [ + 'appid' => $foodhelp['app_id'], + 'pagepath' => 'pages/index/index' + ], + 'data' => [ + 'first' => [ + 'value' => '恭喜您获得试用特权', + 'color' => '#173177' + ], + 'keyword1' => [ + 'value' => $title,//申请项目 + 'color' => '#173177' + ], + 'keyword2' => [ + 'value' => date('Y/m/d H:i:s',$time),//有效期至 + 'color' => '#173177' + ], + 'remark' => [ + 'value' => '感谢您的使用。', + 'color' => '#173177' + ] + ] + ]; + return send_wechat_msg($msg,$user_id); + } +} +/** + * 商户骑手 - 抢单提醒 - 公众号 + */ +if (!function_exists('sand_grab_msg')) { + function sand_grab_msg($order,$applet_id = 0) + { + $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 + if(empty($webtplmsg['grab'])){ + return true; + } + $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 + $msg = [ + 'touser' => '', + 'template_id' => trim($webtplmsg['grab']), + 'miniprogram' => [ + 'appid' => $foodhelp['app_id'], + 'pagepath' => 'pages/order/index' + ], + 'data' => [ + 'first' => [ + 'value' => '您有新订单可抢单', + 'color' => '#173177' + ], + 'keyword1' => [ + 'value' => $order['shop']['shop_name'],//商家名称 + 'color' => '#173177' + ], + 'keyword2' => [ + 'value' => $order['shop']['address'],//配送起点 + 'color' => '#173177' + ], + 'keyword3' => [ + 'value' => $order['address']['detail'],//配送终点 + 'color' => '#173177' + ], + 'keyword4' => [ + 'value' => $order['shop']['phone'],//商家电话 + 'color' => '#173177' + ], + 'remark' => [ + 'value' => '有疑问请联系商家,点击查看详情>>', + 'color' => '#173177' + ] + ] + ]; + $model = new ShopClerkModel; + $clerk = $model->getAll($order['shop_id'],30); + for($n=0;$n 0){ + send_wechat_msg($msg,$clerk[$n]['user_id']); + } + } + } +} +/** + * 商户店长 - 退款申请通知 - 公众号 + */ +if (!function_exists('sand_refund_msg')) { + function sand_refund_msg($order,$applet_id = 0) + { + $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 + if(empty($webtplmsg['refund'])){ + return true; + } + $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 + $msg = [ + 'touser' => '', + 'template_id' => trim($webtplmsg['refund']), + 'miniprogram' => [ + 'appid' => $foodhelp['app_id'], + 'pagepath' => 'pages/order/index' + ], + 'data' => [ + 'first' => [ + 'value' => '收到一条新退款, 请尽快受理', + 'color' => '#173177' + ], + 'keyword1' => [ + 'value' => $order['shop']['shop_name'],//门店 + 'color' => '#173177' + ], + 'keyword2' => [ + 'value' => $order['order_no'],//订单编号 + 'color' => '#173177' + ], + 'keyword3' => [ + 'value' => '¥'.$order['refund_price'].'元',//退款金额 + 'color' => '#173177' + ], + 'keyword4' => [ + 'value' => date('Y/m/d H:i:s',time()),//退款时间 + 'color' => '#173177' + ], + 'remark' => [ + 'value' => $order['refund_desc'], + 'color' => '#173177' + ] + ] + ]; + $model = new ShopClerkModel; + $clerk = $model->getAll($order['shop_id'],20); + for($n=0;$n 0){ + send_wechat_msg($msg,$clerk[$n]['user_id']); + } + } + } +} +/** + * 商户店长 新订单提醒模板消息 - 公众号 + */ +if (!function_exists('sand_new_order_msg')) { + function sand_new_order_msg($order,$applet_id = 0) + { + $webtplmsg = SettingModel::getItem('webtplmsg',$applet_id); //获取模板消息编号 + if(empty($webtplmsg['new_order'])){ + return true; + } + $foodhelp = get_addons_config('foodhelp'); //获取商家助手小程序 + if($order['order_mode']['value']==30){ + $title = '自取'; + }elseif($order['order_mode']['value']==20){ + $title = '外卖'; + }else{ + $title = '堂食'; + } + $msg = [ + 'touser' => '', + 'template_id' => trim($webtplmsg['new_order']), + 'miniprogram' => [ + 'appid' => $foodhelp['app_id'], + 'pagepath' => 'pages/order/index' + ], + 'data' => [ + 'first' => [ + 'value' => '收到一个新的'.$title.'订单', + 'color' => '#173177' + ], + 'keyword1' => [ + 'value' => $order['order_no'], + 'color' => '#173177' + ], + 'keyword2' => [ + 'value' => '¥'.$order['pay_price'].'元', + 'color' => '#173177' + ], + 'keyword3' => [ + 'value' => $order['create_time'], + 'color' => '#173177' + ], + 'remark' => [ + 'value' => '请及时处理您的订单。', + 'color' => '#173177' + ] + ] + ]; + $model = new ShopClerkModel; + $clerk = $model->getAll($order['shop_id'],20); + for($n=0;$n 0){ + send_wechat_msg($msg,$clerk[$n]['user_id']); + } + } + } +} +/** + * 发布模板消息 - 公众号 + */ +if (!function_exists('send_wechat_msg')) { + 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); + return true; + } +} +if (!function_exists('copydirs')) { + /** + * 复制文件夹 + * @param string $source 源文件夹 + * @param string $dest 目标文件夹 + */ + function copydirs($source, $dest) + { + if (!is_dir($dest)) { + mkdir($dest, 0777, true); + } + foreach ( + $iterator = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), + RecursiveIteratorIterator::SELF_FIRST + ) as $item + ) { + if ($item->isDir()) { + $sontDir = $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName(); + + if (!is_dir($sontDir)) { + mkdir($sontDir, 0777, true); + } + } else { + copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); + } + } + } +} +if (!function_exists('rmdirs')) { + /** + * 删除文件夹 + * @param string $dirname 目录 + * @param bool $withself 是否删除自身 + * @return boolean + */ + function rmdirs($dirname, $withself = true) + { + if (!is_dir($dirname)) { + return false; + } + $files = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS), + RecursiveIteratorIterator::CHILD_FIRST + ); + foreach ($files as $fileinfo) { + $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink'); + $todo($fileinfo->getRealPath()); + } + if ($withself) { + @rmdir($dirname); + } + return true; + } +} +/** + * 写入日志 + */ +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; + try { + // 文件路径 + $filePath = $dir . '/logs/'; + // 路径不存在则创建 + !is_dir($filePath) && mkdir($filePath, 0777, true); + // 写入文件 + return file_put_contents($filePath . date('Ymd') . '.log', $content, FILE_APPEND); + } catch (\Exception $e) { + return false; + } } \ No newline at end of file diff --git a/extend/hema/wechat/Index.php b/extend/hema/wechat/Index.php index f294b71..eaf7c3c 100644 --- a/extend/hema/wechat/Index.php +++ b/extend/hema/wechat/Index.php @@ -1,91 +1,94 @@ - 'wx89c12dd426a55a2e', - 'appSecret' => '33e66bcf944f9810abbb5ddd7825403d', - 'token' => 'cxhxy', - - ]; - - - } - - - 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'); - - 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') { - if ($data['Event'] == 'subscribe') { - $wx = new Driver; - if ($wechat_user = $wx->getWechatUserInfo($data['FromUserName'], 10001)) { - //用户操作 - $model = new UserModel; - if ($user = $model->subscribe($wechat_user, $this->wechat['applet_id'])) { - - } - } 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'); - } - } - } - } - - + 'wx89c12dd426a55a2e', + 'appSecret' => '33e66bcf944f9810abbb5ddd7825403d', + 'token' => 'cxhxy', + + ]; + + } + + + public function index() + { + if (isset($_GET['echostr'])) { + echo $_GET["echostr"]; + exit; + } + + //接收微信推送数据 + $data = file_get_contents('php://input'); + +// $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; + $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)); + + } + + die('success'); + } + } + } + + } + + } \ No newline at end of file diff --git a/extend/hema/wechat/logs/20231206.log b/extend/hema/wechat/logs/20231206.log new file mode 100644 index 0000000..7db3031 --- /dev/null +++ b/extend/hema/wechat/logs/20231206.log @@ -0,0 +1,12 @@ +[2023-12-06 18:11:51] +222222222222222222 + +[2023-12-06 18:11:51] +{"ToUserName":"gh_f3e7ab01d5bd","FromUserName":"oy5Wi6kx3rPhdeV001GbKcy-P9gk","CreateTime":"1701857511","MsgType":"event","Event":"unsubscribe","EventKey":{}} + +[2023-12-06 18:11:54] +222222222222222222 + +[2023-12-06 18:11:54] +{"ToUserName":"gh_f3e7ab01d5bd","FromUserName":"oy5Wi6kx3rPhdeV001GbKcy-P9gk","CreateTime":"1701857514","MsgType":"event","Event":"subscribe","EventKey":{}} + diff --git a/runtime/cache/eb/300fe116b3568a7e80a6c2812b1a38.php b/runtime/cache/eb/300fe116b3568a7e80a6c2812b1a38.php index e63c99f..32eaf40 100644 --- a/runtime/cache/eb/300fe116b3568a7e80a6c2812b1a38.php +++ b/runtime/cache/eb/300fe116b3568a7e80a6c2812b1a38.php @@ -1,4 +1,4 @@ -1701848041 \ No newline at end of file +1701879149 \ No newline at end of file