Coupon.php 1.9 KB

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