IndexController.php 19 KB

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