IndexController.php 18 KB

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