审查视图

application/api/controller/Index.php 9.6 KB
李忠强 authored
1 2 3 4 5 6 7
<?php

namespace app\api\controller;

use app\api\model\Category;
use app\api\model\Goods;
use app\api\model\News;
何书鹏 authored
8
use app\api\model\UserSearch;
李忠强 authored
9 10 11 12 13 14 15 16 17
use app\common\controller\Api;
use think\Config;
use think\Db;

/**
 * 首页
 */
class Index extends Api
{
李忠强 authored
18
    protected $noNeedLogin = ['*'];
李忠强 authored
19 20 21 22 23 24 25 26 27 28
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle    (首页轮播图)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':'轮播图数组'
李忠强 authored
29 30 31
        'type':'类型:1=邀请有奖,2=商品详情'
        'goods_id':'商品id'
        'image':'轮播图'
李忠强 authored
32 33 34 35 36 37
    })
     */
    public function banner()
    {

        $model  = new News();
李忠强 authored
38
        $list = $model->where('status','normal')->field('type,goods_id,image')->select();
李忠强 authored
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        $this->success('banner',$list);
    }

    /**
     * @ApiTitle    (首页分类)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':[
        {
        "id": 4,
        "name": "电子产品",
        "image": "https://her-family.oss-cn-qingdao.aliyuncs.com/addons_store_uploads/20181105/509af801726984aaa359b4bf249f5716.png",
        "image_text": "图片地址"
        },
        {
        "id": 6,
        "name": "水果",
        "image": "https://her-family.oss-cn-qingdao.aliyuncs.com/addons_store_uploads/20181105/c83a0019dfa7a768037e98f02b70efd5.jpg",
        "image_text": "图片地址"
        }
    ]
    })
     */
    public function category()
    {

        $model  = new Category();
何书鹏 authored
69
        $list = $model->where('is_index','1')->field('id,name,image')->order('weigh desc')->limit(10)->select();
李忠强 authored
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        $this->success('首页分类',$list);
    }

    /**
     * @ApiTitle    (首页活动&&通知文本)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':{
        "left": {
            "id": 1,
            "name": "新人用户",
            "place": "left"
        },
        "rightTop": {
            "id": 2,
            "name": "限时秒杀",
            "place": "rightTop"
        },
        "rightDown": {
            "id": 3,
            "name": "进口商品",
            "place": "rightDown"
        }
        "content": "首页通知文本"
    }
    })
     */
    public function activity()
    {
        $list = Db::name('activity')->select();
        $data = [];
        foreach ($list as $key => $value){
            if ($value['place'] == 'left'){
                $data['left'] = $value;
            }elseif ($value['place'] == 'rightTop'){
                $data['rightTop'] = $value;
            }else{
                $data['rightDown'] = $value;
            }
        }
        $data['content'] = Config::get('site.notice');
        $this->success('首页活动标题',$data);
    }


    /**
     * @ApiTitle    (猜你喜欢)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name=page, type=integer, required=false, description="页数 默认1")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':{
        "total": 4,
        "per_page": 10,
        "current_page": 1,
        "last_page": 1,
        "data": [
            {
            "goods_id": 21,
            "goods_name": "小米Mix3",
            "image": null,
            "cart_number": 10, 购物车数量
            "price": "100.00", 价格
            "line_price": "1000.00", 划线价
            "image_text": "图片地址"
何书鹏 authored
140
            "stock_num": 68, 商品总库存
李忠强 authored
141 142 143 144 145 146 147 148
            }
        ]
    })
     */
    public function userLike()
    {
        $page = $this->request->post('page',1);
        $model = new Goods();
李忠强 authored
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
        if ($this->auth->isLogin()){
            $list = $model->where('is_index','1')
                ->where('is_delete','0')
                ->where('goods_status','10')
                ->field('goods_id,goods_name,image')
                ->paginate(10,false,['page'=>$page])
                ->each(function ($item,$key){
                    $item['cart_number'] = Db::name('cart')
                        ->where('user_id',$this->auth->id)
                        ->where('goods_id',$item['goods_id'])
                        ->sum('number');
                    $goods_spec = Db::name('litestore_goods_spec')
                        ->where('goods_id',$item['goods_id'])
                        ->find();
                    $item['price'] = $goods_spec['goods_price'];
                    $item['line_price'] = $goods_spec['line_price'];
何书鹏 authored
165 166
                    // 总库存
                    $item->append(['stock_num']);
李忠强 authored
167 168 169 170 171 172 173 174 175 176 177 178 179 180
                });
        }else{
            $list = $model->where('is_index','1')
                ->where('is_delete','0')
                ->where('goods_status','10')
                ->field('goods_id,goods_name,image')
                ->paginate(10,false,['page'=>$page])
                ->each(function ($item,$key){
                    $item['cart_number'] = 0;
                    $goods_spec = Db::name('litestore_goods_spec')
                        ->where('goods_id',$item['goods_id'])
                        ->find();
                    $item['price'] = $goods_spec['goods_price'];
                    $item['line_price'] = $goods_spec['line_price'];
何书鹏 authored
181 182
                    // 总库存
                    $item->append(['stock_num']);
李忠强 authored
183 184 185
                });
        }
李忠强 authored
186 187 188
        $this->success('猜你喜欢',$list);
    }
李忠强 authored
189 190 191 192 193 194 195 196 197

    /**
     * @ApiTitle    (搜索)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name=text, type=string, required=true, description="搜索文字")
     * @ApiParams   (name=page, type=integer, required=false, description="页数 默认1")
     * @ApiReturn   ({
    'code':'1',
李忠强 authored
198
    'msg':'搜索'
李忠强 authored
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    'data':{
        "total": 4,
        "per_page": 10,
        "current_page": 1,
        "last_page": 1,
        "data": [
            {
            "goods_id": 21,
            "goods_name": "小米Mix3",
            "image": null,
            "cart_number": 10, 购物车数量
            "price": "100.00", 价格
            "line_price": "1000.00", 划线价
            "image_text": "图片地址"
            }
        ]
    })
     */
    public function search()
    {
        $text = $this->request->post('text');
        $page = $this->request->post('page',1);
        if (!$text) $this->error('请填写搜索内容');
        $model = new Goods();
        if ($this->auth->isLogin()){
            $list = $model->where('keywords','like','%'.$text.'%')
                ->where('is_delete','0')
                ->where('goods_status','10')
                ->field('goods_id,goods_name,image')
                ->paginate(10,false,['page'=>$page])
                ->each(function ($item,$key){
                    $item['cart_number'] = Db::name('cart')
                        ->where('user_id',$this->auth->id)
                        ->where('goods_id',$item['goods_id'])
                        ->sum('number');
                    $goods_spec = Db::name('litestore_goods_spec')
                        ->where('goods_id',$item['goods_id'])
                        ->find();
                    $item['price'] = $goods_spec['goods_price'];
                    $item['line_price'] = $goods_spec['line_price'];
                });
        }else{
            $list = $model->where('keywords','like','%'.$text.'%')
                ->where('is_delete','0')
                ->where('goods_status','10')
                ->field('goods_id,goods_name,image')
                ->paginate(10,false,['page'=>$page])
                ->each(function ($item,$key){
                    $item['cart_number'] = 0;
                    $goods_spec = Db::name('litestore_goods_spec')
                        ->where('goods_id',$item['goods_id'])
                        ->find();
                    $item['price'] = $goods_spec['goods_price'];
                    $item['line_price'] = $goods_spec['line_price'];
                });
        }
何书鹏 authored
255 256 257 258 259 260 261 262 263 264
        // 搜索记录
        $search_text =  UserSearch::where(['user_id'=>$this->auth->id,'text'=>$text])->find();
        if($search_text){
            $search_text->save(['updatetime' => time()]);
        }else{
            UserSearch::create([
                'user_id' => $this->auth->id,
                'text' => $text
            ]);
        }
李忠强 authored
265 266 267
        $this->success('搜索',$list);
    }
李忠强 authored
268 269 270 271 272 273 274 275

    /**
     * @ApiTitle    (搜索历史)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn   ({
    'code':'1',
    'msg':'搜索历史'
何书鹏 authored
276
    'data':null
李忠强 authored
277 278 279 280 281
    })
     */
    public function searchHistory()
    {
        if ($this->auth->isLogin()){
何书鹏 authored
282
            $list = UserSearch::where('user_id',$this->auth->id)
李忠强 authored
283
                ->limit(10)
何书鹏 authored
284
                ->order('updatetime','desc')
李忠强 authored
285 286 287 288 289 290 291
                ->column('text');
        }else{
            $list = [];
        }
        $this->success('搜索历史',$list);
    }
何书鹏 authored
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    /**
     * @ApiTitle    (搜索历史-清空)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn   ({
    'code':'1',
    'msg':'成功'
    'data':null
    })
     */
    public function searchHistoryClear()
    {
        UserSearch::where('user_id',$this->auth->id)->delete();
        $this->success('成功');
    }
李忠强 authored
307
}