StoreCoupon.php 3.3 KB

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