123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- namespace App\Http\Controllers\Api\V1;
- use App\Models\BaseSettingsModel;
- use App\Models\DreamInfoModel;
- use App\Models\SearchInfoModel;
- use App\Models\UserCareDream;
- use App\Models\UserInfoModel;
- use Illuminate\Http\Request;
- class IndexController extends Controller
- {
- /**
- * @api {get} /api/index/home 首页
- * @apiDescription 首页
- * @apiGroup Index
- * @apiParam {string} type (热门)hot/(潮流)trend/(最新)news
- * @apiParam {int} [page=1] 页码(分页参数)
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- *{
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "banners": [ 轮播图
- * {
- * "id": 34,
- * "key": "4",
- * "value": "http://w17.9026.com/img/banner/banner_4.png",
- * "sort": 0,
- * "category": "banner",
- * "pid": 0,
- * "status": 1,
- * "created_at": null,
- * "updated_at": null,
- * "deleted_at": null
- * }
- * ],
- * "users": [], 动态用户
- * "dreams": { 梦想列表
- * "current_page": 1,
- * "data": [
- * {
- * "id": 5,
- * "user_id": 1,
- * "name": "梦想标题1",
- * "about": "梦想介绍",
- * "coin": 2500,
- * "time": 21, 实现时间
- * "get_coin": 0,
- * "status": 0,
- * "video": null,
- * "score": 100079365,
- * "sign": "",
- * "created_at": "2017-06-25 12:45:22",
- * "updated_at": "2017-06-25 12:45:22",
- * "care_num": 0, 关注人数
- * "img": {
- * "title": "",
- * "pic": "https://timgsa.baidu.com/timg?ica852e6e9c6ec46b7b14f12f.jpg" 梦想封面图片
- * }
- * },
- * ],
- * "from": 1,
- * "last_page": 1,
- * "next_page_url": null,
- * "path": "http://www.miao.com/api/index/home",
- * "per_page": 20,
- * "prev_page_url": null,
- * "to": 5,
- * "total": 5
- * }
- * }
- *}
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- * {
- * "state": false,
- * "code": 1000,
- * "message": "传入参数不正确",
- * "data": null or []
- * }
- */
- public function home(Request $request)
- {
- $users = [];
- $type = $request->type;
- if ($type == 'trend') {
- $dreams = DreamInfoModel::orderBy('score','desc')->with('user')->offset(20)->limit(100)->paginate(20);
- $this->dreams($dreams);
- return $this->api(compact('users','dreams'));
- } elseif ($type == 'news') {
- $dreams = DreamInfoModel::orderBy('score','desc')->with('user')->offset(100)->limit(500)->paginate(20);
- $this->dreams($dreams);
- return $this->api(compact('users','dreams'));
- } else{
- $banners = $this->getBanner();
- $dreams = DreamInfoModel::orderBy('score','desc')->with('user')->limit(20)->paginate(20);
- $this->dreams($dreams);
- return $this->api(compact('banners','users','dreams'));
- }
- }
- /**
- * @api {get} /api/index/search 搜索
- * @apiDescription 搜索
- * @apiGroup Index
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiParam {string} keyword 关键字可选 (ha/name)
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * get
- *{
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "hot_searches": {
- * "name": 1,
- * "梦想": 1,
- * "ha": 1
- * },
- * "history_searches": [
- * {
- * "id": 3,
- * "user_id": 1,
- * "search": "ha",
- * "times": 1,
- * "created_at": "2017-06-25 16:21:47",
- * "updated_at": "2017-06-25 16:21:47"
- * },
- * ]
- * }
- *}
- * post
- *{
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "user_infos": [
- * {
- * "id": 1,
- * "phone": "13880642880",
- * "nickname": "name1",
- * "avatar": "https://timgsa.baidu.com/ti.thumb.224_0.jpeg",
- * },
- * ],
- * "dream_infos": [
- * {
- * "id": 5,
- * "user_id": 1,
- * "name": "梦想标题1",
- * "about": "梦想介绍",
- * },
- * ],
- * "signs": [
- * {
- * "id": 1013,
- * "key": "",
- * "value": "ha", 标签名
- * },
- * ]
- * }
- *}
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- */
- public function search(Request $request)
- {
- $user = $this->getUser();
- $keyword ='%'.$request->keyword.'%';
- $user_infos = UserInfoModel::where('nickname','like',$keyword)->get();
- $dream_infos = DreamInfoModel::where('name','like',$keyword)->
- orWhere('sign','like',$keyword)->get();
- $signs = BaseSettingsModel::where('category','sign')->where('value','like',$keyword)->get();
- if (empty($request->keyword)) {
- // 历史搜索
- $history_searches = $user->search()->orderBy('id','desc')->limit(10)->get();
- // 热门搜索
- $data2 = SearchInfoModel::get();
- $hot_searches = [];
- foreach ($data2 as $k => $v) {
- if (count($hot_searches) == 8) {
- break;
- }
- if (!array_key_exists($v->search,$hot_searches)) {
- $hot_searches[$v->search] = $v->times;
- }else{
- $hot_searches[$v->search] += $v->times;
- }
- }
- arsort($hot_searches);
- return $this->api(compact('hot_searches','history_searches'));
- }
- // 写入搜索记录
- $this->insertSearchTable($user->id,$request->keyword);
- return $this->api(compact('user_infos','dream_infos','signs'));
- }
- /**
- * @api {get} /api/index/user_search 用户搜索
- * @apiDescription 用户搜索
- * @apiGroup Index
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiParam {string} keyword 关键字
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- *{
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data":[
- * {
- * "nickname": "ha", 昵称
- * "pic": "", 头像
- * ...
- * },
- * ]
- *}
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- */
- public function userSearch(Request $request)
- {
- $user = $this->getUser();
- if (empty($request->keyword)) {
- return $this->api('');
- }
- $keyword ='%'.$request->keyword.'%';
- $users = UserInfoModel::where('nickname','like',$keyword)->get();
- $this->insertSearchTable($user->id,$request->keyword);
- return $this->api($users);
- }
- //获取轮播图
- public function getBanner()
- {
- $banner = BaseSettingsModel::where(['category' => 'banner'])->where(['status' => '1'])
- ->orderBy('sort')->get()->toArray();
- return $banner;
- }
- // 完善梦想信息
- public function dreams($dreams)
- {
- foreach ($dreams as $k => $dream) {
- $data = UserCareDream::where('dream_id',$dream->id)->get();
- $dream->care_num = count($data);
- $dream->img = $dream->img?$dream->img->pic:'';
- }
- }
- // 查看关注用户的最新动态
- // public function newsInfo(Request $request)
- // {
- //// 查看后user_care_user dream_info 更新为零 首页不再显示
- // $user = $this->getUser();
- // $other_id = $request->id;
- // if (empty($other_id)) return $this->error(ErrorCode::MEMBER_NOT_EXIST);
- // UserCareDream::where('user_id',$user->id)->where('dream_user_id',$other_id)->update(['interaction_number'=>0]);
- // $data = UserInfoModel::where('id',$other_id)->with(['allInteraction'=>function ($query){
- // $query->orderBy('id','desc');
- // }])->first();
- //// $data = UserInfoModel::find($other_id)->allInteraction;
- //// dd($data) ;
- //// dd($data->allInteraction);
- // foreach ($data->allInteraction as $item) {
- // $item->time = DreamInfoModel::find($item->dream_id)->time;
- // $item->comments = $item->comments;
- // foreach ($item->comments as $k => $v) {
- // $v->pic = UserInfoModel::find($v->user_id)->pic;
- // $v->replay = $v->replay;
- // foreach ($v->replay as $k1 => $v1) {
- // $v1->pic = UserInfoModel::find($v1->user_id)->pic;
- // }
- // }
- // }
- // return $this->api($data);
- // }
- public function insertSearchTable($id,$keyword)
- {
- $info = SearchInfoModel::where('user_id',$id)->
- where('search',trim($keyword))->first();
- if (count($info) == 0) {
- SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
- }else{
- $info->times += 1;
- $info->save();
- }
- }
- }
|