<?php namespace app\api\controller; use app\admin\model\Coupon; use app\admin\model\Rcoupon; use app\admin\model\Record; use app\common\controller\Api; use think\Validate; /** * 新人特惠接口 */ class News extends Api { protected $noNeedLogin = ['newsCouponList','newsGoodsList']; protected $noNeedRight = ['*']; protected $uid = ''; public function _initialize() { parent::_initialize(); $this->uid = $this->auth->getUserId(); } /** * @ApiTitle (领取优惠券) * @ApiSummary (领取优惠券) * @ApiMethod (POST) * @ApiRoute (/api/news/receiveAllCoupon) * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") * * @ApiParams (name="coupon_id", type="string", required=true, description="优惠券id(多个以逗号隔开)") * * @ApiReturn({ "code": 1, "msg": "成功", "time": "1575358991", "data": null }) */ public function receiveAllCoupon(){ if($this->request->isPost()){ $coupon_id = $this->request->post('coupon_id'); $rule = config('verify.receive_coupon'); $validate = new Validate($rule['rule'],$rule['msg']); if (!$validate->check(['coupon_id'=>$coupon_id])) { $this->error($validate->getError()); } //查询已经领取过 $receive = Common::selectWhereData('rcoupon',['uid'=>$this->uid],'id,c_id'); $receive_s = array_column($receive,'c_id'); $coupon_id_s = explode(',',$coupon_id); $coupon_count = count($coupon_id_s); $gift = Common::selectWhereData('coupon',['id'=>['in',$coupon_id_s]],'id,gift'); //判断该优惠券是否属于分享优惠券 $is_gift = 0; $gift_id = []; foreach($gift as $g_value){ if($g_value['gift'] != 0){ //分享券 $is_gift = 1; array_push($gift_id,$g_value['id']); } } $arr = []; $k = 0; foreach($coupon_id_s as $value){ $k+=0; if(!in_array($value,$receive_s)){ //不在已领取 if(!in_array($value,$gift_id)){ //不在分享券 $arr[$k]['c_id'] = $value; $arr[$k]['uid'] = $this->uid; $k++; } } } $r_couponModel = new Rcoupon(); $res = $r_couponModel->saveAll($arr); if($res){ //减优惠券数量 $couponModel = new Coupon(); //获取已领取的优惠券id $receive_coupon_id = array_column($arr,'c_id'); $couponModel->whereIn('id',$receive_coupon_id)->setDec('coupon_number',1); if($is_gift == 0){ //无分享券 $this->success('成功'); }else{ //有分享券 if($coupon_count != 1){ //一键领取多张优惠券(新人券) $this->success('优惠券中包含分享优惠券,请请分享后领取'); } } }else{ if($is_gift == 0){ //无分享券 $this->error('失败'); }else{ //有分享券 if($coupon_count == 1){ //领取单张优惠券 $this->error('该优惠券为分享优惠券,请分享后领取'); }else{ //一键领取多张优惠券(新人券) $this->error('失败'); } } } }else{ $this->error('请求方式错误'); } } /** * @ApiTitle (新人专享礼包) * @ApiSummary (新人专享礼包) * @ApiMethod (GET) * @ApiRoute (/api/news/newsCouponList) * * @ApiReturn({ "code": 1, "msg": "成功", "time": "1575352892", "data":[ { "id": 4,//优惠券id "is_receive": 0,//是否领取(0:否,1:是) "coupon_tag": "无门槛",//优惠券(无门槛或满减金额) "coupon_price": "8.5折"//(折扣或减少金额) }, { "id": 2, "coupon_tag": "无门槛", "coupon_price": "¥300" }, { "id": 1, "coupon_tag": "满1000可用", "coupon_price": "¥200" } ] }) */ public function newsCouponList(){ if($this->request->isGet()){ $flag = config('verify.flag'); $current_time = time(); $where = ['is_new'=>$flag[1],'coupon_number'=>['<>',0],'start_time'=>['<',$current_time],'end_time'=>['>',$current_time]]; //查询已经领取过 $receive = Common::selectWhereData('rcoupon',['uid'=>$this->uid],'id,c_id'); $receive_s = array_column($receive,'c_id'); $data = Common::selectWhereData('coupon',$where,'id,c_type,coupon_type,full_reduce,reduce,discount','sort desc,id desc'); $res = []; foreach($data as $key=>$value){ $res[$key]['id'] = $value['id']; $res[$key]['is_receive'] = 0;//未领取 if(in_array($value['id'],$receive_s)){ $res[$key]['is_receive'] = 1;//已领取 } //无门槛,有门槛 if($value['c_type'] == 0){ //无门槛 if($value['coupon_type'] == 0){ //减少券,去掉满减金额,折扣字段, unset($value['full_reduce']); unset($value['discount']); $res[$key]['coupon_tag'] = '无门槛'; $res[$key]['coupon_price'] = '¥'.$value['reduce']; }else{ //折扣券,去掉满减金额,减少金额字段, unset($value['full_reduce']); unset($value['reduce']); $res[$key]['coupon_tag'] = '无门槛'; $res[$key]['coupon_price'] = $value['discount'].'折'; } }else{ //有门槛 if($value['coupon_type'] == 0){ //减少券,去掉折扣字段, unset($value['discount']); $res[$key]['coupon_tag'] = '满'.$value['full_reduce'].'可用'; $res[$key]['coupon_price'] = '¥'.$value['reduce']; }else{ //折扣券,去掉减少金额字段, unset($value['reduce']); $res[$key]['coupon_tag'] = '满'.$value['full_reduce'].'可用'; $res[$key]['coupon_price'] = $value['discount'].'折'; } } } $this->success('成功',$res); }else{ $this->error('请求方式错误'); } } /** * @ApiTitle (新人特惠商品列表) * @ApiSummary (新人特惠商品列表) * @ApiMethod (GET) * @ApiRoute (/api/news/newsGoodsList) * * @ApiParams (name="page", type="inter", required=true, description="分页页码") * * @ApiReturn({ "code": 1, "msg": "成功", "time": "1574941706", "data": { "data": [ { "id": 7,//商品id "image": "http://jinglong.springchunjia.cn/uploads/20191128/8a677f5a0418059bf1b974c50026af13.png",//图片路径 "name": "MONENT 动感系列",//商品名称 "tag": [//商品标签 "日式简约", "隐秘乡奢", "家庭情侣" ], "style": [//商品规格 "主餐匙,茶匙各1件", "古堡灰" ], "sale_price": 2299//销售价格 "expense_price": //运费(0:显示包运费标签) "is_new_tag": 0//新人价格标签(0:不显示,1:显示) }, { "id": 4, "image": "http://jinglong.springchunjia.cn/uploads/20191128/93971e55b83d1a09c1831f8197514305.png", "name": "MONENT 动感系列", "tag": [ "AB级", "ABX级", "ABN级" ], "new_price": 2499, "sale_price": 2599 }, ], "total_page": 1 } }) */ public function newsGoodsList(){ if($this->request->isGet()){ $page = $this->request->get('page'); $rule = config('verify.page'); $validate = new Validate($rule['rule'],$rule['msg']); if (!$validate->check(['page'=>$page])) { $this->error($validate->getError()); } $flag = config('verify.flag'); $where = ['is_new'=>$flag[1]]; $limit = config('verify.goods_limit'); $arr = Common::goodsList($where,$page,$this->uid,$limit,'hots desc,id desc'); $this->success('成功',$arr); }else{ $this->error('请求方式错误'); } } }