IndexController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. $this->dreams($dreams);
  205. return $this->api(compact('banner','users','dreams'));
  206. }
  207. }
  208. /**
  209. * @api {get} /api/index/search 搜索(search)
  210. * @apiDescription 搜索(search)
  211. * @apiGroup Index
  212. * @apiPermission none
  213. * @apiVersion 0.1.0
  214. * @apiParam {string} keyword 关键字可选 (ha)
  215. * @apiSuccessExample {json} Success-Response:
  216. * HTTP/1.1 200 OK
  217. * get
  218. *{
  219. * "status": true,
  220. * "status_code": 0,
  221. * "message": "",
  222. * "data": {
  223. * "arr": { // 热门搜索
  224. * ...
  225. * },
  226. * "data1": [
  227. * {
  228. * "search": "ha", // 历史搜索
  229. * }
  230. * ]
  231. * }
  232. *}
  233. * post
  234. * {
  235. * "status": true,
  236. * "status_code": 0,
  237. * "message": "",
  238. * "data": {
  239. * "data1": [
  240. * ... //用户
  241. * ],
  242. * "data2": [
  243. * ... //梦想
  244. * ],
  245. * "data3": [
  246. * ... //标签
  247. * ]
  248. * }
  249. *}
  250. * @apiErrorExample {json} Error-Response:
  251. * HTTP/1.1 400 Bad Request
  252. */
  253. public function search(Request $request)
  254. {
  255. $user = $this->getUser();
  256. $keyword ='%'.$request->keyword.'%';
  257. $data1 = UserInfoModel::where('nickname','like',$keyword)->get();
  258. $data2 = DreamInfoModel::where('dream','like',$keyword)->
  259. orWhere('sign','like',$keyword)->get();
  260. $data3 = BaseSettingsModel::where('category','sign')->where('value','like',$keyword)->get();
  261. if (empty($request->keyword)) {
  262. // 历史搜索
  263. $data1 = $user->search()->orderBy('id','desc')->limit(10)->get();
  264. // 热门搜索
  265. $data2 = SearchInfoModel::get();
  266. $arr = [];
  267. foreach ($data2 as $k => $v) {
  268. if (count($arr) == 8) {
  269. break;
  270. }
  271. if (!array_key_exists($v->search,$arr)) {
  272. $arr[$v->search] = $v->times;
  273. }else{
  274. $arr[$v->search] += $v->times;
  275. }
  276. }
  277. arsort($arr);
  278. return $this->api(compact('arr','data1'));
  279. }
  280. // 写入搜索记录
  281. $this->insertSearchTable($user->id,$request->keyword);
  282. return $this->api(compact('data1','data2','data3'));
  283. }
  284. /**
  285. * @api {get} /api/index/user_search 用户搜索(search)
  286. * @apiDescription 用户搜索(search)
  287. * @apiGroup Index
  288. * @apiPermission none
  289. * @apiVersion 0.1.0
  290. * @apiParam {string} keyword 关键字
  291. * @apiSuccessExample {json} Success-Response:
  292. * HTTP/1.1 200 OK
  293. *{
  294. * "status": true,
  295. * "status_code": 0,
  296. * "message": "",
  297. * "data": {
  298. * "data1": [
  299. * "nickname": "ha", 昵称
  300. * "pic": "", 头像
  301. * ...
  302. * ]
  303. * }
  304. *}
  305. * @apiErrorExample {json} Error-Response:
  306. * HTTP/1.1 400 Bad Request
  307. */
  308. public function userSearch(Request $request)
  309. {
  310. $user = $this->getUser();
  311. if (empty($request->keyword)) {
  312. return $this->api('');
  313. }
  314. $keyword ='%'.$request->keyword.'%';
  315. $data1 = UserInfoModel::where('nickname','like',$keyword)->get();
  316. $this->insertSearchTable($user->id,$request->keyword);
  317. return $this->api(compact('data1'));
  318. }
  319. /**
  320. * @api {get} /api/index/dream_search 梦想搜索(search)
  321. * @apiDescription 梦想搜索(search)
  322. * @apiGroup Index
  323. * @apiPermission none
  324. * @apiVersion 0.1.0
  325. * @apiParam {string} keyword 关键字
  326. * @apiSuccessExample {json} Success-Response:
  327. * HTTP/1.1 200 OK
  328. *{
  329. * "status": true,
  330. * "status_code": 0,
  331. * "message": "",
  332. * "data": {
  333. * "data": [
  334. * {
  335. * "dream": "haha", 梦想名
  336. * "user_pic": [
  337. * {
  338. * "pic": "", 用户头像
  339. * }
  340. * ],
  341. * "dream_img": "", 梦想图片
  342. * }
  343. * ]
  344. * }
  345. *}
  346. * @apiErrorExample {json} Error-Response:
  347. * HTTP/1.1 400 Bad Request
  348. */
  349. public function dreamSearch(Request $request)
  350. {
  351. $user = $this->getUser();
  352. if (empty($request->keyword)) {
  353. return $this->api('');
  354. }
  355. $keyword ='%'.$request->keyword.'%';
  356. $data = DreamInfoModel::where('dream','like',$keyword)->
  357. orWhere('sign','like',$keyword)->get();
  358. foreach ($data as $k => $value) {
  359. $value->user_pic = $value->dreamFindUser;
  360. $value->dream_img = $value->dreamImgsFirst->pic;
  361. }
  362. $this->insertSearchTable($user->id,$request->keyword);
  363. return $this->api(compact('data'));
  364. }
  365. //获取轮播图
  366. public function getBanner()
  367. {
  368. $banner = BaseSettingsModel::where(['category' => 'banner'])->where(['status' => '1'])
  369. ->orderBy('sort')->get()->toArray();
  370. return $banner;
  371. }
  372. // 完善梦想信息
  373. public function dreams($dreams)
  374. {
  375. foreach ($dreams as $k => $dream) {
  376. $dream->dream_user = $dream->dreamUser;
  377. // 计算被关注总人数
  378. $user_id = $dream->user_id;
  379. $data = UserCareDream::where('dream_id',$dream->id)->get();
  380. $dream->care_num = count($data);
  381. $dream->dream_imgs_first = $dream->dreamImgsFirst?$dream->dreamImgsFirst->pic:'';
  382. // 梦想详情 detailed 图片 ,评论 ,回复
  383. $arr = [] ;
  384. $arr['dream_imgs'] = $dream->dreamImgs;
  385. $dream->dream_detailed = $arr;
  386. }
  387. }
  388. // 查看关注用户的最新动态
  389. /**
  390. * @api {get} /api/index/news_info 关注用户动态详情
  391. * @apiDescription 关注用户动态详情
  392. * @apiGroup Index
  393. * @apiPermission none
  394. * @apiVersion 0.1.0
  395. * @apiParam {int} id 被点击用户ID
  396. * @apiSuccessExample {json} Success-Response:
  397. * HTTP/1.1 200 OK
  398. *{
  399. * "status": true,
  400. * "status_code": 0,
  401. * "message": "",
  402. * "data": {
  403. * "all_interaction": [ 互动
  404. * {
  405. * "id": 7,
  406. * "dream_id": 2,
  407. * "title": "梦想动态有力来了",
  408. * "pic1": "http\"//www.baidu.com",
  409. * "pic2": "",
  410. * "pic3": "",
  411. * "pic4": "",
  412. * "pic5": "",
  413. * "pic6": "",
  414. * "pic7": "",
  415. * "pic8": "",
  416. * "pic9": "",
  417. * "status": 1,
  418. * "created_at": "2017-06-22 21:09:18",
  419. * "updated_at": "2017-06-22 21:09:18",
  420. * "deleted_at": null,
  421. * "user_id": 2,
  422. * "time": 720000, 倒计时
  423. * "comments": [ 评论
  424. * {
  425. * "interaction_id": 7,
  426. * "user_id": 1,
  427. * "level": 0,
  428. * "content": "不错的梦想",
  429. * "updated_at": null,
  430. * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg",
  431. * "replay": [ 回复
  432. * {
  433. * "user_id": 1,
  434. * "level": 0,
  435. * "content": "我也觉得不错",
  436. * "updated_at": null,
  437. * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg"
  438. * },
  439. * {
  440. * "user_id": 6,
  441. * "level": 0,
  442. * "content": "祝你梦想成真",
  443. * "updated_at": null,
  444. * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg"
  445. * }
  446. * ]
  447. * }
  448. * ]
  449. * },
  450. * ]
  451. * }
  452. *}
  453. * @apiErrorExample {json} Error-Response:
  454. * HTTP/1.1 400 Bad Request
  455. */
  456. public function newsInfo(Request $request)
  457. {
  458. // 查看后user_care_user dream_info 更新为零 首页不再显示
  459. $user = $this->getUser();
  460. $other_id = $request->id;
  461. if (empty($other_id)) return $this->error(ErrorCode::MEMBER_NOT_EXIST);
  462. UserCareDream::where('user_id',$user->id)->where('dream_user_id',$other_id)->update(['interaction_number'=>0]);
  463. $data = UserInfoModel::where('id',$other_id)->with(['allInteraction'=>function ($query){
  464. $query->orderBy('id','desc');
  465. }])->first();
  466. // $data = UserInfoModel::find($other_id)->allInteraction;
  467. // dd($data) ;
  468. // dd($data->allInteraction);
  469. foreach ($data->allInteraction as $item) {
  470. $item->time = DreamInfoModel::find($item->dream_id)->time;
  471. $item->comments = $item->comments;
  472. foreach ($item->comments as $k => $v) {
  473. $v->pic = UserInfoModel::find($v->user_id)->pic;
  474. $v->replay = $v->replay;
  475. foreach ($v->replay as $k1 => $v1) {
  476. $v1->pic = UserInfoModel::find($v1->user_id)->pic;
  477. }
  478. }
  479. }
  480. return $this->api($data);
  481. }
  482. public function insertSearchTable($id,$keyword)
  483. {
  484. $info = SearchInfoModel::where('user_id',$id)->
  485. where('search',trim($keyword))->first();
  486. if (count($info) == 0) {
  487. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  488. }else{
  489. $info->times += 1;
  490. $info->save();
  491. }
  492. }
  493. }