Goods.php 11.3 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Validate;
/**
 * 商品接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    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,
            "file": "http://jinglong.springchunjia.cn/uploads/20191127/05d216e117e11bd3de352c155b42eaaa.mp4",//视频路径
            "name": "MONENT 动感系列",//商品名称
            "name_en": "Monent dynamic series",//商品名称(英文)
            "new_price": 2499,//新人价格
            "sale_price": 2599,//销售价格
            "market_price": 2699,//市场价格
            "expense_price": 0,//运费(0:显示包运费标签)
            "style": [//商品规格
                "主餐匙,茶匙各1件",
                "古堡灰"
            ],
            "tag": [//商品标签
                "AB级",
                "ABX级",
                "ABN级"
            ],
            "stock": 100,//商品库存
            "introduce": "轻波款,为客厅缀上霞光淡雾",//商品简介
            "detail": "",//商品详情(富文本)
            "frame": [//视频第一到第六帧图片(无视频返回空数组)
                "http://jinglong.springchunjia.cn/uploads/20191127/05d216e117e11bd3de352c155b42eaaa.mp4?vframe/jpg/offset/1",
                "http://jinglong.springchunjia.cn/uploads/20191127/05d216e117e11bd3de352c155b42eaaa.mp4?vframe/jpg/offset/2",
                "http://jinglong.springchunjia.cn/uploads/20191127/05d216e117e11bd3de352c155b42eaaa.mp4?vframe/jpg/offset/3",
                "http://jinglong.springchunjia.cn/uploads/20191127/05d216e117e11bd3de352c155b42eaaa.mp4?vframe/jpg/offset/4",
                "http://jinglong.springchunjia.cn/uploads/20191127/05d216e117e11bd3de352c155b42eaaa.mp4?vframe/jpg/offset/5",
                "http://jinglong.springchunjia.cn/uploads/20191127/05d216e117e11bd3de352c155b42eaaa.mp4?vframe/jpg/offset/6"
            ],
             "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());
            }

            $res = Common::findSoftWhereData('goods',['id'=>$goods_id],'type_name,is_design,is_recommend,sort,image,createtime,updatetime,deletetime,flag,hots,sales',true);
            if($res){
                if($res['file']){
                    //图片取视频帧
                    $frame = [];
                    for($i=1;$i<7;$i++){
                        $image_url = $this->auth->absolutionUrlOneFrame($res['file'],$i);
                        array_push($frame,$image_url);
                    }
                    $res['frame'] = $frame;
                    $res['file'] = $this->auth->absolutionUrlOne($res['file']);
                }else{
                    $res['frame'] = [];
                    $res['file'] = '';
                }
                $res['style'] = explode('|',$res['style']);
                $res['tag'] = explode('|',$res['tag']);
                //所属品牌分类
                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'] = '';
                }

                //浏览商品增加次数
                $goodsModel = new \app\admin\model\Goods();
                $goodsModel->where(['id'=>$goods_id])->setInc('hots',1);
            }
            $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": [//商品标签
                        "日式简约",
                        "隐秘乡奢",
                        "家庭情侣"
                    ],
                    "new_price": 2199,//新人价格(未登录,未购买记录(个人中心返是否有购买记录),显示该字段,另加“新人价”标签)
                    "sale_price": 2299//销售价格
                    "expense_price": //运费(0:显示包运费标签)
                },
                {
                    "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']];
            }
            $res = Common::selectSoftWhereLimitData('goods',$where,'id,image,name,tag,new_price,sale_price,expense_price');
            foreach ($res as &$value){
                $value['image'] = $this->auth->absolutionUrlOne($value['image']);
                $value['tag'] = explode('|',$value['tag']);
            }
            $this->success('成功',$res);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @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": [//商品标签
                        "日式简约",
                        "隐秘乡奢",
                        "家庭情侣"
                    ],
                    "style": [//商品规格
                        "主餐匙,茶匙各1件",
                        "古堡灰"
                    ],
                    "new_price": 2199,//新人价格(未登录,未购买记录(个人中心返是否有购买记录),显示该字段,另加“新人价”标签)
                    "sale_price": 2299//销售价格
                    "expense_price": //运费(0:显示包运费标签)
                },
                {
                    "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';
            if($hots){
                if($hots == 1){
                    $order = 'hots desc,id desc';
                }else if($hots == 2){
                    $order = 'hots asc';
                }
            }else if($sales){
                if($sales == 1){
                    $order = 'sales desc,id desc';
                }else if($sales == 2){
                    $order = 'sales asc';
                }
            }else if($price){

            }
            $where = ['type'=>0,'t_id'=>$s_id];
            $limit = config('verify.goods_limit');
            $arr = Common::goodsList($where,$page,$limit,$order);
            $this->success('成功',$arr);
        }else{
            $this->error('请求方式错误');
        }
    }

}