From ff3201375de459728581f77842614e3919a40227 Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Wed, 11 Mar 2026 16:01:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A8=E9=80=81=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UsedCarDetailController.php | 13 +- app/common/service/UsedCarDetailService.php | 88 +++---- app/queue/redis/ApprovalCallback.php | 83 ------- app/queue/redis/EmailMessage.php | 51 ---- app/queue/redis/FeishuMessage.php | 131 ----------- app/queue/redis/InquiryToCrmNew.php | 222 ------------------ app/queue/redis/Notice.php | 61 ----- app/queue/redis/PermAgt.php | 95 -------- app/queue/redis/Server.php | 57 ----- app/queue/redis/StockOrderPdf.php | 51 ---- app/queue/redis/WordPressPush.php | 138 +++++++++++ app/queue/redis/WsMsg.php | 68 ------ config/plugin/webman/redis-queue/process.php | 17 +- 13 files changed, 188 insertions(+), 887 deletions(-) delete mode 100644 app/queue/redis/ApprovalCallback.php delete mode 100644 app/queue/redis/EmailMessage.php delete mode 100644 app/queue/redis/FeishuMessage.php delete mode 100644 app/queue/redis/InquiryToCrmNew.php delete mode 100644 app/queue/redis/Notice.php delete mode 100644 app/queue/redis/PermAgt.php delete mode 100644 app/queue/redis/Server.php delete mode 100644 app/queue/redis/StockOrderPdf.php create mode 100644 app/queue/redis/WordPressPush.php delete mode 100644 app/queue/redis/WsMsg.php diff --git a/app/common/controller/UsedCarDetailController.php b/app/common/controller/UsedCarDetailController.php index 64a33c5..f5fbb40 100644 --- a/app/common/controller/UsedCarDetailController.php +++ b/app/common/controller/UsedCarDetailController.php @@ -268,10 +268,17 @@ class UsedCarDetailController extends BaseController try { $data = $request->post(); - $this->service->audit($request, $data); + $result = $this->service->audit($request, $data); $statusText = $data['audit_status'] == 1 ? '通过' : '拒绝'; - return $this->success([], "审核{$statusText}成功"); + $message = "审核{$statusText}成功"; + + // 审核通过时,提示推送任务已加入队列 + if ($data['audit_status'] == 1 && $result['push_queued']) { + $message .= ',WordPress推送任务已加入队列,将在后台处理'; + } + + return $this->success($result, $message); } catch (\Throwable $e) { return $this->error($e->getMessage()); } @@ -344,7 +351,7 @@ class UsedCarDetailController extends BaseController $result = $this->service->crawlAndStore($url, $userId); - return $this->success($result, '抓取成功,已保存为待审核状态'); + return $this->success($result, '抓取成功,WordPress推送任务已加入队列,将在后台处理'); } catch (\Throwable $e) { Log::error('抓取懂车帝二手车失败', [ 'url' => $request->post('url'), diff --git a/app/common/service/UsedCarDetailService.php b/app/common/service/UsedCarDetailService.php index f0e98da..83e0e9e 100644 --- a/app/common/service/UsedCarDetailService.php +++ b/app/common/service/UsedCarDetailService.php @@ -424,70 +424,33 @@ class UsedCarDetailService // 更新车辆状态 $result = UsedCarDetail::where('id', $carId)->update(['status' => $auditStatus]); - // 审核通过后自动推送到WordPress - $pushResult = ['success' => true, 'message' => '']; + // 审核通过后,将推送任务发送到队列 + $pushQueued = false; if ($auditStatus == 1) { - $pushResult = $this->pushToWordpressAfterAudit($carId); - - // 推送失败,将车辆状态修改为3 - if (!$pushResult['success']) { - UsedCarDetail::where('id', $carId)->update(['status' => 3]); + try { + // 发送到 Redis 队列异步处理 + \Webman\RedisQueue\Redis::send('wordpress-push', [ + 'car_id' => $carId, + ]); + $pushQueued = true; + Log::info('WordPress推送任务已加入队列', ['car_id' => $carId]); + } catch (\Throwable $e) { + Log::error('WordPress推送任务加入队列失败', [ + 'car_id' => $carId, + 'error' => $e->getMessage(), + ]); + $pushQueued = false; } } - // 返回审核结果和推送结果 + // 返回审核结果 return [ 'audit_success' => true, - 'push_success' => $pushResult['success'], - 'push_message' => $pushResult['message'], - 'message' => $pushResult['success'] ? '审核通过并推送成功' : '审核通过,但推送失败:' . $pushResult['message'], + 'push_queued' => $pushQueued, + 'message' => $pushQueued ? '审核通过,WordPress推送任务已加入队列' : '审核通过', ]; } - /** - * 审核通过后推送车辆到WordPress - * @return array ['success' => bool, 'message' => string] - */ - private function pushToWordpressAfterAudit(int $carId): array - { - try { - // 获取启用的WordPress站点 - $site = \app\common\model\WordpressSite::where('status', 1)->first(); - if (!$site) { - $msg = '没有启用的WordPress站点'; - Log::warning('WordPress推送失败:' . $msg, ['car_id' => $carId]); - return ['success' => false, 'message' => $msg]; - } - - // 检查是否已推送过 - $existing = \app\common\model\WordpressUsedCar::where('wp_site_id', $site->id) - ->where('used_car_id', $carId) - ->where('status', 1) - ->first(); -// if ($existing) { -// $msg = '车辆已推送,跳过'; -// Log::info('WordPress推送:' . $msg, ['car_id' => $carId]); -// return ['success' => true, 'message' => $msg]; -// } - - // 调用推送服务 - $wpService = new \app\common\service\WordpressSiteService(); - $wpService->createProductForNewSite($site, $carId); - - Log::info('WordPress推送成功', ['car_id' => $carId, 'site_id' => $site->id]); - return ['success' => true, 'message' => '']; - - } catch (\Throwable $e) { - $msg = $e->getMessage(); - Log::error('WordPress推送失败', [ - 'car_id' => $carId, - 'error' => $msg, - 'trace' => $e->getTraceAsString(), - ]); - return ['success' => false, 'message' => $msg]; - } - } - /** * 获取审核记录列表 */ @@ -957,7 +920,7 @@ class UsedCarDetailService 'crawl_year' => $info['year'] ?? '', 'crawl_msrp' => $info['new_price'] ?? '', 'crawl_car_id' => $info['car_id'] ?? 0, - 'status' => 0, // 待审核 + 'status' => 3, // 待推送状态 'images' => $imagesValue, 'power' => $info['horsepower'] ?? '', ]; @@ -971,6 +934,19 @@ class UsedCarDetailService // 5. 创建记录 $car = UsedCarDetail::create($data); + // 6. 将推送任务发送到队列 + try { + \Webman\RedisQueue\Redis::send('wordpress-push', [ + 'car_id' => $car->id, + ]); + Log::info('WordPress推送任务已加入队列', ['car_id' => $car->id]); + } catch (\Throwable $e) { + Log::error('WordPress推送任务加入队列失败', [ + 'car_id' => $car->id, + 'error' => $e->getMessage(), + ]); + } + return $car->toArray(); } diff --git a/app/queue/redis/ApprovalCallback.php b/app/queue/redis/ApprovalCallback.php deleted file mode 100644 index 14a4670..0000000 --- a/app/queue/redis/ApprovalCallback.php +++ /dev/null @@ -1,83 +0,0 @@ - $this->approvalStockOrders($data), - 'purchase_orders' => $this->approvalPurchaseOrders($data), - 'stock_orders_nullify' => '', - default => throw new BusinessException('未知的审批类型,无法执行回调操作') - }; - return true; - } catch (\Throwable $th) { - flg('审批回调异常:'.$th->getFile()."|".$th->getLine()."|".$th->getMessage(), false); - throw new BusinessException($th->getFile()."|".$th->getLine()."|".$th->getMessage()); - } - } - - // 消费失败回调 - /* - $package = [ - 'id' => 1357277951, // 消息ID - 'time' => 1709170510, // 消息时间 - 'delay' => 0, // 延迟时间 - 'attempts' => 2, // 消费次数 - 'queue' => 'send-mail', // 队列名 - 'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容 - 'max_attempts' => 5, // 最大重试次数 - 'error' => '错误信息' // 错误信息 - ] - */ - public function onConsumeFailure(\Throwable $e, $package) - { - echo "consume failure\n"; - echo $e->getMessage() . "\n"; - // 无需反序列化 - var_export($package); - } - - /** - * 备货单处理 - */ - private function approvalStockOrders($data) - { - // 生成PDF文件 - $service = new \app\crm\service\StockOrdersService(); - $result = $service->createdPdfFile($data['biz_id']); - return $result; - } - - /** - * 采购单处理 - */ - private function approvalPurchaseOrders($data) - { - $udata['id'] = $data['biz_id']; - if ($data['status'] === 'approve') { - $udata['status'] = 2; - } - - if ($data['status'] === 'reject') { - $udata['status'] = 3; - } - - $service = new \app\common\service\PurchaseOrdersService(); - $result = $service->updateStatus($udata); - return $result; - } -} \ No newline at end of file diff --git a/app/queue/redis/EmailMessage.php b/app/queue/redis/EmailMessage.php deleted file mode 100644 index f170a78..0000000 --- a/app/queue/redis/EmailMessage.php +++ /dev/null @@ -1,51 +0,0 @@ -getContent($data['id']); - - MailSend::send($data['email'], $data['title'], replaceTpl($content)); - } catch (\Throwable $th) { - throw $th; - } - } - - // 消费失败回调 - /* - $package = [ - 'id' => 1357277951, // 消息ID - 'time' => 1709170510, // 消息时间 - 'delay' => 0, // 延迟时间 - 'attempts' => 2, // 消费次数 - 'queue' => 'send-mail', // 队列名 - 'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容 - 'max_attempts' => 5, // 最大重试次数 - 'error' => '错误信息' // 错误信息 - ] - */ - public function onConsumeFailure(\Throwable $e, $package) - { - echo "consume failure\n"; - echo $e->getMessage() . "\n"; - // 无需反序列化 - var_export($package); - } -} \ No newline at end of file diff --git a/app/queue/redis/FeishuMessage.php b/app/queue/redis/FeishuMessage.php deleted file mode 100644 index 7aa510c..0000000 --- a/app/queue/redis/FeishuMessage.php +++ /dev/null @@ -1,131 +0,0 @@ -where('id', $data['id'])->first(); - - // 跟单人 - $reInfo = User::where('id', $info->user_id)->first(); - - // 飞书应用 - $feishuApp = new CorpFeishuApp(); - $feishuAppInfo = $feishuApp->where('corp_id', $reInfo->corp_id)->where('system_id', 1)->first(); - - // 推送数据 - $options = [ - 'contacts' => $info->contacts, - 'follow_last' => $info->follow_last ? date('Y-m-d H:i', $info->follow_last) : '', - 'follow_next' => $info->follow_next ? date('Y-m-d H:i', $info->follow_next) : '', - - 'app_id' => $feishuAppInfo->app_id, - 'app_secret' => $feishuAppInfo->app_secret, - 'state' => $feishuAppInfo->state, - 'msg_type' => 'interactive', - 'receive_id_type' => 'open_id', - 'receive_id' => $reInfo->open_id - ]; - } - - if ($data['category'] === 'inquiry') { - // 询单信息 - $inquiryLogService = new InquiryLogService(); - $info = $inquiryLogService->getInquiryLogBy($data['id']); - - // 接受者处理 - if ($data['receive_type'] == 'group') { - $appId = 'cli_a6183b882cb8500d'; - $appSecret = 'VqYib48u4EtDIa6nG9qH3f0fYCA1bCvs'; - $state = 'hesu'; - $receiveId = 'oc_a52d9f89ad76735279088c78beade8dc'; - $receiveIdType = 'chat_id'; - } else { - // 跟单人 - $reInfo = User::where('id', $info->recipient)->first(); - // 飞书应用 - $feishuApp = new CorpFeishuApp(); - $feishuAppInfo = $feishuApp->where('corp_id', $reInfo->corp_id)->where('system_id', 1)->first(); - // 推送信息 - $appId = $feishuAppInfo->app_id; - $appSecret = $feishuAppInfo->app_secret; - $state = $feishuAppInfo->state; - $receiveId = $reInfo->open_id; - $receiveIdType = 'open_id'; - } - - // 推送数据 - $options = [ - 'order_no' => $info->order_no, - 'quality' => $inquiryLogService->qualityName($info->quality), - 'channel' => $info->channelInfo ? $info->channelInfo->name : '', - 'country' => $info->countryInfo ? $info->countryInfo->cn_addr : '', - 'enter_user' => $info->enter ? $info->enter->name : '', - 'r_time' => $info->r_time ? date('Y-m-d H:i:s', $info->r_time) : '', - 't_time' => $info->t_time ? date('Y-m-d H:i:s', $info->t_time) : '', - - 'app_id' => $appId, - 'app_secret' => $appSecret, - 'state' => $state, - 'msg_type' => 'interactive', - 'receive_id_type' => $receiveIdType, - 'receive_id' => $receiveId - ]; - } - - $feishuMsg = new FeishuMsg($options); - $feishuMsg->messageSend($data['scene'], $options); - } catch (\Throwable $th) { - throw $th; - } - } - - // 消费失败回调 - /* - $package = [ - 'id' => 1357277951, // 消息ID - 'time' => 1709170510, // 消息时间 - 'delay' => 0, // 延迟时间 - 'attempts' => 2, // 消费次数 - 'queue' => 'send-mail', // 队列名 - 'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容 - 'max_attempts' => 5, // 最大重试次数 - 'error' => '错误信息' // 错误信息 - ] - */ - public function onConsumeFailure(\Throwable $e, $package) - { - echo "consume failure\n"; - echo $e->getMessage() . "\n"; - // 无需反序列化 - var_export($package); - } -} \ No newline at end of file diff --git a/app/queue/redis/InquiryToCrmNew.php b/app/queue/redis/InquiryToCrmNew.php deleted file mode 100644 index 2ad666f..0000000 --- a/app/queue/redis/InquiryToCrmNew.php +++ /dev/null @@ -1,222 +0,0 @@ -where('id', $data['id'])->first(); - - // 无论添加 或 更新,询单必然要存在 - if (!$info) { - throw new BusinessException('询盘不存在:' . $data['id']); - } - - if ($info->status == 0 && $info->recipient == 0) { - // 如果询单未发、未分,则不与crm同步,不做任何处理 - return true; - } - - // 更改库信息 - $this->syncCustomer($info); - } catch (\Throwable $th) { - throw $th; - } - } - - public function pushWs($title, $content, $recipient = []) - { - $wsdata = [ - 'sender_id' => 0, - 'sender_type' => 'system', - 'title' => $title, - 'content' => $content, - 'scene' => 1, - 'recipient' => $recipient - ]; - // 异步推送 - \Webman\RedisQueue\Redis::send('notice', $wsdata); - } - - public function pushEmail($id, $title, $email) - { - $options = [ - 'id' => $id, - 'email' => $email, - 'title' => $title, - ]; - \Webman\RedisQueue\Redis::send('email-message', $options); - } - - public function syncCustomer(Model $info) - { - $uInfo = $this->recipientInfo($info); - $crmCustomerInfo = CrmCustomer::where('sn', $info->order_no)->first(); - if ($crmCustomerInfo) { - - // 消息推送 - if ($info->status == 2 && empty($crmCustomerInfo->user_id)) { - // $notice = '分配'; - $title = '[询单·分配] 新的客户线索【' . $info->order_no . '】,请及时处理'; - $content = '[询单·分配] 新的客户线索【' . $info->order_no . '】,请及时处理'; - $recipient = [$info->recipient]; - // $this->pushWs($title, $content, $recipient); - $this->pushEmail($info->id, $title, $uInfo['uinfo']['email']); - } else { - if ($info->status == 2 && $crmCustomerInfo->user_id == $info->recipient) { - // $notice = '更新'; - $title = '[询单·更新] 客户线索' . $info->order_no . '有新的调整,请及时处理'; - $content = '[询单·更新] 客户线索' . $info->order_no . '有新的调整,请及时处理'; - $recipient = [$info->recipient]; - // $this->pushWs($title, $content, $recipient); - $this->pushEmail($info->id, $title, $uInfo['uinfo']['email']); - } else { - // $notice = '重新分配'; - $title = '[询单·分配] 新的客户线索【' . $info->order_no . '】,请及时处理'; - $content = '[询单·分配] 新的客户线索【' . $info->order_no . '】,请及时处理'; - $recipient = [$info->recipient]; - // $this->pushWs($title, $content, $recipient); - $this->pushEmail($info->id, $title, $uInfo['uinfo']['email']); - } - } - - $udata = [ - 'corp_id' => $uInfo['corpId'], - 'user_id' => $info->recipient, - 'channel_id' => $info->channel, - 'country_id' => $info->country, - 'magnitude' => $info->quality, - 'email' => $info->email, - 'phone' => $info->tel, - 'WhatsApp' => $info->whatsapp, - 'contacts' => $info->contacts, - 'desc_collect' => $info->content, - ]; - if (!$crmCustomerInfo->update($udata)) { - throw new BusinessException('同步失败,更新客户线索失败'); - } - } else { - $crmCustomer = new CrmCustomer(); - $crmCustomer->sn = $info->order_no; - $crmCustomer->corp_id = $uInfo['corpId']; - $crmCustomer->user_id = $uInfo['recipient']; - $crmCustomer->type = 1; - $crmCustomer->channel_id = $info->channel; - $crmCustomer->country_id = $info->country; - $crmCustomer->magnitude = $info->quality; - $crmCustomer->email = $info->email; - $crmCustomer->phone = $info->tel; - $crmCustomer->WhatsApp = $info->whatsapp; - $crmCustomer->contacts = $info->contacts; - $crmCustomer->desc_collect = $info->content; - $crmCustomer->enter_user_id = $info->enter_user_id; - $crmCustomer->obtain = time(); - if(empty($uInfo['recipient'])){ - $crmCustomer->deleted_at = time(); - } - // 修改同步CRM状态 - $info->sync_crm = 1; - // 保存 - if (!$crmCustomer->save() || !$info->save()) { - throw new BusinessException('同步失败,创建客户线索失败'); - } - - // 消息推送 - if ($info->status == 1) { - // $notice = '发布'; - $title = '[询单·公海] 新的客户线索【' . $info->order_no . '】发布'; - $content = '[询单·公海] 新的客户线索【' . $info->order_no . '】发布'; - $recipient = [$info->recipient]; - // $this->pushWs($title, $content); - $this->pushEmail($info->id, $title, $uInfo['uinfo']['email']); - } - - if ($info->status == 2) { - // $notice = '分配'; - $title = '[询单·分配] 新的客户线索【' . $info->order_no . '】,请及时处理'; - $content = '[询单·分配] 新的客户线索【' . $info->order_no . '】,请及时处理'; - $recipient = [$info->recipient]; - // $this->pushWs($title, $content, $recipient); - $this->pushEmail($info->id, $title, $uInfo['uinfo']['email']); - } - } - return true; - } - - public function recipientInfo(Model $info) - { - $recipient = 0; - $corpId = 0; - $uinfo = null; - if (!empty($info->recipient)) { - $recipient = $info->recipient; - $uinfo = User::where('id', $info->recipient)->first(); - if ($uinfo) { - $corpId = $uinfo->corp_id; - } - } - return [ - 'recipient' => $recipient, - 'corpId' => $corpId, - 'uinfo' => $uinfo - ]; - } - - // 消费失败回调 - /* - $package = [ - 'id' => 1357277951, // 消息ID - 'time' => 1709170510, // 消息时间 - 'delay' => 0, // 延迟时间 - 'attempts' => 2, // 消费次数 - 'queue' => 'send-mail', // 队列名 - 'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容 - 'max_attempts' => 5, // 最大重试次数 - 'error' => '错误信息' // 错误信息 - ] - */ - public function onConsumeFailure(\Throwable $e, $package) - { - echo "consume failure\n"; - echo $e->getMessage() . "\n"; - // 无需反序列化 - var_export($package); - } -} \ No newline at end of file diff --git a/app/queue/redis/Notice.php b/app/queue/redis/Notice.php deleted file mode 100644 index f166085..0000000 --- a/app/queue/redis/Notice.php +++ /dev/null @@ -1,61 +0,0 @@ -pushNotice($data); - - // 邮件消息 - if (isset($data['email']) && $data['email'] === 1) { - $emails = $service->getUserEmailsById($data['recipient']); - foreach ($emails as $email) { - if (empty($email)) { - continue; - } - \app\common\utils\email\MailSend::send($email, $data['title'], $data['content']); - } - } - return true; - } catch (\Throwable $th) { - flg("消息推送异常:".$th->getFile()."|".$th->getLine()."|".$th->getMessage()); - throw new BusinessException($th->getFile()."|".$th->getLine()."|".$th->getMessage()); - } - } - - // 消费失败回调 - /* - $package = [ - 'id' => 1357277951, // 消息ID - 'time' => 1709170510, // 消息时间 - 'delay' => 0, // 延迟时间 - 'attempts' => 2, // 消费次数 - 'queue' => 'send-mail', // 队列名 - 'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容 - 'max_attempts' => 5, // 最大重试次数 - 'error' => '错误信息' // 错误信息 - ] - */ - public function onConsumeFailure(\Throwable $e, $package) - { - echo "consume failure\n"; - echo $e->getMessage() . "\n"; - // 无需反序列化 - var_export($package); - } -} \ No newline at end of file diff --git a/app/queue/redis/PermAgt.php b/app/queue/redis/PermAgt.php deleted file mode 100644 index 42a4898..0000000 --- a/app/queue/redis/PermAgt.php +++ /dev/null @@ -1,95 +0,0 @@ - $userId, - 'role_id' => 1 - ], - [ - 'user_id' => $userId, - 'role_id' => 2 - ], - ]; - $systems = [ - [ - 'user_id' => $userId, - 'system_id' => 1 - ], - [ - 'user_id' => $userId, - 'system_id' => 2 - ], - [ - 'user_id' => $userId, - 'system_id' => 3 - ], - ]; - } else { - $roles = [ - 'user_id' => $userId, - 'role_id' => 2 - ]; - $systems = [ - [ - 'user_id' => $userId, - 'system_id' => 2 - ], - [ - 'user_id' => $userId, - 'system_id' => 3 - ], - ]; - } - - Db::table('user_roles')->insert($roles); - Db::table('user_systems')->insert($systems); - return true; - } catch (\Throwable $th) { - throw new BusinessException($th->getFile()."|".$th->getLine()."|".$th->getMessage()); - } - } - - // 消费失败回调 - /* - $package = [ - 'id' => 1357277951, // 消息ID - 'time' => 1709170510, // 消息时间 - 'delay' => 0, // 延迟时间 - 'attempts' => 2, // 消费次数 - 'queue' => 'send-mail', // 队列名 - 'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容 - 'max_attempts' => 5, // 最大重试次数 - 'error' => '错误信息' // 错误信息 - ] - */ - public function onConsumeFailure(\Throwable $e, $package) - { - echo "consume failure\n"; - echo $e->getMessage() . "\n"; - // 无需反序列化 - var_export($package); - } -} \ No newline at end of file diff --git a/app/queue/redis/Server.php b/app/queue/redis/Server.php deleted file mode 100644 index 2d9814e..0000000 --- a/app/queue/redis/Server.php +++ /dev/null @@ -1,57 +0,0 @@ -getFile()."|".$th->getLine()."|".$th->getMessage()); - throw new BusinessException($th->getFile()."|".$th->getLine()."|".$th->getMessage()); - } - } - - // 消费失败回调 - /* - $package = [ - 'id' => 1357277951, // 消息ID - 'time' => 1709170510, // 消息时间 - 'delay' => 0, // 延迟时间 - 'attempts' => 2, // 消费次数 - 'queue' => 'send-mail', // 队列名 - 'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容 - 'max_attempts' => 5, // 最大重试次数 - 'error' => '错误信息' // 错误信息 - ] - */ - public function onConsumeFailure(\Throwable $e, $package) - { - echo "consume failure\n"; - echo $e->getMessage() . "\n"; - // 无需反序列化 - var_export($package); - } -} \ No newline at end of file diff --git a/app/queue/redis/StockOrderPdf.php b/app/queue/redis/StockOrderPdf.php deleted file mode 100644 index 057a761..0000000 --- a/app/queue/redis/StockOrderPdf.php +++ /dev/null @@ -1,51 +0,0 @@ -createdPdfFile($data['id']); - - return true; - } catch (\Throwable $th) { - var_dump('4 :'.$th->getFile()."|".$th->getLine()."|".$th->getMessage()); - throw new BusinessException($th->getFile()."|".$th->getLine()."|".$th->getMessage()); - } - } - - // 消费失败回调 - /* - $package = [ - 'id' => 1357277951, // 消息ID - 'time' => 1709170510, // 消息时间 - 'delay' => 0, // 延迟时间 - 'attempts' => 2, // 消费次数 - 'queue' => 'send-mail', // 队列名 - 'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容 - 'max_attempts' => 5, // 最大重试次数 - 'error' => '错误信息' // 错误信息 - ] - */ - public function onConsumeFailure(\Throwable $e, $package) - { - echo "consume failure\n"; - echo $e->getMessage() . "\n"; - // 无需反序列化 - var_export($package); - } -} \ No newline at end of file diff --git a/app/queue/redis/WordPressPush.php b/app/queue/redis/WordPressPush.php new file mode 100644 index 0000000..fcd6fab --- /dev/null +++ b/app/queue/redis/WordPressPush.php @@ -0,0 +1,138 @@ + $data]); + return; + } + + try { + // 获取车辆信息 + $car = UsedCarDetail::find($carId); + if (!$car) { + Log::warning('WordPress推送:车辆不存在', ['car_id' => $carId]); + return; + } + + // 获取WordPress站点 + if ($siteId) { + $site = WordpressSite::find($siteId); + } else { + $site = WordpressSite::where('status', 1)->first(); + } + + if (!$site) { + $msg = '没有启用的WordPress站点'; + Log::warning('WordPress推送失败:' . $msg, ['car_id' => $carId]); + // 推送失败,更新车辆状态为5 + $car->update(['status' => 5]); + return; + } + + // 检查是否已推送过 + $existing = WordpressUsedCar::where('wp_site_id', $site->id) + ->where('used_car_id', $carId) + ->where('status', 1) + ->first(); + + if ($existing) { + Log::info('WordPress推送:车辆已推送过,跳过', [ + 'car_id' => $carId, + 'wp_product_id' => $existing->wp_product_id + ]); + return; + } + + // 执行推送 + $wpService = new WordpressSiteService(); + $result = $wpService->createProductForNewSite($site, $carId); + + // 记录推送结果 + if ($result['success']) { + // 推送成功,更新车辆状态为4 + $car->update(['status' => 4]); + Log::info('WordPress推送成功,车辆状态已更新为推送成功', [ + 'car_id' => $carId, + 'wp_product_id' => $result['product_id'] ?? null, + ]); + } else { + // 推送失败,更新车辆状态为5 + $car->update(['status' => 5]); + Log::error('WordPress推送失败,车辆状态已更新为推送失败', [ + 'car_id' => $carId, + 'error' => $result['message'] ?? '未知错误', + ]); + } + + } catch (\Throwable $e) { + Log::error('WordPress推送异常', [ + 'car_id' => $carId, + 'error' => $e->getMessage(), + 'trace' => $e->getTraceAsString(), + ]); + // 推送失败,更新车辆状态为5 + try { + if ($car) { + $car->update(['status' => 5]); + } + } catch (\Throwable $updateError) { + Log::error('更新车辆状态失败', [ + 'car_id' => $carId, + 'error' => $updateError->getMessage(), + ]); + } + throw $e; + } + } + + /** + * 消费失败回调 + */ + public function onConsumeFailure(\Throwable $e, $package) + { + Log::error('WordPress推送消费失败', [ + 'error' => $e->getMessage(), + 'package' => $package, + ]); + + // 获取车辆ID并更新状态 + $carId = $package['data']['car_id'] ?? null; + if ($carId) { + try { + UsedCarDetail::where('id', $carId)->update(['status' => 5]); + } catch (\Throwable $updateError) { + Log::error('推送失败后更新车辆状态异常', [ + 'car_id' => $carId, + 'error' => $updateError->getMessage(), + ]); + } + } + } +} diff --git a/app/queue/redis/WsMsg.php b/app/queue/redis/WsMsg.php deleted file mode 100644 index dcd15ce..0000000 --- a/app/queue/redis/WsMsg.php +++ /dev/null @@ -1,68 +0,0 @@ - 186, - 3 => 226, - ]; - - if (isset($data['is_first']) && $data['is_first'] === true) { - $num = (new \app\common\utils\approval\service\ApprovalFlowService())->getInitTodoCount($data['user_id']); - } else { - $num = $data['num']; - } - - if ($data['event'] === 'approval.todo.counts') { - $systemIds = UserSystems::where('user_id', $data['user_id'])->pluck('system_id')->toArray(); - if (count($systemIds) > 0) { - foreach ($systemIds as $systemId) { - if (isset($ly[$systemId])) { - $chain = \app\common\service\MenuBadgeService::buildBadgeChain(menuId: $ly[$systemId], leafCount: $num); - (new \app\common\utils\WsEvents())->pushWebsocket('approval.todo.counts', $data['user_id'], $chain); - } - } - } - } - } catch (\Throwable $th) { - dpr($th); - throw $th; - } - } - - // 消费失败回调 - /* - $package = [ - 'id' => 1357277951, // 消息ID - 'time' => 1709170510, // 消息时间 - 'delay' => 0, // 延迟时间 - 'attempts' => 2, // 消费次数 - 'queue' => 'send-mail', // 队列名 - 'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容 - 'max_attempts' => 5, // 最大重试次数 - 'error' => '错误信息' // 错误信息 - ] - */ - public function onConsumeFailure(\Throwable $e, $package) - { - echo "consume failure\n"; - echo $e->getMessage() . "\n"; - // 无需反序列化 - var_export($package); - } -} \ No newline at end of file diff --git a/config/plugin/webman/redis-queue/process.php b/config/plugin/webman/redis-queue/process.php index 58735ee..976459f 100644 --- a/config/plugin/webman/redis-queue/process.php +++ b/config/plugin/webman/redis-queue/process.php @@ -1,12 +1,11 @@ [ -// 'handler' => Webman\RedisQueue\Process\Consumer::class, -// 'count' => getenv('REDIS_QUEUE_PROCESS_COUNT'), // 可以设置多进程同时消费 -// 'constructor' => [ -// // 消费者类目录 -// 'consumer_dir' => app_path() . '/queue/redis', -// 'consumer_dir' => app_path() . getenv('REDIS_QUEUE_CONSUMER_DIR') -// ] -// ] + 'consumer' => [ + 'handler' => Webman\RedisQueue\Process\Consumer::class, + 'count' => 1, // 消费者进程数量 + 'constructor' => [ + // 消费者类目录 + 'consumer_dir' => app_path() . '/queue/redis', + ] + ] ]; \ No newline at end of file