IndexController.php 13 KB

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