IndexController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. public function getBanner()
  325. {
  326. $banner = BaseSettingsModel::where(['category' => 'banner'])->where(['status' => '1'])
  327. ->orderBy('sort')->get()->toArray();
  328. return $banner;
  329. }
  330. // 完善梦想信息
  331. public function dreams($dreams)
  332. {
  333. foreach ($dreams as $k => $dream) {
  334. $dream->dream_user = $dream->user;
  335. // 计算被关注总人数
  336. $user_id = $dream->user_id;
  337. $data = UserCareDream::where('dream_id',$dream->id)->get();
  338. $dream->care_num = count($data);
  339. $dream->dream_imgs_first = $dream->dreamImgsFirst?$dream->dreamImgsFirst->pic:'';
  340. // 梦想详情 detailed 图片 ,评论 ,回复
  341. $arr = [] ;
  342. $arr['dream_imgs'] = $dream->imgs;
  343. $dream->dream_detailed = $arr;
  344. }
  345. }
  346. // 查看关注用户的最新动态
  347. /**
  348. * @api {get} /api/index/news_info 关注用户动态详情
  349. * @apiDescription 关注用户动态详情
  350. * @apiGroup Index
  351. * @apiPermission none
  352. * @apiVersion 0.1.0
  353. * @apiParam {int} id 被点击用户ID
  354. * @apiSuccessExample {json} Success-Response:
  355. * HTTP/1.1 200 OK
  356. *{
  357. * "status": true,
  358. * "status_code": 0,
  359. * "message": "",
  360. * "data": {
  361. * "all_interaction": [ 互动
  362. * {
  363. * "id": 7,
  364. * "dream_id": 2,
  365. * "title": "梦想动态有力来了",
  366. * "pic1": "http\"//www.baidu.com",
  367. * "pic2": "",
  368. * "pic3": "",
  369. * "pic4": "",
  370. * "pic5": "",
  371. * "pic6": "",
  372. * "pic7": "",
  373. * "pic8": "",
  374. * "pic9": "",
  375. * "status": 1,
  376. * "created_at": "2017-06-22 21:09:18",
  377. * "updated_at": "2017-06-22 21:09:18",
  378. * "deleted_at": null,
  379. * "user_id": 2,
  380. * "time": 720000, 倒计时
  381. * "comments": [ 评论
  382. * {
  383. * "interaction_id": 7,
  384. * "user_id": 1,
  385. * "level": 0,
  386. * "content": "不错的梦想",
  387. * "updated_at": null,
  388. * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg",
  389. * "replay": [ 回复
  390. * {
  391. * "user_id": 1,
  392. * "level": 0,
  393. * "content": "我也觉得不错",
  394. * "updated_at": null,
  395. * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg"
  396. * },
  397. * {
  398. * "user_id": 6,
  399. * "level": 0,
  400. * "content": "祝你梦想成真",
  401. * "updated_at": null,
  402. * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg"
  403. * }
  404. * ]
  405. * }
  406. * ]
  407. * },
  408. * ]
  409. * }
  410. *}
  411. * @apiErrorExample {json} Error-Response:
  412. * HTTP/1.1 400 Bad Request
  413. */
  414. public function newsInfo(Request $request)
  415. {
  416. // 查看后user_care_user dream_info 更新为零 首页不再显示
  417. $user = $this->getUser();
  418. $other_id = $request->id;
  419. if (empty($other_id)) return $this->error(ErrorCode::MEMBER_NOT_EXIST);
  420. UserCareDream::where('user_id',$user->id)->where('dream_user_id',$other_id)->update(['interaction_number'=>0]);
  421. $data = UserInfoModel::where('id',$other_id)->with(['allInteraction'=>function ($query){
  422. $query->orderBy('id','desc');
  423. }])->first();
  424. // $data = UserInfoModel::find($other_id)->allInteraction;
  425. // dd($data) ;
  426. // dd($data->allInteraction);
  427. foreach ($data->allInteraction as $item) {
  428. $item->time = DreamInfoModel::find($item->dream_id)->time;
  429. $item->comments = $item->comments;
  430. foreach ($item->comments as $k => $v) {
  431. $v->pic = UserInfoModel::find($v->user_id)->pic;
  432. $v->replay = $v->replay;
  433. foreach ($v->replay as $k1 => $v1) {
  434. $v1->pic = UserInfoModel::find($v1->user_id)->pic;
  435. }
  436. }
  437. }
  438. return $this->api($data);
  439. }
  440. }