6
0
Fork 0
release
李静 2021-12-17 16:52:15 +08:00
parent bb91f7a727
commit fc4040d820
4 changed files with 88 additions and 76 deletions

View File

@ -8,7 +8,7 @@ class OrderStatus
public const PENDING = 0; // 待付款 public const PENDING = 0; // 待付款
public const PAID = 1; // 已付款/待发货 public const PAID = 1; // 已付款/待发货
public const SHIPPING = 2; // 发货中 public const SHIPPING = 2; // 发货中
public const SHIPPED = 3; // 已发货 public const SHIPPED = 3; // 已发货/待收货
public const COMPLETED = 9; // 已完成 public const COMPLETED = 9; // 已完成
public const CANCELLED = 10; // 已取消 public const CANCELLED = 10; // 已取消
} }

View File

@ -7,7 +7,6 @@ use App\Endpoint\Api\Http\Resources\OrderResource;
use App\Endpoint\Api\Http\Resources\OrderResourceCollection; use App\Endpoint\Api\Http\Resources\OrderResourceCollection;
use App\Exceptions\BizException; use App\Exceptions\BizException;
use App\Helpers\Paginator as PaginatorHelper; use App\Helpers\Paginator as PaginatorHelper;
use App\Models\Order;
use App\Services\OrderService; use App\Services\OrderService;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -131,20 +130,7 @@ class OrderController extends Controller
*/ */
public function confirm($id, Request $request) 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(); return response()->noContent();
} }
@ -158,19 +144,7 @@ class OrderController extends Controller
*/ */
public function cancel($id, Request $request) public function cancel($id, Request $request)
{ {
$user = $request->user(); // todo 取消订单
DB::transaction(function () use ($id, $user) {
$order = $user->orders()->lockForUpdate()->findOrFail($id);
if (! $order->isCancelable()) {
throw new BizException('订单状态异常');
}
$order->update([
'status' => Order::STATUS_CANCELLED,
]);
});
return response()->noContent(); return response()->noContent();
} }

View File

@ -22,10 +22,6 @@ class Order extends Model
public const SHIPPING_STATE_PROCESSING = 1; // 发货中 public const SHIPPING_STATE_PROCESSING = 1; // 发货中
public const SHIPPING_STATE_PROCESSED = 2; // 已完成 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 * @var array
*/ */
@ -69,15 +65,6 @@ class Order extends Model
'completed_at', '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() 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 * @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 * @return bool
*/ */
public function isCancelable(): bool public function isShipping(): bool
{ {
return in_array($this->status, [static::STATUS_PENDING, static::STATUS_PAID]) return $this->status === static::STATUS_PAID
&& $this->shipping_state === static::SHIPPING_STATE_PENDING; && $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() public function getExpiresAtAttribute()
{ {
if ($this->status !== static::STATUS_PENDING) { if (! $this->isPending()) {
return 0; return 0;
} }
return now()->diffInSeconds( $seconds = now()->diffInSeconds(
$this->created_at->addSeconds(1800), false $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 public function getOrderStatusAttribute(): int
{ {
$status = OrderStatus::UNKNOWN; // 待付款
if ($this->isPending()) {
if ($this->status === static::STATUS_PENDING) { return OrderStatus::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; // 已付款(待发货)
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;
} }
} }

View File

@ -0,0 +1,9 @@
<?php
return [
// 订单支付过期时间(秒)
'order_payment_expires_at' => 1800,
// 售后过期时间(天)
'sale_after_expire_days' => 7,
];