IndexController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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('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. $t = ceil((time()-strtotime($dream->created_at))/60); //梦想发布分钟数
  267. $care_num = count($data);
  268. if (count($data)==0) $care_num=1;
  269. $setting = BaseSettingsModel::where('category','paihang')->first();
  270. $a = $setting?$setting->key:1;
  271. $x = $setting?$setting->sort:10;
  272. if($x==0) $x = 10;
  273. $dream->score = (log($care_num,$x) + ($a/$t) + $dream->parameter)*100000000000000 ;
  274. $dream->save();
  275. $dream->care_num = count($data);
  276. $dream->img = $dream->img?$dream->img->pic:'';
  277. }
  278. }
  279. // 查看关注用户的最新动态
  280. // public function newsInfo(Request $request)
  281. // {
  282. //// 查看后user_care_user dream_info 更新为零 首页不再显示
  283. // $user = $this->getUser();
  284. // $other_id = $request->id;
  285. // if (empty($other_id)) return $this->error(ErrorCode::MEMBER_NOT_EXIST);
  286. // UserCareDream::where('user_id',$user->id)->where('dream_user_id',$other_id)->update(['interaction_number'=>0]);
  287. // $data = UserInfoModel::where('id',$other_id)->with(['allInteraction'=>function ($query){
  288. // $query->orderBy('id','desc');
  289. // }])->first();
  290. //// $data = UserInfoModel::find($other_id)->allInteraction;
  291. //// dd($data) ;
  292. //// dd($data->allInteraction);
  293. // foreach ($data->allInteraction as $item) {
  294. // $item->time = DreamInfoModel::find($item->dream_id)->time;
  295. // $item->comments = $item->comments;
  296. // foreach ($item->comments as $k => $v) {
  297. // $v->pic = UserInfoModel::find($v->user_id)->pic;
  298. // $v->replay = $v->replay;
  299. // foreach ($v->replay as $k1 => $v1) {
  300. // $v1->pic = UserInfoModel::find($v1->user_id)->pic;
  301. // }
  302. // }
  303. // }
  304. // return $this->api($data);
  305. // }
  306. public function insertSearchTable($id,$keyword)
  307. {
  308. $info = SearchInfoModel::where('user_id',$id)->
  309. where('search',trim($keyword))->first();
  310. if (count($info) == 0) {
  311. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  312. }else{
  313. $info->times += 1;
  314. $info->save();
  315. }
  316. }
  317. /**
  318. * @api {get} /api/index/filter 筛选
  319. * @apiDescription 筛选
  320. * @apiGroup Index
  321. * @apiPermission none
  322. * @apiVersion 0.1.0
  323. * @apiParam {string} [sex] 性别 //1男,2女,0:全部,
  324. * @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...
  325. * @apiParam {string} [area] 地区 例:成都
  326. * @apiSuccessExample {json} Success-Response:
  327. * HTTP/1.1 200 OK
  328. *{
  329. * "status": true,
  330. * "status_code": 0,
  331. * "message": "",
  332. * "data":[
  333. * {
  334. * "nickname": "ha", 昵称
  335. * "pic": "", 头像
  336. * ...
  337. * },
  338. * ]
  339. *}
  340. * @apiErrorExample {json} Error-Response:
  341. * HTTP/1.1 400 Bad Request
  342. */
  343. public function filter(Request $request)
  344. {
  345. $sex = $request->sex;
  346. $age = $request->age;
  347. $area = $request->area;
  348. $login_user = $this->getUser();
  349. $id = $login_user->id;
  350. $query = new DreamInfoModel();
  351. if (!empty($age)) {
  352. $age_arr = explode(',',$age); // 被选中的年龄数组
  353. foreach ($age_arr as $value) {
  354. switch ($value) {
  355. case 1:
  356. $query = $query->orWhereHas('user', function ($select) use ($age) {
  357. $select->whereYear('birthday','>=',(date('Y')-21))->whereYear('birthday','<=',(date('Y')-18));
  358. });
  359. break;
  360. case 2:
  361. $query = $query->orWhereHas('user', function ($select) use ($age) {
  362. $select->whereYear('birthday','>=',(date('Y')-25))->whereYear('birthday','<=',(date('Y')-22));
  363. });
  364. break;
  365. case 3:
  366. $query = $query->orWhereHas('user', function ($select) use ($age) {
  367. $select->whereYear('birthday','>=',(date('Y')-29))->whereYear('birthday','<=',(date('Y')-26));
  368. });
  369. break;
  370. case 4:
  371. $query = $query->orWhereHas('user', function ($select) use ($age) {
  372. $select->whereYear('birthday','>=',(date('Y')-33))->whereYear('birthday','<=',(date('Y')-30));
  373. });
  374. break;
  375. case 5:
  376. $query = $query->orWhereHas('user', function ($select) use ($age) {
  377. $select->whereYear('birthday','>=',(date('Y')-37))->whereYear('birthday','<=',(date('Y')-34));
  378. });
  379. break;
  380. case 6:
  381. $query = $query->orWhereHas('user', function ($select) use ($age) {
  382. $select->whereYear('birthday','<=',(date('Y')-38));
  383. });
  384. break;
  385. case 7:
  386. $query = $query->orWhereHas('user', function ($select) use ($age) {
  387. $select->whereYear('birthday','>=',(date('Y')-17));
  388. });
  389. break;
  390. }
  391. }
  392. if (($sex) == 1) {
  393. $query = $query->whereHas('user', function ($select) use ($sex) {
  394. $select->where('sex',0);
  395. });
  396. }
  397. if (($sex) == 2) {
  398. $query = $query->whereHas('user', function ($select) use ($sex) {
  399. $select->where('sex',1);
  400. });
  401. }
  402. }
  403. /* if ($age == 7) {
  404. $query = $query->whereHas('user', function ($select) use ($age) {
  405. $select->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-18)));
  406. });
  407. }
  408. if ($age == 1) {
  409. $query = $query->whereHas('user', function ($select) use ($age) {
  410. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-21)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-18)));
  411. });
  412. }
  413. if ($age == 2) {
  414. $query = $query->whereHas('user', function ($select) use ($age) {
  415. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-25)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-22)));
  416. });
  417. }
  418. if ($age == 3) {
  419. $query = $query->whereHas('user', function ($select) use ($age) {
  420. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-29)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-26)));
  421. });
  422. }
  423. if ($age == 4) {
  424. $query = $query->whereHas('user', function ($select) use ($age) {
  425. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-33)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-30)));
  426. });
  427. }
  428. if ($age == 5) {
  429. $query = $query->whereHas('user', function ($select) use ($age) {
  430. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-37)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-34)));
  431. });
  432. }
  433. if ($age == 6) {
  434. $query = $query->whereHas('user', function ($select) use ($age) {
  435. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-38)));
  436. });
  437. }*/
  438. if (!empty($area)) {
  439. $query = $query->whereHas('user', function ($select) use ($area) {
  440. $select->where('city','like','%'.$area.'%');
  441. });
  442. }
  443. \Log::info($query->toSql());
  444. $arr1 =DreamInfoModel::orderBy('id')->limit(20)->select('id')->get()->toArray();
  445. $id_arr1 = array_column($arr1,'id');
  446. $arr2 =DreamInfoModel::orderBy('id','desc')->limit(120)->select('id')->get()->toArray();
  447. $id_arr2 = array_column($arr2,'id');
  448. $dtusers =UserCareUser::where('user_id',$id)->with('other_user')->
  449. where('dream_number','>',0)->orderBy('created_at')->get()->toArray();
  450. $hdusers = CommentInfoModel::where(function ($query) use ($id) {
  451. $query->where('user_id',$id)->orWhere('to_user_id',$id);
  452. })->where('is_read',0)->with('to_user')->orderBy('created_at')->get()->toArray();
  453. $users = [] ;
  454. foreach ($dtusers as $k => $v){
  455. $users[] = $v['other_user'];
  456. }
  457. foreach ($hdusers as $k => $v){
  458. $users[] = $v['to_user'];
  459. }
  460. $type = $request->type;
  461. if ($type == 'trend') {
  462. $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);
  463. $this->dreams($dreams);
  464. return $this->api(compact('users','dreams'));
  465. } elseif ($type == 'news') {
  466. $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);
  467. $this->dreams($dreams);
  468. return $this->api(compact('users','dreams'));
  469. } else{
  470. $banners = $this->getBanner();
  471. $dreams = $query->where('end_time','<',date('Y-m-d H:i:s'))->orderBy('score','desc')->with('user')->limit(20)->paginate(20);
  472. $this->dreams($dreams);
  473. return $this->api(compact('banners','users','dreams'));
  474. }
  475. }
  476. }