Files
lgp-admin-plus-api/app/Service/wx/WxProductService.php
LQ bcf54c2727 初始化
缺陷:主题配色需要优化整体的同风格
2026-08-14 23:21:21 +08:00

126 lines
4.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Service\wx;
use App\BaseApp\BaseWxService;
use App\Models\business\CatalogueModel;
use App\Models\business\CategoryModel;
use App\Models\business\ImageModel;
use App\Models\business\WxUserModel;
use App\Service\business\PriceService;
/**
* 小程序商品列表与详情
*
* 价格倍率与可见性统一走 PriceService后台与小程序共用同一套规则
* 避免出现「后台看原价、小程序看倍率价」对不上账的老问题。
*/
class WxProductService extends BaseWxService
{
/**
* 商品列表:分类点到二级时按父分类下的所有子分类查
*/
public function list(): array
{
$title = (string) request()->get('title', '');
$categoryId = request()->get('category_id');
$childIds = [];
if (!empty($categoryId)) {
$childIds = CategoryModel::where('pid', $categoryId)->where('deleted_at', 0)->pluck('id')->all();
}
// 小程序前端发的是 size见 pages/product|search getProductList老接口曾用 pageSize
// 这里两者都接,避免改一边漏一边
$pageSize = (int) (request()->get('pageSize') ?: request()->get('size') ?: 10);
$result = CatalogueModel::when($title !== '', function ($query) use ($title) {
$query->where('title', 'like', '%' . $title . '%');
})->when(!empty($categoryId) && empty($childIds), function ($query) use ($categoryId) {
$query->where('category_id', $categoryId);
})->when(!empty($childIds), function ($query) use ($childIds) {
$query->whereIn('category_id', $childIds);
})->where('deleted_at', 0)
->orderBy('id', 'desc')
->paginate($pageSize)
->toArray();
return [
'page' => $result['current_page'],
'size' => $result['per_page'],
'page_count' => $result['last_page'],
// 小程序 productWaterfall / product.vue 读的是 last_page不是 page_count两个键都给
'last_page' => $result['last_page'],
'total' => $result['total'],
'items' => $result['data'],
'data' => $result['data'],
];
}
/**
* 商品详情
*
* 带 p_user_id 说明是代理商分享进来的:普通新用户会被绑到该代理商名下并继承其倍率,
* 这是老项目的既有行为,不能改,否则代理商体系的价格会全部失效。
*/
public function detail(int $id, int $shareUserId = 0): array
{
if ($shareUserId > 0) {
$this->inheritAgentPrice($shareUserId);
}
$info = CatalogueModel::with([
'category',
'images' => fn ($query) => $query->where('deleted_at', 0),
'priceSheet' => fn ($query) => $query->where('deleted_at', 0),
])->where('deleted_at', 0)->find($id);
if (empty($info)) {
$this->utils->errorThrow('商品不存在');
}
$info = $info->toArray();
// 渲染图作为详情轮播
$info['carousel'] = [];
foreach ($info['images'] ?? [] as $image) {
if ((int) $image['type'] === ImageModel::TYPE_RENDER) {
$info['carousel'][] = $image['url'];
}
}
$showPrice = $this->canSeePrice();
$applied = PriceService::getInstance()->applyToRows($info['price_sheet'] ?? [], $showPrice, $this->priceMultiplier());
$info['price_sheet'] = $applied['rows'];
$info['is_show_price'] = $applied['is_show_price'];
return $info;
}
/**
* 继承分享代理商的倍率
*
* 只有「还没有上级、自己也不是代理商」的用户会被绑定;已绑定同一个上级时同步最新倍率,
* 代理商改了倍率下级要跟着变。
*/
private function inheritAgentPrice(int $shareUserId): void
{
$isAgent = (int) ($this->userInfo['is_p'] ?? 0) === 1;
$pid = (int) ($this->userInfo['pid'] ?? 0);
if ($isAgent) {
return;
}
if ($pid !== 0 && $pid !== $shareUserId) {
return;
}
$agent = WxUserModel::where('id', $shareUserId)->where('deleted_at', 0)->first();
if (empty($agent) || (int) $agent['is_p'] !== 1) {
return;
}
$this->userInfo['show_price'] = 1;
$this->userInfo['price_number'] = $agent['price_number'];
WxUserModel::where('id', $this->userId)->update([
'pid' => $shareUserId,
'show_price' => 1,
'price_number' => $agent['price_number'],
'updated_at' => time(),
]);
}
}