From 348a990f883af9402ba7af480c39c3cffec95768 Mon Sep 17 00:00:00 2001 From: ASUS <515617283@qq.com> Date: Wed, 15 Oct 2025 19:30:38 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E4=B8=80=E6=AD=A5=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/command/ApplySmsTemp.php | 10 +- app/command/BirthdayGiftSms.php | 2 +- app/command/QuerySmsStatus.php | 70 ++++++---- app/command/QuerySmsTempStatus.php | 72 +++++++++++ app/command/SendMarkSms.php | 2 +- app/command/SendWechatTemp.php | 2 +- app/functions.php | 80 +++++++++++- app/model/AlibabaSms.php | 29 +++-- app/model/ShopInfo.php | 191 ++++++++++++++++++++++++++++ app/queue/redis/ApplySmsTemp.php | 14 +- app/queue/redis/BirthdayGiftSms.php | 50 +++++++- app/queue/redis/SendMarkSms.php | 75 ++++++++++- config/cons.php | 3 +- 13 files changed, 547 insertions(+), 53 deletions(-) create mode 100644 app/command/QuerySmsTempStatus.php create mode 100644 app/model/ShopInfo.php diff --git a/app/command/ApplySmsTemp.php b/app/command/ApplySmsTemp.php index c3a04e7..c718c37 100644 --- a/app/command/ApplySmsTemp.php +++ b/app/command/ApplySmsTemp.php @@ -5,6 +5,7 @@ namespace app\command; use app\common\model\MqLog; use app\common\model\OrderInfo; use app\common\model\RabbitMqConfig; +use app\model\ShopInfo; use ba\Random; use PhpAmqpLib\Connection\AMQPStreamConnection; use support\think\Db; @@ -37,6 +38,13 @@ class ApplySmsTemp extends Command */ protected function execute(InputInterface $input, OutputInterface $output): int { + $str = '亲爱的${用户昵称}您好,${店铺名称}祝您生日快乐!感谢您一直的陪伴。为您准备了${数量}张超值优惠券,已放入账户,愿我们的礼物能为您增添一份喜悦。拒收请回复R'; + $templateContent_arr = ShopInfo::handleSmsTemplates($str); + $a = '{"店铺名称":"火锅店","用户昵称":"嘿嘿","数量":"123"}'; + p(replace_json_keys($a, json_encode($templateContent_arr, 256))); + $t_str = replace_placeholder_keys($str, $templateContent_arr); + p($templateContent_arr, $t_str); + $host = RabbitMqConfig::$host; $port = RabbitMqConfig::$port; @@ -59,7 +67,7 @@ class ApplySmsTemp extends Command ); $rabbit_channel = $connection->channel(); $rabbit_channel->queue_declare($queue, false, true, false, false, false, [ - 'x-message-ttl' => ['I', 180000] +// 'x-message-ttl' => ['I', 180000] ]); $callback = function ($msg) use ($queue){ diff --git a/app/command/BirthdayGiftSms.php b/app/command/BirthdayGiftSms.php index 0ead13b..b9dd398 100644 --- a/app/command/BirthdayGiftSms.php +++ b/app/command/BirthdayGiftSms.php @@ -59,7 +59,7 @@ class BirthdayGiftSms extends Command ); $rabbit_channel = $connection->channel(); $rabbit_channel->queue_declare($queue, false, true, false, false, false, [ - 'x-message-ttl' => ['I', 180000] +// 'x-message-ttl' => ['I', 180000] ]); $callback = function ($msg) use ($queue){ diff --git a/app/command/QuerySmsStatus.php b/app/command/QuerySmsStatus.php index bf48fc5..fee974b 100644 --- a/app/command/QuerySmsStatus.php +++ b/app/command/QuerySmsStatus.php @@ -3,6 +3,7 @@ namespace app\command; use app\model\AlibabaSms; +use app\model\ShopInfo; use support\Log; use support\think\Db; use Symfony\Component\Console\Command\Command; @@ -28,44 +29,63 @@ class QuerySmsStatus extends Command } /** - * 查询模版审核状态 1小时执行一次 + * 查询短信发送状态 * @param InputInterface $input * @param OutputInterface $output * @return int */ protected function execute(InputInterface $input, OutputInterface $output): int { - $list = Db::table('sms_shop_template')->where(['status' => 1])->select()->toArray(); - Log::info('短信模版审核查询开始->' . json_encode($list)); - if($list) { - foreach ($list as $k => $value) { + // 生日有礼 + $bir_list = Db::table('mk_birthday_gift_record')->where(['status' => 'await_push'])->select()->toArray(); + if($bir_list) { + Log::info('生日有礼短信发送状态查询开始->' . json_encode($bir_list)); + foreach ($bir_list as $k => $value) { $data = [ - 'TemplateCode' => $value['template_code'] + 'TemplateCode' => $value['template_code'], + 'BizId' => $value['biz_id'], + 'SendDate' => date('Ymd'), + 'PageSize' => 1, + 'CurrentPage' => 20, ]; - $res = AlibabaSms::GetSmsTemplate($data); - if($res['Code'] == 'OK') { - $msg = ''; - if($res['TemplateStatus'] == 0) { - $status = 1; - }elseif ($res['TemplateStatus'] == 1) { - $status = 2; - }elseif ($res['TemplateStatus'] == 2) { - $status = -1; - $msg = $res['AuditInfo']['RejectInfo']; - }elseif ($res['TemplateStatus'] == 10) { - $status = -1; - $msg = '取消审核'; + $res = AlibabaSms::QuerySendDetails($data); + try { + Db::startTrans(); + if($res['Code'] == 'OK') { + $push_resp = $res['ErrCode']; + if($res['SmsSendDetailDTOs']['SmsSendDetailDTO'][0]['SendStatus'] == 3) { + $push_status = 'success'; + // 扣掉余额 + ShopInfo::moeny(config('cons.sms_price'), $value['main_shop_id'], 2, ); + }elseif ($res['SmsSendDetailDTOs']['SmsSendDetailDTO'][0]['SendStatus'] == 2) { + $push_status = 'failed'; + }elseif ($res['SmsSendDetailDTOs']['SmsSendDetailDTO'][0]['SendStatus'] == 1) { + $push_status = 'await_push'; + } + $update_data = [ + 'status' => $push_status, + 'push_resp' => $push_resp, + ]; + $update_status = Db::table('mk_birthday_gift_record')->where(['id' => $value['id']])->update($update_data); + Db::commit(); + Log::info('短信查询完毕数据库更新结果---》[ID]' . $value['id'] . '更新结果-->' . $update_status . '-->更新数据 . ' . json_encode($update_data)); } - $update_data = [ - 'status' => $status, - 'fail_msg' => $msg?:$msg, - ]; - $update_status = Db::table('sms_shop_template')->where(['id' => $value['id']])->update($update_data); - Log::info('模版查询完毕数据库更新结果---》[ID]' . $value['id'] . '更新结果-->' . $update_status . '-->更新数据 . ' . json_encode($update_data)); + }catch (\Exception $e) { + Db::rollback(); + Log::info('短信查询错误---》[ID]' . $e->getMessage()); } + sleep(1); } } + + + // 活动开始 + + + + + return self::SUCCESS; } diff --git a/app/command/QuerySmsTempStatus.php b/app/command/QuerySmsTempStatus.php new file mode 100644 index 0000000..c081797 --- /dev/null +++ b/app/command/QuerySmsTempStatus.php @@ -0,0 +1,72 @@ +addArgument('name', InputArgument::OPTIONAL, 'Name description'); + } + + /** + * 查询模版审核状态 1小时执行一次 + * @param InputInterface $input + * @param OutputInterface $output + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + $list = Db::table('sms_shop_template')->where(['status' => 1])->select()->toArray(); + Log::info('短信模版审核查询开始->' . json_encode($list)); + if($list) { + foreach ($list as $k => $value) { + $data = [ + 'TemplateCode' => $value['template_code'] + ]; + $res = AlibabaSms::GetSmsTemplate($data); + if($res['Code'] == 'OK') { + $msg = ''; + if($res['TemplateStatus'] == 0) { + $status = 1; + }elseif ($res['TemplateStatus'] == 1) { + $status = 2; + }elseif ($res['TemplateStatus'] == 2) { + $status = -1; + $msg = $res['AuditInfo']['RejectInfo']; + }elseif ($res['TemplateStatus'] == 10) { + $status = -1; + $msg = '取消审核'; + } + $update_data = [ + 'status' => $status, + 'fail_msg' => $msg?:$msg, + ]; + $update_status = Db::table('sms_shop_template')->where(['id' => $value['id']])->update($update_data); + Log::info('模版查询完毕数据库更新结果---》[ID]' . $value['id'] . '更新结果-->' . $update_status . '-->更新数据 . ' . json_encode($update_data)); + } + sleep(1); + } + } + return self::SUCCESS; + } + +} diff --git a/app/command/SendMarkSms.php b/app/command/SendMarkSms.php index 58f2992..0671c7f 100644 --- a/app/command/SendMarkSms.php +++ b/app/command/SendMarkSms.php @@ -59,7 +59,7 @@ class SendMarkSms extends Command ); $rabbit_channel = $connection->channel(); $rabbit_channel->queue_declare($queue, false, true, false, false, false, [ - 'x-message-ttl' => ['I', 180000] +// 'x-message-ttl' => ['I', 180000] ]); $callback = function ($msg) use ($queue){ diff --git a/app/command/SendWechatTemp.php b/app/command/SendWechatTemp.php index 5d67e4c..ff214de 100644 --- a/app/command/SendWechatTemp.php +++ b/app/command/SendWechatTemp.php @@ -59,7 +59,7 @@ class SendWechatTemp extends Command ); $rabbit_channel = $connection->channel(); $rabbit_channel->queue_declare($queue, false, true, false, false, false, [ - 'x-message-ttl' => ['I', 180000] +// 'x-message-ttl' => ['I', 180000] ]); $callback = function ($msg) use ($queue){ diff --git a/app/functions.php b/app/functions.php index 89ada6f..a3e932f 100644 --- a/app/functions.php +++ b/app/functions.php @@ -95,4 +95,82 @@ if(!function_exists('http_post')) { } return $response; } -} \ No newline at end of file +} + +if (!function_exists('curl_post')) { + function curl_post($url, $params, $op) + { + $postData = json_encode($params, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); + if(!empty($params['shopId'])) { + Log::write('耗材预警提交json' . $postData); + } + $opts = array( + CURLOPT_TIMEOUT => 30, + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_SSL_VERIFYHOST => false, + CURLOPT_ENCODING => 'gzip', + ); + $opts[CURLOPT_URL] = $url; + $opts[CURLOPT_POST] = 1; + $opts[CURLOPT_POSTFIELDS] = $postData; + $opts[CURLOPT_HTTPHEADER] = $op; + $ch = curl_init(); + curl_setopt_array($ch, $opts); + $responsedata = curl_exec($ch); + $error = curl_error($ch); + curl_close($ch); + return $responsedata; + } +} + +// 处理${}字符串 +if (!function_exists('handldollerstr')) { + function handldollerstr($str) + { + $pattern = '/\$\{(.*?)\}/'; + preg_match_all($pattern, $str, $matches); + $result = $matches[1]; + return $result; + } +} + +if (!function_exists('replace_placeholder_keys')) { + function replace_placeholder_keys($str, $replaceArray) + { + preg_match_all('/\$\{.*?\}/', $str, $matches); + $originalPlaceholders = $matches[0]; // 结果: ['${用户昵称}', '${店铺名称}', ...] + $arrayKeys = array_keys($replaceArray); // 结果: ['username', 'shopname', 'time', 'item1', 'item2'] + $replacePairs = []; + foreach ($originalPlaceholders as $index => $placeholder) { + if (isset($arrayKeys[$index])) { + // 关键:明确拼接 ${键} 结构 + $replacePairs[$placeholder] = '${' . $arrayKeys[$index] . '}'; + } + } + $newStr = strtr($str, $replacePairs); + return $newStr; + } +} + + +if (!function_exists('replace_json_keys')) { + function replace_json_keys($originalJson, $mapJson) + { + $originalData = json_decode($originalJson, true); + $mapData = json_decode($mapJson, true); + + $reverseMap = array_flip($mapData); + + $newData = []; + + foreach ($originalData as $oldKey => $value) { + // 如果原始键在映射中存在,则替换为对应的新键;否则保留原键 + $newKey = $reverseMap[$oldKey] ?? $oldKey; + $newData[$newKey] = $value; + } + $newJson = json_encode($newData, JSON_UNESCAPED_UNICODE); + return $newJson; + } +} + diff --git a/app/model/AlibabaSms.php b/app/model/AlibabaSms.php index 130107b..6c6e4f2 100644 --- a/app/model/AlibabaSms.php +++ b/app/model/AlibabaSms.php @@ -13,6 +13,7 @@ use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\CreateSmsTemplateRequest; use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\GetSmsTemplateRequest; use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\UpdateSmsTemplateRequest; use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\DeleteSmsTemplateRequest; +use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\QuerySendDetailsRequest; use Darabonba\OpenApi\Models\Config; use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest; use AlibabaCloud\Tea\Utils\Utils; @@ -51,11 +52,7 @@ class AlibabaSms extends Model // 复制代码运行请自行打印 API 的返回值 $res = $client->sendSmsWithOptions($sendSmsRequest, $runtime); Log::info('发送短信接口返回---》' . json_encode($res->body->toArray())); - if($res->body->code == 'OK') { - return true; - }else { - return false; - } + return $res->body->toArray(); } catch (Exception $error) { if (!($error instanceof TeaError)) { @@ -159,18 +156,26 @@ class AlibabaSms extends Model } - - - - - - /** * 查询短信发送结果 */ public function QuerySendDetails($data) { - + $client = self::createClient(); + $querySendDetailsRequest = new QuerySendDetailsRequest($data); + $runtime = new RuntimeOptions([]); + try { + $res = $client->querySendDetailsWithOptions($querySendDetailsRequest, $runtime); + Log::info('查询短信发送结果接口返回---》' . json_encode($res->body->toArray())); + return $res->body->toArray(); + } + catch (Exception $error) { + if (!($error instanceof TeaError)) { + $error = new TeaError([], $error->getMessage(), $error->getCode(), $error); + } + Utils::assertAsString($error->message); + Log::info('查询短信发送结果接口错误--' . $error->message); + } } } diff --git a/app/model/ShopInfo.php b/app/model/ShopInfo.php new file mode 100644 index 0000000..258afb9 --- /dev/null +++ b/app/model/ShopInfo.php @@ -0,0 +1,191 @@ +where(['shop_id' => $shop_id])->find(); + if($shop_money) { + $db = Db::table('sms_shop_money')->where(['shop_id' => $shop_id]); + if($type == 1) { + $befor_money = $shop_money['money'] + $money; + $db->inc('money', $money); + }elseif ($type == 2) { + $befor_money = $shop_money['money'] - $money; + $db->dec('money', $money); + } + $db->update(); + // 新增记录 + Db::table('sms_shop_money_detail')->insert([ + 'shop_id' => $shop_id, + 'type' => $type, + 'expense' => $money, + 'balance' => $befor_money, + ]); + } + } + + + public static function javaGetUserList($k_user_params, $page, $size) + { + $get_list_url = config('cons.push_user_list_url'); + $get_list_params = [ + 'page' => $page, + 'size' => $size, + 'eventId' => $k_user_params['event_id'], + 'shopId' => $k_user_params['shop_id'], + 'sexMan' => $k_user_params['sex_man']==1?$k_user_params['sex_man']:'', + 'sexWoman' => $k_user_params['sex_woman']==1?$k_user_params['sex_woman']:'', + 'sexUnknown' => $k_user_params['sex_unknown']==1?$k_user_params['sex_unknown']:'', + 'isVip' => $k_user_params['is_vip']==1?$k_user_params['is_vip']:'', + 'isRecharge' => $k_user_params['is_recharge']==1?$k_user_params['is_recharge']:'', + 'noOrder' => $k_user_params['no_order']==1?$k_user_params['no_order']:'', + 'oneOrder' => $k_user_params['one_order']==1?$k_user_params['one_order']:'', + 'fiveOrder' => $k_user_params['five_order']==1?$k_user_params['five_order']:'', + 'orderTimeToday' => $k_user_params['order_time_today']==1?$k_user_params['order_time_today']:'', + 'orderTimeYesterday' => $k_user_params['order_time_yesterday']==1?$k_user_params['order_time_yesterday']:'', + 'orderTimeTwoWeeks' => $k_user_params['order_time_two_weeks']==1?$k_user_params['order_time_two_weeks']:'', + 'orderTimeMoreThanTwoWeeks' => $k_user_params['order_time_more_than_two_weeks']==1?$k_user_params['order_time_more_than_two_weeks']:'', + ]; + $ret = curl_post($get_list_url, $get_list_params, [ + 'Content-Type: application/json', + ]); + return json_decode($ret, true); + } + + + public static function getUserList($k_user_params, $page = 1, $size = 10) + { + $data_arr = self::javaGetUserList($k_user_params, $page, $size); + $for_user_list = []; + if($data_arr['code'] == 0) { + $user_list = self::evnuserlist($data_arr['data']['records']); + if($data_arr['data']['totalPage'] > 1) { + for ($i = 2; $i <= $data_arr['data']['totalPage']; $i++) { + $data_arr = self::javaGetUserList($k_user_params, $i, $size); + if($data_arr['code'] == 0) { + if(empty($for_user_list)) { + $for_user_list = self::evnuserlist($data_arr['data']['records']); + }else { + $o_for_user_list = self::evnuserlist($data_arr['data']['records']); + $for_user_list = array_merge($for_user_list, $o_for_user_list); + } + } + } + $user_list = array_merge($user_list, $for_user_list); + } + }else { + Log::info('获取用户列表时出错' . json_encode($data_arr)); + } + return $user_list; + } + + public static function evnuserlist($data) + { + foreach ($data as $k => $user) { + $push_user[] = [ + 'user_id' => $user['id'], + 'phone' => $user['phone'], + 'nickName' => $user['nickName'], + ]; + } + return $push_user; + } + + + public static function getTempDineList() + { + return [ + '用户昵称' => [ + 'username', '用户昵称' + ], + '店铺名称' => [ + 'shopname', '店铺名称' + ], + '活动名称' => [ + 'activityname', '活动名称', + ], + '活动时间' => [ + 'activitytime', '活动时间', + ], + '数量' => [ + 'num', '数量', + ], + '金额' => [ + 'money', '金额', + ], + '时间' => [ + 'time', '时间', + ], + ]; + } + + // 处理短信模版替换内容 + public static function handleSmsTemplates($content) + { + $arr = handldollerstr($content); + $x_arr = self::getTempDineList(); + $new_arr = []; + $item_new_arr = []; + if($arr) { + $i = 1; + foreach ($arr as $k => $v) { + if(isset($x_arr[$v])) { + $k_str = $x_arr[$v][0]; + $v_str = $x_arr[$v][1]; + $new_arr[$k_str] = $v_str; + }else { + $item = 'item' . $i; + $item_new_arr[$item] = '自定义' . $i; + $i++; + } + } + if($new_arr && $item_new_arr) { + $new_arr = array_merge($new_arr, $item_new_arr); + } + if(!$new_arr && $item_new_arr) { + $new_arr = $item_new_arr; + } + } + return $new_arr; + } + + // 处理发送短信时短信模版替换内容 + public static function handlesendSmsTemplates($content) + { + $arr = handldollerstr($content); + $x_arr = self::getTempDineList(); + $new_arr = []; + $item_new_arr = []; + if($arr) { + $i = 1; + foreach ($arr as $k => $v) { + if(isset($x_arr[$v])) { + $k_str = $x_arr[$v][0]; + $v_str = $x_arr[$v][1]; + $new_arr[$k_str] = $v_str; + }else { + $item = 'item' . $i; + $item_new_arr[$item] = '自定义' . $i; + $i++; + } + } + if($new_arr && $item_new_arr) { + $new_arr = array_merge($new_arr, $item_new_arr); + } + if(!$new_arr && $item_new_arr) { + $new_arr = $item_new_arr; + } + } + return $new_arr; + } + + + +} \ No newline at end of file diff --git a/app/queue/redis/ApplySmsTemp.php b/app/queue/redis/ApplySmsTemp.php index baf2a9d..f7caf7a 100644 --- a/app/queue/redis/ApplySmsTemp.php +++ b/app/queue/redis/ApplySmsTemp.php @@ -4,6 +4,7 @@ namespace app\queue\redis; use app\model\AlibabaSms; +use app\model\ShopInfo; use support\Log; use support\think\Db; use Webman\RedisQueue\Consumer; @@ -28,12 +29,17 @@ class ApplySmsTemp implements Consumer // 查询没有提交审核的模版 $temp = Db::table('sms_shop_template')->where(['id' => $shop_template_id])->where('status', 'in', [0, -2])->find(); if($temp) { + // 处理替换内容 + $templateContent_arr = ShopInfo::handleSmsTemplates($temp['content']); + $templateContent_json = json_encode($templateContent_arr); + $content = replace_placeholder_keys($temp['content'], $templateContent_arr); // 待申请 if($temp['status'] == 0) { $data = [ 'TemplateName' => $temp['title'], - 'TemplateContent' => $temp['content'], - 'Remark' => !empty($temp['remark']) ? $temp['remark'] : '', + 'TemplateContent' => $content, + 'TemplateRule' => $templateContent_json, + 'Remark' => !empty($temp['scene_detail']) ? $temp['scene_detail'] : '', 'TemplateType' => 2, 'RelatedSignName' => config('cons.sms_sign'), ]; @@ -45,7 +51,8 @@ class ApplySmsTemp implements Consumer 'TemplateCode' => $temp['template_code'], 'TemplateName' => $temp['title'], 'TemplateContent' => $temp['content'], - 'Remark' => !empty($temp['remark']) ? $temp['remark'] : '', + 'TemplateRule' => $templateContent_json, + 'Remark' => !empty($temp['scene_detail']) ? $temp['scene_detail'] : '', 'TemplateType' => 2, 'RelatedSignName' => config('cons.sms_sign'), ]; @@ -69,6 +76,7 @@ class ApplySmsTemp implements Consumer $update_data = [ 'status' => $status, 'template_code' => $template_code, + 'content_json' => $templateContent_json, 'fail_msg' => $fail_msg, 'order_id' => $order_id, 'update_time' => date('Y-m-d H:i:s'), diff --git a/app/queue/redis/BirthdayGiftSms.php b/app/queue/redis/BirthdayGiftSms.php index dce69e1..8e8bebd 100644 --- a/app/queue/redis/BirthdayGiftSms.php +++ b/app/queue/redis/BirthdayGiftSms.php @@ -17,9 +17,53 @@ class BirthdayGiftSms implements Consumer public function consume($data) { Log::info('消息队列【生日有礼】接收到消息' . $data); + if($data) { + $record = Db::table('mk_birthday_gift_record')->where(['id' => $data])->find(); + if($record) { + // 待发送 + if($record['push_status'] == 'wait_push') { + $user = Db::table('tb_shop_user')->where(['id' => $record['user_id']])->find(); + if($user && !empty($user['phone'])) { + // 检测余额 + $shop_money = Db::table('sms_shop_money')->where(['shop_id' => $record['main_shop_id']])->find(); + if($shop_money) { + // 余额计算 + if($shop_money['money'] > config('cons.sms_price')) { + $data = [ + 'templateCode' => $record['template_code'], + 'templateParam' => $record['template_content'], + 'phoneNumbers' => $user['phone'], + 'signName' => config('cons.sms_sign'), + ]; + $res = AlibabaSms::main($data); + if($res['Code'] == 'OK') { + Db::table('mk_birthday_gift_record')->where(['id' => $record['id']])->update([ + 'push_status' => 'await_push', + 'biz_id' => $res['BizId'], + 'push_time' => date('Y-m-d H:i:s'), + ]); + }else { + Db::table('mk_birthday_gift_record')->where(['id' => $record['id']])->update([ + 'push_status' => 'failed', + 'push_time' => date('Y-m-d H:i:s'), + ]); + } + }else { + Log::info('商户【' . $record['main_shop_id'] . '】余额不足'); + return false; + } + }else { + Log::info('商户【' . $record['main_shop_id'] . '】余额为空'); + return false; + } - - - + }else { + Log::info('【生日有礼】时查询用户不存在---》' . $record['user_id']); + } + } + }else { + Log::info('生日有礼记录未查询到'); + } + } } } \ No newline at end of file diff --git a/app/queue/redis/SendMarkSms.php b/app/queue/redis/SendMarkSms.php index d3d3030..cad3e52 100644 --- a/app/queue/redis/SendMarkSms.php +++ b/app/queue/redis/SendMarkSms.php @@ -4,6 +4,7 @@ namespace app\queue\redis; use app\model\AlibabaSms; +use app\model\ShopInfo; use support\Log; use support\think\Db; use Webman\RedisQueue\Consumer; @@ -17,10 +18,76 @@ class SendMarkSms implements Consumer public function consume($data) { Log::info('消息队列【发送营销短信】接收到消息' . $data); + if($data) { + // 发送任务 + $record = Db::table('sms_push_event')->where(['id' => $data, 'push_type' => 2])->find(); + if($record) { + // 待发送 + if($record['status'] == 0) { + // 范围用户 + if($record['user_type'] == 2) { + $k_user_params = Db::table('sms_push_event_user')->where(['event_id' => $record['id']])->find(); + if($k_user_params) { + $user_list = ShopInfo::getUserList($k_user_params); + }else { + Log::info('范围用户查找出错'); + return false; + } + }elseif ($record['status'] == 1) { + // 全部用户 + $user_list = Db::table('tb_shop_user')->whereNotNull('phone')->select(); + $user_list = ShopInfo::evnuserlist($user_list); + } + $y_temp = Db::table('sms_shop_template')->where(['id' => $record['push_event_id']])->find(); + if($user_list && $y_temp) { + foreach ($user_list as $k => $v) { + // 检测余额 + $shop_money = Db::table('sms_shop_money')->where(['shop_id' => $record['shop_id']])->find(); + if($shop_money) { + // 余额计算 + if($shop_money['money'] > config('cons.sms_price')) { + $templateParam = replace_json_keys($record['json'], $y_temp['content_json']); + $data = [ + 'templateCode' => $record['template_code'], + 'templateParam' => $templateParam, + 'phoneNumbers' => $user_list['phone'], + 'signName' => config('cons.sms_sign'), + ]; + $res = AlibabaSms::main($data); + Db::table('sms_push_event')->where(['id' => $record['id']])->update([ + 'status' => '1', + 'update_time' => date('Y-m-d H:i:s'), + 'send_time' => date('Y-m-d H:i:s'), + ]); - - - - + if($res['Code'] == 'OK') { + Db::table('sms_push_event_record')->insert([ + 'shop_id' => $record['shop_id'], + 'event_id' => $record['id'], + 'user_id' => $v['user_id'], + 'user_phone' => $v['phone'], + 'biz_id' => $res['BizId'], + 'send_time' => date('Y-m-d H:i:s'), + 'create_time' => date('Y-m-d H:i:s'), + 'status' => 1, + ]); + } + }else { + Log::info('商户【' . $record['main_shop_id'] . '】余额不足'); + return false; + } + }else { + Log::info('商户【' . $record['main_shop_id'] . '】余额为空'); + return false; + } + } + }else { + Log::info('【发送营销短信】无目标用户---》' . $record['user_id']); + } + } + }else { + Log::info('发送营销短信未查询到'); + } + } } } \ No newline at end of file diff --git a/config/cons.php b/config/cons.php index 8fd320b..fcf8cd2 100644 --- a/config/cons.php +++ b/config/cons.php @@ -10,5 +10,6 @@ return [ 'access_key_id' => 'LTAI5t9Wn4qu9UFcvy5LUH4y', 'access_key_secret' => 'UnqHyd2ByXTYisnIyXl5PsJyveIEau', ], - 'sms_sign' => '陕西超掌柜科技' + 'sms_sign' => '陕西超掌柜科技', + 'push_user_list_url' => 'http://192.168.1.42/account/admin/shopUser/getPushEventUser', ];