IndexController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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/index 首页
  15. * @apiDescription 首页
  16. * @apiGroup Index
  17. * @apiParam {string} type (热门)hot/(潮流)trend/(最新)news
  18. * @apiPermission none
  19. * @apiVersion 0.1.0
  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 index(Request $request)
  55. {
  56. $user = $this->getUser();
  57. // 关注的用户
  58. $arr = $user->UserCareUser;
  59. $users = [] ;
  60. foreach ($arr as $k => $v){
  61. if ($v->pivot->dream_num > 0) {
  62. $v->news_num = $v->pivot->dream_num;
  63. $users[] = $v;
  64. }
  65. }
  66. $type = $request->type;
  67. if ($type == 'hot') {
  68. $banner = $this->getBanner();
  69. // 获取其他用户信息 及梦想
  70. $dreams = DreamInfoModel::orderBy('score','desc')->limit(20)->paginate(20);
  71. $this->dreams($dreams);
  72. return $this->api(compact('banner','users','dreams'));
  73. } elseif ($type == 'trend') {
  74. $dreams = DreamInfoModel::orderBy('score','desc')->offset(20)->limit(100)->paginate(20);
  75. $this->dreams($dreams);
  76. return $this->api(compact('users','dreams'));
  77. } elseif ($type == 'news') {
  78. $dreams = DreamInfoModel::orderBy('score','desc')->offset(100)->limit(500)->paginate(20);
  79. $this->dreams($dreams);
  80. return $this->api(compact('users','dreams'));
  81. }else{
  82. return $this->error(ErrorCode::KEY_ERROR);
  83. }
  84. }
  85. /**
  86. * @api {get} /api/index/search 搜索(search)
  87. * @apiDescription 搜索(search)
  88. * @apiGroup Index
  89. * @apiPermission none
  90. * @apiVersion 0.1.0
  91. * @apiParam {string} keyword 关键字可选
  92. * @apiSuccessExample {json} Success-Response:
  93. * HTTP/1.1 200 OK
  94. * get
  95. *{
  96. * "status": true,
  97. * "status_code": 0,
  98. * "message": "",
  99. * "data": {
  100. * "arr": { // 热门搜索
  101. * ...
  102. * },
  103. * "data1": [
  104. * {
  105. * "search": "ha", // 历史搜索
  106. * }
  107. * ]
  108. * }
  109. *}
  110. * post
  111. * {
  112. * "status": true,
  113. * "status_code": 0,
  114. * "message": "",
  115. * "data": {
  116. * "data1": [
  117. * ... //用户
  118. * ],
  119. * "data2": [
  120. * ... //梦想
  121. * ],
  122. * "data3": [
  123. * ... //标签
  124. * ]
  125. * }
  126. *}
  127. * @apiErrorExample {json} Error-Response:
  128. * HTTP/1.1 400 Bad Request
  129. */
  130. public function search(Request $request)
  131. {
  132. $user = $this->getUser();
  133. $keyword ='%'.$request->keyword.'%';
  134. $data1 = UserInfoModel::where('nickname','like',$keyword)->paginate(20);
  135. $data2 = DreamInfoModel::where('dream','like',$keyword)->
  136. orWhere('dream','like',$keyword)->paginate(20);
  137. $data3 = BaseSettingsModel::where('category','sign')->where('value','like',$keyword)->paginate(20);
  138. if (empty($request->keyword)) {
  139. // 历史搜索
  140. $data1 = $user->search()->orderBy('id','desc')->limit(10)->paginate(20);
  141. // 热门搜索
  142. $data2 = SearchInfoModel::paginate(20);
  143. $arr = [];
  144. foreach ($data2 as $k => $v) {
  145. if (count($arr) == 8) {
  146. break;
  147. }
  148. if (!array_key_exists($v->search,$arr)) {
  149. $arr[$v->search] = $v->times;
  150. }else{
  151. $arr[$v->search] += $v->times;
  152. }
  153. }
  154. arsort($arr);
  155. return $this->api(compact('arr','data1'));
  156. }
  157. return $this->api(compact('data1','data2','data3'));
  158. }
  159. /**
  160. * @api {get} /api/index/user_search 用户搜索(search)
  161. * @apiDescription 用户搜索(search)
  162. * @apiGroup Index
  163. * @apiPermission none
  164. * @apiVersion 0.1.0
  165. * @apiParam {string} keyword 关键字
  166. * @apiSuccessExample {json} Success-Response:
  167. * HTTP/1.1 200 OK
  168. *{
  169. * "status": true,
  170. * "status_code": 0,
  171. * "message": "",
  172. * "data": {
  173. * "data1": [
  174. * "nickname": "ha", 昵称
  175. * "pic": "", 头像
  176. * ...
  177. * ]
  178. * }
  179. *}
  180. * @apiErrorExample {json} Error-Response:
  181. * HTTP/1.1 400 Bad Request
  182. */
  183. public function userSearch(Request $request)
  184. {
  185. if (empty($request->keyword)) {
  186. return $this->api('');
  187. }
  188. $keyword ='%'.$request->keyword.'%';
  189. $data1 = UserInfoModel::where('nickname','like',$keyword)->paginate(20);
  190. return $this->api(compact('data1'));
  191. }
  192. /**
  193. * @api {get} /api/index/dream_search 梦想搜索(search)
  194. * @apiDescription 梦想搜索(search)
  195. * @apiGroup Index
  196. * @apiPermission none
  197. * @apiVersion 0.1.0
  198. * @apiParam {string} keyword 关键字
  199. * @apiSuccessExample {json} Success-Response:
  200. * HTTP/1.1 200 OK
  201. *{
  202. * "status": true,
  203. * "status_code": 0,
  204. * "message": "",
  205. * "data": {
  206. * "data": [
  207. * {
  208. * "dream": "haha", 梦想名
  209. * "user_pic": [
  210. * {
  211. * "pic": "", 用户头像
  212. * }
  213. * ],
  214. * "dream_img": "", 梦想图片
  215. * }
  216. * ]
  217. * }
  218. *}
  219. * @apiErrorExample {json} Error-Response:
  220. * HTTP/1.1 400 Bad Request
  221. */
  222. public function dreamSearch(Request $request)
  223. {
  224. if (empty($request->keyword)) {
  225. return $this->api('');
  226. }
  227. $keyword ='%'.$request->keyword.'%';
  228. $data = DreamInfoModel::where('dream','like',$keyword)->
  229. orWhere('dream','like',$keyword)->paginate(20);
  230. foreach ($data as $k => $value) {
  231. $value->user_pic = $value->dreamFindUser;
  232. $value->dream_img = $value->dreamImgsFirst->pic;
  233. }
  234. return $this->api(compact('data'));
  235. }
  236. //获取轮播图
  237. public function getBanner()
  238. {
  239. $banner = BaseSettingsModel::where(['category' => 'banner'])->where(['status' => '1'])
  240. ->orderBy('sort')->limit('3')->get()->toArray();
  241. return $banner;
  242. }
  243. // 完善梦想信息
  244. public function dreams($dreams)
  245. {
  246. foreach ($dreams as $k => $dream) {
  247. $dream->dream_find_user = $dream->dreamFindUser;
  248. // 计算被关注总人数
  249. $user_id = UserDream::where('dream_id',$dream->id)->first()->user_id;
  250. $data = UserCareUser::where('other_user_id',$user_id)->paginate(20);
  251. $dream->care_num = count($data);
  252. $dream->dream_first_pic = $dream->dreamImgsFirst;
  253. }
  254. }
  255. // 查看关注用户的最新动态
  256. /**
  257. * @api {get} /api/index/news_info 关注用户动态详情
  258. * @apiDescription 关注用户动态详情
  259. * @apiGroup Index
  260. * @apiPermission none
  261. * @apiVersion 0.1.0
  262. * @apiParam {int} id 被点击用户ID
  263. * @apiSuccessExample {json} Success-Response:
  264. * HTTP/1.1 200 OK
  265. *{
  266. * "status": true,
  267. * "status_code": 0,
  268. * "message": "",
  269. * "data": [
  270. * {
  271. * "dream": "1的梦想",
  272. * "about": "介绍",
  273. * "video": "",
  274. * "time": 0,
  275. * "status": 0,
  276. * "updated_at": null,
  277. * "dream_imgs": [
  278. * {
  279. * "title": "1", 梦想图片介绍
  280. * "pic": "111", 梦想图片
  281. * },
  282. * ],
  283. * "comments": [
  284. * {
  285. * "user_id": 2,
  286. * "level": 0,
  287. * "content": "评论内容", 评论内容
  288. * "updated_at": null,
  289. * "pic": "", 评论者头像
  290. * "replay": [
  291. * {
  292. * "level": 0,
  293. * "content": "回复内容", 回复内容
  294. * "updated_at": null,
  295. * "pic": "" 回复者头像
  296. * }
  297. * ]
  298. * }
  299. * ],
  300. * }
  301. * ]
  302. * }
  303. * ]
  304. *}
  305. * @apiErrorExample {json} Error-Response:
  306. * HTTP/1.1 400 Bad Request
  307. */
  308. public function newsInfo(Request $request)
  309. {
  310. // 查看后user_care_user dream_info 更新为零 首页不再显示
  311. $user = $this->getUser();
  312. $other_id = $request->id;
  313. if (empty($other_id)) return $this->error(ErrorCode::MEMBER_NOT_EXIST);
  314. UserCareUser::where('user_id',$user->id)->where('other_user_id',$other_id)->update(['dream_num'=>0]);
  315. $data = UserInfoModel::find($other_id)->UserDream;
  316. foreach ($data as $item) {
  317. $item->dream_imgs = $item->dreamImgs ;
  318. $item->comments = $item->DreamInfo;
  319. foreach ( $item->comments as $k => $v){
  320. $v->pic = UserInfoModel::find($v->user_id)->pic;
  321. $v->replay = $v->replay;
  322. foreach ($v->replay as $k1 => $v1){
  323. $v1->pic = UserInfoModel::find($v1->user_id)->pic;
  324. }
  325. }
  326. }
  327. return $this->api($data);
  328. }
  329. }