getUser(); //获取轮播图 $banner = $this->getBanner(); // 关注的用户 $arr = $user->UserCareUser; $other_user = [] ; foreach ($arr as $k => $v){ if ($v->pivot->dream_num > 0) { $v->news_num = $v->pivot->dream_num; $other_user[] = $v; } } // 获取其他用户信息 及梦想 $dreams = DreamInfoModel::orderBy('score','desc')->limit(20)->paginate(20); $this->dreams($dreams); return $this->api(compact('banner','other_user','dreams')); } /** * @api {get} /api/index/trend 潮流(trend) * @apiDescription 潮流(trend) * @apiGroup Index * @apiPermission none * @apiVersion 0.1.0 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK *{ *"status": true, *"status_code": 0, *"message": "", *"data": { * "other_user": [ 动态用户 * 'news_num':2 新消息数目 * ], * "dreams": [ * "care_num": 0, 关注人数 * "time": 1222222, 梦想倒计时 * "dream_imgs_first": { * "pic": "", 梦想封面图片 * } * "dream_find_user": [ * { * ... 发布梦想的用户信息 * } * ], * ] * } * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } */ public function trend(Request $request) { $user = $this->getUser(); // 关注的用户 $arr = $user->UserCareUser; $other_user = [] ; foreach ($arr as $k => $v){ if ($v->pivot->dream_num > 0) { $v->news_num = $v->pivot->dream_num; $other_user[] = $v; } } // 获取其他用户信息 及梦想 $dreams = DreamInfoModel::orderBy('score','desc')->offset(20)->limit(100)->paginate(20); $this->dreams($dreams); return $this->api(compact('other_user','dreams')); } /** * @api {get} /api/index/new 最新(news) * @apiDescription 最新(news) * @apiGroup Index * @apiPermission none * @apiVersion 0.1.0 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK *{ *"status": true, *"status_code": 0, *"message": "", *"data": { * "other_user": [ * 'news_num':2 新消息数目 * ], 动态用户 * "dreams": [ * "care_num": 0, 关注人数 * "time": 1222222, 梦想倒计时 * "dream_imgs_first": { * "pic": "", 梦想封面图片 * } * "dream_find_user": [ * { * ... 发布梦想的用户信息 * } * ], * ] * } * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } */ public function news(Request $request) { $user = $this->getUser(); // 关注的用户 $arr = $user->UserCareUser; $other_user = [] ; foreach ($arr as $k => $v){ if ($v->pivot->dream_num > 0) { $v->news_num = $v->pivot->dream_num; $other_user[] = $v; } } // 获取其他用户信息 及梦想 $dreams = DreamInfoModel::orderBy('score','desc')->offset(100)->limit(500)->paginate(20); $this->dreams($dreams); return $this->api(compact('other_user','dreams')); } /** * @api {get} /api/index/search 搜索(search) * @apiDescription 搜索(search) * @apiGroup Index * @apiPermission none * @apiVersion 0.1.0 * @apiParam {string} keyword 关键字可选 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * get *{ * "status": true, * "status_code": 0, * "message": "", * "data": { * "arr": { // 热门搜索 * ... * }, * "data1": [ * { * "search": "ha", // 历史搜索 * } * ] * } *} * post * { * "status": true, * "status_code": 0, * "message": "", * "data": { * "data1": [ * ... //用户 * ], * "data2": [ * ... //梦想 * ], * "data3": [ * ... //标签 * ] * } *} * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request */ public function search(Request $request) { $user = $this->getUser(); $keyword ='%'.$request->keyword.'%'; $data1 = UserInfoModel::where('nickname','like',$keyword)->paginate(20); $data2 = DreamInfoModel::where('dream','like',$keyword)-> orWhere('dream','like',$keyword)->paginate(20); $data3 = BaseSettingsModel::where('category','sign')->where('value','like',$keyword)->paginate(20); if (empty($request->keyword)) { // 历史搜索 $data1 = $user->search()->orderBy('id','desc')->limit(10)->paginate(20); // 热门搜索 $data2 = SearchInfoModel::paginate(20); $arr = []; foreach ($data2 as $k => $v) { if (count($arr) == 8) { break; } if (!array_key_exists($v->search,$arr)) { $arr[$v->search] = $v->times; }else{ $arr[$v->search] += $v->times; } } arsort($arr); return $this->api(compact('arr','data1')); } return $this->api(compact('data1','data2','data3')); } /** * @api {get} /api/index/user_search 用户搜索(search) * @apiDescription 用户搜索(search) * @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": { * "data1": [ * "nickname": "ha", 昵称 * "pic": "", 头像 * ... * ] * } *} * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request */ public function userSearch(Request $request) { if (empty($request->keyword)) { return $this->api(''); } $keyword ='%'.$request->keyword.'%'; $data1 = UserInfoModel::where('nickname','like',$keyword)->paginate(20); return $this->api(compact('data1')); } /** * @api {get} /api/index/dream_search 梦想搜索(search) * @apiDescription 梦想搜索(search) * @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": { "data": [ { "dream": "haha", 梦想名 "user_pic": [ { "pic": "", 用户头像 } ], "dream_img": "", 梦想图片 } ] } } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request */ public function dreamSearch(Request $request) { if (empty($request->keyword)) { return $this->api(''); } $keyword ='%'.$request->keyword.'%'; $data = DreamInfoModel::where('dream','like',$keyword)-> orWhere('dream','like',$keyword)->paginate(20); foreach ($data as $k => $value) { $value->user_pic = $value->dreamFindUser; $value->dream_img = $value->dreamImgsFirst->pic; } return $this->api(compact('data')); } //获取轮播图 public function getBanner() { $banner = BaseSettingsModel::where(['category' => 'banner'])->where(['status' => '1']) ->orderBy('sort')->limit('3')->paginate(20)->toArray(); return $banner; } // 完善梦想信息 public function dreams($dreams) { foreach ($dreams as $k => $dream) { $dream->dream_find_user = $dream->dreamFindUser; // 计算被关注总人数 $user_id = UserDream::where('dream_id',$dream->id)->first()->user_id; $data = UserCareUser::where('other_user_id',$user_id)->paginate(20); $dream->care_num = count($data); $dream->dream_first_pic = $dream->dreamImgsFirst; } } // 查看关注用户的最新动态 /** * @api {get} /api/index/news_info 关注用户动态详情 * @apiDescription 关注用户动态详情 * @apiGroup Index * @apiPermission none * @apiVersion 0.1.0 * @apiParam {int} id 被点击用户ID * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK { "status": true, "status_code": 0, "message": "", "data": [ { "id": 12, "dream": "123", "about": "124", "sign": null, "videos": "234", "money": 2345, "time": 0, 倒计时 s "get_money": 0, "mark": 0, "status": 0, "score": 3, "created_at": "2017-06-15 08:46:52", "updated_at": "2017-06-15 08:46:52", "deleted_at": null, "imgs": [ { "id": 8, "dream_id": 12, "title": "", "pic": "....png", 梦想图片 }, ], } ] } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request */ 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); UserCareUser::where('user_id',$user->id)->where('other_user_id',$other_id)->update(['dream_num'=>0]); $data = UserInfoModel::find($other_id)->UserDream; foreach ($data as $item) { $item->imgs = $item->dreamImgs ; } return $this->api($data); } }