IndexController.php 12 KB

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