订单详情
parent
4bfef1d220
commit
482d9fdafa
|
|
@ -3,7 +3,8 @@
|
|||
namespace App\Endpoint\Api\Http\Controllers\Order;
|
||||
|
||||
use App\Endpoint\Api\Http\Controllers\Controller;
|
||||
use App\Endpoint\Api\Http\Resources\OrderSimpleResource;
|
||||
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;
|
||||
|
|
@ -31,7 +32,7 @@ class OrderController extends Controller
|
|||
->latest('id')
|
||||
->simplePaginate($perPage);
|
||||
|
||||
return OrderSimpleResource::collection($orders);
|
||||
return OrderResourceCollection::make($orders);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -103,7 +104,22 @@ class OrderController extends Controller
|
|||
throw new BizException('系统繁忙,请稍后再试');
|
||||
}
|
||||
|
||||
return OrderSimpleResource::make($order);
|
||||
return OrderResource::make($order);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看订单
|
||||
*
|
||||
* @param int $id
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function show($id, Request $request)
|
||||
{
|
||||
$order = $request->user()->orders()->findOrFail($id);
|
||||
$order->load('products');
|
||||
|
||||
return OrderResource::make($order);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace App\Endpoint\Api\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class OrderResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'sn' => $this->sn,
|
||||
'products' => OrderProductResource::collection($this->whenLoaded('products')),
|
||||
'coupon_discount_amount' => $this->coupon_discount_amount_format,
|
||||
'vip_discount_amount' => $this->vip_discount_amount_format,
|
||||
'reduced_amount' => $this->reduced_amount_format,
|
||||
'shipping_fee' => $this->shipping_fee_format,
|
||||
'products_total_amount' => $this->products_total_amount_format,
|
||||
'total_amount' => $this->total_amount_format,
|
||||
'status' => $this->status,
|
||||
'note' => (string) $this->note,
|
||||
'consignee_name' => $this->consignee_name,
|
||||
'consignee_telephone' => $this->consignee_telephone,
|
||||
'consignee_zone' => $this->consignee_zone,
|
||||
'consignee_address' => $this->consignee_address,
|
||||
'pay_way' => (string) $this->pay_way,
|
||||
'pay_at' => (string) $this->pay_at?->toDateTimeString(),
|
||||
'completed_at' => (string) $this->completed_at?->toDateTimeString(),
|
||||
'created_at' => $this->created_at->toDateTimeString(),
|
||||
'expires_at' => $this->expires_at,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace App\Endpoint\Api\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
|
||||
class OrderResourceCollection extends ResourceCollection
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return $this->collection->map(function ($item) {
|
||||
return [
|
||||
'id' => $item->id,
|
||||
'sn' => $item->sn,
|
||||
'total_amount' => $item->total_amount_format,
|
||||
'status' => $item->status,
|
||||
'created_date' => $item->created_at->toDateString(),
|
||||
'products' => OrderProductResource::collection($item->whenLoaded('products')),
|
||||
'expires_at' => $item->expires_at,
|
||||
];
|
||||
})->toArray();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Endpoint\Api\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class OrderSimpleResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'sn' => $this->sn,
|
||||
'total_amount' => $this->total_amount_format,
|
||||
'status' => $this->status,
|
||||
'created_date' => $this->created_at->toDateString(),
|
||||
'products' => OrderProductResource::collection($this->whenLoaded('products')),
|
||||
'expires_at' => $this->expires_at,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -16,6 +16,10 @@ class Order extends Model
|
|||
public const STATUS_COMPLETED = 9; // 已完成
|
||||
public const STATUS_CANCELLED = 10; // 已取消
|
||||
|
||||
public const PAY_WAY_ALIPAY = 'alipay';
|
||||
public const PAY_WAY_WXPAY = 'wxpay';
|
||||
public const PAY_WAY_BALANCE = 'balance';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
|
|
@ -37,8 +41,9 @@ class Order extends Model
|
|||
protected $fillable = [
|
||||
'user_id',
|
||||
'sn',
|
||||
'coupon_disount_amount',
|
||||
'vip_disount_amount',
|
||||
'user_coupon_id',
|
||||
'coupon_discount_amount',
|
||||
'vip_discount_amount',
|
||||
'reduced_amount',
|
||||
'shipping_fee',
|
||||
'products_total_amount',
|
||||
|
|
@ -56,6 +61,15 @@ class Order extends Model
|
|||
'completed_at',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public static $payWayTexts = [
|
||||
self::PAY_WAY_ALIPAY => '支付宝',
|
||||
self::PAY_WAY_WXPAY => '微信支付',
|
||||
self::PAY_WAY_BALANCE => '余额',
|
||||
];
|
||||
|
||||
/**
|
||||
* 属于此订单的商品
|
||||
*/
|
||||
|
|
@ -84,6 +98,46 @@ class Order extends Model
|
|||
return in_array($this->status, [static::STATUS_PENDING, static::STATUS_PAID]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单券优惠金额
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCouponDiscountAmountFormatAttribute()
|
||||
{
|
||||
return Numeric::trimTrailingZero(bcdiv($this->attributes['coupon_discount_amount'], 100, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单会员折扣金额
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getVipDiscountAmountFormatAttribute()
|
||||
{
|
||||
return Numeric::trimTrailingZero(bcdiv($this->attributes['vip_discount_amount'], 100, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单减免金额
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getReducedAmountFormatAttribute()
|
||||
{
|
||||
return Numeric::trimTrailingZero(bcdiv($this->attributes['reduced_amount'], 100, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单邮费
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getShippingFeeFormatAttribute()
|
||||
{
|
||||
return Numeric::trimTrailingZero(bcdiv($this->attributes['shipping_fee'], 100, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单支付金额
|
||||
*
|
||||
|
|
@ -94,6 +148,16 @@ class Order extends Model
|
|||
return Numeric::trimTrailingZero(bcdiv($this->attributes['total_amount'], 100, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单商品总额
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getProductsTotalAmountFormatAttribute()
|
||||
{
|
||||
return Numeric::trimTrailingZero(bcdiv($this->attributes['products_total_amount'], 100, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* 待支付订单过期时间
|
||||
*
|
||||
|
|
|
|||
|
|
@ -169,6 +169,7 @@ class OrderService
|
|||
|
||||
$order = $user->orders()->create([
|
||||
'sn' => OrderHelper::serialNumber(),
|
||||
'user_coupon_id' => $coupon?->id,
|
||||
'coupon_discount_amount' => $couponDiscountAmount,
|
||||
'vip_discount_amount' => $vipDiscountAmount,
|
||||
'shipping_fee' => $shippingFee,
|
||||
|
|
@ -232,7 +233,7 @@ class OrderService
|
|||
protected function deductSkuStock(ProductSku $sku, int $quantity): void
|
||||
{
|
||||
$sku->update([
|
||||
'stock' => DB::raw("stock + {$quantity}"), // 库存
|
||||
'stock' => DB::raw("stock - {$quantity}"), // 库存
|
||||
'sales' => DB::raw("sales + {$quantity}"), // 销量
|
||||
]);
|
||||
|
||||
|
|
@ -496,7 +497,7 @@ class OrderService
|
|||
|----------------------------------------
|
||||
*/
|
||||
|
||||
$discountAmounts[$skuId] = (int) ($amount * $coupon->coupon_amount / 100);
|
||||
$discountAmounts[$skuId] = (int) bcmul($amount, bcdiv(100 - $coupon->coupon_amount, 100, 2));
|
||||
} else {
|
||||
/*
|
||||
|----------------------------------------
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddCouponIdToOrdersTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('orders', function (Blueprint $table) {
|
||||
$table->unsignedBigInteger('user_coupon_id')->unique()->nullable()->comment('用户优惠券ID');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('orders', function (Blueprint $table) {
|
||||
$table->dropColumn(['user_coupon_id']);
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue