| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace app\api\controller\service;
- use app\common\controller\Api;
- /**
- * 首页接口
- */
- class Coupon extends Api
- {
- protected $noNeedLogin = [''];
- protected $noNeedRight = ['*'];
- /**
- * 优惠券列表
- * @return void
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getList()
- {
- if (!$this->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);
- }
- }
|