lararvel 定时任务

42 篇文章 1 订阅

1,创建要定时的任务文件

php artisan make:command AutoReceipt

2 查看创建的文件并写入代码

3,写入命令名和业务代码 

<?php

namespace App\Console\Commands;

use App\Http\Adapter\Pay\HePayCheckAdapter;
use App\Http\Adapter\Pay\PayAdapter;
use App\Http\Adapter\Pay\ReceivePayAdapter;
use App\Model\Source\Member\Member;
use App\Model\Source\Order\Order;
use App\Model\Source\Shop\Shop;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use vakata\database\Exception;

class AutoReceipt extends Command
{
    /**
     * 这里是命令名 重要,在Kernel.php 里需要用到
     *
     * @var string
     */
    protected $signature = 'command:auto_receipt';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '自动收货';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //这里是业务代码
        try {
            $shop_id = 0;
            DB::beginTransaction();

            $date = date('Y-m-d H:i:s', time());
            $res_tmp = Order::with(['toGoods'])->where('send_at', '<', $date)->where('status', 2)->where('is_refund', 0)->get();

            $res_tmp = $res_tmp->toArray();
            foreach ($res_tmp as $v) {
                $user_id = $v['user_id'];
                $user = Member::find($user_id);
                if (is_array($v['to_goods'])) {
                    $shop_id = $v['to_goods'][0]['shop_id'];
                } else {
                    $shop_id = $v['to_goods']['shop_id'];
                };
                if (!$shop_id) {
                    Log::error($v['order_sn'] . ":商家信息异常");
                    DB::rollBack();
                    continue;
                }
                $shop = Shop::find($shop_id);
                if (!$shop) {
                    Log::error($v['order_sn'] . ":商家信息异常!");
                    DB::rollBack();
                    continue;
                }

                $order_pay_data = [
                    'from_user' => $user['member_id'],
                    'to_user' => $shop['he_id'],
                    'amount' => $v['order_true_pay']
                ];
                $pay_status = new PayAdapter(new ReceivePayAdapter());
                $pay_refund_status = $pay_status->pay($order_pay_data);
                if (!$pay_refund_status->code) {
                    Log::error($v['order_sn'] . ':' . $pay_refund_status->msg);
                    DB::rollBack();
                    continue;
                }

                $res = Order::where('user_id', $user_id)->find($v['id']);

                $res->status = 3;
                if ($res->save()) {
                    DB::commit();
                    Log::error($v['order_sn'] . ':自动收货成功');
                } else {
                    DB::rollBack();
                    Log::error($v['order_sn'] . ':失败');
                    continue;
                };
            }

        } catch (Exception $e) {
            Log::error($v['order_sn'] . ':' . $e->getMessage());
            return false;
        }

    }
}

4,查看创建的命令

php artisan list

5,启用计划任务:在服务器中加入到计划任务 crontab -e

//每分钟执行一次
*/1 * * * * /www/server/php/73/bin/php  /www/wwwroot/blog/artisan schedule:run >> /dev/null 2>&1

6,laravel log文件里查看具体执行情况

注意问题   如果时间不对,可能是linux 或 laravel时间设置有问题

linux 可以使用date -R 查看

lararvel 的话可以查看 config/app.php 里的 

'timezone' => 'PRC', 

设置成本地时区

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值