Discount.php 4.2 KB

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