项目需要定时清理长时间未支付的订单。可用定时任务解决。
第一步,配置command.php文件,目录在application/command.php
<?php
return [
'app\home\command\OrderCancel',
];
第二步,建立命令类文件,新建application/home/command/OrderCancel.php
<?php
namespace app\home\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\api\model\Order;
class OrderCancel extends Command
{
protected function configure()
{
$this->setName('order_cancel')->setDescription('订单不支付取消命令');
}
protected function execute(Input $input, Output $output)
{
//获取符合取消条件的订单
$order_lists = Order::where('status',1)->where('createtime','<',(time()-3600))->column('id');
if(!empty($order_lists)){
$orderModel = new Order();
foreach ($order_lists as $key=>$value){
//取消订单逻辑
$orderModel->cancelOrder($value);
}
}
$output->writeln('执行成功');
}
}
第三步,测试-命令帮助-命令行下运行
php think order_cancel
第四步宝塔添加Sell命令
cd /data/wwwroot/xxxx.cn
php think order_cancel
这样就实现定时取消过期订单的功能。