Index.php 4.5 KB
<?php

namespace app\api\controller;

use app\api\model\Category;
use app\api\model\Goods;
use app\api\model\News;
use app\common\controller\Api;
use think\Config;
use think\Db;

/**
 * 首页
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle    (首页轮播图)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':'轮播图数组'
    })
     */
    public function banner()
    {

        $model  = new News();
        $list = $model->where('status','normal')->column('image');
        $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();
        $list = $model->where('is_index','1')->field('id,name,image')->limit(10)->select();
        $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": "图片地址"
            }
        ]
    })
     */
    public function userLike()
    {
        $page = $this->request->post('page',1);
        $model = new Goods();
        $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'];
            });
        $this->success('猜你喜欢',$list);
    }

}