Discount.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\Service\DiscountService;
  5. use app\api\validate\Discount as DiscountValidate;
  6. use app\common\exception\BusinessException;
  7. use app\common\Enum\ErrorCodeEnum;
  8. /**
  9. * 折扣活动接口
  10. */
  11. class Discount extends Api
  12. {
  13. protected $noNeedLogin = ['lists', 'goods','getCurrentActivity'];
  14. protected $noNeedRight = '*';
  15. /**
  16. * 获取当前折扣商品列表
  17. */
  18. public function lists()
  19. {
  20. $params = $this->request->param();
  21. // 参数验证
  22. $validate = new DiscountValidate();
  23. if (!$validate->scene('lists')->check($params)) {
  24. $this->error($validate->getError());
  25. }
  26. $page = $params['page'] ?? 1;
  27. $pageSize = $params['pageSize'] ?? 10;
  28. $arrGoodsIds = $params['goods_ids'] ?? [];
  29. // 获取折扣商品
  30. $discountGoods = DiscountService::getCurrentDiscountGoods($arrGoodsIds);
  31. // 分页处理
  32. $total = count($discountGoods);
  33. $offset = ($page - 1) * $pageSize;
  34. $goods = array_slice($discountGoods, $offset, $pageSize);
  35. // 处理数据,添加CDN地址和时间格式化
  36. foreach ($goods as &$item) {
  37. // 处理图片地址
  38. $item['image'] = cdnurl($item['image'], true);
  39. // 添加活动剩余时间
  40. $item['remaining_time'] = DiscountService::formatRemainingTime($item['end_time']);
  41. // 处理折扣信息
  42. foreach ($item['discount_info'] as &$discount) {
  43. if (!empty($discount['sku_image'])) {
  44. $discount['sku_image'] = cdnurl($discount['sku_image'], true);
  45. }
  46. }
  47. }
  48. $this->success('获取成功', [
  49. 'list' => $goods,
  50. 'total' => $total,
  51. 'page' => $page,
  52. 'pageSize' => $pageSize,
  53. 'totalPages' => ceil($total / $pageSize)
  54. ]);
  55. }
  56. // 增加一个 当前时间段的折扣活动
  57. public function getCurrentActivity()
  58. {
  59. $activity = DiscountService::getCurrentActivity();
  60. // 添加活动剩余时间
  61. if(!empty($activity)){
  62. $activity['remaining_time'] = DiscountService::formatRemainingTime($activity['end_time']);
  63. }
  64. $this->success('获取成功', $activity);
  65. }
  66. /**
  67. * 获取指定商品的折扣信息
  68. */
  69. public function goods()
  70. {
  71. $params = $this->request->param();
  72. // 参数验证
  73. $validate = new DiscountValidate();
  74. if (!$validate->scene('goods')->check($params)) {
  75. $this->error($validate->getError());
  76. }
  77. $goodsId = $this->request->param('goods_id', 0, 'intval');
  78. $skuId = $this->request->param('sku_id', 0, 'intval');
  79. try {
  80. // 获取商品折扣信息
  81. $discountInfo = DiscountService::getGoodsDiscountInfo($goodsId, $skuId);
  82. if (empty($discountInfo)) {
  83. throw new BusinessException('该商品当前没有参与折扣活动', ErrorCodeEnum::GOODS_NO_DISCOUNT);
  84. }
  85. // 添加活动剩余时间
  86. $discountInfo['remaining_time'] = DiscountService::formatRemainingTime($discountInfo['end_time']);
  87. $this->success('获取成功', $discountInfo);
  88. } catch (\Exception $e) {
  89. $this->error('查询失败:' . $e->getMessage());
  90. }
  91. }
  92. /**
  93. * 批量检查商品折扣信息
  94. */
  95. public function batch()
  96. {
  97. $params = $this->request->param();
  98. // 参数验证
  99. $validate = new DiscountValidate();
  100. if (!$validate->scene('batch')->check($params)) {
  101. $this->error($validate->getError());
  102. }
  103. $goodsIds = $params['goods_ids'] ?? [];
  104. try {
  105. // 批量获取折扣信息
  106. $discountData = DiscountService::getBatchGoodsDiscountInfo($goodsIds);
  107. $this->success('获取成功', $discountData);
  108. } catch (\Exception $e) {
  109. $this->error('查询失败:' . $e->getMessage());
  110. }
  111. }
  112. }