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