StoreCoupon.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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 crmeb\basic\BaseController;
  13. use app\common\repositories\store\coupon\StoreCouponProductRepository;
  14. use app\common\repositories\store\coupon\StoreCouponRepository;
  15. use app\common\repositories\store\coupon\StoreCouponUserRepository;
  16. use app\common\repositories\store\product\ProductRepository;
  17. use think\App;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. use think\facade\Db;
  22. /**
  23. * Class StoreCoupon
  24. * @package app\controller\api\store\product
  25. * @author xaboy
  26. * @day 2020/6/1
  27. */
  28. class StoreCoupon extends BaseController
  29. {
  30. /**
  31. * @var
  32. */
  33. protected $uid;
  34. /**
  35. * StoreCoupon constructor.
  36. * @param App $app
  37. */
  38. public function __construct(App $app)
  39. {
  40. parent::__construct($app);
  41. if ($this->request->isLogin()) $this->uid = $this->request->uid();
  42. }
  43. /**
  44. * @param StoreCouponUserRepository $repository
  45. * @return mixed
  46. * @throws DataNotFoundException
  47. * @throws DbException
  48. * @throws ModelNotFoundException
  49. * @author xaboy
  50. * @day 2020/6/3
  51. */
  52. public function lst(StoreCouponUserRepository $repository)
  53. {
  54. [$page, $limit] = $this->getPage();
  55. $where = $this->request->params(['statusTag']);
  56. $where['uid'] = $this->uid;
  57. return app('json')->success($repository->userList($where, $page, $limit));
  58. }
  59. /**
  60. * @param StoreCouponRepository $repository
  61. * @param StoreCouponProductRepository $couponProductRepository
  62. * @return mixed
  63. * @throws DataNotFoundException
  64. * @throws DbException
  65. * @throws ModelNotFoundException
  66. * @author xaboy
  67. * @day 2020/6/1
  68. */
  69. public function coupon(StoreCouponRepository $repository, StoreCouponProductRepository $couponProductRepository)
  70. {
  71. $ids = array_filter(explode(',', $this->request->param('ids')));
  72. if (!count($ids))
  73. return app('json')->success([]);
  74. $productCouponIds = $couponProductRepository->productByCouponId($ids);
  75. $productCoupon = count($productCouponIds) ? $repository->validProductCoupon($productCouponIds, $this->uid)->toArray() : [];
  76. return app('json')->success($productCoupon);
  77. }
  78. /**
  79. * @param $id
  80. * @param StoreCouponRepository $repository
  81. * @return mixed
  82. * @throws DataNotFoundException
  83. * @throws DbException
  84. * @throws ModelNotFoundException
  85. * @author xaboy
  86. * @day 2020/6/1
  87. */
  88. public function merCoupon($id, StoreCouponRepository $repository)
  89. {
  90. $all = (int)$this->request->param('all');
  91. $coupon = $repository->validMerCoupon($id, $this->uid, $all === 1 ? null : 0)->toArray();
  92. return app('json')->success($coupon);
  93. }
  94. /**
  95. * @param $id
  96. * @param StoreCouponRepository $repository
  97. * @return mixed
  98. * @author xaboy
  99. * @day 2020/6/1
  100. */
  101. public function receiveCoupon($id, StoreCouponRepository $repository)
  102. {
  103. if (!$repository->exists($id))
  104. return app('json')->fail('优惠券不存在');
  105. try {
  106. $repository->receiveCoupon($id, $this->uid);
  107. } catch (\Exception $e) {
  108. return app('json')->fail('优惠券已被领完');
  109. }
  110. return app('json')->success('领取成功');
  111. }
  112. }