diff --git a/service/app/command/NotUsedOrderSms.php b/service/app/command/NotUsedOrderSms.php new file mode 100644 index 00000000..b76909a6 --- /dev/null +++ b/service/app/command/NotUsedOrderSms.php @@ -0,0 +1,78 @@ +addArgument('name', InputArgument::OPTIONAL, 'Name description'); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + $this->output($output, self::$defaultName . " started"); + + $orders = $this->orders(); + $total = count($orders); + + $this->output($output, "got {$total} orders"); + + if (1 > $total) { + return self::SUCCESS; + } + + foreach($orders as $order) { + if (empty($order->mobile)) { + continue; + } + + $result = Orders::reminderOrders($order); + $text = '已发送提醒。'; + + if (Error::is($result)) { + $text = '发送短信失败,' . $result['message'] ?? ''; + } + + $this->output($output, "单号 {$order->sn} : {$text}"); + } + + return self::SUCCESS; + } + + private function output(OutputInterface $output, string $message) + { + $output->writeln(sprintf('{"time":"%s", "message":"%s"}', date('Y-m-d H:i:s'), $message)); + } + + /** + * @return OrdersModel[] + */ + private function orders(): array + { + $list = OrdersModel::where('admin_id', '>', 0)->whereRaw(OrdersModel::AllOssStatusSql[1])->select()->all(); + return $list; + } +} diff --git a/service/app/command/SpiderDy.php b/service/app/command/SpiderDy.php index 25846b17..8ee59b9d 100644 --- a/service/app/command/SpiderDy.php +++ b/service/app/command/SpiderDy.php @@ -10,6 +10,7 @@ use app\model\Orders; use app\server\Douyin; use app\server\Meituan; use app\server\Kuaishou; +use app\server\Orders as ServerOrders; use app\server\SMS; use stdClass; use support\Log; @@ -338,9 +339,13 @@ class SpiderDy extends Command break; } + // 待使用订单提醒 + $reminder_orders = []; + foreach ($list as $order) { $item = Orders::where('os', $order->os)->where('sn', $order->sn)->find(); + if (empty($item)) { $order->is_zhibo = $this->zhibo($order->product_name, $order->os); @@ -362,6 +367,9 @@ class SpiderDy extends Command ($order->os == 2 && array_key_exists($order->order_status, [2 => 2, 3 => 3, 4 => 4])) || ($order->os == 3 && $order->order_status == 1)) { // $this->sms($admin_id, $order); + + // 待使用订单提醒 + $reminder_orders[] = $order; } //新获得一个用户的,提示管理员有新的订单 Redis::incrBy('CRM:USER:ONLINE:NEW:' . $admin_id, 1); @@ -380,7 +388,10 @@ class SpiderDy extends Command if ($back) { $this->_finance(0, $item->id, $item->asset_price); } + } + if (!empty($reminder_orders)) { + ServerOrders::reminderOrders(...$reminder_orders); } $page++; diff --git a/service/app/command/SpiderMt.php b/service/app/command/SpiderMt.php index c690ccdc..854ca965 100644 --- a/service/app/command/SpiderMt.php +++ b/service/app/command/SpiderMt.php @@ -10,6 +10,7 @@ use app\model\Orders; use app\server\Douyin; use app\server\Meituan; use app\server\Kuaishou; +use app\server\Orders as ServerOrders; use app\server\SMS; use stdClass; use support\Log; @@ -373,6 +374,9 @@ class SpiderMt extends Command break; } + // 待使用订单提醒 + $reminder_orders = []; + foreach ($list as $order) { $item = Orders::where('os', $order->os)->where('sn', $order->sn)->find(); @@ -397,6 +401,9 @@ class SpiderMt extends Command ($order->os == 2 && array_key_exists($order->order_status, [2 => 2, 3 => 3, 4 => 4])) || ($order->os == 3 && $order->order_status == 1)) { $this->sms($admin_id, $order); + + // 待使用订单提醒 + $reminder_orders[] = $order; } //新获得一个用户的,提示管理员有新的订单 Redis::incrBy('CRM:USER:ONLINE:NEW:' . $admin_id, 1); @@ -415,7 +422,10 @@ class SpiderMt extends Command if ($back) { $this->_finance(0, $item->id, $item->asset_price); } + } + if (!empty($reminder_orders)) { + ServerOrders::reminderOrders(...$reminder_orders); } $page++; diff --git a/service/app/common/Error.php b/service/app/common/Error.php new file mode 100644 index 00000000..e9000131 --- /dev/null +++ b/service/app/common/Error.php @@ -0,0 +1,28 @@ + -9999, 'message' => '']; + const ERR_SMS_BLACKS = ['code' => 100001, 'message' => 'mobile is in blacks']; + const ERR_SMS_SEND_FAIL = ['code' => 100002, 'message' => 'send error']; + + public static function undefined(string $message) + { + return ['code' => self::ERR_UNDEFINED, 'message' => $message]; + } + + public static function setMessage(string $message, array $err) + { + return ['code' => $err['code'] ?? 0, 'message' => $message]; + } + + public static function is(array $err): bool + { + return isset($err['code']) && $err['code'] != 0; + } + + public static function compare(array $err, array $needle): bool + { + return isset($err['code']) && isset($needle['code']) && $err['code'] === $needle['code']; + } +} \ No newline at end of file diff --git a/service/app/common/Keys.php b/service/app/common/Keys.php new file mode 100644 index 00000000..6b6abf43 --- /dev/null +++ b/service/app/common/Keys.php @@ -0,0 +1,45 @@ +getConstants(); + } catch (\ReflectionException $e) { + Log::error(__METHOD__, [$e->getMessage(),$e->getCode()]); + } + + } + if (isset(self::$instance[$name])) { + $format = self::$instance[$name]; + } else { + $format = ''; + } + + return vsprintf($format, $arguments); + } +} + diff --git a/service/app/server/Blacks.php b/service/app/server/Blacks.php new file mode 100644 index 00000000..19fb2af7 --- /dev/null +++ b/service/app/server/Blacks.php @@ -0,0 +1,30 @@ +column('id', 'mobile'); + + if (!empty($list)) { + $chunks = array_chunk($list, 50, true); + + foreach($chunks as $mobiles) { + Redis::hMSet($key, $mobiles); + } + + Redis::expire($key, 3600); + } + } + + return Redis::hExists($key, $mobile); + } +} diff --git a/service/app/server/Orders.php b/service/app/server/Orders.php new file mode 100644 index 00000000..76c8a943 --- /dev/null +++ b/service/app/server/Orders.php @@ -0,0 +1,71 @@ +admin_id; + $sns[] = $order->sn; + } + + $admins = AdminsModel::whereIn('id', $admin_ids)->select()->column('mobile', 'id'); + + if (empty($admins)) { + return array_fill_keys($sns, Error::undefined('admin check error')); + } + + $ttl = 86400; + $result = []; + + foreach($orders as $order) { + $admin_mobile = $admins[$order->admin_id] ?? ''; + + if (empty($order->mobile) || empty($admin_mobile)) { + $result[$order->sn] = Error::undefined('client mobile or admin mobile invalid'); + continue; + } + + if (Blacks::CExists($order->mobile)) { + $result[$order->sn] = Error::ERR_SMS_BLACKS; + Log::info(__METHOD__ . " blacks", [$order->mobile, $order->sn]); + continue; + } + + $key = sprintf("CRM:%s:%s_%s", 'reminderOrders', $order->sn, $order->mobile); + $ok = Redis::set($key, time(), 'EX', $ttl, 'NX'); + + if (!$ok) { + $result[$order->sn] = Error::undefined('reminder cooldown'); + continue; + } + + $res = SMS::juhe_sms_send($order->mobile, SMS::JUHE_TMP_REMINDER_ORDER, ['title' => $order->product_name, 'mobile' => $admin_mobile]); + $err_code = $res['error_code'] ?? 0; + + if ($err_code != 0) { + Log::error(__METHOD__ . " send error", [$res, $order->mobile, $order->sn]); + $result[$order->sn] = Error::ERR_SMS_SEND_FAIL; + continue; + } + + $result[$order->sn] = []; + } + + return count($orders) > 1 ? $result : reset($result); + } +} diff --git a/service/app/server/SMS.php b/service/app/server/SMS.php index a2eedcab..a62c29dc 100644 --- a/service/app/server/SMS.php +++ b/service/app/server/SMS.php @@ -2,6 +2,8 @@ namespace app\server; class SMS { + const JUHE_TMP_REMINDER_ORDER = 263585; + static function juhe_sms_send($mobile, $tempid, $vars) { foreach($vars as &$v) {