IndexController.php 18 KB

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