IndexController.php 18 KB

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