request->param(); // 参数验证 $validate = new DiscountValidate(); if (!$validate->scene('lists')->check($params)) { $this->error($validate->getError()); } $page = $params['page'] ?? 1; $pageSize = $params['pageSize'] ?? 10; $arrGoodsIds = $params['goods_ids'] ?? []; // 获取折扣商品 $discountGoods = DiscountService::getCurrentDiscountGoods($arrGoodsIds); // 分页处理 $total = count($discountGoods); $offset = ($page - 1) * $pageSize; $goods = array_slice($discountGoods, $offset, $pageSize); // 处理数据,添加CDN地址和时间格式化 foreach ($goods as &$item) { // 处理图片地址 $item['image'] = cdnurl($item['image'], true); // 添加活动剩余时间 $item['remaining_time'] = DiscountService::formatRemainingTime($item['end_time']); // 处理折扣信息 foreach ($item['discount_info'] as &$discount) { if (!empty($discount['sku_image'])) { $discount['sku_image'] = cdnurl($discount['sku_image'], true); } } } $this->success('获取成功', [ 'list' => $goods, 'total' => $total, 'page' => $page, 'pageSize' => $pageSize, 'totalPages' => ceil($total / $pageSize) ]); } // 增加一个 当前时间段的折扣活动 public function getCurrentActivity() { $activity = DiscountService::getCurrentActivity(); // 添加活动剩余时间 if(!empty($activity)){ $activity['remaining_time'] = DiscountService::formatRemainingTime($activity['end_time']); } $this->success('获取成功', $activity); } /** * 获取指定商品的折扣信息 */ public function goods() { $params = $this->request->param(); // 参数验证 $validate = new DiscountValidate(); if (!$validate->scene('goods')->check($params)) { $this->error($validate->getError()); } $goodsId = $this->request->param('goods_id', 0, 'intval'); $skuId = $this->request->param('sku_id', 0, 'intval'); try { // 获取商品折扣信息 $discountInfo = DiscountService::getGoodsDiscountInfo($goodsId, $skuId); if (empty($discountInfo)) { throw new BusinessException('该商品当前没有参与折扣活动', ErrorCodeEnum::GOODS_NO_DISCOUNT); } // 添加活动剩余时间 $discountInfo['remaining_time'] = DiscountService::formatRemainingTime($discountInfo['end_time']); $this->success('获取成功', $discountInfo); } catch (\Exception $e) { $this->error('查询失败:' . $e->getMessage()); } } /** * 批量检查商品折扣信息 */ public function batch() { $params = $this->request->param(); // 参数验证 $validate = new DiscountValidate(); if (!$validate->scene('batch')->check($params)) { $this->error($validate->getError()); } $goodsIds = $params['goods_ids'] ?? []; try { // 批量获取折扣信息 $discountData = DiscountService::getBatchGoodsDiscountInfo($goodsIds); $this->success('获取成功', $discountData); } catch (\Exception $e) { $this->error('查询失败:' . $e->getMessage()); } } }