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(), ]); } }