Coupon.php 1.7 KB

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