StoreCoupon.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\store\product;
  12. use app\common\repositories\store\coupon\StoreCouponProductRepository;
  13. use app\common\repositories\store\coupon\StoreCouponRepository;
  14. use app\common\repositories\store\coupon\StoreCouponUserRepository;
  15. use crmeb\basic\BaseController;
  16. use think\App;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. /**
  21. * Class StoreCoupon
  22. * @package app\controller\api\store\product
  23. * @author xaboy
  24. * @day 2020/6/1
  25. */
  26. class StoreCoupon extends BaseController
  27. {
  28. /**
  29. * @var
  30. */
  31. protected $uid;
  32. /**
  33. * StoreCoupon constructor.
  34. * @param App $app
  35. */
  36. public function __construct(App $app)
  37. {
  38. parent::__construct($app);
  39. if ($this->request->isLogin()) $this->uid = $this->request->uid();
  40. }
  41. /**
  42. * 获取用户优惠券列表
  43. *
  44. * 本方法用于获取当前用户所拥有的优惠券列表。通过接收前端请求中的页码和每页数量,
  45. * 以及可能的状态标签参数,来筛选并返回符合条件的优惠券列表。
  46. *
  47. * @param StoreCouponUserRepository $repository 优惠券用户仓库,用于查询优惠券用户信息。
  48. * @return json 返回一个包含优惠券列表信息的JSON对象,成功时包含数据列表和成功信息,
  49. * 失败时包含错误代码和消息。
  50. */
  51. public function lst(StoreCouponUserRepository $repository)
  52. {
  53. // 获取请求中的页码和每页数量
  54. [$page, $limit] = $this->getPage();
  55. // 从请求中获取状态标签参数
  56. $where = $this->request->params(['statusTag']);
  57. // 设置查询条件:用户ID
  58. $where['uid'] = $this->uid;
  59. // 返回查询结果,包含分页信息
  60. return app('json')->success($repository->userList($where, $page, $limit));
  61. }
  62. /**
  63. * 根据优惠券ID获取有效的优惠券信息
  64. *
  65. * 本函数用于处理请求,根据传入的优惠券ID数组,查询并返回有效的优惠券信息。
  66. * 它首先从请求中获取优惠券ID的列表,然后通过查询产品与优惠券的关系,筛选出有效的优惠券。
  67. * 最后,将有效的优惠券信息以JSON格式返回给前端。
  68. *
  69. * @param StoreCouponRepository $repository 优惠券仓库接口,用于查询优惠券信息
  70. * @param StoreCouponProductRepository $couponProductRepository 优惠券产品仓库接口,用于查询产品和优惠券的关系
  71. * @return \think\response\Json 返回一个JSON对象,包含有效的优惠券信息或空数组
  72. */
  73. public function coupon(StoreCouponRepository $repository, StoreCouponProductRepository $couponProductRepository)
  74. {
  75. // 从请求中获取优惠券ID的字符串,然后过滤掉空值,得到一个数组
  76. $ids = array_filter(explode(',', $this->request->param('ids')));
  77. // 如果获取的优惠券ID数组为空,则直接返回空的JSON成功响应
  78. if (!count($ids))
  79. return app('json')->success([]);
  80. // 根据优惠券ID数组,查询这些优惠券所关联的产品ID
  81. $productCouponIds = $couponProductRepository->productByCouponId($ids);
  82. // 如果查询到关联产品ID,就进一步查询这些优惠券是否有效,并根据用户ID过滤,最后转换为数组
  83. // 如果没有查询到关联产品ID,则直接返回空数组
  84. $productCoupon = count($productCouponIds) ? $repository->validProductCoupon($productCouponIds, $this->uid)->toArray() : [];
  85. // 返回包含有效优惠券信息的JSON成功响应
  86. return app('json')->success($productCoupon);
  87. }
  88. /**
  89. * 获取商家优惠券信息
  90. *
  91. * 本函数用于根据商家ID和用户ID,获取指定商家的优惠券信息。如果请求参数中包含'all'参数,
  92. * 则表示获取所有优惠券,否则只获取可使用的优惠券。
  93. *
  94. * @param int $id 商家ID
  95. * @param StoreCouponRepository $repository 优惠券仓库对象,用于查询优惠券信息
  96. * @return json 返回包含优惠券信息的JSON对象,如果成功,则包含优惠券详情;如果失败,则包含错误信息。
  97. */
  98. public function merCoupon($id, StoreCouponRepository $repository)
  99. {
  100. // 获取请求参数'all'的值,用于判断是否查询所有优惠券
  101. $all = (int)$this->request->param('all');
  102. // 根据商家ID、用户ID和查询所有优惠券的标志,查询有效的商家优惠券信息
  103. // 如果'all'为1,则查询所有优惠券,否则只查询可使用的优惠券
  104. $coupon = $repository->validMerCoupon($id, $this->uid, $all === 1 ? null : 0)->toArray();
  105. // 返回查询结果,使用JSON格式响应
  106. return app('json')->success($coupon);
  107. }
  108. /**
  109. * 用户领取优惠券功能
  110. *
  111. * 此方法用于处理用户领取指定优惠券的操作。它首先检查优惠券是否存在,
  112. * 如果存在,则将该优惠券标记为被当前用户领取。如果优惠券不存在,
  113. * 则向用户返回错误信息;如果领取成功,则返回成功信息。
  114. *
  115. * @param int $id 优惠券ID
  116. * @param StoreCouponRepository $repository 优惠券仓库对象,用于操作优惠券数据
  117. * @return mixed 返回JSON格式的响应,包含成功或失败的信息
  118. */
  119. public function receiveCoupon($id, StoreCouponRepository $repository)
  120. {
  121. // 检查优惠券是否存在
  122. if (!$repository->exists($id)) {
  123. // 如果优惠券不存在,返回失败信息
  124. return app('json')->fail('优惠券不存在');
  125. }
  126. // 标记优惠券为被当前用户领取
  127. $repository->receiveCoupon($id, $this->uid);
  128. // 领取成功,返回成功信息
  129. return app('json')->success('领取成功');
  130. }
  131. /**
  132. * 可领取的优惠券列表
  133. * @author Qinii
  134. * @day 3/14/22
  135. */
  136. public function getList(StoreCouponRepository $couponRepository)
  137. {
  138. $where = $this->request->params(['type','mer_id', 'product','is_pc',['send_type',0]]);
  139. [$page, $limit] = $this->getPage();
  140. $data = $couponRepository->apiList($where, $page, $limit, $this->uid);
  141. return app('json')->success($data);
  142. }
  143. /**
  144. * 新人注册赠送优惠券
  145. * @param StoreCouponRepository $couponRepository
  146. * @return \think\response\Json
  147. * @author Qinii
  148. */
  149. public function newPeople(StoreCouponRepository $couponRepository)
  150. {
  151. $coupons = $couponRepository->newPeopleCoupon();
  152. foreach ($coupons as $coupon){
  153. if($coupon['coupon_type']){
  154. $coupon['use_end_time'] = explode(' ', $coupon['use_end_time'])[0] ?? '';
  155. $coupon['use_start_time'] = explode(' ', $coupon['use_start_time'])[0] ?? '';
  156. }else{
  157. $coupon['use_start_time'] = date('Y-m-d');
  158. $coupon['use_end_time'] = date('Y-m-d', strtotime('+ ' . $coupon['coupon_time'] . ' day'));
  159. }
  160. if($coupon['use_end_time']){
  161. $coupon['use_end_time'] = date('Y.m.d',strtotime($coupon['use_end_time']));
  162. }
  163. if($coupon['use_start_time']){
  164. $coupon['use_start_time'] = date('Y.m.d',strtotime($coupon['use_start_time']));
  165. }
  166. }
  167. $data = systemConfig(['newcomer_status','register_money_status','register_integral_status', 'register_give_integral','register_give_money','register_popup_pic','register_popup_url']);
  168. $data['coupon'] = $coupons;
  169. return app('json')->success($data);
  170. }
  171. }