Skip to content

Commit

Permalink
加入三十天后自动收货的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
no-serve-people committed Dec 13, 2018
1 parent ac3fec8 commit 658e002
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/Admin/Controllers/OrdersController.php
Expand Up @@ -4,6 +4,8 @@

use App\Exceptions\InvalidRequestException;
use App\Http\Requests\Request;
use App\Jobs\AutoReceive;
use App\Jobs\CloseOrder;
use App\Models\Order;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
Expand Down Expand Up @@ -72,6 +74,7 @@ public function ship(Order $order, Request $request)
'ship_data' => $data,
]);
// 返回上一页
$this->dispatch(new AutoReceive($order,config('app.auto_receive_ttl')));
return redirect()->back();
}

Expand Down
46 changes: 46 additions & 0 deletions app/Jobs/AutoReceive.php
@@ -0,0 +1,46 @@
<?php

namespace App\Jobs;

use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

// 代表这个类需要被放到队列中执行,而不是触发时立即执行
class AutoReceive implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $order;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Order $order, $delay)
{
$this->order = $order;
// 设置延迟的时间,delay() 方法的参数代表多少秒之后执行
$this->delay($delay);
}

/**
* Execute the job.
* 定义这个任务类具体的执行逻辑
* 当队列处理器从队列中取出任务时,会调用 handle() 方法
* @return void
*/
public function handle()
{
// 判断对应的订单是否已经被收货
// 如果已经收货则不需要确认收货,直接退出
if ($this->order->ship_status === Order::SHIP_STATUS_RECEIVED) {
return;
}
// 将订单的 ship_status 字段标记为 received,即确认收货
$this->order->update(['ship_status' => Order::SHIP_STATUS_RECEIVED]);
}
}
2 changes: 2 additions & 0 deletions config/app.php
Expand Up @@ -231,4 +231,6 @@
'faker_locale' => 'zh_CN',
//关闭订单任务延时时间 30分钟
'order_ttl' => 1800,
// 30天自动确认收货
'auto_receive_ttl' => 2592000,
];

0 comments on commit 658e002

Please sign in to comment.