diff --git a/app/Constants/OrderStatus.php b/app/Constants/OrderStatus.php index 0874006d..40144680 100644 --- a/app/Constants/OrderStatus.php +++ b/app/Constants/OrderStatus.php @@ -8,7 +8,7 @@ class OrderStatus public const PENDING = 0; // 待付款 public const PAID = 1; // 已付款/待发货 public const SHIPPING = 2; // 发货中 - public const SHIPPED = 3; // 已发货 + public const SHIPPED = 3; // 已发货/待收货 public const COMPLETED = 9; // 已完成 public const CANCELLED = 10; // 已取消 } diff --git a/app/Endpoint/Api/Http/Controllers/Order/OrderController.php b/app/Endpoint/Api/Http/Controllers/Order/OrderController.php index 53ee2422..842f3c62 100644 --- a/app/Endpoint/Api/Http/Controllers/Order/OrderController.php +++ b/app/Endpoint/Api/Http/Controllers/Order/OrderController.php @@ -7,7 +7,6 @@ use App\Endpoint\Api\Http\Resources\OrderResource; use App\Endpoint\Api\Http\Resources\OrderResourceCollection; use App\Exceptions\BizException; use App\Helpers\Paginator as PaginatorHelper; -use App\Models\Order; use App\Services\OrderService; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Http\Request; @@ -131,20 +130,7 @@ class OrderController extends Controller */ public function confirm($id, Request $request) { - $user = $request->user(); - - DB::transaction(function () use ($id, $user) { - $order = $user->orders()->lockForUpdate()->findOrFail($id); - - if (! $order->isConfirmable()) { - throw new BizException('订单状态异常'); - } - - $order->update([ - 'status' => Order::STATUS_COMPLETED, - 'completed_at' => now(), - ]); - }); + // 确认收货 return response()->noContent(); } @@ -158,19 +144,7 @@ class OrderController extends Controller */ public function cancel($id, Request $request) { - $user = $request->user(); - - DB::transaction(function () use ($id, $user) { - $order = $user->orders()->lockForUpdate()->findOrFail($id); - - if (! $order->isCancelable()) { - throw new BizException('订单状态异常'); - } - - $order->update([ - 'status' => Order::STATUS_CANCELLED, - ]); - }); + // todo 取消订单 return response()->noContent(); } diff --git a/app/Models/Order.php b/app/Models/Order.php index 21cf1310..e06a2cd4 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -22,10 +22,6 @@ class Order extends Model public const SHIPPING_STATE_PROCESSING = 1; // 发货中 public const SHIPPING_STATE_PROCESSED = 2; // 已完成 - public const PAY_WAY_ALIPAY = 'alipay'; - public const PAY_WAY_WXPAY = 'wxpay'; - public const PAY_WAY_BALANCE = 'balance'; - /** * @var array */ @@ -69,15 +65,6 @@ class Order extends Model 'completed_at', ]; - /** - * @var array - */ - public static $payWayTexts = [ - self::PAY_WAY_ALIPAY => '支付宝', - self::PAY_WAY_WXPAY => '微信支付', - self::PAY_WAY_BALANCE => '余额', - ]; - /** * 下单人 * @@ -103,7 +90,8 @@ class Order extends Model */ public function scopeExpired() { - return $this->where('status', static::STATUS_PENDING)->where('created_at', '<=', now()->subSeconds(1800)); + return $this->where('status', static::STATUS_PENDING) + ->where('created_at', '<=', now()->subSeconds(config('settings.order_payment_expires_at'))); } /** @@ -125,24 +113,56 @@ class Order extends Model } /** - * 确认此订单是否可以被确认 + * 确认此订单是否是待发货 * * @return bool */ - public function isConfirmable(): bool + public function isPaid(): bool { - return $this->status === static::STATUS_PAID; + return $this->status === static::STATUS_PAID + && $this->shipping_state === static::SHIPPING_STATE_PENDING; } /** - * 确认此订单是否可以被取消 + * 确认此订单是否是发货中 * * @return bool */ - public function isCancelable(): bool + public function isShipping(): bool { - return in_array($this->status, [static::STATUS_PENDING, static::STATUS_PAID]) - && $this->shipping_state === static::SHIPPING_STATE_PENDING; + return $this->status === static::STATUS_PAID + && $this->shipping_state === static::SHIPPING_STATE_PROCESSING; + } + + /** + * 确认此订单是否是已发货 + * + * @return bool + */ + public function isShipped(): bool + { + return $this->status === static::STATUS_PAID + && $this->shipping_state === static::SHIPPING_STATE_PROCESSED; + } + + /** + * 确认此订单是否是已完成 + * + * @return bool + */ + public function isCompleted(): bool + { + return $this->status === static::STATUS_COMPLETED; + } + + /** + * 确认此订单是否是已取消 + * + * @return bool + */ + public function isCancelled(): bool + { + return $this->status === static::STATUS_CANCELLED; } /** @@ -212,13 +232,15 @@ class Order extends Model */ public function getExpiresAtAttribute() { - if ($this->status !== static::STATUS_PENDING) { + if (! $this->isPending()) { return 0; } - return now()->diffInSeconds( - $this->created_at->addSeconds(1800), false + $seconds = now()->diffInSeconds( + $this->created_at->addSeconds(config('settings.order_payment_expires_at')), false ); + + return $seconds > 0 ? $seconds : 0; } /** @@ -228,30 +250,37 @@ class Order extends Model */ 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; + // 待付款 + if ($this->isPending()) { + return OrderStatus::PENDING; } - return $status; + // 已付款(待发货) + if ($this->isPaid()) { + return OrderStatus::PAID; + } + + // 发货中 + if ($this->isShipping()) { + return OrderStatus::SHIPPING; + } + + // 已发货 + if ($this->isShipped()) { + return OrderStatus::SHIPPED; + } + + // 已完成 + if ($this->isCompleted()) { + return OrderStatus::COMPLETED; + } + + // 已取消 + if ($this->isCancelled()) { + return OrderStatus::CANCELLED; + } + + // 其它 + return OrderStatus::UNKNOWN; } } diff --git a/config/settings.php b/config/settings.php new file mode 100644 index 00000000..cbde7e09 --- /dev/null +++ b/config/settings.php @@ -0,0 +1,9 @@ + 1800, + + // 售后过期时间(天) + 'sale_after_expire_days' => 7, +];