IndexController.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. $this->dreams($dream_infos);
  157. $signs = BaseSettingsModel::where('category','sign')->where('value','like',$keyword)->get();
  158. if (empty($request->keyword)) {
  159. // 历史搜索
  160. $history_searches = $user->search()->orderBy('id','desc')->limit(10)->get();
  161. // 热门搜索
  162. $data2 = SearchInfoModel::get();
  163. $hot_searches = [];
  164. foreach ($data2 as $k => $v) {
  165. if (count($hot_searches) == 6) {
  166. break;
  167. }
  168. if (!array_key_exists($v->search,$hot_searches)) {
  169. $hot_searches[$v->search] = $v->times;
  170. }else{
  171. $hot_searches[$v->search] += $v->times;
  172. }
  173. }
  174. arsort($hot_searches);
  175. return $this->api(compact('hot_searches','history_searches'));
  176. }
  177. // 写入搜索记录
  178. $this->insertSearchTable($user->id,$request->keyword);
  179. return $this->api(compact('user_infos','dream_infos','signs'));
  180. }
  181. /**
  182. * @api {get} /api/index/user_search 用户搜索
  183. * @apiDescription 用户搜索
  184. * @apiGroup Index
  185. * @apiPermission none
  186. * @apiVersion 0.1.0
  187. * @apiParam {string} keyword 关键字
  188. * @apiSuccessExample {json} Success-Response:
  189. * HTTP/1.1 200 OK
  190. *{
  191. * "status": true,
  192. * "status_code": 0,
  193. * "message": "",
  194. * "data":[
  195. * {
  196. * "nickname": "ha", 昵称
  197. * "pic": "", 头像
  198. * ...
  199. * },
  200. * ]
  201. *}
  202. * @apiErrorExample {json} Error-Response:
  203. * HTTP/1.1 400 Bad Request
  204. */
  205. public function userSearch(Request $request)
  206. {
  207. $user = $this->getUser();
  208. if (empty($request->keyword)) {
  209. return $this->api('');
  210. }
  211. $keyword ='%'.$request->keyword.'%';
  212. $users = UserInfoModel::where('nickname','like',$keyword)->get();
  213. $this->insertSearchTable($user->id,$request->keyword);
  214. return $this->api($users);
  215. }
  216. //获取轮播图
  217. public function getBanner()
  218. {
  219. $banner = BaseSettingsModel::where(['category' => 'banner'])->where(['status' => '1'])
  220. ->orderBy('sort')->get()->toArray();
  221. return $banner;
  222. }
  223. // 完善梦想信息
  224. public function dreams($dreams)
  225. {
  226. foreach ($dreams as $k => $dream) {
  227. $data = UserCareDream::where('dream_id',$dream->id)->get();
  228. $dream->care_num = count($data);
  229. $dream->img = $dream->img?$dream->img->pic:'';
  230. }
  231. }
  232. // 查看关注用户的最新动态
  233. // public function newsInfo(Request $request)
  234. // {
  235. //// 查看后user_care_user dream_info 更新为零 首页不再显示
  236. // $user = $this->getUser();
  237. // $other_id = $request->id;
  238. // if (empty($other_id)) return $this->error(ErrorCode::MEMBER_NOT_EXIST);
  239. // UserCareDream::where('user_id',$user->id)->where('dream_user_id',$other_id)->update(['interaction_number'=>0]);
  240. // $data = UserInfoModel::where('id',$other_id)->with(['allInteraction'=>function ($query){
  241. // $query->orderBy('id','desc');
  242. // }])->first();
  243. //// $data = UserInfoModel::find($other_id)->allInteraction;
  244. //// dd($data) ;
  245. //// dd($data->allInteraction);
  246. // foreach ($data->allInteraction as $item) {
  247. // $item->time = DreamInfoModel::find($item->dream_id)->time;
  248. // $item->comments = $item->comments;
  249. // foreach ($item->comments as $k => $v) {
  250. // $v->pic = UserInfoModel::find($v->user_id)->pic;
  251. // $v->replay = $v->replay;
  252. // foreach ($v->replay as $k1 => $v1) {
  253. // $v1->pic = UserInfoModel::find($v1->user_id)->pic;
  254. // }
  255. // }
  256. // }
  257. // return $this->api($data);
  258. // }
  259. public function insertSearchTable($id,$keyword)
  260. {
  261. $info = SearchInfoModel::where('user_id',$id)->
  262. where('search',trim($keyword))->first();
  263. if (count($info) == 0) {
  264. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  265. }else{
  266. $info->times += 1;
  267. $info->save();
  268. }
  269. }
  270. }