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