StoreCouponsController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace app\api\controller\store;
  3. use app\models\store\StoreCouponIssue;
  4. use app\Request;
  5. use crmeb\services\UtilService;
  6. use app\models\store\StoreCouponUser;
  7. /**
  8. * 优惠券类
  9. * Class StoreCouponsController
  10. * @package app\api\controller\store
  11. */
  12. class StoreCouponsController
  13. {
  14. /**
  15. * 可领取优惠券列表
  16. * @param Request $request
  17. * @return mixed
  18. */
  19. public function lst(Request $request)
  20. {
  21. $data = UtilService::getMore([
  22. ['type',0],
  23. ['page', 0],
  24. ['limit', 0],
  25. ['product_id',0]
  26. ], $request);
  27. return app('json')->successful(StoreCouponIssue::getIssueCouponList($request->uid(), $data['limit'], $data['page'],$data['type'],$data['product_id']));
  28. }
  29. /**
  30. * 领取优惠券
  31. *
  32. * @param Request $request
  33. * @return mixed
  34. */
  35. public function receive(Request $request)
  36. {
  37. list($couponId) = UtilService::getMore([['couponId', 0]], $request, true);
  38. if (!$couponId || !is_numeric($couponId)) return app('json')->fail('参数错误!');
  39. if (StoreCouponIssue::issueUserCoupon($couponId, $request->uid())) {
  40. return app('json')->successful('领取成功');
  41. } else {
  42. return app('json')->fail(StoreCouponIssue::getErrorInfo('领取失败!'));
  43. }
  44. }
  45. /**
  46. * 用户已领取优惠券
  47. * @param Request $request
  48. * @param $types
  49. * @return mixed
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. * @throws \think\exception\DbException
  53. */
  54. public function user(Request $request, $types)
  55. {
  56. switch ($types) {
  57. case 0:
  58. case '':
  59. $list = StoreCouponUser::getUserAllCoupon($request->uid());
  60. break;
  61. case 1:
  62. $list = StoreCouponUser::getUserValidCoupon($request->uid());
  63. break;
  64. case 2:
  65. $list = StoreCouponUser::getUserAlreadyUsedCoupon($request->uid());
  66. break;
  67. default:
  68. $list = StoreCouponUser::getUserBeOverdueCoupon($request->uid());
  69. break;
  70. }
  71. foreach ($list as &$v) {
  72. $v['add_time'] = date('Y/m/d', $v['add_time']);
  73. $v['end_time'] = date('Y/m/d', $v['end_time']);
  74. }
  75. return app('json')->successful($list);
  76. }
  77. /**
  78. * 批量领取优惠券
  79. * @param Request $request
  80. * @return mixed
  81. */
  82. public function receive_batch(Request $request)
  83. {
  84. list($couponIds) = UtilService::postMore([
  85. ['couponId', []],
  86. ], $request, true);
  87. if (!count($couponIds)) return app('json')->fail('参数错误');
  88. $couponIdsError = [];
  89. $count = 0;
  90. $msg = '';
  91. foreach ($couponIds as $key => &$item) {
  92. if (!StoreCouponIssue::issueUserCoupon($item, $request->uid())) {
  93. $couponIdsError[$count]['id'] = $item;
  94. $couponIdsError[$count]['msg'] = StoreCouponIssue::getErrorInfo('领取失败');
  95. } else {
  96. $couponIdsError[$count]['id'] = $item;
  97. $couponIdsError[$count]['msg'] = '领取成功';
  98. }
  99. $count++;
  100. }
  101. foreach ($couponIdsError as $key => &$value) {
  102. $msg = $msg . StoreCouponIssue::getIssueCouponTitle($value['id']) . ',' . $value['msg'];
  103. }
  104. return app('json')->fail($msg);
  105. }
  106. /**
  107. * 优惠券 订单获取
  108. * @param Request $request
  109. * @param $price
  110. * @return mixed
  111. */
  112. public function order(Request $request, $cartId, $price)
  113. {
  114. return app('json')->successful(StoreCouponUser::beUsableCouponList($request->uid(), $cartId, $price));
  115. }
  116. }