Goods.php 15.9 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Validate;
/**
 * 商品接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['goodsDetail','otherBrowseGoodsList','sortGoodsList'];
    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",//视频路径
            "image": "http://jinglong.springchunjia.cn/uploads/20191127/05d216e117e11bd3de352c155b42eaaa.jpg",//缩略图路径
            "name": "MONENT 动感系列",//商品名称
            "name_en": "Monent dynamic series",//商品名称(英文)
            "sale_price": 2599,//销售价格
            "market_price": 2699,//市场价格
            "expense_price": 0,//运费(0:显示包运费标签)
            "is_collection": 0,//是否收藏(0:否,1:是)
            "is_new_tag": 0//新人价格标签(0:不显示,1:显示)
            "is_use_number": 0//优惠券可领取数量
            "sale_price": [//商品规格
                100,
                200
            ],
            "style": [//商品规格
                "主餐匙,茶匙各1件",
                "古堡灰"
            ],
            "tag": [//商品标签
                "AB级",
                "ABX级",
                "ABN级"
            ],
            "stock": [//商品标签
                100,
                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,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']);
                $res['sale_price'] = explode('|',$res['sale_price1']);
                $res['stock'] = explode('|',$res['stock']);
                $res['image'] = $this->auth->absolutionUrlOne($res['image']);
                //所属品牌分类
                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'] = '';
                }

                $res1 = Common::findWhereData('collection',['uid'=>$this->uid,'g_id'=>$goods_id],'id');
                if($res1){
                    $res['is_collection'] = 1;//已收藏
                }else{
                    $res['is_collection'] = 0;//未收藏
                }

                $is_news = Common::is_new($this->uid);
                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;//不用显示新人价标签(新人价标签商品不会出来)
                }

                //查询已经领取过
                $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;
                //浏览商品增加次数
                $goodsModel = new \app\admin\model\Goods();
                $goodsModel->where(['id'=>$goods_id])->setInc('hots',1);

                unset($res['is_new']);
            }
            $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:显示包运费标签)
                    "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 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']];
            }
            $limit = config('verify.limit');
            $res = Common::goodsList($where,1,$this->uid,$limit,'hots desc,id desc');
            $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件",
                        "古堡灰"
                    ],
                    "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 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';
            $flag = '';
            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){
                if($price == 1){
                    $flag = 1;
                }else if($price == 2){
                    $flag = 2;
                }
            }
            $where = ['type'=>0,'t_id'=>$s_id];
            $limit = config('verify.goods_limit');
            $arr = Common::goodsList($where,$page,$this->uid,$limit,$order,'',$flag);
            $this->success('成功',$arr);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @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('请求方式错误');
        }
    }

}