IndexController.php 19 KB

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