IndexController.php 18 KB

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