IndexController.php 9.9 KB

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