IndexController.php 10 KB

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