IndexController.php 12 KB

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