初始化项目
This commit is contained in:
@@ -0,0 +1,346 @@
|
||||
<?php
|
||||
namespace app\console\command;
|
||||
|
||||
use app\common\service\QueueService;
|
||||
use app\console\model\OrderGoods;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use app\store\model\order\Record as RecordModel;
|
||||
use app\common\service\SybOil;
|
||||
use think\facade\Db;
|
||||
|
||||
class CallbackQueue extends Command
|
||||
{
|
||||
protected $queueService;
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('callback:queue')
|
||||
->setDescription('Process messages from the Redis queue');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$this->queueService = new QueueService();
|
||||
|
||||
while (true) {
|
||||
$message = $this->queueService->pop('callback_queue');
|
||||
if ($message) {
|
||||
// 处理消息
|
||||
$this->processMessage($message);
|
||||
} else {
|
||||
// 暂停一段时间再检查队列
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// protected function processMessage($message)
|
||||
// {
|
||||
// $SybOil = new SybOil();
|
||||
// $SybOil->saveLogStore('callback_queue队列数据处理:【'.$message.'】');
|
||||
// // 拿到队列数据处理
|
||||
// $data = json_decode($message, true);
|
||||
// // 检查解码是否成功
|
||||
// if ($data === false) {
|
||||
// $SybOil->saveLogStore('callback_queue队列数据解码失败:' . json_last_error_msg());
|
||||
// return;
|
||||
// }
|
||||
// // 确保 $data 中包含 header 和 res
|
||||
// if (!isset($data['header']) || !isset($data['res'])) {
|
||||
// $SybOil->saveLogStore('数据格式不正确:【' . $message . '】');
|
||||
// return;
|
||||
// }
|
||||
// $headers = $data['header'];
|
||||
// $res = $data['res'];
|
||||
// // 此处可以根据业务逻辑处理接收到的数据
|
||||
// try {
|
||||
// switch ($headers['method']) {
|
||||
// case "syb.notify.pay.result":
|
||||
// // 二维码交易结果通知
|
||||
// if($res['oilCardType'] == 'card') {
|
||||
// $where = array(
|
||||
// ['oilCardNo', '=', $res['oilCardNo']],
|
||||
// ['goods_price', '=', $res['amount']],
|
||||
// );
|
||||
// }else{
|
||||
// $where = array(
|
||||
// ['shareNo', '=', $res['shareNo']],
|
||||
// ['goods_price', '=', $res['amount']],
|
||||
// );
|
||||
// }
|
||||
// $orderGoods = Db::name('order_goods')
|
||||
// ->where($where)
|
||||
// ->find();
|
||||
// if($orderGoods['order_goods_id']) {
|
||||
// $field['qrCode'] = $res['qrCode'];
|
||||
// if ($res['status'] == 'success') {
|
||||
// $field['use_status'] = 1;
|
||||
// } else {
|
||||
// $field['use_text'] = $res['errMessage'];
|
||||
// }
|
||||
// // 更新用户使用状态
|
||||
// Db::name('order_goods')
|
||||
// ->where('order_goods_id', $orderGoods['order_goods_id'])
|
||||
// ->update($field);
|
||||
// }else{
|
||||
// $SybOil->saveLogStore('未找到订单信息:【'.$message.'】');
|
||||
// }
|
||||
// break;
|
||||
// case "syb.notify.oilCard.change":
|
||||
// // 油卡变动通知
|
||||
// $where = array(
|
||||
// ['oilCardNo', '=', $res['oilCardNo']],
|
||||
// ['goods_price', '=', $res['amount']],
|
||||
// );
|
||||
// $orderGoods = Db::name('order_goods')
|
||||
// ->where($where)
|
||||
// ->find();
|
||||
// $res['order_id'] = $orderGoods['order_id'];
|
||||
// $res['order_goods_id'] = $orderGoods['order_goods_id'];
|
||||
// $res['user_id'] = $orderGoods['user_id'];
|
||||
// $res['store_id'] = $orderGoods['store_id'];
|
||||
// $RecordModel = new RecordModel();
|
||||
// // 油卡变动记录写入结果
|
||||
// $RecordModel->add($res);
|
||||
// // 更新油券使用状态
|
||||
// if($orderGoods['order_goods_id'] && $res['optType'] == 'add_oil'){
|
||||
// Db::name('order_goods')
|
||||
// ->where('order_goods_id', $orderGoods['order_goods_id'])
|
||||
// ->update(array(
|
||||
// 'use_status' => 1,
|
||||
// 'use_time' => time(),
|
||||
// 'use_text' => '加油',
|
||||
// ));
|
||||
// // 修改主订单为已完成
|
||||
// Db::name('order')
|
||||
// ->where('order_id', $orderGoods['order_id'])
|
||||
// ->update(array(
|
||||
// 'order_status' => 30
|
||||
// ));
|
||||
// // 更新用户使用状态
|
||||
// Db::name('account')
|
||||
// ->where('account_id', $orderGoods['account_id'])
|
||||
// ->update(['last_called_at' => time(), 'state' => 0]);
|
||||
// $SybOil->saveLogStore('加油回调处理成功了:'.json_encode($res));
|
||||
// }else{
|
||||
// $SybOil->saveLogStore('没有处理结果:【'.$message.'】');
|
||||
// }
|
||||
// break;
|
||||
// case "syb.notify.oilCard.share":
|
||||
// // 分油结果通知
|
||||
// if($res['oilCardType'] == 'card') {
|
||||
// $where = array(
|
||||
// ['oilCardNo', '=', $res['oilCardNo']],
|
||||
// ['goods_price', '=', $res['amount']],
|
||||
// );
|
||||
// }else{
|
||||
// $where = array(
|
||||
// ['shareNo', '=', $res['shareNo']],
|
||||
// ['goods_price', '=', $res['amount']],
|
||||
// );
|
||||
// }
|
||||
// $orderGoods = Db::name('order_goods')
|
||||
// ->where($where)
|
||||
// ->find();
|
||||
// if($orderGoods['order_goods_id']) {
|
||||
// $field = ['oil_time' => time()];
|
||||
// if ($res['status'] == 'success') {
|
||||
// $field['oil_status'] = 1;
|
||||
// } else {
|
||||
// $field['oil_status'] = 0;
|
||||
// $field['oil_text'] = $res['msg'];
|
||||
// }
|
||||
// Db::name('order_goods')
|
||||
// ->where('order_goods_id', $orderGoods['order_goods_id'])
|
||||
// ->update($field);
|
||||
//
|
||||
// $SybOil->saveLogStore('分油回调处理成功了:'.json_encode($res));
|
||||
// }else{
|
||||
// $this->queueService->push('callback_queue', json_encode(['header'=>$headers,'res'=>$res],320));
|
||||
// $SybOil->saveLogStore('重新加入队列:【'.$message.'】');
|
||||
// }
|
||||
// break;
|
||||
// default:
|
||||
// $SybOil->saveLogStore('队列数据处理异常,未知:【'.$message.'】');
|
||||
// }
|
||||
// } catch (\Exception $e) {
|
||||
// $SybOil->saveLogStore(
|
||||
// '队列回调处理异常:' . $e->getMessage() .
|
||||
// ' 在文件:' . $e->getFile() .
|
||||
// ' 第:' . $e->getLine() .
|
||||
// ' 行。堆栈跟踪:' . $e->getTraceAsString()
|
||||
// );
|
||||
//
|
||||
// }
|
||||
// }
|
||||
|
||||
protected function processMessage($message)
|
||||
{
|
||||
$SybOil = new SybOil();
|
||||
$SybOil->saveLogStore('callback_queue队列数据处理:【'.$message.'】');
|
||||
// 拿到队列数据处理
|
||||
$data = json_decode($message, true);
|
||||
// 检查解码是否成功
|
||||
if ($data === false) {
|
||||
$SybOil->saveLogStore('callback_queue队列数据解码失败:' . json_last_error_msg());
|
||||
return;
|
||||
}
|
||||
// 确保 $data 中包含 header 和 res
|
||||
if (!isset($data['header']) || !isset($data['res'])) {
|
||||
$SybOil->saveLogStore('数据格式不正确:【' . $message . '】');
|
||||
return;
|
||||
}
|
||||
$headers = $data['header'];
|
||||
$res = $data['res'];
|
||||
// 此处可以根据业务逻辑处理接收到的数据
|
||||
try {
|
||||
switch ($headers['method']) {
|
||||
case "use":
|
||||
// 油卡变动通知
|
||||
$where = array(
|
||||
['shareNo', '=', $res['trade_no']],
|
||||
);
|
||||
$orderGoods = Db::name('order_goods')
|
||||
->where($where)
|
||||
->find();
|
||||
$res['order_id'] = $orderGoods['order_id'];
|
||||
$res['order_goods_id'] = $orderGoods['order_goods_id'];
|
||||
$res['user_id'] = $orderGoods['user_id'];
|
||||
$res['store_id'] = $orderGoods['store_id'];
|
||||
$res['optType'] = 'add_oil';
|
||||
$RecordModel = new RecordModel();
|
||||
// 油卡变动记录写入结果
|
||||
$RecordModel->add($res);
|
||||
// 更新油券使用状态
|
||||
if($orderGoods['order_goods_id'] && $res['useStatus'] == '2'){
|
||||
Db::name('order_goods')
|
||||
->where('order_goods_id', $orderGoods['order_goods_id'])
|
||||
->update(array(
|
||||
'use_status' => 1,
|
||||
'use_time' => time(),
|
||||
'use_text' => '加油',
|
||||
));
|
||||
// 订单下的券都已使用
|
||||
if(Db::name('order_goods')->where([['order_id' , '=', $orderGoods['order_id']], ['use_status', '=', '0']])->count() == 0) {
|
||||
// 修改主订单为已完成
|
||||
Db::name('order')
|
||||
->where('order_id', $orderGoods['order_id'])
|
||||
->update(array(
|
||||
'order_status' => 30
|
||||
));
|
||||
}
|
||||
// 更新用户使用状态
|
||||
Db::name('account')
|
||||
->where('account_id', $orderGoods['account_id'])
|
||||
->update(['last_called_at' => time(), 'state' => 0]);
|
||||
$SybOil->saveLogStore('加油回调处理成功了:'.json_encode($res));
|
||||
}else{
|
||||
$SybOil->saveLogStore('没有处理结果:【'.$message.'】');
|
||||
}
|
||||
break;
|
||||
case "oil":
|
||||
// 分油结果通知
|
||||
$where = array(
|
||||
['shareNo', '=', $res['trade_no']],
|
||||
);
|
||||
$orderGoods = Db::name('order_goods')
|
||||
->where($where)
|
||||
->find();
|
||||
if($orderGoods['order_goods_id'] && $res['useStatus'] == '1') {
|
||||
$field = ['oil_time' => time()];
|
||||
$field['oil_status'] = 1;
|
||||
$field['qrCode'] = $res['useUrl'] ?? '';
|
||||
Db::name('order_goods')
|
||||
->where('order_goods_id', $orderGoods['order_goods_id'])
|
||||
->update($field);
|
||||
|
||||
$SybOil->saveLogStore('分油回调处理成功了:'.json_encode($res));
|
||||
}else{
|
||||
// $this->queueService->push('callback_queue', json_encode(['header'=>$headers,'res'=>$res],320));
|
||||
// $SybOil->saveLogStore('重新加入队列:【'.$message.'】');
|
||||
}
|
||||
break;
|
||||
case "use_partners":
|
||||
// 油卡变动通知
|
||||
$where = array(
|
||||
['shareNo', '=', $res['trade_no']],
|
||||
);
|
||||
$order = Db::name('partners_order')
|
||||
->where($where)
|
||||
->find();
|
||||
$res['order_id'] = $order['order_id'];
|
||||
$res['order_goods_id'] = $order['goods_id'];
|
||||
$res['user_id'] = 0;
|
||||
$res['store_id'] = $order['store_id'];
|
||||
$res['optType'] = 'add_oil';
|
||||
$RecordModel = new RecordModel();
|
||||
// 油卡变动记录写入结果
|
||||
$RecordModel->add($res);
|
||||
// 更新油券使用状态
|
||||
if($order['order_id'] && $res['useStatus'] == '2'){
|
||||
Db::name('partners_order')
|
||||
->where('order_id', $order['order_id'])
|
||||
->update(array(
|
||||
'use_status' => 1,
|
||||
'use_time' => time(),
|
||||
'use_text' => '加油',
|
||||
));
|
||||
$SybOil->saveLogStore('加油回调处理成功了:'.json_encode($res));
|
||||
|
||||
// 通知对接公司
|
||||
$queueService = new QueueService();
|
||||
$queueService->push('notify_queue', json_encode([
|
||||
'order_id' => $order['order_id'],
|
||||
'optType' => 'add_oil',
|
||||
'retry_count' => 0,
|
||||
], 320));
|
||||
}else{
|
||||
$SybOil->saveLogStore('没有处理结果:【'.$message.'】');
|
||||
}
|
||||
break;
|
||||
case "oil_partners":
|
||||
// 分油结果通知
|
||||
$where = array(
|
||||
['shareNo', '=', $res['trade_no']],
|
||||
);
|
||||
$order = Db::name('partners_order')
|
||||
->where($where)
|
||||
->find();
|
||||
if($order['order_id'] && $res['useStatus'] == '1') {
|
||||
$field = ['oil_time' => time()];
|
||||
$field['oil_status'] = 1;
|
||||
$field['qrCode'] = $res['useUrl'] ?? '';
|
||||
Db::name('partners_order')
|
||||
->where('order_id', $order['order_id'])
|
||||
->update($field);
|
||||
|
||||
$SybOil->saveLogStore('分油回调处理成功了:'.json_encode($res));
|
||||
|
||||
// 通知对接公司
|
||||
$queueService = new QueueService();
|
||||
$queueService->push('notify_queue', json_encode([
|
||||
'order_id' => $order['order_id'],
|
||||
'optType' => 'share_oil',
|
||||
'retry_count' => 0,
|
||||
], 320));
|
||||
}else{
|
||||
// $this->queueService->push('callback_queue', json_encode(['header'=>$headers,'res'=>$res],320));
|
||||
// $SybOil->saveLogStore('重新加入队列:【'.$message.'】');
|
||||
}
|
||||
break;
|
||||
default:
|
||||
$SybOil->saveLogStore('队列数据处理异常,未知:【'.$message.'】');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$SybOil->saveLogStore(
|
||||
'队列回调处理异常:' . $e->getMessage() .
|
||||
' 在文件:' . $e->getFile() .
|
||||
' 第:' . $e->getLine() .
|
||||
' 行。堆栈跟踪:' . $e->getTraceAsString()
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
namespace app\console\command;
|
||||
|
||||
use app\store\model\User as UserModel;
|
||||
use app\common\service\QueueService;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use app\store\model\card\CardLog as CardLogModel;
|
||||
use app\common\service\SybOil;
|
||||
use think\facade\Db;
|
||||
|
||||
class CardQueue extends Command
|
||||
{
|
||||
protected $queueService;
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('card:queue')
|
||||
->setDescription('Process messages from the Redis queue');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$this->queueService = new QueueService();
|
||||
|
||||
while (true) {
|
||||
$message = $this->queueService->pop('card_queue');
|
||||
if ($message) {
|
||||
// 处理消息
|
||||
$this->processMessage($message);
|
||||
} else {
|
||||
// 暂停一段时间再检查队列
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function processMessage($message)
|
||||
{
|
||||
$SybOil = new SybOil();
|
||||
// 拿到队列数据处理
|
||||
$data = json_decode($message, true);
|
||||
$SybOil->saveLogStore('card_queue队列数据处理:【'.$message.'】');
|
||||
$userId = $data['user_id'];
|
||||
$cardId = $data['card_id'];
|
||||
// 查询兑换卡
|
||||
$card = Db::name('card')
|
||||
->where('card_id', $cardId)
|
||||
->find();
|
||||
$field = [
|
||||
'card_id'=>$cardId,
|
||||
'card_number'=>$card['card_number'],
|
||||
'user_id'=>$userId,
|
||||
'money'=>$card['amount'],
|
||||
'store_id'=>$card['store_id'],
|
||||
];
|
||||
if ($card['is_used'] != 10) {
|
||||
CardLogModel::add(array_merge(
|
||||
[
|
||||
'state'=>10,
|
||||
'describe'=>'实体兑换卡已使用',
|
||||
],
|
||||
$field
|
||||
));
|
||||
return false; // 无效的卡或密钥
|
||||
}
|
||||
Db::startTrans();
|
||||
try {
|
||||
// 更新兑换卡使用状态
|
||||
$affectedRows = Db::name('card')
|
||||
->where('card_id', $card['card_id'])
|
||||
->where('version', $card['version'])
|
||||
->update([
|
||||
'is_used' => 20,
|
||||
'used_time' => time(),
|
||||
'user_id' => $userId,
|
||||
'version' => $card['version'] + 1,
|
||||
]);
|
||||
|
||||
if ($affectedRows === 0) {
|
||||
Db::rollback();
|
||||
// 增加兑换记录
|
||||
CardLogModel::add(array_merge(
|
||||
[
|
||||
'state'=>10,
|
||||
'describe'=>'兑换失败,已使用',
|
||||
],
|
||||
$field
|
||||
));
|
||||
return false;
|
||||
}
|
||||
// 更新用户余额
|
||||
$obj = UserModel::detail($userId);
|
||||
// 增加分销对象的余额
|
||||
$inc = $obj->exchangeToBalance($obj['mobile'],[
|
||||
'money'=>$card['amount'],
|
||||
'mode'=>'inc',
|
||||
'remark'=>'实体卡兑换',
|
||||
], $card['store_id']);
|
||||
if(!$inc){
|
||||
Db::rollback();
|
||||
CardLogModel::add(array_merge(
|
||||
[
|
||||
'state'=>10,
|
||||
'describe'=>'增加用户余额失败',
|
||||
],
|
||||
$field
|
||||
));
|
||||
return false;
|
||||
}
|
||||
// 增加兑换记录
|
||||
CardLogModel::add(array_merge(
|
||||
[
|
||||
'state'=>20,
|
||||
'describe'=>'兑换成功',
|
||||
],
|
||||
$field
|
||||
));
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$SybOil->saveLogStore($e->getMessage());
|
||||
Db::rollback();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
namespace app\console\command;
|
||||
|
||||
use app\common\service\PartnersOil;
|
||||
use app\common\service\QueueService;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use app\common\model\partners\Order as OrderModel;
|
||||
use app\common\model\Partners as partnersModel;
|
||||
use think\facade\Db;
|
||||
use think\facade\Log;
|
||||
|
||||
class NotifyQueue extends Command
|
||||
{
|
||||
protected $queueService;
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('notify:queue')
|
||||
->setDescription('Process messages from the Redis queue');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$this->queueService = new QueueService();
|
||||
|
||||
while (true) {
|
||||
// 先迁移到期的延迟任务
|
||||
$this->queueService->migrateDelayedToReady('notify_queue');
|
||||
|
||||
$message = $this->queueService->pop('notify_queue');
|
||||
if ($message) {
|
||||
// 处理消息
|
||||
$data = json_decode($message, true);
|
||||
|
||||
// 如果没有重试计数,默认 0
|
||||
$retryCount = isset($data['retry_count']) ? intval($data['retry_count']) : 0;
|
||||
|
||||
$orderId = $data['order_id'] ?? null;
|
||||
if (!$orderId) {
|
||||
Log::error("队列消息缺少 order_id: " . $message);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 调用回调接口
|
||||
$success = $this->notifyPartner($data);
|
||||
|
||||
if ($success) {
|
||||
Log::info("订单 {$orderId} 回调成功");
|
||||
} else {
|
||||
Log::warning("订单 {$orderId} 第 {$retryCount} 次回调失败");
|
||||
|
||||
if ($retryCount < 3) {
|
||||
$retryCount++;
|
||||
$delay = $this->getDelayTime($retryCount);
|
||||
|
||||
// 重新放回延迟队列
|
||||
$data['retry_count'] = $retryCount;
|
||||
$this->queueService->laterPush('notify_queue', json_encode($data, 320), $delay);
|
||||
|
||||
Log::info("订单 {$orderId} 已重新入队,延迟 {$delay} 秒后再通知");
|
||||
} else {
|
||||
Log::error("订单 {$orderId} 回调失败超过最大次数,放弃重试");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 暂停一段时间再检查队列
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟发送回调
|
||||
* @param array $data
|
||||
* @return bool
|
||||
*/
|
||||
protected function notifyPartner($data)
|
||||
{
|
||||
$order = OrderModel::detail($data['order_id']);
|
||||
if(!$order){
|
||||
return false;
|
||||
}
|
||||
$partners = partnersModel::detail($order['partners_id']);
|
||||
if(!$partners){
|
||||
return false;
|
||||
}
|
||||
$partnersOil = new PartnersOil();
|
||||
$data = [
|
||||
'oilCardNo' => $order['oilCardNo'],
|
||||
'amount' => $order['total_price'],
|
||||
'shareNo' => $order['shareNo'],
|
||||
'type' => $data['optType'] == 'share_oil' ? 'add' : 'sub',
|
||||
'optType' => $data['optType'],
|
||||
];
|
||||
$header['method'] = 'syb.notify.oilCard.change';
|
||||
$res = $partnersOil->sendRequest($partners, $partners['callback'], $data, $header);
|
||||
$code = $res['code'] ?? '';
|
||||
if($res && $code == '00'){
|
||||
if($data['optType'] == 'share_oil') {
|
||||
$order->save([
|
||||
'oil_callback' => 1,
|
||||
'oil_callback_time' => time(),
|
||||
]);
|
||||
}else{
|
||||
$order->save([
|
||||
'use_callback' => 1,
|
||||
'use_callback_time' => time(),
|
||||
]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 延迟时间策略
|
||||
*/
|
||||
private function getDelayTime($retryCount)
|
||||
{
|
||||
switch ($retryCount) {
|
||||
case 1: return 60; // 1分钟
|
||||
case 2: return 300; // 5分钟
|
||||
case 3: return 1800; // 30分钟
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
namespace app\console\command;
|
||||
|
||||
use app\common\service\IdingjuOil;
|
||||
use app\common\service\QueueService;
|
||||
use app\store\model\Company as CompanyModel;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use app\common\model\partners\Order as OrderModel;
|
||||
use think\facade\Db;
|
||||
|
||||
class PartnersQueue extends Command
|
||||
{
|
||||
protected $queueService;
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('partners:queue')
|
||||
->setDescription('Process messages from the Redis queue');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$this->queueService = new QueueService();
|
||||
|
||||
while (true) {
|
||||
$message = $this->queueService->pop('partners_queue');
|
||||
if ($message) {
|
||||
// 处理消息
|
||||
$this->processMessage($message);
|
||||
} else {
|
||||
// 暂停一段时间再检查队列
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function processMessage($message)
|
||||
{
|
||||
$IdingjuOil = new IdingjuOil();
|
||||
$IdingjuOil->saveLogStore('partners_queue队列数据处理:【'.$message.'】');
|
||||
// 拿到队列数据处理
|
||||
$data = json_decode($message, true);
|
||||
// 检查解码是否成功
|
||||
if ($data === false) {
|
||||
$IdingjuOil->saveLogStore('partners_queue队列数据解码失败:' . json_last_error_msg());
|
||||
return;
|
||||
}
|
||||
$order = OrderModel::detail($data['order_id']);
|
||||
if (!$order) {
|
||||
$IdingjuOil->saveLogStore('未找到对应订单:【' . $data['order_id'] . '】');
|
||||
return;
|
||||
}
|
||||
// 此处可以根据业务逻辑处理接收到的数据
|
||||
try {
|
||||
if($order['order_status'] == 10 && $order['oil_status'] == 0 && $order['oilCardType'] == 'zsy_ticket'){
|
||||
$data['amount'] = $order['total_price']; // 金额
|
||||
$company = CompanyModel::detail(['orgNo' => $this->getCompany($order['goods_no'])]);
|
||||
if (!$company) {
|
||||
$IdingjuOil->saveLogStore('没有可用参数:【' . $order['goods_no'] . '】');
|
||||
return;
|
||||
}
|
||||
|
||||
// 对接平台接口
|
||||
$res = $IdingjuOil->oilOilCardShare([
|
||||
'goodsNo'=>$order['goods_no'],
|
||||
'amount'=>$data['amount'],
|
||||
'userNo'=>'',
|
||||
'mobile'=>$order['mobile'],
|
||||
'saleOrderNo'=>$order['oilCardNo'],
|
||||
'oilCardType'=>$order['oilCardType'],
|
||||
'notify_url'=>config('app.base_url') . 'partnersoil.php',
|
||||
'use_notify_url'=>config('app.base_url') . 'partnersoiluse.php'
|
||||
], $company);
|
||||
|
||||
if ($res['code'] != 1) {
|
||||
$IdingjuOil->saveLogStore(($res['errMessage'] ?? $res['message']));
|
||||
return;
|
||||
}
|
||||
$item['shareNo'] = $res['data']['trade_no'] ?? '';
|
||||
$item['qrCode'] = $res['data']['useUrl'] ?? '';
|
||||
$item['oil_status_zsy'] = $res['data']['state'] ?? 0;
|
||||
$item['oil_time'] = time();
|
||||
$item['oil_status'] = $item['oil_status_zsy'] == 2 ? 1 : 0; // 已发券 为1
|
||||
$resUpdate = $order->save($item);
|
||||
if (!$resUpdate) {
|
||||
$IdingjuOil->saveLogStore("订单更新失败,order_id: {$data['order_id']}");
|
||||
}
|
||||
if($item['oil_status_zsy'] == 2) {
|
||||
// 存入队列处理
|
||||
$queueService = new QueueService();
|
||||
$queueService->push('notify_queue', json_encode([
|
||||
'order_id' => $order['order_id'],
|
||||
'type' => 'add',
|
||||
'optType' => 'share_oil',
|
||||
'retry_count' => 0,
|
||||
], 320));
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$IdingjuOil->saveLogStore(
|
||||
'partners_queue队列处理异常:' . $e->getMessage() .
|
||||
' 在文件:' . $e->getFile() .
|
||||
' 第:' . $e->getLine() .
|
||||
' 行。堆栈跟踪:' . $e->getTraceAsString()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $goodsNo
|
||||
* @return string
|
||||
*/
|
||||
public function getCompany($goodsNo): string
|
||||
{
|
||||
switch ($goodsNo) {
|
||||
case 'djgdface100':
|
||||
case 'djgdface200':
|
||||
case 'djgdface300':
|
||||
return 'umQZhIHKofyA';
|
||||
case 'jmgxyyface100':
|
||||
case 'jmgxyyface200':
|
||||
case 'jmgxyyface300':
|
||||
|
||||
case 'jmgzface100':
|
||||
case 'jmgzface200':
|
||||
case 'jmgzface300':
|
||||
|
||||
case 'jmfjface100':
|
||||
case 'jmfjface200':
|
||||
case 'jmfjface300':
|
||||
return 'HpWqXIgYvTBg';
|
||||
case 'face003':
|
||||
return 'NKJBgaaRNdiL'; // 测试参数
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
namespace app\console\command;
|
||||
|
||||
use app\common\service\QueueService;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use app\store\model\Account as AccountModel;
|
||||
use app\store\model\Company as CompanyModel;
|
||||
use app\common\service\SybOil;
|
||||
|
||||
class ProcessQueue extends Command
|
||||
{
|
||||
protected $queueService;
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('process:queue')
|
||||
->setDescription('Process messages from the Redis queue');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$this->queueService = new QueueService();
|
||||
|
||||
while (true) {
|
||||
$message = $this->queueService->pop('account_queue');
|
||||
if ($message) {
|
||||
// 处理消息
|
||||
$this->processMessage($message);
|
||||
} else {
|
||||
// 暂停一段时间再检查队列
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function processMessage($message)
|
||||
{
|
||||
$SybOil = new SybOil();
|
||||
// 在这里处理回调数据
|
||||
$SybOil->saveLogStore('account_queue队列数据处理:【'.$message.'】');
|
||||
$account = AccountModel::detail($message);
|
||||
$company = CompanyModel::detail($account['company_id']);
|
||||
// 对接平台接口
|
||||
$res = $SybOil->userOptAdd(['mobile'=>$account['mobile'],'userName'=>$account['userName'],'userIdCard'=>$account['userIdCard'],'userType'=>$account['userType']], $company);
|
||||
if($res['code'] == '00' && isset($res['data']['userNo'])){
|
||||
$data['userNo'] = $res['data']['userNo'];
|
||||
$account->edit($data);
|
||||
}else{
|
||||
$SybOil->saveLogStore('平台创建用户失败【'.($res['errMessage'] ?? $res['message']).'】');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 萤火科技 <admin@yiovo.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\console\command;
|
||||
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Argument;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\facade\Event;
|
||||
use Workerman\Worker;
|
||||
|
||||
/**
|
||||
* 定时器 (Workerman)
|
||||
* 用于执行系统里的定时任务, 例如自动确认收货
|
||||
* 使用方法: 打开命令行 - 执行命令: php think timer start
|
||||
* Class Timer
|
||||
* @package app\common\command
|
||||
*/
|
||||
class Timer extends Command
|
||||
{
|
||||
// 定时器句柄/ID
|
||||
protected $timer;
|
||||
|
||||
// 时间间隔 (单位: 秒, 默认5秒)
|
||||
protected $interval = 1;
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
// 指令配置
|
||||
$this->setName('timer')
|
||||
->addArgument('status', Argument::REQUIRED, 'start/stop/reload/status/connections')
|
||||
->addOption('d', null, Option::VALUE_NONE, 'daemon(守护进程)方式启动')
|
||||
->addOption('i', null, Option::VALUE_OPTIONAL, '多长时间执行一次')
|
||||
->setDescription('start/stop/restart 定时任务');
|
||||
}
|
||||
|
||||
protected function init(Input $input, Output $output)
|
||||
{
|
||||
global $argv;
|
||||
|
||||
if ($input->hasOption('i'))
|
||||
$this->interval = floatval($input->getOption('i'));
|
||||
|
||||
$argv[1] = $input->getArgument('status') ?: 'start';
|
||||
if ($input->hasOption('d')) {
|
||||
$argv[2] = '-d';
|
||||
} else {
|
||||
unset($argv[2]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建定时器
|
||||
* @param Input $input
|
||||
* @param Output $output
|
||||
* @return int|void|null
|
||||
*/
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$this->init($input, $output);
|
||||
// 创建定时器任务
|
||||
$worker = new Worker();
|
||||
$worker->onWorkerStart = [$this, 'start'];
|
||||
$worker->runAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 定时器执行的内容
|
||||
* @return false|int
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
// 每隔n秒执行一次
|
||||
return $this->timer = \Workerman\Lib\Timer::add($this->interval, function () {
|
||||
try {
|
||||
// 这里执行系统预设的定时任务事件
|
||||
echo 'timer...' . PHP_EOL;
|
||||
Event::trigger('StoreTask');
|
||||
} catch (\Throwable $e) {
|
||||
echo 'ERROR: ' . $e->getMessage() . PHP_EOL;
|
||||
$this->stop();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止/删除定时器
|
||||
* @return bool
|
||||
*/
|
||||
public function stop(): bool
|
||||
{
|
||||
return \Workerman\Lib\Timer::del($this->timer);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user