setName('MqNewPayCancel')->setDescription('新用户取消充值延时图文消息队列任务'); } protected function execute(Input $input, Output $output) { Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下 try { $mq = new Rabbitmq(); $mq->receive('Q_NewPayCancelReceive', function (AMQPMessage $msg) use ($input, $output) { $data = json_decode($msg->body, true); Log::info("MQ 新用户取消充值 处理 用户ID:{$data['user_id']} OPENID:{$data['openid']} 商品ID:{$data['goods_id']}"); $output->writeln("MQ 新用户取消充值 处理 用户ID:{$data['user_id']} OPENID:{$data['openid']} 商品ID:{$data['goods_id']}"); $this->sendMessage($data, $input, $output); // 发送图文 $channel = $msg->delivery_info['channel']; $channel->basic_ack($msg->delivery_info['delivery_tag']); }); } catch (Exception $e) { Log::error("MQ 新用户取消充值 触发异常!message:" . $e->getMessage()); $output->writeln("MQ 新用户取消充值 触发异常!message:" . $e->getMessage()); } } /** * 发送图文消息 * * @param $data * @return bool true发送 false取消 */ private function sendMessage($data, Input $input, Output $output) { try { //查看标记状态 $userInfo = model('User')->getUserInfo($data['user_id']); $ispay = $userInfo['first_cancel_pay'] ?? 0; //查看活动状态 $activity = new Activity(); $activityinfo = $activity->get(100); if (!$ispay) { Log::info("MQ 新用户取消充值 处理 已充值!自动跳过!"); $output->writeln("MQ 新用户取消充值 处理 已充值!自动跳过!"); $activity->getConnection()->free(); $activity->getConnection()->close(); unset($activity); return false; } else if($activityinfo['status']!=1){ Log::info("MQ 新用户取消充值 活动结束!自动跳过!"); $output->writeln("MQ 新用户取消充值 活动结束!自动跳过!"); $activity->getConnection()->free(); $activity->getConnection()->close(); unset($activity); return false; } else{ //发送消息 $adminConfig = new AdminConfig(); $info = $adminConfig->getAdminInfoAll($data['channel_id']); $wechat = new WeChatObject($info); $officialAccount = $wechat->getOfficialAccount(); $officialAccount ->customer_service ->message(new News([new NewsItem($data['msg'])])) ->to($data['openid']) ->send(); $adminConfig->getConnection()->free(); $adminConfig->getConnection()->close(); $activity->getConnection()->free(); $activity->getConnection()->close(); unset($adminConfig); unset($activity); return true; } } catch (\Exception $exception) { Log::error("MQ 新用户取消充值 图文消息触发异常!message:" . $exception->getMessage()); $output->writeln("MQ 新用户取消充值 图文消息触发异常!message:" . $exception->getMessage()); return false; } } }