Coupon.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace app\api\controller\user;
  3. use app\api\controller\Controller;
  4. use app\api\model\UserCoupon as UserCouponModel;
  5. /**
  6. * 用户优惠券
  7. * Class Coupon
  8. * @package app\api\controller
  9. */
  10. class Coupon extends Controller
  11. {
  12. /* @var UserCouponModel $model */
  13. private $model;
  14. /* @var \app\api\model\User $model */
  15. private $user;
  16. /**
  17. * 构造方法
  18. * @throws \app\common\exception\BaseException
  19. * @throws \think\exception\DbException
  20. */
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new UserCouponModel;
  25. $this->user = $this->getUser();
  26. }
  27. /**
  28. * 优惠券列表
  29. * @param string $data_type
  30. * @return array
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. * @throws \think\exception\DbException
  34. */
  35. public function lists($data_type = 'all')
  36. {
  37. $is_use = false;
  38. $is_expire = false;
  39. switch ($data_type) {
  40. case 'not_use':
  41. $is_use = false;
  42. break;
  43. case 'is_use':
  44. $is_use = true;
  45. break;
  46. case 'is_expire':
  47. $is_expire = true;
  48. break;
  49. }
  50. $list = $this->model->getList($this->user['user_id'], $is_use, $is_expire);
  51. return $this->renderSuccess(compact('list'));
  52. }
  53. /**
  54. * 领取优惠券
  55. * @param $coupon_id
  56. * @return array
  57. * @throws \think\exception\DbException
  58. */
  59. public function receive($coupon_id)
  60. {
  61. if ($this->model->receive($this->user, $coupon_id)) {
  62. return $this->renderSuccess([], '领取成功');
  63. }
  64. return $this->renderError($this->model->getError() ?: '添加失败');
  65. }
  66. }