IndexController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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\UserInfoModel;
  7. use Illuminate\Http\Request;
  8. use App\Services\Base\ErrorCode;
  9. class IndexController extends Controller
  10. {
  11. /**
  12. * @api {get} /api/index/hot 热门(hot)
  13. * @apiDescription 登陆(hot)
  14. * @apiGroup Index
  15. * @apiPermission none
  16. * @apiVersion 0.1.0
  17. * @apiParam {string} phone 手机号码
  18. * @apiSuccessExample {json} Success-Response:
  19. * HTTP/1.1 200 OK
  20. * {
  21. * "state": true,
  22. * "code": 0,
  23. * "message": "",
  24. * "data": {
  25. * "verify_code": "1234"//该值调试时使用,sms调通后取消
  26. * }
  27. * }
  28. * @apiErrorExample {json} Error-Response:
  29. * HTTP/1.1 400 Bad Request
  30. * {
  31. * "state": false,
  32. * "code": 1000,
  33. * "message": "传入参数不正确",
  34. * "data": null or []
  35. * }
  36. * 可能出现的错误代码:
  37. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  38. */
  39. public function hot(Request $request)
  40. {
  41. $user = $this->getUser();
  42. //获取轮播图
  43. $banner = $this->getBanner();
  44. // 关注的用户
  45. $other_user = $user->UserCareUser;
  46. // 获取其他用户信息 及梦想
  47. $dreams = DreamInfoModel::orderBy('score','desc')->limit(20)->get();
  48. foreach ($dreams as $k => $dream) {
  49. $dream->dream_find_user = $dream->dreamFindUser;
  50. $dream->dream_first_pic = $dream->dreamImgsFirst;
  51. }
  52. return $this->api(compact('banner','other_user','dreams'));
  53. }
  54. /**
  55. * @api {get} /api/index/trend 潮流(trend)
  56. * @apiDescription 潮流(trend)
  57. * @apiGroup Index
  58. * @apiPermission none
  59. * @apiVersion 0.1.0
  60. * @apiParam {string} phone 手机号码
  61. * @apiSuccessExample {json} Success-Response:
  62. * HTTP/1.1 200 OK
  63. * {
  64. * "state": true,
  65. * "code": 0,
  66. * "message": "",
  67. * "data": {
  68. * "verify_code": "1234"//该值调试时使用,sms调通后取消
  69. * }
  70. * }
  71. * @apiErrorExample {json} Error-Response:
  72. * HTTP/1.1 400 Bad Request
  73. * {
  74. * "state": false,
  75. * "code": 1000,
  76. * "message": "传入参数不正确",
  77. * "data": null or []
  78. * }
  79. * 可能出现的错误代码:
  80. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  81. */
  82. public function trend(Request $request)
  83. {
  84. $user = $this->getUser();
  85. // 关注的用户
  86. $other_user = $user->UserCareUser;
  87. // 获取其他用户信息 及梦想
  88. $dreams = DreamInfoModel::orderBy('score','desc')->offset(20)->limit(100)->get();
  89. foreach ($dreams as $k => $dream) {
  90. $dream->dream_find_user = $dream->dreamFindUser;
  91. $dream->dream_first_pic = $dream->dreamImgsFirst;
  92. }
  93. return $this->api(compact('other_user','dreams'));
  94. }
  95. /**
  96. * @api {get} /api/index/new 最新(news)
  97. * @apiDescription 最新(news)
  98. * @apiGroup Index
  99. * @apiPermission none
  100. * @apiVersion 0.1.0
  101. * @apiParam {string} phone 手机号码
  102. * @apiSuccessExample {json} Success-Response:
  103. * HTTP/1.1 200 OK
  104. * {
  105. * "state": true,
  106. * "code": 0,
  107. * "message": "",
  108. * "data": {
  109. * "verify_code": "1234"//该值调试时使用,sms调通后取消
  110. * }
  111. * }
  112. * @apiErrorExample {json} Error-Response:
  113. * HTTP/1.1 400 Bad Request
  114. * {
  115. * "state": false,
  116. * "code": 1000,
  117. * "message": "传入参数不正确",
  118. * "data": null or []
  119. * }
  120. * 可能出现的错误代码:
  121. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  122. */
  123. public function news(Request $request)
  124. {
  125. $user = $this->getUser();
  126. // 关注的用户
  127. $other_user = $user->UserCareUser;
  128. // 获取其他用户信息 及梦想
  129. $dreams = DreamInfoModel::orderBy('score','desc')->offset(100)->limit(500)->get();
  130. foreach ($dreams as $k => $dream) {
  131. $dream->dream_find_user = $dream->dreamFindUser;
  132. $dream->dream_first_pic = $dream->dreamImgsFirst;
  133. }
  134. return $this->api(compact('other_user','dreams'));
  135. }
  136. /**
  137. * @api {get} {post} /api/index/search 搜索(search) get post
  138. * @apiDescription 搜索(search)
  139. * @apiGroup Index
  140. * @apiPermission none
  141. * @apiVersion 0.1.0
  142. * @apiParam {string} keyword 关键字
  143. * @apiSuccessExample {json} Success-Response:
  144. * HTTP/1.1 200 OK
  145. * get
  146. *{
  147. * "status": true,
  148. * "status_code": 0,
  149. * "message": "",
  150. * "data": {
  151. * "arr": { // 热门搜索
  152. * ...
  153. * },
  154. * "data1": [
  155. * {
  156. * "search": "ha", // 历史搜索
  157. * }
  158. * ]
  159. * }
  160. *}
  161. * post
  162. * {
  163. * "status": true,
  164. * "status_code": 0,
  165. * "message": "",
  166. * "data": {
  167. * "data1": [
  168. * ... //用户
  169. * ],
  170. * "data2": [
  171. * ... //梦想
  172. * ],
  173. * "data3": [
  174. * ... //标签
  175. * ]
  176. * }
  177. *}
  178. * @apiErrorExample {json} Error-Response:
  179. * HTTP/1.1 400 Bad Request
  180. */
  181. public function search(Request $request)
  182. {
  183. $user = $this->getUser();
  184. if ($request->method() == 'GET') {
  185. // 历史搜索
  186. $data1 = $user->search()->orderBy('id','desc')->limit(10)->get();
  187. // 热门搜索
  188. $data2 = SearchInfoModel::get();
  189. $arr = [];
  190. foreach ($data2 as $k => $v) {
  191. if (count($arr) == 8) {
  192. break;
  193. }
  194. if (!array_key_exists($v->search,$arr)) {
  195. $arr[$v->search] = $v->times;
  196. }else{
  197. $arr[$v->search] += $v->times;
  198. }
  199. }
  200. arsort($arr);
  201. return $this->api(compact('arr','data1'));
  202. }
  203. $validator = \Validator::make($request->all(),
  204. [
  205. 'keyword' => 'required',
  206. ],
  207. [
  208. 'keyword.required' => '关键字必填',
  209. ]
  210. );
  211. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  212. $keyword ='%'.$request->keyword.'%';
  213. $data1 = UserInfoModel::where('nickname','like',$keyword)->get();
  214. $data2 = DreamInfoModel::where('dream','like',$keyword)->
  215. orWhere('dream','like',$keyword)->get();
  216. $data3 = BaseSettingsModel::where('category','sign')->get();
  217. return $this->api(compact('data1','data2','data3'));
  218. }
  219. //获取轮播图
  220. public function getBanner()
  221. {
  222. $banner = BaseSettingsModel::where(['category' => 'banner'])->where(['status' => '1'])
  223. ->orderBy('sort')->limit('3')->get()->toArray();
  224. return $banner;
  225. }
  226. }