IndexController.php 16 KB

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