Index.php 14.6 KB
<?php


namespace app\api\controller;

use function Couchbase\defaultDecoder;

/**
 * 首页相关
 * @package app\api\controller
 */
class Index extends BaseApi
{
    protected $noNeedLogin = ['search_hot'];
    protected $noNeedRight = '*';

    /**
     * 获取首页动态
     * @ApiTitle    (获取首页动态)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/articleList)
     * @ApiParams   (name="page", type="integer", required=true, description="分页次数")
     * @ApiParams   (name="num", type="integer", required=true, description="分页数量")
     * @ApiParams   (name="status", type="integer", required=true, description="1=推荐,2=关注")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "查询数据成功",
        "time": "1608857680",
        "data": [
            {
                "id": 9,
                "user_id": 1,
                "article_category_id": 7,
                "content": "内",
                "images": [
                    "http://qldk2g57y.hn-bkt.clouddn.com/uploads/20201221/667431fe2f31e5d0e76344ba7545ea20.jpg"
                ],
                "createtime": "2020-12-24 15:13:48",
                "updatetime": "2020-12-24 15:13:48",
                "deletetime": null,
                "message": null,
                "is_friend": "是否是好友 1=是 0=不是",
                "str_time": "首页显示时间"
            }
        ]
        })
         */
    public function articleList(){
        //1.获取数据
        $data = $this->get_data_array([
            ['page','分页次数不能为空'],
            ['num','分页数量不能为空'],
            ['status','状态必须填写'],
        ]);
        //2.获取列表数据
        if ($data['status'] == 1){
            //推荐
            $art_list = model('article')
                ->where('deletetime',null)
                ->with(['user'])
                ->order('createtime','desc')
                ->page($data['page'],$data['num'])
                ->select();
        }else if ($data['status'] == 2){
            //查询关注的user_id
            $user_ids = model('user_attention')->where('user_id',$this->auth->id)->column('in_user_id');
            //查询动态
            $art_list = model('article')
                ->where('deletetime',null)
                ->with(['user'])
                ->whereIn('user_id',$user_ids)
                ->order('createtime','desc')
                ->page($data['page'],$data['num'])
                ->select();
        }
        //3.判断是不是好友
        $user_id = $this->auth->id;
        if (!empty($art_list)){
            foreach ($art_list as $key => $val){
                //1.查询关注和被关注
                $attention = model('user_attention')->where(function ($query) use ($val,$user_id){
                    $query->where('user_id',$user_id)->where('in_user_id',$val['user_id']);
                })->whereOr(function ($query) use ($val,$user_id){
                    $query->where('user_id',$val['user_id'])->where('in_user_id',$user_id);
                })->count();
                if ($attention == 2){
                    $art_list[$key]['is_friend'] = 1;
                }else{
                    $art_list[$key]['is_friend'] = 0;
                }
            }
        }
        $this->success('查询数据成功',$art_list);
    }
    /**
     * 获取动态栏目列表
     * @ApiTitle    (获取动态栏目列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/articleCategoryList)
     * @ApiParams   (name="num", type="integer", required=false, description="请求数量 传为所有")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ()
     */
    public function articleCategoryList(){
        $num = input('param.num');
        if ($num){
            $articleCategoryList = model('article_category')->order('weigh','desc')->limit($num)->select();
        }else{
            $articleCategoryList = model('article_category')->order('weigh','desc')->select();
        }
        $this->success('查询成功',$articleCategoryList);
    }
    /**
     * 首页分类列表+搜索
     * @ApiTitle    (首页分类列表+搜索)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/CategoryList)
     * @ApiParams   (name="page", type="integer", required=true, description="分页次数")
     * @ApiParams   (name="num", type="integer", required=true, description="分页数量")
     * @ApiParams   (name="article_category_id", type="integer", required=true, description="动态栏目id")
     * @ApiParams   (name="search", type="string", required=false, description="搜索内容")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "查询数据成功",
        "time": "1608857680",
        "data": [
        {
        "id": 9,
        "user_id": 1,
        "article_category_id": 7,
        "content": "内",
        "images": [
        "http://qldk2g57y.hn-bkt.clouddn.com/uploads/20201221/667431fe2f31e5d0e76344ba7545ea20.jpg"
        ],
        "createtime": "2020-12-24 15:13:48",
        "updatetime": "2020-12-24 15:13:48",
        "deletetime": null,
        "message": null,
        "is_friend": "是否是好友 1=是 0=不是",
        "str_time": "首页显示时间"
        }
        ]
        })
     */
    public function CategoryList(){
        //1.获取数据
        $data = $this->get_data_array([
            ['page','分页次数不能为空'],
            ['num','分页数量不能为空'],
            ['article_category_id','动态栏目id必须填写'],
        ]);
        //2.获取列表数据
        $art_list = model('article')
            ->where('deletetime',null)
            ->where('article_category_id',$data['article_category_id'])
            ->with(['user'])
            ->order('createtime','desc')
            ->page($data['page'],$data['num'])
            ->select();
        //3.判断是不是搜索
        $search = input('param.search');
        if (isset($search) && !empty($search)){
            $art_list = model('article')
                ->where('deletetime',null)
                ->where('article_category_id',$data['article_category_id'])
                ->where('content','like','%'.$search.'%')
                ->with(['user'])
                ->order('createtime','desc')
                ->page($data['page'],$data['num'])
                ->select();
        }
        //3.判断是不是好友
        $user_id = $this->auth->id;
        if (!empty($art_list)){
            foreach ($art_list as $key => $val){
                //1.查询关注和被关注
                $attention = model('user_attention')->where(function ($query) use ($val,$user_id){
                    $query->where('user_id',$user_id)->where('in_user_id',$val['user_id']);
                })->whereOr(function ($query) use ($val,$user_id){
                    $query->where('user_id',$val['user_id'])->where('in_user_id',$user_id);
                })->count();
                if ($attention == 2){
                    $art_list[$key]['is_friend'] = 1;
                }else{
                    $art_list[$key]['is_friend'] = 0;
                }
            }
        }
        $this->success('查询数据成功',$art_list);
    }
    /**
     * 搜索动态
     * @ApiTitle    (搜索动态)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/searchArticleList)
     * @ApiParams   (name="page", type="integer", required=true, description="分页次数")
     * @ApiParams   (name="num", type="integer", required=true, description="分页数量")
     * @ApiParams   (name="search", type="string", required=true, description="搜索内容")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "查询数据成功",
        "time": "1608857680",
        "data": [
            {
            "id": 9,
            "user_id": 1,
            "article_category_id": 7,
            "content": "内容",
            "images": [
            "http://qldk2g57y.hn-bkt.clouddn.com/uploads/20201221/667431fe2f31e5d0e76344ba7545ea20.jpg"
            ],
            "createtime": "2020-12-24 15:13:48",
            "updatetime": "2020-12-24 15:13:48",
            "deletetime": null,
            "message": null,
            "is_friend": "是否是好友 1=是 0=不是",
            "str_time": "首页显示时间"
            }
        ]
        })
     */
    public function searchArticleList(){
        //1.获取数据
        $data = $this->get_data_array([
            ['page','分页次数不能为空'],
            ['num','分页数量不能为空'],
            ['search','搜索内容必须填写'],
        ]);
        //2.查询搜索数据
        $art_list = model('article')
            ->where('deletetime',null)
            ->where('content','like','%'.$data['search'].'%')
            ->with(['user'])
            ->order('createtime','desc')
            ->page($data['page'],$data['num'])
            ->select();
        //3.判断是不是好友
        $user_id = $this->auth->id;
        if (!empty($art_list)){
            foreach ($art_list as $key => $val){
                //1.查询关注和被关注
                $attention = model('user_attention')->where(function ($query) use ($val,$user_id){
                    $query->where('user_id',$user_id)->where('in_user_id',$val['user_id']);
                })->whereOr(function ($query) use ($val,$user_id){
                    $query->where('user_id',$val['user_id'])->where('in_user_id',$user_id);
                })->count();
                if ($attention == 2){
                    $art_list[$key]['is_friend'] = 1;
                }else{
                    $art_list[$key]['is_friend'] = 0;
                }
            }
        }
        //4.添加搜索历史
        $user_search = model('user_search')->where('text',$data['search'])->where('user_id',$this->auth->id)->find();
        if ($user_search){
            $user_search->num += 1;
            $user_search->save();
        }else{
            model('user_search')->create(['text'=>$data['search'],'user_id'=>$this->auth->id]);
        }
        //5.添加热门搜索
        $search_hot = model('search_hot')->where('text',$data['search'])->find();
        if ($search_hot){
            $search_hot->num += 1;
            $search_hot->save();
        }else{
            model('search_hot')->create(['text'=>$data['search']]);
        }
        //6.返回数据
        $this->success('查询数据成功',$art_list);
    }
    /**
     * 搜索历史:必须登录
     * @ApiTitle    (搜索历史:必须登录)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/search_lishi)
     * @ApiParams   (name="num", type="integer", required=true, description="请求数量")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "查询数据成功",
        "time": "1608882197",
        "data": [
            {
            "id": 1,
            "user_id": 1,
            "text": "搜索内容",
            "num": "次数",
            "createtime": "2020-12-25 15:42:54",
            "updatetime": "2020-12-25 15:42:54"
            }
        ]
        })
     */
    public function search_lishi(){
        //1.获取数据
        $data = $this->get_data('num','数量不能为空');
        //2.获取搜索历史
        $user_search = model('user_search')
            ->where('user_id',$this->auth->id)
            ->order('num','desc')
            ->limit($data)
            ->select();
        //3.返回数据
        $this->success('查询数据成功',$user_search);
    }
    /**
     * 删除搜索历史:必须登录
     * @ApiTitle    (删除搜索历史:必须登录)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/search_lishi_del)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "删除数据成功",
        "time": "1608882230",
        "data": null
     })
     */
    public function search_lishi_del(){
        //1.删除搜索记录
        model('user_search')
            ->where('user_id',$this->auth->id)
            ->delete();
        //2.返回数据
        $this->success('删除数据成功');
    }
    /**
     * 热门搜索:无需登录
     * @ApiTitle    (热门搜索:无需登录)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/search_hot)
     * @ApiParams   (name="num", type="integer", required=true, description="请求数量")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "查询数据成功",
        "time": "1608882251",
        "data": [
            {
                "id": 1,
                "text": "搜索内容",
                "num": "搜索次数",
                "createtime": "2020-12-25 15:42:55",
                "updatetime": "2020-12-25 15:42:55"
            }
        ]
        })
     */
    public function search_hot(){
        //1.获取数据
        $num = $this->get_data('num','数量不能为空');
        //2.获取搜索历史
        $user_search = model('search_hot')
            ->order('num','desc')
            ->limit($num)
            ->select();
        //3.返回数据
        $this->success('查询数据成功',$user_search);
    }
}