IndexController.php 18 KB

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