IndexController.php 10 KB

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