Search.php 3.3 KB
<?php

namespace app\api\controller;

use app\admin\model\Record;
use app\common\controller\Api;
use think\Validate;
/**
 * 商品检索接口
 */
class Search extends Api
{
    protected $noNeedLogin = ['searchGoodsList'];
    protected $noNeedRight = ['*'];
    protected $uid = '';
    public function _initialize()
    {
        parent::_initialize();
        $this->uid = $this->auth->getUserId();
    }

    /**
     * @ApiTitle    (商品检索列表)
     * @ApiSummary  (商品检索列表)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/search/searchGoodsList)
     *
     * @ApiParams   (name="keyword", type="string", required=true, description="检索关键字")
     * @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 searchGoodsList(){
        if($this->request->isGet()){
            $keyword = $this->request->get('keyword');
            $page = $this->request->get('page');
            $rule = config('verify.search_goods');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['keyword'=>$keyword,'page'=>$page])) {
                $this->error($validate->getError());
            }

            $where = ['type_name|name'=>['like','%'.$keyword.'%']];
            //暂时
            $arr = Common::searchGoodsList($keyword,$where,$page,$this->uid);
            //写入检索记录
//            $record = Common::findWhereData('record',['uid'=>$this->uid,'keyword'=>$keyword],'id,keyword');
//            if(!$record){
//                //创建记录
//                $recordModel = new Record();
//                $recordModel->create(['uid'=>$this->uid,'keyword'=>$keyword]);
//            }
            $this->success('成功',$arr);
        }else{
            $this->error('请求方式错误');
        }
    }

}