审查视图

application/api/controller/News.php 9.9 KB
1 2 3 4
<?php

namespace app\api\controller;
5 6
use app\admin\model\Coupon;
use app\admin\model\Rcoupon;
7 8 9 10 11 12 13 14
use app\admin\model\Record;
use app\common\controller\Api;
use think\Validate;
/**
 * 新人特惠接口
 */
class News extends Api
{
15
    protected $noNeedLogin = ['newsCouponList','newsGoodsList'];
16 17 18 19 20
    protected $noNeedRight = ['*'];
    protected $uid = '';
    public function _initialize()
    {
        parent::_initialize();
21
        $this->uid = $this->auth->getUserId();
22 23 24
    }

    /**
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
     * @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);
54 55 56 57 58 59 60 61 62 63 64 65
            $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']);
                }
            }
66 67 68 69 70
            $arr = [];
            $k = 0;
            foreach($coupon_id_s as $value){
                $k+=0;
                if(!in_array($value,$receive_s)){
71 72 73 74 75 76 77
                    //不在已领取
                    if(!in_array($value,$gift_id)){
                        //不在分享券
                        $arr[$k]['c_id'] = $value;
                        $arr[$k]['uid'] = $this->uid;
                        $k++;
                    }
78 79 80 81 82 83 84
                }
            }
            $r_couponModel = new Rcoupon();
            $res = $r_couponModel->saveAll($arr);
            if($res){
                //减优惠券数量
                $couponModel = new Coupon();
85 86 87 88 89 90 91 92 93 94 95 96 97 98
                //获取已领取的优惠券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('优惠券中包含分享优惠券,请请分享后领取');
                    }
                }
99
            }else{
100 101 102 103 104 105 106 107 108 109 110 111 112
                if($is_gift == 0){
                    //无分享券
                    $this->error('失败');
                }else{
                    //有分享券
                    if($coupon_count == 1){
                        //领取单张优惠券
                        $this->error('该优惠券为分享优惠券,请分享后领取');
                    }else{
                        //一键领取多张优惠券(新人券)
                        $this->error('失败');
                    }
                }
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
            }
        }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');
152 153
            $current_time = time();
            $where = ['is_new'=>$flag[1],'coupon_number'=>['<>',0],'start_time'=>['<',$current_time],'end_time'=>['>',$current_time]];
154 155 156
            //查询已经领取过
            $receive = Common::selectWhereData('rcoupon',['uid'=>$this->uid],'id,c_id');
            $receive_s = array_column($receive,'c_id');
jinglong authored
157
            $data = Common::selectWhereData('coupon',$where,'id,c_type,coupon_type,full_reduce,reduce,discount','sort desc,id desc');
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
            $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('请求方式错误');
        }
    }

    /**
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
     * @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": [//商品标签
                        "日式简约",
                        "隐秘乡奢",
                        "家庭情侣"
                    ],
jinglong authored
226 227 228 229
                    "style": [//商品规格
                        "主餐匙,茶匙各1件",
                        "古堡灰"
                    ],
230 231
                    "sale_price": 2299//销售价格
                    "expense_price": //运费(0:显示包运费标签)
232
                    "is_new_tag": 0//新人价格标签(0:不显示,1:显示)
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
                },
                {
                    "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');
261
            $where = ['is_new'=>$flag[1]];
262
            $limit = config('verify.goods_limit');
jinglong authored
263
            $arr = Common::goodsList($where,$page,$this->uid,$limit,'hots desc,id desc');
264 265 266 267 268 269 270
            $this->success('成功',$arr);
        }else{
            $this->error('请求方式错误');
        }
    }

}