IndexController.php 18 KB

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