Coupon.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\CouponConfig;
  4. use app\common\model\UserCouponList;
  5. use think\Db;
  6. use function Symfony\Component\String\s;
  7. /**
  8. * @title 优惠券管理
  9. * @controller Coupon
  10. * @package app\api\controller
  11. */
  12. class Coupon extends Base
  13. {
  14. public function initialize(){
  15. parent::initialize();
  16. parent::checkLogin();
  17. }
  18. /**
  19. * @title 获取会员优惠券列表
  20. * @desc 获取会员优惠券列表
  21. * @author qc
  22. * @url /api/Coupon/getUserCouponList
  23. * @method GET
  24. * @header name:Authorization require:1 desc:Token
  25. * @param name:page type:int default:1 desc:页数
  26. * @param name:page_num type:int default:20 desc:每页数
  27. * @param name:status type:int default:1 desc:状态码(1未使用2已使用3已过期)
  28. * @param name:module type:int default:1 desc:应用模块(1所有,2洗鞋,3洗衣)【订单确认页面可用】
  29. * @return name:title type:string default:-- desc:优惠券名称
  30. * @return name:module type:int default:-- desc:应用模块(1所有,2洗鞋,3洗衣)
  31. * @return name:coupon_type type:int default:-- desc:类型(1满减券,2抵扣券【抵一件衣服或是一双鞋价格】)
  32. * @return name:is_new type:int default:-- desc:是否新人券(0否1是)
  33. * @return name:low_amount type:int default:-- desc:最低消费限制(coupon_type=1时用)
  34. * @return name:amount type:int default:-- desc:抵扣金额(coupon_type=1时用)
  35. * @return name:create_at type:string default:-- desc:领取时间
  36. * @return name:past_at type:string default:-- desc:过期时间
  37. * @return name:status type:int default:-- desc:状态码(1未使用2已使用3已过期)
  38. *
  39. */
  40. public function getUserCouponList()
  41. {
  42. $status = input('get.status',1);
  43. $module = input('get.module',1);
  44. $list = UserCouponList::where(['l.user_id'=>$this->user_id,'l.status'=>$status])
  45. ->alias('l.*,c.title,c.module,c.coupon_type,c.is_new')
  46. ->when($module,function ($query)use ($module){
  47. if($module > 1 ) $query->where('module','in','1,'. $module);
  48. })->leftJoin('CouponConfig c','c.id = l.coupon_id')
  49. ->limit($this->off_set,$this->page_num)
  50. ->order('l.id desc')
  51. ->select()->toArray();
  52. $this->success('ok',['list'=>$list]);
  53. }
  54. /**
  55. * @title 兑换码兑换优惠券
  56. * @desc 兑换码兑换优惠券
  57. * @author qc
  58. * @url /api/Coupon/getCouponByCode
  59. * @method POST
  60. * @header name:Authorization require:1 desc:Token
  61. * @param name:code type:string default:1 desc:兑换码
  62. *
  63. */
  64. public function getCouponByCode()
  65. {
  66. $code = input('post.code');
  67. if(!$code) $this->error('请输入兑换码');
  68. $coupon_list = CouponConfig::where(['status'=>1,'is_deleted'=>0])->select()->toArray();
  69. if(empty($coupon_list))$this->error('兑换码错误');
  70. $exchange_num = 0;
  71. foreach ($coupon_list as $v) {
  72. $has_num = UserCouponList::getUserCouponNum($this->user_id,$v['id']);
  73. if($has_num >= $v['user_num']) continue;
  74. $exchange_num++;
  75. UserCouponList::saveUserCoupon($v,$this->user_id);
  76. }
  77. $exchange_num ? $this->success('兑换成功') : $this->error('兑换失败');
  78. }
  79. }