IndexController.php 18 KB

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