IndexController.php 16 KB

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