| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 | <?phpnamespace app\api\controller;use app\common\controller\Api;use app\common\Service\DiscountService;use app\api\validate\Discount as DiscountValidate;/** * 折扣活动接口 */class Discount extends Api{    protected $noNeedLogin = ['lists', 'goods'];    protected $noNeedRight = '*';    /**     * 获取当前折扣商品列表     */    public function lists()    {        $params = $this->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'] ?? [];                // try {            // 获取折扣商品            $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)            ]);                    // } catch (\Exception $e) {        //     $this->error('查询失败:' . $e->getMessage());        // }    }        /**     * 获取指定商品的折扣信息     */    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)) {                $this->error('该商品当前没有参与折扣活动');            }                        // 添加活动剩余时间            $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());        }    }} 
 |