订单优化
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)
|
public function confirm($id, Request $request)
|
||||||
{
|
{
|
||||||
$order = $request->user()->orders()->findOrFail($id);
|
$user = $request->user();
|
||||||
|
|
||||||
if (! $order->isConfirmable()) {
|
DB::transaction(function () use ($id, $user) {
|
||||||
throw new BizException('订单不可确认');
|
$order = $user->orders()->lockForUpdate()->findOrFail($id);
|
||||||
}
|
|
||||||
|
|
||||||
$order->update([
|
if (! $order->isConfirmable()) {
|
||||||
'status' => Order::STATUS_COMPLETED,
|
throw new BizException('订单状态异常');
|
||||||
'completed_at' => now(),
|
}
|
||||||
]);
|
|
||||||
|
$order->update([
|
||||||
|
'status' => Order::STATUS_COMPLETED,
|
||||||
|
'completed_at' => now(),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
return response()->noContent();
|
return response()->noContent();
|
||||||
}
|
}
|
||||||
|
|
@ -154,15 +158,19 @@ class OrderController extends Controller
|
||||||
*/
|
*/
|
||||||
public function cancel($id, Request $request)
|
public function cancel($id, Request $request)
|
||||||
{
|
{
|
||||||
$order = $request->user()->orders()->findOrFail($id);
|
$user = $request->user();
|
||||||
|
|
||||||
if (! $order->isCancelable()) {
|
DB::transaction(function () use ($id, $user) {
|
||||||
throw new BizException('订单不可取消');
|
$order = $user->orders()->lockForUpdate()->findOrFail($id);
|
||||||
}
|
|
||||||
|
|
||||||
$order->update([
|
if (! $order->isCancelable()) {
|
||||||
'status' => Order::STATUS_CANCELLED,
|
throw new BizException('订单状态异常');
|
||||||
]);
|
}
|
||||||
|
|
||||||
|
$order->update([
|
||||||
|
'status' => Order::STATUS_CANCELLED,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
return response()->noContent();
|
return response()->noContent();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ class OrderResource extends JsonResource
|
||||||
'shipping_fee' => $this->shipping_fee_format,
|
'shipping_fee' => $this->shipping_fee_format,
|
||||||
'products_total_amount' => $this->products_total_amount_format,
|
'products_total_amount' => $this->products_total_amount_format,
|
||||||
'total_amount' => $this->total_amount_format,
|
'total_amount' => $this->total_amount_format,
|
||||||
'status' => $this->status,
|
'status' => $this->order_status,
|
||||||
'note' => (string) $this->note,
|
'note' => (string) $this->note,
|
||||||
'consignee_name' => $this->consignee_name,
|
'consignee_name' => $this->consignee_name,
|
||||||
'consignee_telephone' => $this->consignee_telephone,
|
'consignee_telephone' => $this->consignee_telephone,
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ class OrderResourceCollection extends ResourceCollection
|
||||||
'id' => $item->id,
|
'id' => $item->id,
|
||||||
'sn' => $item->sn,
|
'sn' => $item->sn,
|
||||||
'total_amount' => $item->total_amount_format,
|
'total_amount' => $item->total_amount_format,
|
||||||
'status' => $item->status,
|
'status' => $item->order_status,
|
||||||
'created_date' => $item->created_at->toDateString(),
|
'created_date' => $item->created_at->toDateString(),
|
||||||
'products' => OrderProductResource::collection($item->whenLoaded('products')),
|
'products' => OrderProductResource::collection($item->whenLoaded('products')),
|
||||||
'expires_at' => $item->expires_at,
|
'expires_at' => $item->expires_at,
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Constants\OrderStatus;
|
||||||
use App\Helpers\Numeric;
|
use App\Helpers\Numeric;
|
||||||
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
||||||
use EloquentFilter\Filterable;
|
use EloquentFilter\Filterable;
|
||||||
|
|
@ -120,7 +121,7 @@ class Order extends Model
|
||||||
*/
|
*/
|
||||||
public function isConfirmable(): bool
|
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
|
public function isCancelable(): bool
|
||||||
{
|
{
|
||||||
if ($this->isPaid()) {
|
return in_array($this->status, [static::STATUS_PENDING, static::STATUS_PAID])
|
||||||
return $this->shipping_state === static::SHIPPING_STATE_PENDING;
|
&& $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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -232,4 +210,38 @@ class Order extends Model
|
||||||
$this->created_at->addSeconds(1800), false
|
$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