IndexController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/(潮流)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. $users = [];
  88. $type = $request->type;
  89. if ($type == 'trend') {
  90. $dreams = DreamInfoModel::orderBy('score','desc')->with('user')->offset(20)->limit(100)->paginate(20);
  91. $this->dreams($dreams);
  92. return $this->api(compact('users','dreams'));
  93. } elseif ($type == 'news') {
  94. $dreams = DreamInfoModel::orderBy('score','desc')->with('user')->offset(100)->limit(500)->paginate(20);
  95. $this->dreams($dreams);
  96. return $this->api(compact('users','dreams'));
  97. } else{
  98. $banners = $this->getBanner();
  99. $dreams = DreamInfoModel::orderBy('score','desc')->with('user')->limit(20)->paginate(20);
  100. $this->dreams($dreams);
  101. return $this->api(compact('banners','users','dreams'));
  102. }
  103. }
  104. /**
  105. * @api {get} /api/index/search 搜索
  106. * @apiDescription 搜索
  107. * @apiGroup Index
  108. * @apiPermission none
  109. * @apiVersion 0.1.0
  110. * @apiParam {string} keyword 关键字可选 (ha/name)
  111. * @apiSuccessExample {json} Success-Response:
  112. * HTTP/1.1 200 OK
  113. * get
  114. *{
  115. * "status": true,
  116. * "status_code": 0,
  117. * "message": "",
  118. * "data": {
  119. * "hot_searches": {
  120. * "name": 1,
  121. * "梦想": 1,
  122. * "ha": 1
  123. * },
  124. * "history_searches": [
  125. * {
  126. * "id": 3,
  127. * "user_id": 1,
  128. * "search": "ha",
  129. * "times": 1,
  130. * "created_at": "2017-06-25 16:21:47",
  131. * "updated_at": "2017-06-25 16:21:47"
  132. * },
  133. * ]
  134. * }
  135. *}
  136. * @apiErrorExample {json} Error-Response:
  137. * HTTP/1.1 400 Bad Request
  138. */
  139. public function search(Request $request)
  140. {
  141. $user = $this->getUser();
  142. $keyword ='%'.$request->keyword.'%';
  143. $user_infos = UserInfoModel::where('nickname','like',$keyword)->get();
  144. $dream_infos = DreamInfoModel::where(function ($query) use($keyword) {
  145. $query->where('name','like',$keyword)
  146. ->where('end_time','<=',time());
  147. })
  148. ->orWhere(function ($query) use($keyword){
  149. $query->where('sign','like',$keyword)
  150. ->where('end_time','<=',time());
  151. })->with(['user','img'])->get();
  152. $signs = BaseSettingsModel::where('category','sign')->where('value','like',$keyword)->get();
  153. if (empty($request->keyword)) {
  154. // 历史搜索
  155. $history_searches = $user->search()->orderBy('id','desc')->limit(10)->get();
  156. // 热门搜索
  157. $data2 = SearchInfoModel::get();
  158. $hot_searches = [];
  159. foreach ($data2 as $k => $v) {
  160. if (count($hot_searches) == 6) {
  161. break;
  162. }
  163. if (!array_key_exists($v->search,$hot_searches)) {
  164. $hot_searches[$v->search] = $v->times;
  165. }else{
  166. $hot_searches[$v->search] += $v->times;
  167. }
  168. }
  169. arsort($hot_searches);
  170. return $this->api(compact('hot_searches','history_searches'));
  171. }
  172. // 写入搜索记录
  173. $this->insertSearchTable($user->id,$request->keyword);
  174. return $this->api(compact('user_infos','dream_infos','signs'));
  175. }
  176. /**
  177. * @api {get} /api/index/user_search 用户搜索
  178. * @apiDescription 用户搜索
  179. * @apiGroup Index
  180. * @apiPermission none
  181. * @apiVersion 0.1.0
  182. * @apiParam {string} keyword 关键字
  183. * @apiSuccessExample {json} Success-Response:
  184. * HTTP/1.1 200 OK
  185. *{
  186. * "status": true,
  187. * "status_code": 0,
  188. * "message": "",
  189. * "data":[
  190. * {
  191. * "nickname": "ha", 昵称
  192. * "pic": "", 头像
  193. * ...
  194. * },
  195. * ]
  196. *}
  197. * @apiErrorExample {json} Error-Response:
  198. * HTTP/1.1 400 Bad Request
  199. */
  200. public function userSearch(Request $request)
  201. {
  202. $user = $this->getUser();
  203. if (empty($request->keyword)) {
  204. return $this->api('');
  205. }
  206. $keyword ='%'.$request->keyword.'%';
  207. $users = UserInfoModel::where('nickname','like',$keyword)->get();
  208. $this->insertSearchTable($user->id,$request->keyword);
  209. return $this->api($users);
  210. }
  211. //获取轮播图
  212. public function getBanner()
  213. {
  214. $banner = BaseSettingsModel::where(['category' => 'banner'])->where(['status' => '1'])
  215. ->orderBy('sort')->get()->toArray();
  216. return $banner;
  217. }
  218. // 完善梦想信息
  219. public function dreams($dreams)
  220. {
  221. foreach ($dreams as $k => $dream) {
  222. $data = UserCareDream::where('dream_id',$dream->id)->get();
  223. $dream->care_num = count($data);
  224. $dream->img = $dream->img?$dream->img->pic:'';
  225. }
  226. }
  227. // 查看关注用户的最新动态
  228. // public function newsInfo(Request $request)
  229. // {
  230. //// 查看后user_care_user dream_info 更新为零 首页不再显示
  231. // $user = $this->getUser();
  232. // $other_id = $request->id;
  233. // if (empty($other_id)) return $this->error(ErrorCode::MEMBER_NOT_EXIST);
  234. // UserCareDream::where('user_id',$user->id)->where('dream_user_id',$other_id)->update(['interaction_number'=>0]);
  235. // $data = UserInfoModel::where('id',$other_id)->with(['allInteraction'=>function ($query){
  236. // $query->orderBy('id','desc');
  237. // }])->first();
  238. //// $data = UserInfoModel::find($other_id)->allInteraction;
  239. //// dd($data) ;
  240. //// dd($data->allInteraction);
  241. // foreach ($data->allInteraction as $item) {
  242. // $item->time = DreamInfoModel::find($item->dream_id)->time;
  243. // $item->comments = $item->comments;
  244. // foreach ($item->comments as $k => $v) {
  245. // $v->pic = UserInfoModel::find($v->user_id)->pic;
  246. // $v->replay = $v->replay;
  247. // foreach ($v->replay as $k1 => $v1) {
  248. // $v1->pic = UserInfoModel::find($v1->user_id)->pic;
  249. // }
  250. // }
  251. // }
  252. // return $this->api($data);
  253. // }
  254. public function insertSearchTable($id,$keyword)
  255. {
  256. $info = SearchInfoModel::where('user_id',$id)->
  257. where('search',trim($keyword))->first();
  258. if (count($info) == 0) {
  259. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  260. }else{
  261. $info->times += 1;
  262. $info->save();
  263. }
  264. }
  265. }