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(); } } }