IndexController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. // 潮流 发布三天之内的梦想
  130. $banners = $this->getBanner();
  131. $dreams = DreamInfoModel::where('status',1)->where('end_time','>',date('Y-m-d H:i:s'))->where('created_at','<',date('Y-m-d H:i:s'))->where('created_at','>',date('Y-m-d H:i:s',time()-3*24*3600))->orderBy('score','desc')->with('user')->paginate(20);
  132. $this->dreams($dreams);
  133. } elseif ($type == 'news') {
  134. // 最新 发布不到一天前的梦想 ->whereNotIn('id', $id_arr2)
  135. $banners = $this->getBanner();
  136. $dreams = DreamInfoModel::where('status',1)->where('end_time','>',date('Y-m-d H:i:s'))->where('created_at','<',date('Y-m-d H:i:s'))->where('created_at','>',date('Y-m-d H:i:s',time()-1*24*3600))->orderBy('created_at','desc')->with('user')->paginate(20);
  137. $this->dreams($dreams);
  138. } else{
  139. // 热门
  140. $banners = $this->getBanner();
  141. $dreams = DreamInfoModel::where('status',1)->where('end_time','>',date('Y-m-d H:i:s'))->orderBy('score','desc')->with('user')->paginate(15);
  142. $this->dreams($dreams);
  143. }
  144. foreach ($dreams as $dream) {
  145. $dream->signs = explode(',',$dream->sign);
  146. }
  147. return $this->api(compact('banners','users','dreams'));
  148. }
  149. /**
  150. * @api {get} /api/index/search 搜索
  151. * @apiDescription 搜索
  152. * @apiGroup Index
  153. * @apiPermission none
  154. * @apiVersion 0.1.0
  155. * @apiParam {string} [keyword] 关键字可选 (ha/name)
  156. * @apiSuccessExample {json} Success-Response:
  157. * HTTP/1.1 200 OK
  158. * get
  159. *{
  160. * "status": true,
  161. * "status_code": 0,
  162. * "message": "",
  163. * "data": {
  164. * "hot_searches": {
  165. * "name": 1,
  166. * "梦想": 1,
  167. * "ha": 1
  168. * },
  169. * "history_searches": [
  170. * {
  171. * "id": 3,
  172. * "user_id": 1,
  173. * "search": "ha",
  174. * "times": 1,
  175. * "created_at": "2017-06-25 16:21:47",
  176. * "updated_at": "2017-06-25 16:21:47"
  177. * },
  178. * ]
  179. * }
  180. *}
  181. * @apiErrorExample {json} Error-Response:
  182. * HTTP/1.1 400 Bad Request
  183. */
  184. public function search(Request $request)
  185. {
  186. $user = $this->getUser();
  187. $keyword ='%'.$request->keyword.'%';
  188. $user_infos = UserInfoModel::where('nickname','like',$keyword)->get();
  189. $dream_infos = DreamInfoModel::where(function ($query) use($keyword) {
  190. // ->where('end_time','>=',time())
  191. $query->where('name','like',$keyword);
  192. })
  193. ->orWhere(function ($query) use($keyword){
  194. $query->where('sign','like',$keyword);
  195. })->with(['user','img'])->get();
  196. $this->dreams($dream_infos);
  197. $signs = BaseSettingsModel::where('category','sign')->where('value','like',$keyword)->get();
  198. if (empty($request->keyword)) {
  199. // 历史搜索
  200. $history_searches = $user->search()->orderBy('id','desc')->limit(10)->get();
  201. // 热门搜索
  202. $data2 = SearchInfoModel::get();
  203. $hot_searches = [];
  204. foreach ($data2 as $k => $v) {
  205. if (count($hot_searches) == 6) {
  206. break;
  207. }
  208. if (!array_key_exists($v->search,$hot_searches)) {
  209. $hot_searches[$v->search] = $v->times;
  210. }else{
  211. $hot_searches[$v->search] += $v->times;
  212. }
  213. }
  214. arsort($hot_searches);
  215. return $this->api(compact('hot_searches','history_searches'));
  216. }
  217. // 写入搜索记录
  218. $this->insertSearchTable($user->id,$request->keyword);
  219. return $this->api(compact('user_infos','dream_infos','signs'));
  220. }
  221. /**
  222. * @api {get} /api/index/user_search 用户搜索
  223. * @apiDescription 用户搜索
  224. * @apiGroup Index
  225. * @apiPermission none
  226. * @apiVersion 0.1.0
  227. * @apiParam {string} keyword 关键字
  228. * @apiSuccessExample {json} Success-Response:
  229. * HTTP/1.1 200 OK
  230. *{
  231. * "status": true,
  232. * "status_code": 0,
  233. * "message": "",
  234. * "data":[
  235. * {
  236. * "nickname": "ha", 昵称
  237. * "pic": "", 头像
  238. * ...
  239. * },
  240. * ]
  241. *}
  242. * @apiErrorExample {json} Error-Response:
  243. * HTTP/1.1 400 Bad Request
  244. */
  245. public function userSearch(Request $request)
  246. {
  247. $user = $this->getUser();
  248. if (empty($request->keyword)) {
  249. return $this->api('');
  250. }
  251. $keyword ='%'.$request->keyword.'%';
  252. $users = UserInfoModel::where('nickname','like',$keyword)->get();
  253. $this->insertSearchTable($user->id,$request->keyword);
  254. return $this->api($users);
  255. }
  256. //获取轮播图
  257. public function getBanner()
  258. {
  259. $banner = BaseSettingsModel::where(['category' => 'banner'])->where(['status' => '1'])
  260. ->orderBy('sort')->get()->toArray();
  261. return $banner;
  262. }
  263. // 完善梦想信息
  264. public function dreams($dreams)
  265. {
  266. foreach ($dreams as $k => $dream) {
  267. $data = UserCareDream::where('dream_id',$dream->id)->get();
  268. $t = ceil((time()-strtotime($dream->created_at))/60); //梦想发布分钟数
  269. $care_num = count($data) + $dream->add_care_number;
  270. if (count($data)==0) $care_num=1;
  271. $setting = BaseSettingsModel::where('category','paihang')->first();
  272. $a = $setting?$setting->key:1;
  273. $x = $setting?$setting->sort:10;
  274. if($x==0) $x = 10;
  275. $dream->score = (log($care_num,$x) + ($a/$t) + $dream->parameter)*100000000000000 ;
  276. $dream->save();
  277. $dream->care_num = count($data);
  278. $dream->img = $dream->img?$dream->img->pic:'';
  279. }
  280. }
  281. // 查看关注用户的最新动态
  282. // public function newsInfo(Request $request)
  283. // {
  284. //// 查看后user_care_user dream_info 更新为零 首页不再显示
  285. // $user = $this->getUser();
  286. // $other_id = $request->id;
  287. // if (empty($other_id)) return $this->error(ErrorCode::MEMBER_NOT_EXIST);
  288. // UserCareDream::where('user_id',$user->id)->where('dream_user_id',$other_id)->update(['interaction_number'=>0]);
  289. // $data = UserInfoModel::where('id',$other_id)->with(['allInteraction'=>function ($query){
  290. // $query->orderBy('id','desc');
  291. // }])->first();
  292. //// $data = UserInfoModel::find($other_id)->allInteraction;
  293. //// dd($data) ;
  294. //// dd($data->allInteraction);
  295. // foreach ($data->allInteraction as $item) {
  296. // $item->time = DreamInfoModel::find($item->dream_id)->time;
  297. // $item->comments = $item->comments;
  298. // foreach ($item->comments as $k => $v) {
  299. // $v->pic = UserInfoModel::find($v->user_id)->pic;
  300. // $v->replay = $v->replay;
  301. // foreach ($v->replay as $k1 => $v1) {
  302. // $v1->pic = UserInfoModel::find($v1->user_id)->pic;
  303. // }
  304. // }
  305. // }
  306. // return $this->api($data);
  307. // }
  308. public function insertSearchTable($id,$keyword)
  309. {
  310. $info = SearchInfoModel::where('user_id',$id)->
  311. where('search',trim($keyword))->first();
  312. if (count($info) == 0) {
  313. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  314. }else{
  315. $info->times += 1;
  316. $info->save();
  317. }
  318. }
  319. /**
  320. * @api {get} /api/index/filter 筛选
  321. * @apiDescription 筛选
  322. * @apiGroup Index
  323. * @apiPermission none
  324. * @apiVersion 0.1.0
  325. * @apiParam {string} [sex] 性别 //1男,2女,0:全部,
  326. * @apiParam {string} [age] 年龄段 0:全部,1:19-25,2:26-35,3:>36,7:<18, 接收1,2,3,4...
  327. * @apiParam {string} [area] 地区 例:成都
  328. * @apiSuccessExample {json} Success-Response:
  329. * HTTP/1.1 200 OK
  330. *{
  331. * "status": true,
  332. * "status_code": 0,
  333. * "message": "",
  334. * "data":[
  335. * {
  336. * "nickname": "ha", 昵称
  337. * "pic": "", 头像
  338. * ...
  339. * },
  340. * ]
  341. *}
  342. * @apiErrorExample {json} Error-Response:
  343. * HTTP/1.1 400 Bad Request
  344. */
  345. public function filter(Request $request)
  346. {
  347. $sex = $request->sex;
  348. $age = $request->age;
  349. $area = $request->area;
  350. $login_user = $this->getUser();
  351. $id = $login_user->id;
  352. $query = new DreamInfoModel();
  353. if (!empty($age)) {
  354. $age_arr = explode(',',$age); // 被选中的年龄数组
  355. foreach ($age_arr as $value) {
  356. switch ($value) {
  357. case 1:
  358. $query = $query->orWhereHas('user', function ($select) use ($age) {
  359. $select->whereYear('birthday','>=',(date('Y')-25))->whereYear('birthday','<=',(date('Y')-19));
  360. });
  361. break;
  362. case 2:
  363. $query = $query->orWhereHas('user', function ($select) use ($age) {
  364. $select->whereYear('birthday','>=',(date('Y')-26))->whereYear('birthday','<=',(date('Y')-35));
  365. });
  366. break;
  367. // case 3:
  368. // $query = $query->orWhereHas('user', function ($select) use ($age) {
  369. // $select->whereYear('birthday','>=',(date('Y')-29))->whereYear('birthday','<=',(date('Y')-26));
  370. // });
  371. // break;
  372. // case 4:
  373. // $query = $query->orWhereHas('user', function ($select) use ($age) {
  374. // $select->whereYear('birthday','>=',(date('Y')-33))->whereYear('birthday','<=',(date('Y')-30));
  375. // });
  376. // break;
  377. // case 5:
  378. // $query = $query->orWhereHas('user', function ($select) use ($age) {
  379. // $select->whereYear('birthday','>=',(date('Y')-37))->whereYear('birthday','<=',(date('Y')-34));
  380. // });
  381. // break;
  382. case 3:
  383. $query = $query->orWhereHas('user', function ($select) use ($age) {
  384. $select->whereYear('birthday','<=',(date('Y')-36));
  385. });
  386. break;
  387. case 7:
  388. $query = $query->orWhereHas('user', function ($select) use ($age) {
  389. $select->whereYear('birthday','>=',(date('Y')-18));
  390. });
  391. break;
  392. }
  393. }
  394. if (($sex) == 1) {
  395. $query = $query->whereHas('user', function ($select) use ($sex) {
  396. $select->where('sex',0);
  397. });
  398. }
  399. if (($sex) == 2) {
  400. $query = $query->whereHas('user', function ($select) use ($sex) {
  401. $select->where('sex',1);
  402. });
  403. }
  404. }
  405. /* if ($age == 7) {
  406. $query = $query->whereHas('user', function ($select) use ($age) {
  407. $select->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-18)));
  408. });
  409. }
  410. if ($age == 1) {
  411. $query = $query->whereHas('user', function ($select) use ($age) {
  412. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-21)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-18)));
  413. });
  414. }
  415. if ($age == 2) {
  416. $query = $query->whereHas('user', function ($select) use ($age) {
  417. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-25)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-22)));
  418. });
  419. }
  420. if ($age == 3) {
  421. $query = $query->whereHas('user', function ($select) use ($age) {
  422. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-29)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-26)));
  423. });
  424. }
  425. if ($age == 4) {
  426. $query = $query->whereHas('user', function ($select) use ($age) {
  427. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-33)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-30)));
  428. });
  429. }
  430. if ($age == 5) {
  431. $query = $query->whereHas('user', function ($select) use ($age) {
  432. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-37)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-34)));
  433. });
  434. }
  435. if ($age == 6) {
  436. $query = $query->whereHas('user', function ($select) use ($age) {
  437. $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-38)));
  438. });
  439. }*/
  440. if (!empty($area)) {
  441. $query = $query->whereHas('user', function ($select) use ($area) {
  442. $select->where('city','like','%'.$area.'%');
  443. });
  444. }
  445. \Log::info($query->toSql());
  446. $arr1 =DreamInfoModel::orderBy('id')->limit(20)->select('id')->get()->toArray();
  447. $id_arr1 = array_column($arr1,'id');
  448. $arr2 =DreamInfoModel::orderBy('id','desc')->limit(120)->select('id')->get()->toArray();
  449. $id_arr2 = array_column($arr2,'id');
  450. $dtusers =UserCareUser::where('user_id',$id)->with('other_user')->
  451. where('dream_number','>',0)->orderBy('created_at')->get()->toArray();
  452. $hdusers = CommentInfoModel::where(function ($query) use ($id) {
  453. $query->where('user_id',$id)->orWhere('to_user_id',$id);
  454. })->where('is_read',0)->with('to_user')->orderBy('created_at')->get()->toArray();
  455. $users = [] ;
  456. foreach ($dtusers as $k => $v){
  457. $users[] = $v['other_user'];
  458. }
  459. foreach ($hdusers as $k => $v){
  460. $users[] = $v['to_user'];
  461. }
  462. $type = $request->type;
  463. if ($type == 'trend') {
  464. $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);
  465. $this->dreams($dreams);
  466. return $this->api(compact('users','dreams'));
  467. } elseif ($type == 'news') {
  468. $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);
  469. $this->dreams($dreams);
  470. return $this->api(compact('users','dreams'));
  471. } else{
  472. $banners = $this->getBanner();
  473. $dreams = $query->where('end_time','<',date('Y-m-d H:i:s'))->orderBy('score','desc')->with('user')->limit(20)->paginate(20);
  474. $this->dreams($dreams);
  475. return $this->api(compact('banners','users','dreams'));
  476. }
  477. }
  478. }