IndexController.php 18 KB

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