订单优化
parent
9edfc4c9f6
commit
fe7aa0b555
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace App\Constants;
|
||||
|
||||
class OrderStatus
|
||||
{
|
||||
public const UNKNOWN = -1; // 未知
|
||||
public const PENDING = 0; // 待付款
|
||||
public const PAID = 1; // 已付款/待发货
|
||||
public const SHIPPING = 2; // 发货中
|
||||
public const SHIPPED = 3; // 已发货
|
||||
public const COMPLETED = 9; // 已完成
|
||||
public const CANCELLED = 10; // 已取消
|
||||
}
|
||||
|
|
@ -131,16 +131,20 @@ class OrderController extends Controller
|
|||
*/
|
||||
public function confirm($id, Request $request)
|
||||
{
|
||||
$order = $request->user()->orders()->findOrFail($id);
|
||||
$user = $request->user();
|
||||
|
||||
if (! $order->isConfirmable()) {
|
||||
throw new BizException('订单不可确认');
|
||||
}
|
||||
DB::transaction(function () use ($id, $user) {
|
||||
$order = $user->orders()->lockForUpdate()->findOrFail($id);
|
||||
|
||||
$order->update([
|
||||
'status' => Order::STATUS_COMPLETED,
|
||||
'completed_at' => now(),
|
||||
]);
|
||||
if (! $order->isConfirmable()) {
|
||||
throw new BizException('订单状态异常');
|
||||
}
|
||||
|
||||
$order->update([
|
||||
'status' => Order::STATUS_COMPLETED,
|
||||
'completed_at' => now(),
|
||||
]);
|
||||
});
|
||||
|
||||
return response()->noContent();
|
||||
}
|
||||
|
|
@ -154,15 +158,19 @@ class OrderController extends Controller
|
|||
*/
|
||||
public function cancel($id, Request $request)
|
||||
{
|
||||
$order = $request->user()->orders()->findOrFail($id);
|
||||
$user = $request->user();
|
||||
|
||||
if (! $order->isCancelable()) {
|
||||
throw new BizException('订单不可取消');
|
||||
}
|
||||
DB::transaction(function () use ($id, $user) {
|
||||
$order = $user->orders()->lockForUpdate()->findOrFail($id);
|
||||
|
||||
$order->update([
|
||||
'status' => Order::STATUS_CANCELLED,
|
||||
]);
|
||||
if (! $order->isCancelable()) {
|
||||
throw new BizException('订单状态异常');
|
||||
}
|
||||
|
||||
$order->update([
|
||||
'status' => Order::STATUS_CANCELLED,
|
||||
]);
|
||||
});
|
||||
|
||||
return response()->noContent();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ class OrderResource extends JsonResource
|
|||
'shipping_fee' => $this->shipping_fee_format,
|
||||
'products_total_amount' => $this->products_total_amount_format,
|
||||
'total_amount' => $this->total_amount_format,
|
||||
'status' => $this->status,
|
||||
'status' => $this->order_status,
|
||||
'note' => (string) $this->note,
|
||||
'consignee_name' => $this->consignee_name,
|
||||
'consignee_telephone' => $this->consignee_telephone,
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ class OrderResourceCollection extends ResourceCollection
|
|||
'id' => $item->id,
|
||||
'sn' => $item->sn,
|
||||
'total_amount' => $item->total_amount_format,
|
||||
'status' => $item->status,
|
||||
'status' => $item->order_status,
|
||||
'created_date' => $item->created_at->toDateString(),
|
||||
'products' => OrderProductResource::collection($item->whenLoaded('products')),
|
||||
'expires_at' => $item->expires_at,
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Constants\OrderStatus;
|
||||
use App\Helpers\Numeric;
|
||||
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
||||
use EloquentFilter\Filterable;
|
||||
|
|
@ -120,7 +121,7 @@ class Order extends Model
|
|||
*/
|
||||
public function isConfirmable(): bool
|
||||
{
|
||||
return $this->isPaid();
|
||||
return $this->status === static::STATUS_PAID;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -130,31 +131,8 @@ class Order extends Model
|
|||
*/
|
||||
public function isCancelable(): bool
|
||||
{
|
||||
if ($this->isPaid()) {
|
||||
return $this->shipping_state === static::SHIPPING_STATE_PENDING;
|
||||
}
|
||||
|
||||
return $this->isPending();
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认此订单是否是待付款
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isPending(): bool
|
||||
{
|
||||
return $this->status === static::STATUS_PENDING;
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认此订单是否是已付款
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isPaid(): bool
|
||||
{
|
||||
return $this->status === static::STATUS_PENDING;
|
||||
return in_array($this->status, [static::STATUS_PENDING, static::STATUS_PAID])
|
||||
&& $this->shipping_state === static::SHIPPING_STATE_PENDING;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -232,4 +210,38 @@ class Order extends Model
|
|||
$this->created_at->addSeconds(1800), false
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单状态
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getOrderStatusAttribute(): int
|
||||
{
|
||||
$status = OrderStatus::UNKNOWN;
|
||||
|
||||
if ($this->status === static::STATUS_PENDING) {
|
||||
$status = OrderStatus::PENDING;
|
||||
} elseif ($this->status === static::STATUS_PAID) {
|
||||
switch ($this->shipping_state) {
|
||||
case static::SHIPPING_STATE_PENDING:
|
||||
$status = OrderStatus::PAID;
|
||||
break;
|
||||
|
||||
case static::SHIPPING_STATE_PROCESSING:
|
||||
$status = OrderStatus::SHIPPING;
|
||||
break;
|
||||
|
||||
case static::SHIPPING_STATE_PROCESSED:
|
||||
$status = OrderStatus::SHIPPED;
|
||||
break;
|
||||
}
|
||||
} elseif ($this->status === static::STATUS_COMPLETED) {
|
||||
$status = OrderStatus::COMPLETED;
|
||||
} elseif ($this->status === static::STATUS_CANCELLED) {
|
||||
$status = OrderStatus::CANCELLED;
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue