IndexController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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\UserCareUser;
  7. use App\Models\UserDream;
  8. use App\Models\UserInfoModel;
  9. use Illuminate\Http\Request;
  10. use App\Services\Base\ErrorCode;
  11. class IndexController extends Controller
  12. {
  13. /**
  14. * @api {get} /api/index/hot 热门(hot)
  15. * @apiDescription 热门(hot)
  16. * @apiGroup Index
  17. * @apiPermission none
  18. * @apiVersion 0.1.0
  19. * @apiParam {int} [page=1] 页码(分页参数)
  20. * @apiSuccessExample {json} Success-Response:
  21. * HTTP/1.1 200 OK
  22. *{
  23. *"status": true,
  24. *"status_code": 0,
  25. *"message": "",
  26. *"data": {
  27. * "banner": [], 轮播图
  28. * "users": [ 动态用户
  29. * 'news_num':2 新消息数目
  30. * ],
  31. * "dreams": [
  32. * "care_num": 0, 关注人数
  33. * "time": 1222222, 梦想倒计时
  34. * "dream_imgs_first": {
  35. * "pic": "", 梦想封面图片
  36. * }
  37. * "dream_find_user": [
  38. * {
  39. * ... 发布梦想的用户信息
  40. * }
  41. * ],
  42. * ]
  43. * }
  44. * }
  45. * @apiErrorExample {json} Error-Response:
  46. * HTTP/1.1 400 Bad Request
  47. * {
  48. * "state": false,
  49. * "code": 1000,
  50. * "message": "传入参数不正确",
  51. * "data": null or []
  52. * }
  53. */
  54. public function hot(Request $request)
  55. {
  56. $user = $this->getUser();
  57. //获取轮播图
  58. $banner = $this->getBanner();
  59. // 关注的用户
  60. $arr = $user->UserCareUser;
  61. $users = [] ;
  62. foreach ($arr as $k => $v){
  63. if ($v->pivot->dream_num > 0) {
  64. $v->news_num = $v->pivot->dream_num;
  65. $users[] = $v;
  66. }
  67. }
  68. // 获取其他用户信息 及梦想
  69. $dreams = DreamInfoModel::orderBy('score','desc')->limit(20)->paginate(20);
  70. $this->dreams($dreams);
  71. return $this->api(compact('banner','users','dreams'));
  72. }
  73. /**
  74. * @api {get} /api/index/trend 潮流(trend)
  75. * @apiDescription 潮流(trend)
  76. * @apiGroup Index
  77. * @apiPermission none
  78. * @apiVersion 0.1.0
  79. * @apiSuccessExample {json} Success-Response:
  80. * HTTP/1.1 200 OK
  81. *{
  82. *"status": true,
  83. *"status_code": 0,
  84. *"message": "",
  85. *"data": {
  86. * "users": [ 动态用户
  87. * 'news_num':2 新消息数目
  88. * ],
  89. * "dreams": [
  90. * "care_num": 0, 关注人数
  91. * "time": 1222222, 梦想倒计时
  92. * "dream_imgs_first": {
  93. * "pic": "", 梦想封面图片
  94. * }
  95. * "dream_find_user": [
  96. * {
  97. * ... 发布梦想的用户信息
  98. * }
  99. * ],
  100. * ]
  101. * }
  102. * }
  103. * @apiErrorExample {json} Error-Response:
  104. * HTTP/1.1 400 Bad Request
  105. * {
  106. * "state": false,
  107. * "code": 1000,
  108. * "message": "传入参数不正确",
  109. * "data": null or []
  110. * }
  111. */
  112. public function trend(Request $request)
  113. {
  114. $user = $this->getUser();
  115. // 关注的用户
  116. $arr = $user->UserCareUser;
  117. $users = [] ;
  118. foreach ($arr as $k => $v){
  119. if ($v->pivot->dream_num > 0) {
  120. $v->news_num = $v->pivot->dream_num;
  121. $users[] = $v;
  122. }
  123. }
  124. // 获取其他用户信息 及梦想
  125. $dreams = DreamInfoModel::orderBy('score','desc')->offset(20)->limit(100)->paginate(20);
  126. $this->dreams($dreams);
  127. return $this->api(compact('users','dreams'));
  128. }
  129. /**
  130. * @api {get} /api/index/new 最新(news)
  131. * @apiDescription 最新(news)
  132. * @apiGroup Index
  133. * @apiPermission none
  134. * @apiVersion 0.1.0
  135. * @apiSuccessExample {json} Success-Response:
  136. * HTTP/1.1 200 OK
  137. *{
  138. *"status": true,
  139. *"status_code": 0,
  140. *"message": "",
  141. *"data": {
  142. * "users": [
  143. * 'news_num':2 新消息数目
  144. * ], 动态用户
  145. * "dreams": [
  146. * "care_num": 0, 关注人数
  147. * "time": 1222222, 梦想倒计时
  148. * "dream_imgs_first": {
  149. * "pic": "", 梦想封面图片
  150. * }
  151. * "dream_find_user": [
  152. * {
  153. * ... 发布梦想的用户信息
  154. * }
  155. * ],
  156. * ]
  157. * }
  158. * }
  159. * @apiErrorExample {json} Error-Response:
  160. * HTTP/1.1 400 Bad Request
  161. * {
  162. * "state": false,
  163. * "code": 1000,
  164. * "message": "传入参数不正确",
  165. * "data": null or []
  166. * }
  167. */
  168. public function news(Request $request)
  169. {
  170. $user = $this->getUser();
  171. // 关注的用户
  172. $arr = $user->UserCareUser;
  173. $users = [] ;
  174. foreach ($arr as $k => $v){
  175. if ($v->pivot->dream_num > 0) {
  176. $v->news_num = $v->pivot->dream_num;
  177. $users[] = $v;
  178. }
  179. }
  180. // 获取其他用户信息 及梦想
  181. $dreams = DreamInfoModel::orderBy('score','desc')->offset(100)->limit(500)->paginate(20);
  182. $this->dreams($dreams);
  183. return $this->api(compact('$users','dreams'));
  184. }
  185. /**
  186. * @api {get} /api/index/search 搜索(search)
  187. * @apiDescription 搜索(search)
  188. * @apiGroup Index
  189. * @apiPermission none
  190. * @apiVersion 0.1.0
  191. * @apiParam {string} keyword 关键字可选
  192. * @apiSuccessExample {json} Success-Response:
  193. * HTTP/1.1 200 OK
  194. * get
  195. *{
  196. * "status": true,
  197. * "status_code": 0,
  198. * "message": "",
  199. * "data": {
  200. * "arr": { // 热门搜索
  201. * ...
  202. * },
  203. * "data1": [
  204. * {
  205. * "search": "ha", // 历史搜索
  206. * }
  207. * ]
  208. * }
  209. *}
  210. * post
  211. * {
  212. * "status": true,
  213. * "status_code": 0,
  214. * "message": "",
  215. * "data": {
  216. * "data1": [
  217. * ... //用户
  218. * ],
  219. * "data2": [
  220. * ... //梦想
  221. * ],
  222. * "data3": [
  223. * ... //标签
  224. * ]
  225. * }
  226. *}
  227. * @apiErrorExample {json} Error-Response:
  228. * HTTP/1.1 400 Bad Request
  229. */
  230. public function search(Request $request)
  231. {
  232. $user = $this->getUser();
  233. $keyword ='%'.$request->keyword.'%';
  234. $data1 = UserInfoModel::where('nickname','like',$keyword)->paginate(20);
  235. $data2 = DreamInfoModel::where('dream','like',$keyword)->
  236. orWhere('dream','like',$keyword)->paginate(20);
  237. $data3 = BaseSettingsModel::where('category','sign')->where('value','like',$keyword)->paginate(20);
  238. if (empty($request->keyword)) {
  239. // 历史搜索
  240. $data1 = $user->search()->orderBy('id','desc')->limit(10)->paginate(20);
  241. // 热门搜索
  242. $data2 = SearchInfoModel::paginate(20);
  243. $arr = [];
  244. foreach ($data2 as $k => $v) {
  245. if (count($arr) == 8) {
  246. break;
  247. }
  248. if (!array_key_exists($v->search,$arr)) {
  249. $arr[$v->search] = $v->times;
  250. }else{
  251. $arr[$v->search] += $v->times;
  252. }
  253. }
  254. arsort($arr);
  255. return $this->api(compact('arr','data1'));
  256. }
  257. return $this->api(compact('data1','data2','data3'));
  258. }
  259. /**
  260. * @api {get} /api/index/user_search 用户搜索(search)
  261. * @apiDescription 用户搜索(search)
  262. * @apiGroup Index
  263. * @apiPermission none
  264. * @apiVersion 0.1.0
  265. * @apiParam {string} keyword 关键字
  266. * @apiSuccessExample {json} Success-Response:
  267. * HTTP/1.1 200 OK
  268. *{
  269. * "status": true,
  270. * "status_code": 0,
  271. * "message": "",
  272. * "data": {
  273. * "data1": [
  274. * "nickname": "ha", 昵称
  275. * "pic": "", 头像
  276. * ...
  277. * ]
  278. * }
  279. *}
  280. * @apiErrorExample {json} Error-Response:
  281. * HTTP/1.1 400 Bad Request
  282. */
  283. public function userSearch(Request $request)
  284. {
  285. if (empty($request->keyword)) {
  286. return $this->api('');
  287. }
  288. $keyword ='%'.$request->keyword.'%';
  289. $data1 = UserInfoModel::where('nickname','like',$keyword)->paginate(20);
  290. return $this->api(compact('data1'));
  291. }
  292. /**
  293. * @api {get} /api/index/dream_search 梦想搜索(search)
  294. * @apiDescription 梦想搜索(search)
  295. * @apiGroup Index
  296. * @apiPermission none
  297. * @apiVersion 0.1.0
  298. * @apiParam {string} keyword 关键字
  299. * @apiSuccessExample {json} Success-Response:
  300. * HTTP/1.1 200 OK
  301. *{
  302. * "status": true,
  303. * "status_code": 0,
  304. * "message": "",
  305. * "data": {
  306. * "data": [
  307. * {
  308. * "dream": "haha", 梦想名
  309. * "user_pic": [
  310. * {
  311. * "pic": "", 用户头像
  312. * }
  313. * ],
  314. * "dream_img": "", 梦想图片
  315. * }
  316. * ]
  317. * }
  318. *}
  319. * @apiErrorExample {json} Error-Response:
  320. * HTTP/1.1 400 Bad Request
  321. */
  322. public function dreamSearch(Request $request)
  323. {
  324. if (empty($request->keyword)) {
  325. return $this->api('');
  326. }
  327. $keyword ='%'.$request->keyword.'%';
  328. $data = DreamInfoModel::where('dream','like',$keyword)->
  329. orWhere('dream','like',$keyword)->paginate(20);
  330. foreach ($data as $k => $value) {
  331. $value->user_pic = $value->dreamFindUser;
  332. $value->dream_img = $value->dreamImgsFirst->pic;
  333. }
  334. return $this->api(compact('data'));
  335. }
  336. //获取轮播图
  337. public function getBanner()
  338. {
  339. $banner = BaseSettingsModel::where(['category' => 'banner'])->where(['status' => '1'])
  340. ->orderBy('sort')->limit('3')->get()->toArray();
  341. return $banner;
  342. }
  343. // 完善梦想信息
  344. public function dreams($dreams)
  345. {
  346. foreach ($dreams as $k => $dream) {
  347. $dream->dream_find_user = $dream->dreamFindUser;
  348. // 计算被关注总人数
  349. $user_id = UserDream::where('dream_id',$dream->id)->first()->user_id;
  350. $data = UserCareUser::where('other_user_id',$user_id)->paginate(20);
  351. $dream->care_num = count($data);
  352. $dream->dream_first_pic = $dream->dreamImgsFirst;
  353. }
  354. }
  355. // 查看关注用户的最新动态
  356. /**
  357. * @api {get} /api/index/news_info 关注用户动态详情
  358. * @apiDescription 关注用户动态详情
  359. * @apiGroup Index
  360. * @apiPermission none
  361. * @apiVersion 0.1.0
  362. * @apiParam {int} id 被点击用户ID
  363. * @apiSuccessExample {json} Success-Response:
  364. * HTTP/1.1 200 OK
  365. *{
  366. * "status": true,
  367. * "status_code": 0,
  368. * "message": "",
  369. * "data": [
  370. * {
  371. * "dream": "1的梦想",
  372. * "about": "介绍",
  373. * "video": "",
  374. * "time": 0,
  375. * "status": 0,
  376. * "updated_at": null,
  377. * "dream_imgs": [
  378. * {
  379. * "title": "1", 梦想图片介绍
  380. * "pic": "111", 梦想图片
  381. * },
  382. * ],
  383. * "comments": [
  384. * {
  385. * "user_id": 2,
  386. * "level": 0,
  387. * "content": "评论内容", 评论内容
  388. * "updated_at": null,
  389. * "pic": "", 评论者头像
  390. * "replay": [
  391. * {
  392. * "level": 0,
  393. * "content": "回复内容", 回复内容
  394. * "updated_at": null,
  395. * "pic": "" 回复者头像
  396. * }
  397. * ]
  398. * }
  399. * ],
  400. * }
  401. * ]
  402. * }
  403. * ]
  404. *}
  405. * @apiErrorExample {json} Error-Response:
  406. * HTTP/1.1 400 Bad Request
  407. */
  408. public function newsInfo(Request $request)
  409. {
  410. // 查看后user_care_user dream_info 更新为零 首页不再显示
  411. $user = $this->getUser();
  412. $other_id = $request->id;
  413. if (empty($other_id)) return $this->error(ErrorCode::MEMBER_NOT_EXIST);
  414. UserCareUser::where('user_id',$user->id)->where('other_user_id',$other_id)->update(['dream_num'=>0]);
  415. $data = UserInfoModel::find($other_id)->UserDream;
  416. foreach ($data as $item) {
  417. $item->dream_imgs = $item->dreamImgs ;
  418. $item->comments = $item->DreamInfo;
  419. foreach ( $item->comments as $k => $v){
  420. $v->pic = UserInfoModel::find($v->user_id)->pic;
  421. $v->replay = $v->replay;
  422. foreach ($v->replay as $k1 => $v1){
  423. $v1->pic = UserInfoModel::find($v1->user_id)->pic;
  424. }
  425. }
  426. }
  427. return $this->api($data);
  428. }
  429. }