IndexController.php 19 KB

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