Discount.php 4.0 KB

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