IndexController.php 11 KB

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