multi-store/process/Task.php

67 lines
2.5 KiB
PHP

<?php
namespace process;
use app\common\enum\OrderEnum;
use app\common\logic\PayNotifyLogic;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_order\StoreOrder;
use app\common\model\store_order_cart_info\StoreOrderCartInfo;
use app\common\model\store_product\StoreProduct;
use app\common\model\user_recharge\UserRecharge;
use think\facade\Db;
use Webman\RedisQueue\Redis;
use Workerman\Crontab\Crontab;
use app\common\service\pay\PayService;
class Task
{
public function onWorkerStart()
{
// 每10分钟执行一次
new Crontab('0 */10 * * * *', function () {
$where = ['paid' => 0];
$where[] = ['create_time', '<', time() - 600]; // 10分钟前创建的订单
$where[] = ['source', '<=',1];
// 删除10分钟未支付的订单
$oid = StoreOrder::where($where)->column('id'); // 删除时间设置为当前时间,即删除
if ($oid) {
StoreOrder::where('id', 'in', $oid)->update(['delete_time' => time()]);
StoreOrderCartInfo::where('oid','in',$oid)->update(['status'=>OrderEnum::REFUND_STATUS_FINISH]);
}
// 获取当前时间
$now = time();
// 计算一个小时前的时间戳
$oneHourAgo = $now - 3600;
//删除未充值的订单
UserRecharge::where('paid', 0)->where('status', 1)->where('create_time', '<', $oneHourAgo)->update(['delete_time' => time()]);
});
//每10秒执行一次
new Crontab('*/10 * * * * *', function () {
$select=Db::name('wechat_micropay')->limit(100)->select();
$pay = new PayService();
foreach ($select as $k=>$v) {
$time = time();
if ($time - $v['create_time'] > 60){
Db::name('wechat_micropay')->where('id',$v['id'])->delete();
continue;
}
$order = [
'out_trade_no' => $v['order_id'],
];
$res = $pay->wechat->query($order);
if ($res['trade_state'] == 'SUCCESS' && $res['trade_state_desc'] == '支付成功') {
if(isset($data['pay_type']) && $data['pay_type']=='recharge'){
PayNotifyLogic::handle('recharge', $res['out_trade_no'], $res);
}else{
PayNotifyLogic::handle('wechat_common', $res['out_trade_no'], $res);
}
}
}
});
}
}