Goods.php 12.2 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\Search;
use think\Db;
use think\Validate;

/**
 * 商品接口**
 */
class Goods extends Api
{
    protected  $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $user_id = '';//token存贮user_id
    protected $normal = '';//商品正常状态,1:下架
    protected $is_search = [0,1];//检索到数据 0:是 1:否
    public function _initialize()
    {
        parent::_initialize();
        $this->normal = config('site.goods_status');
        $this->user_id = $this->auth->getUserId();
    }

    /**
     * @ApiTitle    (对应分类的商品列表)
     * @ApiSummary  (对应分类的商品列表)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/goods/getGoodsList)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="分类id")
     * @ApiParams   (name="page", type="integer", required=true, description="分页页码")
     * @ApiReturn({
            "code": 1,
            "msg": "成功",
            "time": "1553831462",
            "data": [
                {
                    "id": 1,
                    "type": 0,//类型 0:个人 1:企业
                    "title": "我的垃圾商品1",//商品名称
                    "address": "河南省邓州市",//上门地址
                    "pre_time": "2019-03-19 14:36:08",//预约时间
                    "images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg"
                },
                {
                    "id": 3,
                    "type": 0,
                    "title": "废品商品2",
                    "address": "河南省邓州市",
                    "pre_time": "2019-03-19 19:14:07",
                    "images": "/uploads/20190328/c7eab178376c71ae1892cd23a1fb5779.jpg"
                }
            ]
            })
     */
    public function getGoodsList(){
        if($this->request->isGet()){
            $category_id = $this->request->get('id');//分类id
            $page = $this->request->get('page');//分页页码
            $limit = config('site.page_limit');//分页限制数量
            $rule = config('site.pages');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['id'=>$category_id,'page'=>$page])) {
                $this->error($validate->getError());
            }
            $data = Db::table('gc_product')
                ->alias('p')
                ->join('gc_user u','p.uid = u.id','LEFT')
                ->where(['p.category_id'=>$category_id,'p.status'=>$this->normal,'u.status'=>'normal'])
                ->page($page,$limit)
                ->field('p.id,u.type,p.title,u.address,p.pre_time,p.images')
                ->select();
            foreach($data as &$value){
                $value['pre_time'] = date('Y-m-d H:i:s',$value['pre_time']);
            }
            $this->success('成功', $data);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (搜索对应分类的关键字商品列表)
     * @ApiSummary  (搜索对应分类的关键字商品列表)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/goods/searchKey)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="分类id")
     * @ApiParams   (name="keywords", type="string", required=true, description="关键字")
     * @ApiParams   (name="page", type="integer", required=true, description="分页页码")
     * @ApiReturn({
            "code": 1,
            "msg": "成功",
            "time": "1553831605",
            "data": [
                {
                    "id": 1,
                    "type": 0,//类型 0:个人 1:企业
                    "title": "我的垃圾商品1",//商品名称
                    "address": "河南省邓州市",//上门地址
                    "pre_time": "2019-03-19 14:36:08",//预约时间
                    "images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg"
                }
            ]
            })
     */
    public function searchKey(){
        if($this->request->isGet()){
            $category_id = $this->request->get('id');//分类id
            $rule = config('site.keys');
            $validate = new Validate($rule['rule'],$rule['msg']);
            $keywords = $this->request->get('keywords');//关键字
            $page = $this->request->get('page');//分页页码
            $limit = config('site.page_limit');//分页限制数量
            if (!$validate->check(['id'=>$category_id,'keywords'=>$keywords,'page'=>$page])) {
                $this->error($validate->getError());
            }
            $data = Db::table('gc_product')
                ->alias('p')
                ->join('gc_user u','p.uid = u.id','LEFT')
                ->where('p.title','like','%'.$keywords.'%')
                ->where(['p.category_id'=>$category_id,'p.status'=>$this->normal,'u.status'=>'normal'])
                ->page($page,$limit)
                ->field('p.id,u.type,p.title,u.address,p.pre_time,p.images')
                ->select();
            foreach($data as &$value){
                $value['pre_time'] = date('Y-m-d H:i:s',$value['pre_time']);
            }
            $search = new Search();
            $condition = ['uid'=>$this->user_id,'keywords'=>$keywords];
            //未检索到数据
            if(!$data){
                $condition['is_search'] = $this->is_search[1];
            }
            $is_keywords = Db::table('gc_search')
                ->where($condition)
                ->select();
            //判断是否有相同的检索历史
            if(!$is_keywords){
                $search::create($condition);
            }
            $this->success('成功', $data);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (搜索历史记录)
     * @ApiSummary  (搜索历史记录)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/goods/keysHistory)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn({
            "code": 1,
            "msg": "成功",
            "time": "1553831921",
            "data": [
                {
                "id": 4,
                "keywords": "我的世界"//关键字
                },
            ]
            })
     */
    public function keysHistory(){
        if($this->request->isGet()){
            $data = Db::table('gc_search')
                ->where(['uid'=>$this->user_id])
                ->field('id,keywords')
                ->select();
            $this->success('成功', $data);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (清空历史记录)
     * @ApiSummary  (清空历史记录)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/goods/clearHistory)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn({
            "code": 1,
            "msg": "成功",
            "time": "1553831995",
            "data": 4
            })
     */
    public function clearHistory(){
        if($this->request->isGet()){
            $data = Db::table('gc_search')
                ->where(['uid'=>$this->user_id])
                ->delete();
            $this->success('成功', $data);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (商品详情)
     * @ApiSummary  (商品详情)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/goods/goodsDetail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="商品id")
     * @ApiReturn({
            "code": 1,
            "msg": "成功",
            "time": "1553832106",
            "data": [
                "data":[{
                    "id": 1,
                    "type": 0, //类型 0:个人 1:企业
                    "title": "我的垃圾商品1", //商品名称
                    "mobile": 13752001725, //联系电话
                    "price": 500, //商品价格
                    "stock": 1000, //商品库存
                    "address": "天津市南开区卫津路", //上门地址
                    "pre_time": "2019-03-19 14:36:08", //预约时间
                    "introduction" : "商品描述",
                    "description" : "商品详情",
                    "images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg"
                }],
                "comment_count":"5"//商品评价条数
            ]
            })
     */
    public function goodsDetail(){
        if($this->request->isGet()){
            $goods_id = $this->request->get('id');//商品id
            $rule = config('site.goods');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['id'=>$goods_id])) {
                $this->error($validate->getError());
            }
            $comment_count = Db::table('gc_comment')
                ->where(['uid'=>$this->user_id,'p_id'=>$goods_id])
                ->count();
            $data = Db::table('gc_product')
                ->alias('p')
                ->join('gc_user u','p.uid = u.id','LEFT')
                ->where(['p.id'=>$goods_id,'p.status'=>$this->normal,'u.status'=>'normal'])
                ->field('p.id,u.type,u.mobile,p.title,p.price,p.stock,u.address,p.pre_time,p.introduction,p.description,p.images')
                ->select();
            foreach($data as &$value){
                $value['pre_time'] = date('Y-m-d H:i:s',$value['pre_time']);
            }
            $this->success('成功', ['data'=>$data,'comment_count'=>$comment_count]);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (猜你喜欢)
     * @ApiSummary  (猜你喜欢)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/goods/guessLikes)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn({
            "code": 1,
            "msg": "成功",
            "time": "1553832190",
            "data": [
                {
                    "id": 1,
                    "title": "我的垃圾商品1", //商品名称
                    "price": 500, //商品价格
                    "images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg"
                },
                {
                    "id": 2,
                    "title": "废弃家电",
                    "price": 1200.08,
                    "images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg"
                },
                {
                    "id": 3,
                    "title": "废品商品2",
                    "price": 12.05,
                    "images": "/uploads/20190328/c7eab178376c71ae1892cd23a1fb5779.jpg"
                },
            ]
            })
     */
    public function guessLikes(){
        if($this->request->isGet()){
            //随机取出1条检索的历史
            $keywords = Db::table('gc_search')
                ->where(['uid'=>$this->user_id,'is_search'=>$this->is_search[0]])
                ->field('keywords')
                ->orderRaw('rand()')
                ->limit(1)
                ->find();
            $where['p.status']  = $this->normal;
            $where['u.status']  = 'normal';
            if($keywords){
                $where['p.title'] = ['like','%'.$keywords['keywords'].'%'];
            }
            $data = Db::table('gc_product')
                ->alias('p')
                ->join('gc_user u','p.uid = u.id','LEFT')
                ->where($where)
                ->field('p.id,p.title,p.price,p.images')
                ->limit(20)
                ->select();
            $this->success('成功', $data);
        }else{
            $this->error('请求方式错误');
        }
    }
}