审查视图

application/api/controller/Goods.php 15.6 KB
jinglong authored
1 2 3 4 5 6 7 8 9 10 11
<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Validate;
/**
 * 商品接口
 */
class Goods extends Api
{
12
    protected $noNeedLogin = ['goodsDetail','otherBrowseGoodsList','sortGoodsList'];
jinglong authored
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    protected $noNeedRight = ['*'];
    protected $uid = '';
    public function _initialize()
    {
        parent::_initialize();
        $this->uid = $this->auth->getUserId();
    }

    /**
     * @ApiTitle    (商品详情)
     * @ApiSummary  (商品详情)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/goods/goodsDetail)
     *
     * @ApiParams   (name="goods_id", type="inter", required=true, description="商品id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1574993089",
        "data": {
            "id": 1,//商品id
            "type": 1,
            "t_id": 1,
jinglong authored
37 38 39
            "file": [//视频路径,只有一个视频(无视频返回空数组)
                "http://jinglong.springchunjia.cn/uploads/20191127/05d216e117e11bd3de352c155b42eaaa.mp4",//视频路径
            ]
40
            "image": "http://jinglong.springchunjia.cn/uploads/20191127/05d216e117e11bd3de352c155b42eaaa.jpg",//缩略图路径
jinglong authored
41 42 43 44 45
            "name": "MONENT 动感系列",//商品名称
            "name_en": "Monent dynamic series",//商品名称(英文)
            "sale_price": 2599,//销售价格
            "market_price": 2699,//市场价格
            "expense_price": 0,//运费(0:显示包运费标签)
46
            "is_collection": 0,//是否收藏(0:否,1:是)
47
            "is_new_tag": 0//新人价格标签(0:不显示,1:显示)
48
            "is_use_number": 0//优惠券可领取数量
jinglong authored
49 50 51 52
            "sale_price": [//商品规格
                100,
                200
            ],
jinglong authored
53 54 55 56 57 58 59 60 61
            "style": [//商品规格
                "主餐匙,茶匙各1件",
                "古堡灰"
            ],
            "tag": [//商品标签
                "AB级",
                "ABX级",
                "ABN级"
            ],
jinglong authored
62 63 64 65
            "stock": [//商品标签
                100,
                100
            ],//商品库存
jinglong authored
66 67
            "introduce": "轻波款,为客厅缀上霞光淡雾",//商品简介
            "detail": "",//商品详情(富文本)
jinglong authored
68 69 70
            "images": [//轮播图片
                "http://jinglong.springchunjia.cn/uploads/20191127/05d216e117e11bd3de352c155b42eaaa.png",
                "http://jinglong.springchunjia.cn/uploads/20191127/05d216e117e11bd3de352c155b42eaaa.png",
jinglong authored
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
            ],
             "b_type": {//所属品牌(品类则返回空字符串)
                "id": 1,//品牌id
                "image": "http://jinglong.springchunjia.cn/uploads/20191127/d36d8475bc3bdb999dff26d019c608ed.png",//品牌图片路径
                "name": "HEDRMAR1",//品牌名称
                "address": "葡萄牙1"//品牌所属地
            }
        }
    })
     */
    public function goodsDetail(){
        if($this->request->isGet()){
            $goods_id = $this->request->get('goods_id');
            $rule = config('verify.goods_detail');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['goods_id'=>$goods_id])) {
                $this->error($validate->getError());
            }
90
            $res = Common::findSoftWhereData('goods',['id'=>$goods_id],'type_name,is_design,is_recommend,sort,createtime,updatetime,deletetime,flag,hots,sales',true);
jinglong authored
91 92 93
            if($res){
                $res['style'] = explode('|',$res['style']);
                $res['tag'] = explode('|',$res['tag']);
jinglong authored
94
                $res['sale_price'] = explode('|',$res['sale_price1']);
jinglong authored
95
                $res['stock'] = explode('|',$res['stock']);
96
                $res['image'] = $this->auth->absolutionUrlOne($res['image']);
jinglong authored
97 98 99 100 101
                $res['images'] = $this->auth->absolutionUrl($res['images']);
                if(!empty($res['file'])){
                    $res['file'] = [
                        $this->auth->absolutionUrlOne($res['file'])
                    ];
102 103 104
                    $res['file_cover'] = [
                        $this->auth->absolutionUrlOneFrame($res['file'],1)
                    ];
jinglong authored
105 106
                }else{
                    $res['file'] = [];
107
                    $res['file_cover'] = [];
jinglong authored
108
                }
jinglong authored
109 110 111 112 113 114 115 116 117
                //所属品牌分类
                if($res['type'] == 1){
                    //查询所属品牌
                    $b_type = Common::findSoftWhereData('btype',['id'=>$res['t_id']],'id,image,name,address');
                    $b_type['image'] = $this->auth->absolutionUrlOne($b_type['image']);
                    $res['b_type'] = $b_type;
                }else{
                    $res['b_type'] = '';
                }
jinglong authored
118
119 120 121 122 123 124
                $res1 = Common::findWhereData('collection',['uid'=>$this->uid,'g_id'=>$goods_id],'id');
                if($res1){
                    $res['is_collection'] = 1;//已收藏
                }else{
                    $res['is_collection'] = 0;//未收藏
                }
125
jinglong authored
126
                $is_news = Common::is_new($this->uid);
127 128 129 130 131 132 133 134 135 136 137
                if($is_news == 2 || $is_news == 0){//未登录或者新人
                    if($res['is_new'] == 0){
                        //非新人优惠标签
                        $res['is_new_tag'] = 0;//不用显示新人价标签
                    }else{
                        //新人优惠标签
                        $res['is_new_tag'] = 1;//显示新人价标签
                    }
                }else{
                    $res['is_new_tag'] = 0;//不用显示新人价标签(新人价标签商品不会出来)
                }
138 139 140 141 142 143 144 145 146 147 148

                //查询已经领取过
                $receive = Common::selectWhereData('rcoupon',['uid'=>$this->uid],'id,c_id');
                $receive_s = array_column($receive,'c_id');

                $type = config('verify.type');
                $flag = config('verify.flag');
                $where = ['id'=>['not in',$receive_s],'is_new'=>$flag[0],'type'=>$type[2],'bg_id'=>$goods_id,'end_time'=>['>',time()]];

                $coupon_number = Common::count_count('coupon',$where);
                $res['is_use_number'] = $coupon_number;
jinglong authored
149 150 151
                //浏览商品增加次数
                $goodsModel = new \app\admin\model\Goods();
                $goodsModel->where(['id'=>$goods_id])->setInc('hots',1);
152 153

                unset($res['is_new']);
jinglong authored
154 155 156 157

                //增加商品浏览数
                $type = config('verify.browse_type')[1];
                Common::statistics($type,$this->uid,$goods_id);
jinglong authored
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
            }
            $this->success('成功',$res);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (别人也在看商品列表)
     * @ApiSummary  (别人也在看商品列表)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/goods/otherBrowseGoodsList)
     *
     * @ApiParams   (name="goods_id", type="inter", required=true, description="商品id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1574941706",
        "data": {
            "data": [
                {
                    "id": 7,//商品id
                    "image": "http://jinglong.springchunjia.cn/uploads/20191128/8a677f5a0418059bf1b974c50026af13.png",//图片路径
                    "name": "MONENT 动感系列",//商品名称
                    "tag": [//商品标签
                        "日式简约",
                        "隐秘乡奢",
                        "家庭情侣"
                    ],
                    "sale_price": 2299//销售价格
                    "expense_price": //运费(0:显示包运费标签)
190
                    "is_new_tag": 0//新人价格标签(0:不显示,1:显示)
jinglong authored
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
                },
                {
                    "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 otherBrowseGoodsList(){
        if($this->request->isGet()){
            $goods_id = $this->request->get('goods_id');
            $rule = config('verify.goods_detail');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['goods_id'=>$goods_id])) {
                $this->error($validate->getError());
            }

            $result = Common::findSoftWhereData('goods',['id'=>$goods_id],'id,type,t_id');
            $where = [];
            if($result){
                $where['type'] = $result['type'];
                $where['t_id'] = $result['t_id'];
                $where['id'] = ['<>',$result['id']];
            }
225
            $limit = config('verify.limit');
jinglong authored
226
            $res = Common::goodsList($where,1,$this->uid,$limit,'hots desc,id desc');
jinglong authored
227 228 229 230 231 232
            $this->success('成功',$res);
        }else{
            $this->error('请求方式错误');
        }
    }
jinglong authored
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 261
    /**
     * @ApiTitle    (品类商品列表)
     * @ApiSummary  (品类商品列表)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/goods/sortGoodsList)
     *
     * @ApiParams   (name="s_id", type="inter", required=true, description="品类分类id")
     *
     * @ApiParams   (name="hots", type="inter", required=false, description="商品热度排序(1:降序,2:升序)")
     * @ApiParams   (name="sales", type="inter", required=false, description="商品销量排序(1:降序,2:升序)")
     * @ApiParams   (name="price", type="inter", required=false, description="商品价格排序(1:降序,2:升序)")
     *
     * @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
262 263 264 265
                    "style": [//商品规格
                        "主餐匙,茶匙各1件",
                        "古堡灰"
                    ],
jinglong authored
266 267
                    "sale_price": 2299//销售价格
                    "expense_price": //运费(0:显示包运费标签)
268
                    "is_new_tag": 0//新人价格标签(0:不显示,1:显示)
jinglong authored
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
                },
                {
                    "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 sortGoodsList(){
        if($this->request->isGet()){
            $s_id = $this->request->get('s_id');
            $page = $this->request->get('page');
            $rule = config('verify.sort_goods');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['s_id'=>$s_id,'page'=>$page])) {
                $this->error($validate->getError());
            }

            //排序
            $hots = $this->request->get('hots');
            $sales = $this->request->get('sales');
            $price = $this->request->get('price');
            $order = 'id desc';
jinglong authored
302
            $flag = '';
jinglong authored
303 304
            if($hots){
                if($hots == 1){
jinglong authored
305
                    $order = 'hots desc,id desc';
jinglong authored
306 307 308 309 310
                }else if($hots == 2){
                    $order = 'hots asc';
                }
            }else if($sales){
                if($sales == 1){
jinglong authored
311
                    $order = 'sales desc,id desc';
jinglong authored
312 313 314 315
                }else if($sales == 2){
                    $order = 'sales asc';
                }
            }else if($price){
316
                if($price == 1){
jinglong authored
317
                    $flag = 1;
318
                }else if($price == 2){
jinglong authored
319
                    $flag = 2;
320
                }
jinglong authored
321 322 323
            }
            $where = ['type'=>0,'t_id'=>$s_id];
            $limit = config('verify.goods_limit');
jinglong authored
324
            $arr = Common::goodsList($where,$page,$this->uid,$limit,$order,'',$flag);
jinglong authored
325 326 327 328 329 330
            $this->success('成功',$arr);
        }else{
            $this->error('请求方式错误');
        }
    }
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
    /**
     * @ApiTitle    (其他推荐商品列表)
     * @ApiSummary  (其他推荐商品列表)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/goods/otherRecommendGoodsList)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1574941706",
        "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
            },
        ],
    })
     */
    public function otherRecommendGoodsList(){
        if($this->request->isGet()){
            $order_id = $this->request->get('order_id');
            $rule = config('verify.order_detail');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['order_id'=>$order_id])) {
                $this->error($validate->getError());
            }
            //查询订单的商品所属品类id,品牌id
            $res_o_goods = Common::selectSoftWhereData('ogoods',['o_id'=>$order_id],'id,g_id');
            $goods_id_s = array_column($res_o_goods,'g_id');
            $res_goods = Common::selectSoftWhereData('goods',['id'=>['in',$goods_id_s]],'id,t_id');
            $t_id_s = array_column($res_goods,'t_id');
            $limit = config('verify.goods_limit');
            $arr = Common::goodsList(['t_id'=>['in',$t_id_s]],1,$this->uid,$limit,'hots desc',1);
            $this->success('成功',$arr);
        }else{
            $this->error('请求方式错误');
        }
    }
jinglong authored
398
}