IndexController.php 10 KB

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