作者 何书鹏

待支付取消订单队列

<?php
namespace app\api\job;
use think\queue\Job;
/**
* BaseJob 基类
*/
class BaseJob
{
public function failed($data){
// 记录日志
\think\Db::name('jobs_failed')->insert([
'data' => json_encode($data),
'createtime' => time(),
'updatetime' => time()
]);
}
}
\ No newline at end of file
... ...
<?php
namespace app\api\job;
use think\queue\Job;
/**
* 发送优惠券自动操作
*/
class OrderAutoOper extends BaseJob
{
/**
* 订单自动关闭
*/
public function autoClose(Job $job, $data){
try {
$order_id = $data['order_id'];
$order = \app\api\model\Order::get($order_id);
if ($order && $order['pay_status'] == '10' && $order['order_status'] == '10') {
\think\Db::transaction(function () use ($order) {
(new \app\api\controller\Order)->cancelOrderAction($order);
});
}
// 删除 job
$job->delete();
} catch (\Exception $e) {
// 队列执行失败
\think\Log::write('queue-' . get_class() . '-autoClose' . ':执行失败,错误信息:' . $e->getMessage());
}
}
}
... ...