request->isPost()) { $this->error('请求方式异常'); } $uid = $this->auth->id; $post = input('post.','','trim,strip_tags'); if(isset($post['shop_id']) && $post['shop_id'] != '') { $where['shop_id'] = $post['shop_id']; } $where['is_check'] = 1; $where['type'] = ['<>',4]; $where['starttime'] = ['<=',time()]; $where['endtime'] = ['>=',time()]; $list = model('app\api\model\service\Coupon') ->where($where) ->field('id,type,goods_id,shop_id,achieve,reduce,effective_day,starttime,endtime') ->order('id desc') ->select(); foreach ($list as $key=>$value) { $exist = \app\api\model\service\UserCoupon::where(['user_id'=>$uid,'coupon_id'=>$value['id']])->field('id,state,exittime')->find(); if($exist && $exist['exittime'] <=time()) { unset($list[$key]); } $list[$key]['userState'] = $exist?1:0; $list[$key]['goodsName'] = $value['goods_id']?\app\api\model\service\Goods::where(['id'=>$value['goods_id']])->value('name'):''; $list[$key]['shopName'] = $value['shop_id']?\app\api\model\service\Shop::getName($value['shop_id']):''; } $list = array_values($list); $this->success('返回成功', $list); } /** * 领取优惠券 * @return void * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function receive() { if (!$this->request->isPost()) { $this->error('请求方式异常'); } $uid = $this->auth->id; $type = input('type/d',''); if($type == 0){ $coupon_id = input('coupon_id/d',''); $where['id'] = $coupon_id; }elseif ($type == 1){ $code = input('code',''); $where['code'] = $code; } $where['starttime'] = ['<',time()]; $where['endtime'] = ['>',time()]; $couponInfo = model('app\api\model\service\Coupon') ->where($where) ->find(); !$couponInfo && $this->error('优惠券信息异常'); $receiveExist = model('app\api\model\service\UserCoupon')->where(['coupon_id'=>$couponInfo['id'],'user_id'=>$uid])->count(); $receiveExist > 0 && $this->error('请勿重复领取'); \app\api\model\service\UserCoupon::create(['user_id'=>$uid,'coupon_id'=>$couponInfo['id'],'type'=>$couponInfo['type'],'goods_id'=>$couponInfo['goods_id'],'shop_id'=>$couponInfo['shop_id'],'code'=>$couponInfo['code'],'achieve'=>$couponInfo['achieve'],'reduce'=>$couponInfo['reduce'],'exittime'=>86400*$couponInfo['effective_day']+time(),'state'=>0]); if($type == 1) { \app\api\model\service\Coupon::where('id',$couponInfo['id'])->update(['endtime'=>time()]); } $this->success('领取成功'); } /** * 用户优惠券列表 * @return void */ public function userCoupon() { if (!$this->request->isPost()) { $this->error('请求方式异常'); } $uid = $this->auth->id; $post = input('post.','','trim,strip_tags'); $post['user_id'] = $uid; $list = \app\api\model\service\UserCoupon::getList($post); $this->success('信息返回成功',$list); } }