IndexController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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\UserCareDream;
  8. use App\Models\UserCareUser;
  9. use App\Models\UserInfoModel;
  10. use Illuminate\Http\Request;
  11. class IndexController extends Controller
  12. {
  13. /**
  14. * @api {get} /api/index/home 首页
  15. * @apiDescription 首页
  16. * @apiGroup Index
  17. * @apiParam {string} [type=hot] (热门)hot/(潮流)trend/(最新)news
  18. * @apiParam {int} [page=1] 页码(分页参数)
  19. * @apiPermission none
  20. * @apiVersion 0.1.0
  21. * @apiSuccessExample {json} Success-Response:
  22. * HTTP/1.1 200 OK
  23. *{
  24. * "status": true,
  25. * "status_code": 0,
  26. * "message": "",
  27. * "data": {
  28. * "banners": [
  29. * {
  30. * "id": 32,
  31. * "key": "2",
  32. * "value": "http://w17.9026.com/img/banner/banner_2.png",
  33. * "sort": 2,
  34. * "category": "banner",
  35. * "pid": 0,
  36. * "status": 1,
  37. * "created_at": null,
  38. * "updated_at": "2017-06-26 09:21:36",
  39. * "deleted_at": null
  40. * }
  41. * ],
  42. * "dtusers": [ 动态用户
  43. * {
  44. * "id": 3,
  45. * "user_id": 1,
  46. * "other_user_id": 2,
  47. * "dream_number": 1,
  48. * "created_at": null,
  49. * "updated_at": null,
  50. * "other_user": { 动态用户 头像和id
  51. * "id": 2,
  52. * "avatar": "http://www.miao.com/upload/user/20170630/08e235d7211944e9b6482965b4d7c42e.jpg",
  53. * }
  54. * }
  55. * ],
  56. * "hdusers": [], 互动用户
  57. * "dreams": {
  58. * "current_page": 1,
  59. * "data": [
  60. * {
  61. * "id": 5,
  62. * "user_id": 1,
  63. * "name": "梦想标题1",
  64. * "about": "梦想介绍",
  65. * "coin": 2500,
  66. * "end_time": "2017-05-11 17:00:00",
  67. * "get_coin": 0,
  68. * "mark": 0,
  69. * "status": 2,
  70. * "video": "url",
  71. * "score": 100079365,
  72. * "sign": "梦想达人",
  73. * "code": "",
  74. * "parameter": 100,
  75. * "created_at": "2017-06-25 12:45:22",
  76. * "updated_at": "2017-07-03 12:23:37",
  77. * "care_num": 1,
  78. * "img": {
  79. * "title": "",
  80. * "pic": "http://www.miao.com/upload/dream/20170629/a18cadd1560f21453a08fdd81fb8fb2d.jpg"
  81. * },
  82. * "user": {
  83. * "id": 1,
  84. * "avatar": "/upload/user/20170629/e77068cc14c82086fb6b16e5bb7c3dd1.jpg",
  85. * }
  86. * },
  87. * }
  88. * ],
  89. * "from": 1,
  90. * "last_page": 1,
  91. * "next_page_url": null,
  92. * "path": "http://www.miao.com/api/index/home",
  93. * "per_page": 20,
  94. * "prev_page_url": null,
  95. * "to": 11,
  96. * "total": 11
  97. * }
  98. * }
  99. *}
  100. * @apiErrorExample {json} Error-Response:
  101. * HTTP/1.1 400 Bad Request
  102. * {
  103. * "state": false,
  104. * "code": 1000,
  105. * "message": "传入参数不正确",
  106. * "data": null or []
  107. * }
  108. */
  109. public function home(Request $request)
  110. {
  111. $login_user = $this->getUser();
  112. $id = $login_user->id;
  113. $arr1 =DreamInfoModel::orderBy('score','desc')->limit(20)->select('id')->get()->toArray();
  114. $id_arr1 = array_column($arr1,'id');
  115. $arr2 =DreamInfoModel::orderBy('score','desc')->limit(120)->select('id')->get()->toArray();
  116. $id_arr2 = array_column($arr2,'id');
  117. $dtusers =UserCareUser::where('user_id',$id)->with('other_user')->
  118. where('dream_number','>',0)->orderBy('created_at')->get()->toArray();
  119. $hdusers = CommentInfoModel::where(function ($query) use ($id) {
  120. $query->where('user_id',$id)->orWhere('to_user_id',$id);
  121. })->where('is_read',0)->with('to_user')->orderBy('created_at')->get()->toArray();
  122. $type = $request->type;
  123. if ($type == 'trend') {
  124. $dreams = DreamInfoModel::orderBy('score','desc')->with('user')->whereNotIn('id', $id_arr1)->limit(100)->paginate(20);
  125. $this->dreams($dreams);
  126. return $this->api(compact('dtusers','hdusers','dreams'));
  127. } elseif ($type == 'news') {
  128. $dreams = DreamInfoModel::orderBy('score','desc')->orderBy('created_at','desc')->with('user')->whereNotIn('id', $id_arr2)->limit(500)->paginate(20);
  129. $this->dreams($dreams);
  130. return $this->api(compact('dtusers','hdusers','dreams'));
  131. } else{
  132. $banners = $this->getBanner();
  133. $dreams = DreamInfoModel::orderBy('score','desc')->with('user')->limit(20)->paginate(20);
  134. $this->dreams($dreams);
  135. return $this->api(compact('banners','dtusers','hdusers','dreams'));
  136. }
  137. }
  138. /**
  139. * @api {get} /api/index/search 搜索
  140. * @apiDescription 搜索
  141. * @apiGroup Index
  142. * @apiPermission none
  143. * @apiVersion 0.1.0
  144. * @apiParam {string} [keyword] 关键字可选 (ha/name)
  145. * @apiSuccessExample {json} Success-Response:
  146. * HTTP/1.1 200 OK
  147. * get
  148. *{
  149. * "status": true,
  150. * "status_code": 0,
  151. * "message": "",
  152. * "data": {
  153. * "hot_searches": {
  154. * "name": 1,
  155. * "梦想": 1,
  156. * "ha": 1
  157. * },
  158. * "history_searches": [
  159. * {
  160. * "id": 3,
  161. * "user_id": 1,
  162. * "search": "ha",
  163. * "times": 1,
  164. * "created_at": "2017-06-25 16:21:47",
  165. * "updated_at": "2017-06-25 16:21:47"
  166. * },
  167. * ]
  168. * }
  169. *}
  170. * @apiErrorExample {json} Error-Response:
  171. * HTTP/1.1 400 Bad Request
  172. */
  173. public function search(Request $request)
  174. {
  175. $user = $this->getUser();
  176. $keyword ='%'.$request->keyword.'%';
  177. $user_infos = UserInfoModel::where('nickname','like',$keyword)->get();
  178. $dream_infos = DreamInfoModel::where(function ($query) use($keyword) {
  179. $query->where('name','like',$keyword)
  180. ->where('end_time','>=',time());
  181. })
  182. ->orWhere(function ($query) use($keyword){
  183. $query->where('sign','like',$keyword)
  184. ->where('end_time','>=',time());
  185. })->with(['user','img'])->get();
  186. $this->dreams($dream_infos);
  187. $signs = BaseSettingsModel::where('category','sign')->where('value','like',$keyword)->get();
  188. if (empty($request->keyword)) {
  189. // 历史搜索
  190. $history_searches = $user->search()->orderBy('id','desc')->limit(10)->get();
  191. // 热门搜索
  192. $data2 = SearchInfoModel::get();
  193. $hot_searches = [];
  194. foreach ($data2 as $k => $v) {
  195. if (count($hot_searches) == 6) {
  196. break;
  197. }
  198. if (!array_key_exists($v->search,$hot_searches)) {
  199. $hot_searches[$v->search] = $v->times;
  200. }else{
  201. $hot_searches[$v->search] += $v->times;
  202. }
  203. }
  204. arsort($hot_searches);
  205. return $this->api(compact('hot_searches','history_searches'));
  206. }
  207. // 写入搜索记录
  208. $this->insertSearchTable($user->id,$request->keyword);
  209. return $this->api(compact('user_infos','dream_infos','signs'));
  210. }
  211. /**
  212. * @api {get} /api/index/user_search 用户搜索
  213. * @apiDescription 用户搜索
  214. * @apiGroup Index
  215. * @apiPermission none
  216. * @apiVersion 0.1.0
  217. * @apiParam {string} keyword 关键字
  218. * @apiSuccessExample {json} Success-Response:
  219. * HTTP/1.1 200 OK
  220. *{
  221. * "status": true,
  222. * "status_code": 0,
  223. * "message": "",
  224. * "data":[
  225. * {
  226. * "nickname": "ha", 昵称
  227. * "pic": "", 头像
  228. * ...
  229. * },
  230. * ]
  231. *}
  232. * @apiErrorExample {json} Error-Response:
  233. * HTTP/1.1 400 Bad Request
  234. */
  235. public function userSearch(Request $request)
  236. {
  237. $user = $this->getUser();
  238. if (empty($request->keyword)) {
  239. return $this->api('');
  240. }
  241. $keyword ='%'.$request->keyword.'%';
  242. $users = UserInfoModel::where('nickname','like',$keyword)->get();
  243. $this->insertSearchTable($user->id,$request->keyword);
  244. return $this->api($users);
  245. }
  246. //获取轮播图
  247. public function getBanner()
  248. {
  249. $banner = BaseSettingsModel::where(['category' => 'banner'])->where(['status' => '1'])
  250. ->orderBy('sort')->get()->toArray();
  251. return $banner;
  252. }
  253. // 完善梦想信息
  254. public function dreams($dreams)
  255. {
  256. foreach ($dreams as $k => $dream) {
  257. $data = UserCareDream::where('dream_id',$dream->id)->get();
  258. $dream->care_num = count($data);
  259. $dream->img = $dream->img?$dream->img->pic:'';
  260. }
  261. }
  262. // 查看关注用户的最新动态
  263. // public function newsInfo(Request $request)
  264. // {
  265. //// 查看后user_care_user dream_info 更新为零 首页不再显示
  266. // $user = $this->getUser();
  267. // $other_id = $request->id;
  268. // if (empty($other_id)) return $this->error(ErrorCode::MEMBER_NOT_EXIST);
  269. // UserCareDream::where('user_id',$user->id)->where('dream_user_id',$other_id)->update(['interaction_number'=>0]);
  270. // $data = UserInfoModel::where('id',$other_id)->with(['allInteraction'=>function ($query){
  271. // $query->orderBy('id','desc');
  272. // }])->first();
  273. //// $data = UserInfoModel::find($other_id)->allInteraction;
  274. //// dd($data) ;
  275. //// dd($data->allInteraction);
  276. // foreach ($data->allInteraction as $item) {
  277. // $item->time = DreamInfoModel::find($item->dream_id)->time;
  278. // $item->comments = $item->comments;
  279. // foreach ($item->comments as $k => $v) {
  280. // $v->pic = UserInfoModel::find($v->user_id)->pic;
  281. // $v->replay = $v->replay;
  282. // foreach ($v->replay as $k1 => $v1) {
  283. // $v1->pic = UserInfoModel::find($v1->user_id)->pic;
  284. // }
  285. // }
  286. // }
  287. // return $this->api($data);
  288. // }
  289. public function insertSearchTable($id,$keyword)
  290. {
  291. $info = SearchInfoModel::where('user_id',$id)->
  292. where('search',trim($keyword))->first();
  293. if (count($info) == 0) {
  294. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  295. }else{
  296. $info->times += 1;
  297. $info->save();
  298. }
  299. }
  300. }