IndexController.php 16 KB

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