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