IndexController.php 20 KB

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