Coupons.php 14.6 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Validate;
/**
 * 优惠券接口
 */
class Coupons extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $uid = '';
    public function _initialize()
    {
        parent::_initialize();
        $this->uid = $this->auth->getUserId();
    }

    /**
     * @ApiTitle    (领券中心)
     * @ApiSummary  (领券中心)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/coupons/couponCenterList)
     *
     * @ApiParams   (name="page", type="inter", required=true, description="分页页码")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1575365883",
        "data": {
            "data": [
                {
                    "id": 2,//优惠券id
                    "coupon_tag": "无门槛",//优惠券(无门槛,折扣券,满减券)
                    "coupon_price": "¥300",//(折扣或减少金额)
                    "coupon_tag1": "无门槛",优惠券(无门槛,满多少可用)
                    "is_receive": 0,//是否领取(0:否,1:是)
                    "coupon_name": "全场优惠券",//优惠券名称
                    "coupon_number": 100,//优惠券剩余数量(0:领光了)
                    "type": "全场通用"//优惠券用途
                },
                {
                    "id": 11,
                    "coupon_tag": "无门槛",
                    "coupon_price": "¥100",
                    "coupon_tag1": "无门槛",
                    "is_receive": 0,
                    "coupon_name": "品牌箱包优惠券",
                    "coupon_number": 100,
                    "type": "部分品牌可用"
                },
            ],
            "total_page": 1//总页码
        }
    })
     */
    public function couponCenterList(){
        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');
            $current_time = time();
            $where = ['is_new'=>$flag[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');
            $limit = config('verify.goods_limit');
            $data = Common::selectWherePageData('coupon',$where,'id,type,coupon_name,c_type,coupon_type,full_reduce,reduce,discount,coupon_number',$page,$limit,'sort desc,id desc');
            $res = [];
            $arr = [];
            foreach($data as $key=>$value){

                $res[$key]['id'] = $value['id'];
                //无门槛,有门槛
                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'];
                        $res[$key]['coupon_tag1'] = '无门槛';
                    }else{
                        //折扣券,去掉满减金额,减少金额字段,
                        unset($value['full_reduce']);
                        unset($value['reduce']);
                        $res[$key]['coupon_tag'] = '折扣券';
                        $res[$key]['coupon_price'] = $value['discount'].'折';
                        $res[$key]['coupon_tag1'] = '无门槛';
                    }
                }else{
                    //有门槛
                    if($value['coupon_type'] == 0){
                        //减少券,去掉折扣字段,
                        unset($value['discount']);
                        $res[$key]['coupon_tag'] = '满减券';
                        $res[$key]['coupon_price'] = '¥'.$value['reduce'];
                        $res[$key]['coupon_tag1'] = '满'.$value['full_reduce'].'可用';
                    }else{
                        //折扣券,去掉减少金额字段,
                        unset($value['reduce']);
                        $res[$key]['coupon_tag'] = '折扣券';
                        $res[$key]['coupon_price'] = $value['discount'].'折';
                        $res[$key]['coupon_tag1'] = '满'.$value['full_reduce'].'可用';
                    }
                }

                $res[$key]['is_receive'] = 0;//未领取
                if(in_array($value['id'],$receive_s)){
                    $res[$key]['is_receive'] = 1;//已领取
                }
                $res[$key]['coupon_name'] = $value['coupon_name'];//优惠券名称
                $res[$key]['coupon_number'] = $value['coupon_number'];//优惠券剩余数量
                //全场,品牌,商品
                if($value['type'] == 0){
                    $res[$key]['type'] = '全场通用';

                }else if($value['type'] == 1){
                    $res[$key]['type'] = '部分品牌可用';

                }else{
                    $res[$key]['type'] = '部分商品可用';

                }
            }
            $arr['data'] = $res;
            //总页数
            $arr['total_page'] = Common::count('coupon',$where,$limit);

            $this->success('成功',$arr);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (品牌优惠券)
     * @ApiSummary  (品牌优惠券)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/coupons/brandCouponList)
     *
     * @ApiParams   (name="b_id", type="inter", required=true, description="品牌分类id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1575367045",
        "data": [
            {
                "id": 5,优惠券id
                "coupon_number": 100,//优惠券剩余数量(0:显示领光了)
                "is_receive": 0,//是否领取(0:否,1:是)
                "coupon_tag": "无门槛",//优惠券(无门槛或满减金额)
                "coupon_price": "¥100"//(折扣或减少金额)
            },
            {
                "id": 11,
                "coupon_number": 100,
                "is_receive": 0,
                "coupon_tag": "无门槛",
                "coupon_price": "¥100"
            }
        ]
    })
     */
    public function brandCouponList(){
        if($this->request->isGet()){
            $b_id = $this->request->get('b_id');
            $rule = config('verify.brand_detail');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['b_id'=>$b_id])) {
                $this->error($validate->getError());
            }

            $flag = config('verify.flag');
            $current_time = time();
            $where = ['is_new'=>$flag[0],'type'=>$flag[1],'bg_id'=>$b_id,'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,coupon_number','sort desc,id desc');
            $res = [];
            foreach($data as $key=>$value){
                $res[$key]['id'] = $value['id'];
                $res[$key]['coupon_number'] = $value['coupon_number'];

                $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/coupons/goodsCouponList)
     *
     * @ApiParams   (name="goods_id", type="inter", required=true, description="商品id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1575372162",
        "data": [
            {
                "id": 2,//优惠券id
                "coupon_tag": "无门槛",//优惠券(无门槛,折扣券,满减券)
                "coupon_price": "¥300",//(折扣或减少金额)
                "coupon_tag1": "无门槛",优惠券(无门槛,满多少可用)
                "is_receive": 0,//是否领取(0:否,1:是)
                "coupon_name": "全场优惠券",//优惠券名称
                "coupon_number": 100,//优惠券剩余数量(0:领光了)
                "type": "全场通用"//优惠券用途
            },
            {
                "id": 2,//优惠券id
                "coupon_tag": "无门槛",//优惠券(无门槛,折扣券,满减券)
                "coupon_price": "¥300",//(折扣或减少金额)
                "coupon_tag1": "无门槛",优惠券(无门槛,满多少可用)
                "is_receive": 0,//是否领取(0:否,1:是)
                "coupon_name": "全场优惠券",//优惠券名称
                "coupon_number": 100,//优惠券剩余数量(0:领光了)
                "type": "全场通用"//优惠券用途
            }
        ]
    })
     */
    public function goodsCouponList(){
        if($this->request->isGet()){
            $goods_id = $this->request->get('goods_id');
            $rule = config('verify.goods_coupon');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['goods_id'=>$goods_id])) {
                $this->error($validate->getError());
            }

            $type = config('verify.type');
            $current_time = time();
            $where = ['type'=>$type[2],'bg_id'=>$goods_id,'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,type,coupon_name,c_type,coupon_type,full_reduce,reduce,discount,coupon_number','sort desc,id desc');
            $res = [];

            foreach($data as $key=>$value){

                $res[$key]['id'] = $value['id'];
                //无门槛,有门槛
                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'];
                        $res[$key]['coupon_tag1'] = '无门槛';
                    }else{
                        //折扣券,去掉满减金额,减少金额字段,
                        unset($value['full_reduce']);
                        unset($value['reduce']);
                        $res[$key]['coupon_tag'] = '折扣券';
                        $res[$key]['coupon_price'] = $value['discount'].'折';
                        $res[$key]['coupon_tag1'] = '无门槛';
                    }
                }else{
                    //有门槛
                    if($value['coupon_type'] == 0){
                        //减少券,去掉折扣字段,
                        unset($value['discount']);
                        $res[$key]['coupon_tag'] = '满减券';
                        $res[$key]['coupon_price'] = '¥'.$value['reduce'];
                        $res[$key]['coupon_tag1'] = '满'.$value['full_reduce'].'可用';
                    }else{
                        //折扣券,去掉减少金额字段,
                        unset($value['reduce']);
                        $res[$key]['coupon_tag'] = '折扣券';
                        $res[$key]['coupon_price'] = $value['discount'].'折';
                        $res[$key]['coupon_tag1'] = '满'.$value['full_reduce'].'可用';
                    }
                }

                $res[$key]['is_receive'] = 0;//未领取
                if(in_array($value['id'],$receive_s)){
                    $res[$key]['is_receive'] = 1;//已领取
                }
                $res[$key]['coupon_name'] = $value['coupon_name'];//优惠券名称
                $res[$key]['coupon_number'] = $value['coupon_number'];//优惠券剩余数量
                //全场,品牌,商品
                $res[$key]['type'] = '本商品可用';
            }
            $this->success('成功',$res);
        }else{
            $this->error('请求方式错误');
        }
    }

}