IndexController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. // 访问首页时 当用户的梦想结束时通知用户
  111. // $this->systemInfo($id);
  112. $arr1 =DreamInfoModel::orderBy('score','desc')->limit(30)->select('id')->get()->toArray();
  113. $id_arr1 = array_column($arr1,'id');
  114. $arr2 =DreamInfoModel::orderBy('score','desc')->offset(15)->limit(120)->select('id')->get()->toArray();
  115. $id_arr2 = array_column($arr2,'id');
  116. $users_care_dream = UserCareDream::where('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. $users = [] ;
  119. if (!empty($user_ids)) {
  120. foreach ($user_ids as $dream_id => $user_id) {
  121. $user = UserInfoModel::find($user_id);
  122. $user->dream_id = $dream_id;
  123. $users[] = $user;
  124. }
  125. }
  126. $type = $request->type;
  127. if ($type == 'trend') {
  128. $banners = $this->getBanner();
  129. $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);
  130. $this->dreams($dreams);
  131. } elseif ($type == 'news') {
  132. $banners = $this->getBanner();
  133. $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);
  134. $this->dreams($dreams);
  135. } else{
  136. $banners = $this->getBanner();
  137. $dreams = DreamInfoModel::where('end_time','>',date('Y-m-d H:i:s'))->orderBy('score','desc')->with('user')->limit(15)->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. public function systemInfo($id)
  146. {
  147. $user = UserInfoModel::find($id);
  148. $end_dream_info = DreamInfoModel::where('user_id',$id)->where('end_time','>',date('Y-m-d H:i:s'))->get();
  149. if (count($end_dream_info)>0) {
  150. foreach ($end_dream_info as $item){
  151. $message = "你的梦想《".$item->name."》已结束,点击了解接下来的步骤";
  152. $arr = [
  153. 'user_id'=>0,
  154. 'message'=>$message,
  155. 'to_user_id'=>$id,
  156. 'dream_id'=>$item->id,
  157. 'is_end'=>1,
  158. 'type_id'=>1,
  159. 'attr_id'=>6,
  160. ];
  161. SystemInfoModel::firstOrCreate($arr);
  162. // 给支持者发送消息
  163. $support_dream = SupportDreamModel::where('dream_id',$item->id)->get();
  164. $top = [] ;
  165. foreach ($support_dream as $item2) {
  166. if (!array_key_exists($item2->user_id,$top)) {
  167. $top[$item2->user_id] = $item2->score;
  168. }else{
  169. $top[$item2->user_id] += $item2->score;
  170. }
  171. }
  172. arsort($top);
  173. $new_arr = array_values($top);
  174. foreach ($top as $k => $v) {
  175. $key = array_search($v,$new_arr);
  176. $message = $item->name."已经结束啦!谢谢你的支持,你可是他的第".($key+1)."支持者哦!";
  177. if (empty($key)) { //最大支持者
  178. $message = "恭喜你成为《".$item->name."》的《梦主》!
  179. ". $user->nickname."会以你提供的微信/电话联系你约好时间地点亲自感谢你给予的支持和鼓励。如果你不希望见面或
  180. 不想要梦想者拥有你的联系方式,请按《不需要见面》的按钮或联系客服。";
  181. $arr2 = [
  182. 'user_id'=>0,
  183. 'message'=>$message,
  184. 'to_user_id'=>$k,
  185. 'dream_id'=>$item->id,
  186. 'is_end'=>1,
  187. 'is_url'=>1,
  188. 'type_id'=>2,
  189. 'is_max'=>1,
  190. ];
  191. }else{
  192. $arr2 = [
  193. 'user_id'=>0,
  194. 'message'=>$message,
  195. 'to_user_id'=>$k,
  196. 'dream_id'=>$item->id,
  197. 'is_end'=>1,
  198. 'is_url'=>1,
  199. 'type_id'=>2,
  200. ];
  201. }
  202. SystemInfoModel::firstOrCreate($arr2);
  203. }
  204. }
  205. // 2 我关注的梦想倒计时 通知消息
  206. $dreams = UserCareDream::where('user_id',$id)->with('dreams')->get();
  207. foreach ($dreams as $item) {
  208. if (date('Y-m-d',strtotime($item->dreams->end_time)) == date('Y-m-d',time()+1*24*3600)) {
  209. $message = $item->name.'过1天就要结束啦!';
  210. $arr3 = [
  211. 'user_id'=>0,
  212. 'message'=>$message,
  213. 'to_user_id'=>$id,
  214. 'dream_id'=>$item->dream_id,
  215. 'is_url'=>1,
  216. 'type_id'=>2,
  217. 'attr_id'=>8,
  218. ];
  219. SystemInfoModel::firstOrCreate($arr3);
  220. }
  221. }
  222. /* if (!empty($arr2)) {
  223. SystemInfoModel::insert($arr2);
  224. }*/
  225. }
  226. }
  227. /**
  228. * @api {get} /api/index/search 搜索
  229. * @apiDescription 搜索
  230. * @apiGroup Index
  231. * @apiPermission none
  232. * @apiVersion 0.1.0
  233. * @apiParam {string} [keyword] 关键字可选 (ha/name)
  234. * @apiSuccessExample {json} Success-Response:
  235. * HTTP/1.1 200 OK
  236. * get
  237. *{
  238. * "status": true,
  239. * "status_code": 0,
  240. * "message": "",
  241. * "data": {
  242. * "hot_searches": {
  243. * "name": 1,
  244. * "梦想": 1,
  245. * "ha": 1
  246. * },
  247. * "history_searches": [
  248. * {
  249. * "id": 3,
  250. * "user_id": 1,
  251. * "search": "ha",
  252. * "times": 1,
  253. * "created_at": "2017-06-25 16:21:47",
  254. * "updated_at": "2017-06-25 16:21:47"
  255. * },
  256. * ]
  257. * }
  258. *}
  259. * @apiErrorExample {json} Error-Response:
  260. * HTTP/1.1 400 Bad Request
  261. */
  262. public function search(Request $request)
  263. {
  264. $user = $this->getUser();
  265. $keyword ='%'.$request->keyword.'%';
  266. $user_infos = UserInfoModel::where('nickname','like',$keyword)->get();
  267. $dream_infos = DreamInfoModel::where(function ($query) use($keyword) {
  268. // ->where('end_time','>=',time())
  269. $query->where('name','like',$keyword);
  270. })
  271. ->orWhere(function ($query) use($keyword){
  272. $query->where('sign','like',$keyword);
  273. })->with(['user','img'])->get();
  274. $this->dreams($dream_infos);
  275. $signs = BaseSettingsModel::where('category','sign')->where('value','like',$keyword)->get();
  276. if (empty($request->keyword)) {
  277. // 历史搜索
  278. $history_searches = $user->search()->orderBy('id','desc')->limit(10)->get();
  279. // 热门搜索
  280. $data2 = SearchInfoModel::get();
  281. $hot_searches = [];
  282. foreach ($data2 as $k => $v) {
  283. if (count($hot_searches) == 6) {
  284. break;
  285. }
  286. if (!array_key_exists($v->search,$hot_searches)) {
  287. $hot_searches[$v->search] = $v->times;
  288. }else{
  289. $hot_searches[$v->search] += $v->times;
  290. }
  291. }
  292. arsort($hot_searches);
  293. return $this->api(compact('hot_searches','history_searches'));
  294. }
  295. // 写入搜索记录
  296. $this->insertSearchTable($user->id,$request->keyword);
  297. return $this->api(compact('user_infos','dream_infos','signs'));
  298. }
  299. /**
  300. * @api {get} /api/index/user_search 用户搜索
  301. * @apiDescription 用户搜索
  302. * @apiGroup Index
  303. * @apiPermission none
  304. * @apiVersion 0.1.0
  305. * @apiParam {string} keyword 关键字
  306. * @apiSuccessExample {json} Success-Response:
  307. * HTTP/1.1 200 OK
  308. *{
  309. * "status": true,
  310. * "status_code": 0,
  311. * "message": "",
  312. * "data":[
  313. * {
  314. * "nickname": "ha", 昵称
  315. * "pic": "", 头像
  316. * ...
  317. * },
  318. * ]
  319. *}
  320. * @apiErrorExample {json} Error-Response:
  321. * HTTP/1.1 400 Bad Request
  322. */
  323. public function userSearch(Request $request)
  324. {
  325. $user = $this->getUser();
  326. if (empty($request->keyword)) {
  327. return $this->api('');
  328. }
  329. $keyword ='%'.$request->keyword.'%';
  330. $users = UserInfoModel::where('nickname','like',$keyword)->get();
  331. $this->insertSearchTable($user->id,$request->keyword);
  332. return $this->api($users);
  333. }
  334. //获取轮播图
  335. public function getBanner()
  336. {
  337. $banner = BaseSettingsModel::where(['category' => 'banner'])->where(['status' => '1'])
  338. ->orderBy('sort')->get()->toArray();
  339. return $banner;
  340. }
  341. // 完善梦想信息
  342. public function dreams($dreams)
  343. {
  344. foreach ($dreams as $k => $dream) {
  345. $data = UserCareDream::where('dream_id',$dream->id)->get();
  346. $dream->care_num = count($data);
  347. $dream->img = $dream->img?$dream->img->pic:'';
  348. }
  349. }
  350. // 查看关注用户的最新动态
  351. // public function newsInfo(Request $request)
  352. // {
  353. //// 查看后user_care_user dream_info 更新为零 首页不再显示
  354. // $user = $this->getUser();
  355. // $other_id = $request->id;
  356. // if (empty($other_id)) return $this->error(ErrorCode::MEMBER_NOT_EXIST);
  357. // UserCareDream::where('user_id',$user->id)->where('dream_user_id',$other_id)->update(['interaction_number'=>0]);
  358. // $data = UserInfoModel::where('id',$other_id)->with(['allInteraction'=>function ($query){
  359. // $query->orderBy('id','desc');
  360. // }])->first();
  361. //// $data = UserInfoModel::find($other_id)->allInteraction;
  362. //// dd($data) ;
  363. //// dd($data->allInteraction);
  364. // foreach ($data->allInteraction as $item) {
  365. // $item->time = DreamInfoModel::find($item->dream_id)->time;
  366. // $item->comments = $item->comments;
  367. // foreach ($item->comments as $k => $v) {
  368. // $v->pic = UserInfoModel::find($v->user_id)->pic;
  369. // $v->replay = $v->replay;
  370. // foreach ($v->replay as $k1 => $v1) {
  371. // $v1->pic = UserInfoModel::find($v1->user_id)->pic;
  372. // }
  373. // }
  374. // }
  375. // return $this->api($data);
  376. // }
  377. public function insertSearchTable($id,$keyword)
  378. {
  379. $info = SearchInfoModel::where('user_id',$id)->
  380. where('search',trim($keyword))->first();
  381. if (count($info) == 0) {
  382. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  383. }else{
  384. $info->times += 1;
  385. $info->save();
  386. }
  387. }
  388. /**
  389. * @api {get} /api/index/filter 筛选
  390. * @apiDescription 筛选
  391. * @apiGroup Index
  392. * @apiPermission none
  393. * @apiVersion 0.1.0
  394. * @apiParam {string} [sex] 性别 //1男,2女,0:全部,
  395. * @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...
  396. * @apiParam {string} [area] 地区 例:成都
  397. * @apiSuccessExample {json} Success-Response:
  398. * HTTP/1.1 200 OK
  399. *{
  400. * "status": true,
  401. * "status_code": 0,
  402. * "message": "",
  403. * "data":[
  404. * {
  405. * "nickname": "ha", 昵称
  406. * "pic": "", 头像
  407. * ...
  408. * },
  409. * ]
  410. *}
  411. * @apiErrorExample {json} Error-Response:
  412. * HTTP/1.1 400 Bad Request
  413. */
  414. public function filter(Request $request)
  415. {
  416. $sex = $request->sex;
  417. $age = $request->age;
  418. $area = $request->area;
  419. $login_user = $this->getUser();
  420. $id = $login_user->id;
  421. $query = new DreamInfoModel();
  422. if (!empty($age)) {
  423. $age_arr = explode(',',$age); // 被选中的年龄数组
  424. foreach ($age_arr as $value) {
  425. switch ($value) {
  426. case 1:
  427. $query = $query->orWhereHas('user', function ($select) use ($age) {
  428. $select->whereYear('birthday','>=',(date('Y')-21))->whereYear('birthday','<=',(date('Y')-18));
  429. });
  430. break;
  431. case 2:
  432. $query = $query->orWhereHas('user', function ($select) use ($age) {
  433. $select->whereYear('birthday','>=',(date('Y')-25))->whereYear('birthday','<=',(date('Y')-22));
  434. });
  435. break;
  436. case 3:
  437. $query = $query->orWhereHas('user', function ($select) use ($age) {
  438. $select->whereYear('birthday','>=',(date('Y')-29))->whereYear('birthday','<=',(date('Y')-26));
  439. });
  440. break;
  441. case 4:
  442. $query = $query->orWhereHas('user', function ($select) use ($age) {
  443. $select->whereYear('birthday','>=',(date('Y')-33))->whereYear('birthday','<=',(date('Y')-30));
  444. });
  445. break;
  446. case 5:
  447. $query = $query->orWhereHas('user', function ($select) use ($age) {
  448. $select->whereYear('birthday','>=',(date('Y')-37))->whereYear('birthday','<=',(date('Y')-34));
  449. });
  450. break;
  451. case 6:
  452. $query = $query->orWhereHas('user', function ($select) use ($age) {
  453. $select->whereYear('birthday','<=',(date('Y')-38));
  454. });
  455. break;
  456. case 7:
  457. $query = $query->orWhereHas('user', function ($select) use ($age) {
  458. $select->whereYear('birthday','>=',(date('Y')-17));
  459. });
  460. break;
  461. }
  462. }
  463. if (($sex) == 1) {
  464. $query = $query->whereHas('user', function ($select) use ($sex) {
  465. $select->where('sex',0);
  466. });
  467. }
  468. if (($sex) == 2) {
  469. $query = $query->whereHas('user', function ($select) use ($sex) {
  470. $select->where('sex',1);
  471. });
  472. }
  473. }
  474. /* if ($age == 7) {
  475. $query = $query->whereHas('user', function ($select) use ($age) {
  476. $select->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-18)));
  477. });
  478. }
  479. if ($age == 1) {
  480. $query = $query->whereHas('user', function ($select) use ($age) {
  481. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-21)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-18)));
  482. });
  483. }
  484. if ($age == 2) {
  485. $query = $query->whereHas('user', function ($select) use ($age) {
  486. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-25)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-22)));
  487. });
  488. }
  489. if ($age == 3) {
  490. $query = $query->whereHas('user', function ($select) use ($age) {
  491. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-29)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-26)));
  492. });
  493. }
  494. if ($age == 4) {
  495. $query = $query->whereHas('user', function ($select) use ($age) {
  496. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-33)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-30)));
  497. });
  498. }
  499. if ($age == 5) {
  500. $query = $query->whereHas('user', function ($select) use ($age) {
  501. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-37)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-34)));
  502. });
  503. }
  504. if ($age == 6) {
  505. $query = $query->whereHas('user', function ($select) use ($age) {
  506. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-38)));
  507. });
  508. }*/
  509. if (!empty($area)) {
  510. $query = $query->whereHas('user', function ($select) use ($area) {
  511. $select->where('city','like','%'.$area.'%');
  512. });
  513. }
  514. \Log::info($query->toSql());
  515. $arr1 =DreamInfoModel::orderBy('id')->limit(20)->select('id')->get()->toArray();
  516. $id_arr1 = array_column($arr1,'id');
  517. $arr2 =DreamInfoModel::orderBy('id','desc')->limit(120)->select('id')->get()->toArray();
  518. $id_arr2 = array_column($arr2,'id');
  519. $dtusers =UserCareUser::where('user_id',$id)->with('other_user')->
  520. where('dream_number','>',0)->orderBy('created_at')->get()->toArray();
  521. $hdusers = CommentInfoModel::where(function ($query) use ($id) {
  522. $query->where('user_id',$id)->orWhere('to_user_id',$id);
  523. })->where('is_read',0)->with('to_user')->orderBy('created_at')->get()->toArray();
  524. $users = [] ;
  525. foreach ($dtusers as $k => $v){
  526. $users[] = $v['other_user'];
  527. }
  528. foreach ($hdusers as $k => $v){
  529. $users[] = $v['to_user'];
  530. }
  531. $type = $request->type;
  532. if ($type == 'trend') {
  533. $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);
  534. $this->dreams($dreams);
  535. return $this->api(compact('users','dreams'));
  536. } elseif ($type == 'news') {
  537. $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);
  538. $this->dreams($dreams);
  539. return $this->api(compact('users','dreams'));
  540. } else{
  541. $banners = $this->getBanner();
  542. $dreams = $query->where('end_time','<',date('Y-m-d H:i:s'))->orderBy('score','desc')->with('user')->limit(20)->paginate(20);
  543. $this->dreams($dreams);
  544. return $this->api(compact('banners','users','dreams'));
  545. }
  546. }
  547. }