1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace app\api\controller;
- use app\common\model\CouponConfig;
- use app\common\model\UserCouponList;
- use think\Db;
- use function Symfony\Component\String\s;
- /**
- * @title 优惠券管理
- * @controller Coupon
- * @package app\api\controller
- */
- class Coupon extends Base
- {
- public function initialize(){
- parent::initialize();
- parent::checkLogin();
- }
- /**
- * @title 获取会员优惠券列表
- * @desc 获取会员优惠券列表
- * @author qc
- * @url /api/Coupon/getUserCouponList
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:page type:int default:1 desc:页数
- * @param name:page_num type:int default:20 desc:每页数
- * @param name:status type:int default:1 desc:状态码(1未使用2已使用3已过期)
- * @param name:module type:int default:1 desc:应用模块(1所有,2洗鞋,3洗衣)【订单确认页面可用】
- * @return name:title type:string default:-- desc:优惠券名称
- * @return name:module type:int default:-- desc:应用模块(1所有,2洗鞋,3洗衣)
- * @return name:coupon_type type:int default:-- desc:类型(1满减券,2抵扣券【抵一件衣服或是一双鞋价格】)
- * @return name:is_new type:int default:-- desc:是否新人券(0否1是)
- * @return name:low_amount type:int default:-- desc:最低消费限制(coupon_type=1时用)
- * @return name:amount type:int default:-- desc:抵扣金额(coupon_type=1时用)
- * @return name:create_at type:string default:-- desc:领取时间
- * @return name:past_at type:string default:-- desc:过期时间
- * @return name:status type:int default:-- desc:状态码(1未使用2已使用3已过期)
- *
- */
- public function getUserCouponList()
- {
- $status = input('get.status',1);
- $module = input('get.module',1);
- $list = UserCouponList::where(['l.user_id'=>$this->user_id,'l.status'=>$status])
- ->alias('l.*,c.title,c.module,c.coupon_type,c.is_new')
- ->when($module,function ($query)use ($module){
- if($module > 1 ) $query->where('module','in','1,'. $module);
- })->leftJoin('CouponConfig c','c.id = l.coupon_id')
- ->limit($this->off_set,$this->page_num)
- ->order('l.id desc')
- ->select()->toArray();
- $this->success('ok',['list'=>$list]);
- }
- /**
- * @title 兑换码兑换优惠券
- * @desc 兑换码兑换优惠券
- * @author qc
- * @url /api/Coupon/getCouponByCode
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:code type:string default:1 desc:兑换码
- *
- */
- public function getCouponByCode()
- {
- $code = input('post.code');
- if(!$code) $this->error('请输入兑换码');
- $coupon_list = CouponConfig::where(['status'=>1,'is_deleted'=>0])->select()->toArray();
- if(empty($coupon_list))$this->error('兑换码错误');
- $exchange_num = 0;
- foreach ($coupon_list as $v) {
- $has_num = UserCouponList::getUserCouponNum($this->user_id,$v['id']);
- if($has_num >= $v['user_num']) continue;
- $exchange_num++;
- UserCouponList::saveUserCoupon($v,$this->user_id);
- }
- $exchange_num ? $this->success('兑换成功') : $this->error('兑换失败');
- }
- }
|