66 lines
2.5 KiB
PHP
66 lines
2.5 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Mini;
|
||
|
||
use App\Models\CustomerLevelModel;
|
||
use App\Models\HomeSpecialModel;
|
||
use App\Models\ProductModel;
|
||
use App\Services\CartService;
|
||
use Illuminate\Http\JsonResponse;
|
||
use Illuminate\Http\Request;
|
||
use Modules\AnnoRoute\Attribute\GetRoute;
|
||
use Modules\AnnoRoute\Attribute\RequestAttribute;
|
||
|
||
/**
|
||
* 小程序特价推荐
|
||
*/
|
||
#[RequestAttribute('/mini', 'mini', authGuard: 'users')]
|
||
class SpecialController extends BaseMiniController
|
||
{
|
||
/**
|
||
* 特价推荐商品列表(免登录浏览;行结构与 /mini/product/list 一致:
|
||
* 登录门店附等级价 price 与 cart_id/cart_quantity 供列表直接加减购物车;
|
||
* data.cart 为购物车悬浮球汇总,未登录返回零值结构)
|
||
*/
|
||
#[GetRoute('/special/list', authorize: false)]
|
||
public function specials(Request $request): JsonResponse
|
||
{
|
||
$pageSize = (int) $request->input('pageSize', 10);
|
||
$paginator = HomeSpecialModel::query()
|
||
->where('status', HomeSpecialModel::STATUS_NORMAL)
|
||
->whereHas('product', static function ($q) {
|
||
$q->where('status', ProductModel::STATUS_ON);
|
||
})
|
||
->with('product')
|
||
->orderBy('sort')
|
||
->orderBy('id')
|
||
->paginate($pageSize);
|
||
|
||
// 当前门店的等级(售价 = 成本价 × (100 + 等级上浮比例) / 100)与购物车行
|
||
$store = $this->optionalStore($request);
|
||
$level = ($store !== null && $store->level_id > 0) ? $store->level : null;
|
||
$cartService = app(CartService::class);
|
||
$cartRows = $store !== null ? $cartService->cartRowMap($store->id) : [];
|
||
|
||
$paginator->getCollection()->transform(
|
||
static function (HomeSpecialModel $special) use ($level, $cartRows): array {
|
||
$product = $special->product;
|
||
$row = $product->toArray();
|
||
// 实际价(按等级上浮比例换算;成本价不随序列化输出)
|
||
$row['price'] = $level !== null
|
||
? CustomerLevelModel::calcLevelPrice($product->cost_price, $level->percent)
|
||
: null;
|
||
// 购物车数量(列表直接加减用;不在购物车为 0/'0.00')
|
||
$row['cart_id'] = $cartRows[$product->id]['id'] ?? 0;
|
||
$row['cart_quantity'] = $cartRows[$product->id]['quantity'] ?? '0.00';
|
||
return $row;
|
||
}
|
||
);
|
||
|
||
$data = $paginator->toArray();
|
||
$data['cart'] = $cartService->summary($store);
|
||
|
||
return $this->success($data);
|
||
}
|
||
}
|