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\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. use Illuminate\Support\Facades\Log;
  14. class IndexController extends Controller
  15. {
  16. /**
  17. * @api {get} /api/index/home 首页
  18. * @apiDescription 首页
  19. * @apiGroup Index
  20. * @apiParam {string} [type=hot] (热门)hot/(潮流)trend/(最新)news
  21. * @apiParam {int} [page=1] 页码(分页参数)
  22. * @apiPermission none
  23. * @apiVersion 0.1.0
  24. * @apiSuccessExample {json} Success-Response:
  25. * HTTP/1.1 200 OK
  26. *{
  27. * "status": true,
  28. * "status_code": 0,
  29. * "message": "",
  30. * "data": {
  31. * "banners": [
  32. * {
  33. * "id": 32,
  34. * "key": "2",
  35. * "value": "http://w17.9026.com/img/banner/banner_2.png",
  36. * "sort": 2,
  37. * "category": "banner",
  38. * "pid": 0,
  39. * "status": 1,
  40. * "created_at": null,
  41. * "updated_at": "2017-06-26 09:21:36",
  42. * "deleted_at": null
  43. * }
  44. * ],
  45. * "users": [ 关注梦想有新的动态显示用户
  46. * {
  47. * "id": 2,
  48. * "dream_id": 2, 最近动态的梦想id
  49. * "phone": "13880642881",
  50. * "nickname": "name2",
  51. * "avatar": "http://www.miao.com/upload/user/20170630/08e235d7211944e9b6482965b4d7c42e.jpg",
  52. * },
  53. * ],
  54. * "dreams": {
  55. * "current_page": 1,
  56. * "data": [
  57. * {
  58. * "id": 5,
  59. * "user_id": 1,
  60. * "name": "梦想标题1",
  61. * "about": "梦想介绍",
  62. * "coin": 2500,
  63. * "end_time": "2017-05-11 17:00:00",
  64. * "get_coin": 0,
  65. * "mark": 0,
  66. * "status": 2,
  67. * "video": "url",
  68. * "score": 100079365,
  69. * "sign": "梦想达人",
  70. * "signs": "[]", //梦想标签
  71. * "code": "",
  72. * "parameter": 100,
  73. * "created_at": "2017-06-25 12:45:22",
  74. * "updated_at": "2017-07-03 12:23:37",
  75. * "care_num": 1,
  76. * "img": {
  77. * "title": "",
  78. * "pic": "http://www.miao.com/upload/dream/20170629/a18cadd1560f21453a08fdd81fb8fb2d.jpg"
  79. * },
  80. * "user": {
  81. * "id": 1,
  82. * "avatar": "/upload/user/20170629/e77068cc14c82086fb6b16e5bb7c3dd1.jpg",
  83. * }
  84. * },
  85. * }
  86. * ],
  87. * "from": 1,
  88. * "last_page": 1,
  89. * "next_page_url": null,
  90. * "path": "http://www.miao.com/api/index/home",
  91. * "per_page": 20,
  92. * "prev_page_url": null,
  93. * "to": 11,
  94. * "total": 11
  95. * }
  96. * }
  97. *}
  98. * @apiErrorExample {json} Error-Response:
  99. * HTTP/1.1 400 Bad Request
  100. * {
  101. * "state": false,
  102. * "code": 1000,
  103. * "message": "传入参数不正确",
  104. * "data": null or []
  105. * }
  106. */
  107. public function home(Request $request)
  108. {
  109. $login_user = $this->getUser();
  110. $id = $login_user->id;
  111. $arr1 =DreamInfoModel::orderBy('score','desc')->limit(30)->select('id')->get()->toArray();
  112. $id_arr1 = array_column($arr1,'id');
  113. $arr2 =DreamInfoModel::orderBy('score','desc')->offset(15)->limit(120)->select('id')->get()->toArray();
  114. $id_arr2 = array_column($arr2,'id');
  115. // 首页显示用户条件 关注的梦想有新的动态 2动态下有新的评论
  116. $users_care_dream = UserCareDream::where('dream_user_id',$id)->where('interaction_number','>',0)->get()->toArray();
  117. $user_ids = array_unique(array_column($users_care_dream,'dream_user_id','dream_id'));
  118. Log::info($user_ids);
  119. $users = [] ;
  120. if (!empty($user_ids)) {
  121. foreach ($user_ids as $dream_id => $user_id) {
  122. $user = UserInfoModel::find($user_id);
  123. $user->dream_id = $dream_id;
  124. $users[] = $user;
  125. }
  126. }
  127. $type = $request->type;
  128. if ($type == 'trend') {
  129. $banners = $this->getBanner();
  130. $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);
  131. $this->dreams($dreams);
  132. } elseif ($type == 'news') {
  133. $banners = $this->getBanner();
  134. $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);
  135. $this->dreams($dreams);
  136. } else{
  137. // 热门
  138. $banners = $this->getBanner();
  139. $dreams = DreamInfoModel::where('end_time','>',date('Y-m-d H:i:s'))->orderBy('score','desc')->with('user')->paginate(15);
  140. $this->dreams($dreams);
  141. }
  142. foreach ($dreams as $dream) {
  143. $dream->signs = explode(',',$dream->sign);
  144. }
  145. return $this->api(compact('banners','users','dreams'));
  146. }
  147. /**
  148. * @api {get} /api/index/search 搜索
  149. * @apiDescription 搜索
  150. * @apiGroup Index
  151. * @apiPermission none
  152. * @apiVersion 0.1.0
  153. * @apiParam {string} [keyword] 关键字可选 (ha/name)
  154. * @apiSuccessExample {json} Success-Response:
  155. * HTTP/1.1 200 OK
  156. * get
  157. *{
  158. * "status": true,
  159. * "status_code": 0,
  160. * "message": "",
  161. * "data": {
  162. * "hot_searches": {
  163. * "name": 1,
  164. * "梦想": 1,
  165. * "ha": 1
  166. * },
  167. * "history_searches": [
  168. * {
  169. * "id": 3,
  170. * "user_id": 1,
  171. * "search": "ha",
  172. * "times": 1,
  173. * "created_at": "2017-06-25 16:21:47",
  174. * "updated_at": "2017-06-25 16:21:47"
  175. * },
  176. * ]
  177. * }
  178. *}
  179. * @apiErrorExample {json} Error-Response:
  180. * HTTP/1.1 400 Bad Request
  181. */
  182. public function search(Request $request)
  183. {
  184. $user = $this->getUser();
  185. $keyword ='%'.$request->keyword.'%';
  186. $user_infos = UserInfoModel::where('nickname','like',$keyword)->get();
  187. $dream_infos = DreamInfoModel::where(function ($query) use($keyword) {
  188. // ->where('end_time','>=',time())
  189. $query->where('name','like',$keyword);
  190. })
  191. ->orWhere(function ($query) use($keyword){
  192. $query->where('sign','like',$keyword);
  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, 接收1,2,3,4...
  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 (!empty($age)) {
  343. $age_arr = explode(',',$age); // 被选中的年龄数组
  344. foreach ($age_arr as $value) {
  345. switch ($value) {
  346. case 1:
  347. $query = $query->orWhereHas('user', function ($select) use ($age) {
  348. $select->whereYear('birthday','>=',(date('Y')-21))->whereYear('birthday','<=',(date('Y')-18));
  349. });
  350. break;
  351. case 2:
  352. $query = $query->orWhereHas('user', function ($select) use ($age) {
  353. $select->whereYear('birthday','>=',(date('Y')-25))->whereYear('birthday','<=',(date('Y')-22));
  354. });
  355. break;
  356. case 3:
  357. $query = $query->orWhereHas('user', function ($select) use ($age) {
  358. $select->whereYear('birthday','>=',(date('Y')-29))->whereYear('birthday','<=',(date('Y')-26));
  359. });
  360. break;
  361. case 4:
  362. $query = $query->orWhereHas('user', function ($select) use ($age) {
  363. $select->whereYear('birthday','>=',(date('Y')-33))->whereYear('birthday','<=',(date('Y')-30));
  364. });
  365. break;
  366. case 5:
  367. $query = $query->orWhereHas('user', function ($select) use ($age) {
  368. $select->whereYear('birthday','>=',(date('Y')-37))->whereYear('birthday','<=',(date('Y')-34));
  369. });
  370. break;
  371. case 6:
  372. $query = $query->orWhereHas('user', function ($select) use ($age) {
  373. $select->whereYear('birthday','<=',(date('Y')-38));
  374. });
  375. break;
  376. case 7:
  377. $query = $query->orWhereHas('user', function ($select) use ($age) {
  378. $select->whereYear('birthday','>=',(date('Y')-17));
  379. });
  380. break;
  381. }
  382. }
  383. if (($sex) == 1) {
  384. $query = $query->whereHas('user', function ($select) use ($sex) {
  385. $select->where('sex',0);
  386. });
  387. }
  388. if (($sex) == 2) {
  389. $query = $query->whereHas('user', function ($select) use ($sex) {
  390. $select->where('sex',1);
  391. });
  392. }
  393. }
  394. /* if ($age == 7) {
  395. $query = $query->whereHas('user', function ($select) use ($age) {
  396. $select->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-18)));
  397. });
  398. }
  399. if ($age == 1) {
  400. $query = $query->whereHas('user', function ($select) use ($age) {
  401. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-21)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-18)));
  402. });
  403. }
  404. if ($age == 2) {
  405. $query = $query->whereHas('user', function ($select) use ($age) {
  406. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-25)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-22)));
  407. });
  408. }
  409. if ($age == 3) {
  410. $query = $query->whereHas('user', function ($select) use ($age) {
  411. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-29)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-26)));
  412. });
  413. }
  414. if ($age == 4) {
  415. $query = $query->whereHas('user', function ($select) use ($age) {
  416. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-33)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-30)));
  417. });
  418. }
  419. if ($age == 5) {
  420. $query = $query->whereHas('user', function ($select) use ($age) {
  421. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-37)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-34)));
  422. });
  423. }
  424. if ($age == 6) {
  425. $query = $query->whereHas('user', function ($select) use ($age) {
  426. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-38)));
  427. });
  428. }*/
  429. if (!empty($area)) {
  430. $query = $query->whereHas('user', function ($select) use ($area) {
  431. $select->where('city','like','%'.$area.'%');
  432. });
  433. }
  434. \Log::info($query->toSql());
  435. $arr1 =DreamInfoModel::orderBy('id')->limit(20)->select('id')->get()->toArray();
  436. $id_arr1 = array_column($arr1,'id');
  437. $arr2 =DreamInfoModel::orderBy('id','desc')->limit(120)->select('id')->get()->toArray();
  438. $id_arr2 = array_column($arr2,'id');
  439. $dtusers =UserCareUser::where('user_id',$id)->with('other_user')->
  440. where('dream_number','>',0)->orderBy('created_at')->get()->toArray();
  441. $hdusers = CommentInfoModel::where(function ($query) use ($id) {
  442. $query->where('user_id',$id)->orWhere('to_user_id',$id);
  443. })->where('is_read',0)->with('to_user')->orderBy('created_at')->get()->toArray();
  444. $users = [] ;
  445. foreach ($dtusers as $k => $v){
  446. $users[] = $v['other_user'];
  447. }
  448. foreach ($hdusers as $k => $v){
  449. $users[] = $v['to_user'];
  450. }
  451. $type = $request->type;
  452. if ($type == 'trend') {
  453. $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);
  454. $this->dreams($dreams);
  455. return $this->api(compact('users','dreams'));
  456. } elseif ($type == 'news') {
  457. $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);
  458. $this->dreams($dreams);
  459. return $this->api(compact('users','dreams'));
  460. } else{
  461. $banners = $this->getBanner();
  462. $dreams = $query->where('end_time','<',date('Y-m-d H:i:s'))->orderBy('score','desc')->with('user')->limit(20)->paginate(20);
  463. $this->dreams($dreams);
  464. return $this->api(compact('banners','users','dreams'));
  465. }
  466. }
  467. }