IndexController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Models\BaseSettingsModel;
  4. use App\Models\CommentInfoModel;
  5. use App\Models\DreamInfoModel;
  6. use App\Models\SearchInfoModel;
  7. use App\Models\UserCareDream;
  8. use App\Models\UserCareUser;
  9. use App\Models\UserInfoModel;
  10. use Illuminate\Http\Request;
  11. class IndexController extends Controller
  12. {
  13. /**
  14. * @api {get} /api/index/home 首页
  15. * @apiDescription 首页
  16. * @apiGroup Index
  17. * @apiParam {string} [type=hot] (热门)hot/(潮流)trend/(最新)news
  18. * @apiParam {int} [page=1] 页码(分页参数)
  19. * @apiPermission none
  20. * @apiVersion 0.1.0
  21. * @apiSuccessExample {json} Success-Response:
  22. * HTTP/1.1 200 OK
  23. *{
  24. * "status": true,
  25. * "status_code": 0,
  26. * "message": "",
  27. * "data": {
  28. * "banners": [
  29. * {
  30. * "id": 32,
  31. * "key": "2",
  32. * "value": "http://w17.9026.com/img/banner/banner_2.png",
  33. * "sort": 2,
  34. * "category": "banner",
  35. * "pid": 0,
  36. * "status": 1,
  37. * "created_at": null,
  38. * "updated_at": "2017-06-26 09:21:36",
  39. * "deleted_at": null
  40. * }
  41. * ],
  42. * "users": [ 动态用户
  43. * {
  44. * "id": 3,
  45. * "user_id": 1,
  46. * "other_user_id": 2,
  47. * "dream_number": 1,
  48. * "created_at": null,
  49. * "updated_at": null,
  50. * "other_user": {
  51. * "id": 2,
  52. * "avatar": "http://www.miao.com/upload/user/20170630/08e235d7211944e9b6482965b4d7c42e.jpg",
  53. * }
  54. * }
  55. * ],
  56. * "dreams": {
  57. * "current_page": 1,
  58. * "data": [
  59. * {
  60. * "id": 5,
  61. * "user_id": 1,
  62. * "name": "梦想标题1",
  63. * "about": "梦想介绍",
  64. * "coin": 2500,
  65. * "end_time": "2017-05-11 17:00:00",
  66. * "get_coin": 0,
  67. * "mark": 0,
  68. * "status": 2,
  69. * "video": "url",
  70. * "score": 100079365,
  71. * "sign": "梦想达人",
  72. * "code": "",
  73. * "parameter": 100,
  74. * "created_at": "2017-06-25 12:45:22",
  75. * "updated_at": "2017-07-03 12:23:37",
  76. * "care_num": 1,
  77. * "img": {
  78. * "title": "",
  79. * "pic": "http://www.miao.com/upload/dream/20170629/a18cadd1560f21453a08fdd81fb8fb2d.jpg"
  80. * },
  81. * "user": {
  82. * "id": 1,
  83. * "avatar": "/upload/user/20170629/e77068cc14c82086fb6b16e5bb7c3dd1.jpg",
  84. * }
  85. * },
  86. * }
  87. * ],
  88. * "from": 1,
  89. * "last_page": 1,
  90. * "next_page_url": null,
  91. * "path": "http://www.miao.com/api/index/home",
  92. * "per_page": 20,
  93. * "prev_page_url": null,
  94. * "to": 11,
  95. * "total": 11
  96. * }
  97. * }
  98. *}
  99. * @apiErrorExample {json} Error-Response:
  100. * HTTP/1.1 400 Bad Request
  101. * {
  102. * "state": false,
  103. * "code": 1000,
  104. * "message": "传入参数不正确",
  105. * "data": null or []
  106. * }
  107. */
  108. public function home(Request $request)
  109. {
  110. $login_user = $this->getUser();
  111. $id = $login_user->id;
  112. $arr1 =DreamInfoModel::orderBy('score','desc')->limit(30)->select('id')->get()->toArray();
  113. $id_arr1 = array_column($arr1,'id');
  114. $arr2 =DreamInfoModel::orderBy('score','desc')->offset(15)->limit(120)->select('id')->get()->toArray();
  115. $id_arr2 = array_column($arr2,'id');
  116. $dtusers =UserCareUser::where('user_id',$id)->with('other_user')->
  117. where('dream_number','>',0)->orderBy('created_at')->get()->toArray();
  118. $hdusers = CommentInfoModel::where(function ($query) use ($id) {
  119. // $query->where('user_id',$id)->orWhere('to_user_id',$id);
  120. $query->where('user_id',$id);
  121. })->where('is_read',0)->with('to_user')->orderBy('created_at')->get()->toArray();
  122. $users = [] ;
  123. foreach ($dtusers as $k => $v){
  124. $users[] = $v['other_user'];
  125. }
  126. foreach ($hdusers as $k => $v){
  127. $users[] = $v['to_user'];
  128. }
  129. $type = $request->type;
  130. if ($type == 'trend') {
  131. $dreams = DreamInfoModel::orderBy('score','desc')->with('user')->whereNotIn('id', $id_arr1)->limit(100)->paginate(20);
  132. $this->dreams($dreams);
  133. return $this->api(compact('users','dreams'));
  134. } elseif ($type == 'news') {
  135. $dreams = DreamInfoModel::orderBy('score','desc')->orderBy('created_at','desc')->with('user')->whereNotIn('id', $id_arr2)->limit(500)->paginate(20);
  136. $this->dreams($dreams);
  137. return $this->api(compact('users','dreams'));
  138. } else{
  139. $banners = $this->getBanner();
  140. $dreams = DreamInfoModel::orderBy('score','desc')->with('user')->limit(15)->paginate(15);
  141. $this->dreams($dreams);
  142. return $this->api(compact('banners','users','dreams'));
  143. }
  144. }
  145. /**
  146. * @api {get} /api/index/search 搜索
  147. * @apiDescription 搜索
  148. * @apiGroup Index
  149. * @apiPermission none
  150. * @apiVersion 0.1.0
  151. * @apiParam {string} [keyword] 关键字可选 (ha/name)
  152. * @apiSuccessExample {json} Success-Response:
  153. * HTTP/1.1 200 OK
  154. * get
  155. *{
  156. * "status": true,
  157. * "status_code": 0,
  158. * "message": "",
  159. * "data": {
  160. * "hot_searches": {
  161. * "name": 1,
  162. * "梦想": 1,
  163. * "ha": 1
  164. * },
  165. * "history_searches": [
  166. * {
  167. * "id": 3,
  168. * "user_id": 1,
  169. * "search": "ha",
  170. * "times": 1,
  171. * "created_at": "2017-06-25 16:21:47",
  172. * "updated_at": "2017-06-25 16:21:47"
  173. * },
  174. * ]
  175. * }
  176. *}
  177. * @apiErrorExample {json} Error-Response:
  178. * HTTP/1.1 400 Bad Request
  179. */
  180. public function search(Request $request)
  181. {
  182. $user = $this->getUser();
  183. $keyword ='%'.$request->keyword.'%';
  184. $user_infos = UserInfoModel::where('nickname','like',$keyword)->get();
  185. $dream_infos = DreamInfoModel::where(function ($query) use($keyword) {
  186. $query->where('name','like',$keyword)
  187. ->where('end_time','>=',time());
  188. })
  189. ->orWhere(function ($query) use($keyword){
  190. $query->where('sign','like',$keyword)
  191. ->where('end_time','>=',time());
  192. })->with(['user','img'])->get();
  193. $this->dreams($dream_infos);
  194. $signs = BaseSettingsModel::where('category','sign')->where('value','like',$keyword)->get();
  195. if (empty($request->keyword)) {
  196. // 历史搜索
  197. $history_searches = $user->search()->orderBy('id','desc')->limit(10)->get();
  198. // 热门搜索
  199. $data2 = SearchInfoModel::get();
  200. $hot_searches = [];
  201. foreach ($data2 as $k => $v) {
  202. if (count($hot_searches) == 6) {
  203. break;
  204. }
  205. if (!array_key_exists($v->search,$hot_searches)) {
  206. $hot_searches[$v->search] = $v->times;
  207. }else{
  208. $hot_searches[$v->search] += $v->times;
  209. }
  210. }
  211. arsort($hot_searches);
  212. return $this->api(compact('hot_searches','history_searches'));
  213. }
  214. // 写入搜索记录
  215. $this->insertSearchTable($user->id,$request->keyword);
  216. return $this->api(compact('user_infos','dream_infos','signs'));
  217. }
  218. /**
  219. * @api {get} /api/index/user_search 用户搜索
  220. * @apiDescription 用户搜索
  221. * @apiGroup Index
  222. * @apiPermission none
  223. * @apiVersion 0.1.0
  224. * @apiParam {string} keyword 关键字
  225. * @apiSuccessExample {json} Success-Response:
  226. * HTTP/1.1 200 OK
  227. *{
  228. * "status": true,
  229. * "status_code": 0,
  230. * "message": "",
  231. * "data":[
  232. * {
  233. * "nickname": "ha", 昵称
  234. * "pic": "", 头像
  235. * ...
  236. * },
  237. * ]
  238. *}
  239. * @apiErrorExample {json} Error-Response:
  240. * HTTP/1.1 400 Bad Request
  241. */
  242. public function userSearch(Request $request)
  243. {
  244. $user = $this->getUser();
  245. if (empty($request->keyword)) {
  246. return $this->api('');
  247. }
  248. $keyword ='%'.$request->keyword.'%';
  249. $users = UserInfoModel::where('nickname','like',$keyword)->get();
  250. $this->insertSearchTable($user->id,$request->keyword);
  251. return $this->api($users);
  252. }
  253. //获取轮播图
  254. public function getBanner()
  255. {
  256. $banner = BaseSettingsModel::where(['category' => 'banner'])->where(['status' => '1'])
  257. ->orderBy('sort')->get()->toArray();
  258. return $banner;
  259. }
  260. // 完善梦想信息
  261. public function dreams($dreams)
  262. {
  263. foreach ($dreams as $k => $dream) {
  264. $data = UserCareDream::where('dream_id',$dream->id)->get();
  265. $dream->care_num = count($data);
  266. $dream->img = $dream->img?$dream->img->pic:'';
  267. }
  268. }
  269. // 查看关注用户的最新动态
  270. // public function newsInfo(Request $request)
  271. // {
  272. //// 查看后user_care_user dream_info 更新为零 首页不再显示
  273. // $user = $this->getUser();
  274. // $other_id = $request->id;
  275. // if (empty($other_id)) return $this->error(ErrorCode::MEMBER_NOT_EXIST);
  276. // UserCareDream::where('user_id',$user->id)->where('dream_user_id',$other_id)->update(['interaction_number'=>0]);
  277. // $data = UserInfoModel::where('id',$other_id)->with(['allInteraction'=>function ($query){
  278. // $query->orderBy('id','desc');
  279. // }])->first();
  280. //// $data = UserInfoModel::find($other_id)->allInteraction;
  281. //// dd($data) ;
  282. //// dd($data->allInteraction);
  283. // foreach ($data->allInteraction as $item) {
  284. // $item->time = DreamInfoModel::find($item->dream_id)->time;
  285. // $item->comments = $item->comments;
  286. // foreach ($item->comments as $k => $v) {
  287. // $v->pic = UserInfoModel::find($v->user_id)->pic;
  288. // $v->replay = $v->replay;
  289. // foreach ($v->replay as $k1 => $v1) {
  290. // $v1->pic = UserInfoModel::find($v1->user_id)->pic;
  291. // }
  292. // }
  293. // }
  294. // return $this->api($data);
  295. // }
  296. public function insertSearchTable($id,$keyword)
  297. {
  298. $info = SearchInfoModel::where('user_id',$id)->
  299. where('search',trim($keyword))->first();
  300. if (count($info) == 0) {
  301. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  302. }else{
  303. $info->times += 1;
  304. $info->save();
  305. }
  306. }
  307. /**
  308. * @api {get} /api/index/filter 筛选
  309. * @apiDescription 筛选
  310. * @apiGroup Index
  311. * @apiPermission none
  312. * @apiVersion 0.1.0
  313. * @apiParam {string} [sex] 性别 //1男,2女,0:全部,
  314. * @apiParam {string} [age] 年龄段 0:全部,1:18-21,2:22-25,3:26-29,4:30-33,5:34-37,6:>37,
  315. * @apiParam {string} [area] 地区 例:成都
  316. * @apiSuccessExample {json} Success-Response:
  317. * HTTP/1.1 200 OK
  318. *{
  319. * "status": true,
  320. * "status_code": 0,
  321. * "message": "",
  322. * "data":[
  323. * {
  324. * "nickname": "ha", 昵称
  325. * "pic": "", 头像
  326. * ...
  327. * },
  328. * ]
  329. *}
  330. * @apiErrorExample {json} Error-Response:
  331. * HTTP/1.1 400 Bad Request
  332. */
  333. public function filter(Request $request)
  334. {
  335. $sex = $request->sex;
  336. $age = $request->age;
  337. $area = $request->area;
  338. $login_user = $this->getUser();
  339. $id = $login_user->id;
  340. $query = new DreamInfoModel();
  341. if (($sex) == 1) {
  342. $query = $query->whereHas('user', function ($select) use ($sex) {
  343. $select->where('sex',0);
  344. });
  345. }
  346. if (($sex) == 2) {
  347. $query = $query->whereHas('user', function ($select) use ($sex) {
  348. $select->where('sex',1);
  349. });
  350. }
  351. if ($age == 1) {
  352. $query = $query->whereHas('user', function ($select) use ($age) {
  353. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-21)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-18)));
  354. });
  355. }
  356. if ($age == 2) {
  357. $query = $query->whereHas('user', function ($select) use ($age) {
  358. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-25)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-22)));
  359. });
  360. }
  361. if ($age == 3) {
  362. $query = $query->whereHas('user', function ($select) use ($age) {
  363. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-29)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-26)));
  364. });
  365. }
  366. if ($age == 4) {
  367. $query = $query->whereHas('user', function ($select) use ($age) {
  368. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-33)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-30)));
  369. });
  370. }
  371. if ($age == 5) {
  372. $query = $query->whereHas('user', function ($select) use ($age) {
  373. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-37)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-34)));
  374. });
  375. }
  376. if ($age == 6) {
  377. $query = $query->whereHas('user', function ($select) use ($age) {
  378. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-38)));
  379. });
  380. }
  381. if (!empty($area)) {
  382. $query = $query->whereHas('user', function ($select) use ($area) {
  383. $select->where('city','like','%'.$area.'%');
  384. });
  385. }
  386. $arr1 =DreamInfoModel::limit(20)->select('id')->get()->toArray();
  387. $id_arr1 = array_column($arr1,'id');
  388. $arr2 =DreamInfoModel::orderBy('score','desc')->limit(120)->select('id')->get()->toArray();
  389. $id_arr2 = array_column($arr2,'id');
  390. $dtusers =UserCareUser::where('user_id',$id)->with('other_user')->
  391. where('dream_number','>',0)->orderBy('created_at')->get()->toArray();
  392. $hdusers = CommentInfoModel::where(function ($query) use ($id) {
  393. $query->where('user_id',$id)->orWhere('to_user_id',$id);
  394. })->where('is_read',0)->with('to_user')->orderBy('created_at')->get()->toArray();
  395. $users = [] ;
  396. foreach ($dtusers as $k => $v){
  397. $users[] = $v['other_user'];
  398. }
  399. foreach ($hdusers as $k => $v){
  400. $users[] = $v['to_user'];
  401. }
  402. $type = $request->type;
  403. if ($type == 'trend') {
  404. $dreams = $query->orderBy('score','desc')->with('user')->whereNotIn('id', $id_arr1)->limit(100)->paginate(20);
  405. $this->dreams($dreams);
  406. return $this->api(compact('users','dreams'));
  407. } elseif ($type == 'news') {
  408. $dreams = $query->orderBy('score','desc')->orderBy('created_at','desc')->with('user')->whereNotIn('id', $id_arr2)->limit(500)->paginate(20);
  409. $this->dreams($dreams);
  410. return $this->api(compact('users','dreams'));
  411. } else{
  412. $banners = $this->getBanner();
  413. $dreams = $query->orderBy('score','desc')->with('user')->limit(20)->paginate(20);
  414. $this->dreams($dreams);
  415. return $this->api(compact('banners','users','dreams'));
  416. }
  417. }
  418. }