HomeController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Models\BaseSettingsModel;
  4. use App\Models\S1GoodsCateModel;
  5. use App\Models\S1GoodsInfoModel;
  6. use App\Services\Base\ErrorCode;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Validator;
  9. class HomeController extends Controller
  10. {
  11. /**
  12. * @api {get} /api/home/index 首页
  13. * @apiDescription 首页
  14. * @apiGroup Home
  15. * @apiParam {string} appid appid
  16. * @apiParam {int} [head_cate_id] 头部分类id
  17. * @apiPermission none
  18. * @apiVersion 0.1.0
  19. * @apiSuccessExample {json} Success-Response:
  20. * HTTP/1.1 200 OK
  21. {
  22. "status": true,
  23. "status_code": 0,
  24. "message": "",
  25. "data": {
  26. "banner": [ 轮播图
  27. {
  28. "id": 1002,
  29. "value": "/upload/banner/20171015/cd985cb0aac48ca5ea7b34c366c711fa.jpg",
  30. "sort": 1,
  31. }
  32. ],
  33. "cates": [ 分类
  34. {
  35. "id": 4,
  36. "name": "商品分类2",
  37. "pic": "/upload/goods/cate/20171013/923996f99388fa90acfee1bf31a909fa.jpg",
  38. "status": 1,
  39. "sort": 11,
  40. "goods": [
  41. {
  42. "id": 1,
  43. "is_home_cate": 1,
  44. "home_cate_id": 3,
  45. "name": "商品1",
  46. "number": "0011",
  47. "pic": "/upload/s1/goods/face/20171013/effbecdc6d9de83d0128e3f08ec6d636.jpg",
  48. "status": 1,
  49. }
  50. ]
  51. }
  52. ],
  53. "goods": [ 商品
  54. {
  55. "id": 1,
  56. "name": "首页分类1",
  57. "pic": "/upload/goods/cate/20171013/2454820f914e59dd4a90b575cd47e03c.jpg",
  58. "goods": [
  59. {
  60. "id": 1,
  61. "is_home_cate": 1,
  62. "home_cate_id": 3,
  63. "name": "商品1",
  64. "number": "0011",
  65. "pic": "/upload/s1/goods/face/20171013/effbecdc6d9de83d0128e3f08ec6d636.jpg",
  66. "status": 1,
  67. }
  68. ]
  69. },
  70. ]
  71. }
  72. }
  73. * @apiErrorExample {json} Error-Response:
  74. * HTTP/1.1 400 Bad Request
  75. */
  76. public function index(Request $request)
  77. {
  78. $validator = Validator::make($request->all(),
  79. [
  80. 'appid' => 'required',
  81. ],
  82. [
  83. 'appid.required' => 'appid不存在',
  84. ]
  85. );
  86. if ($validator->fails()) {
  87. return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  88. }
  89. $appid = $request->input('appid');
  90. $header_cate_id = $request->input('head_cate_id');
  91. $banner = BaseSettingsModel::where('appid',$appid)->where('category','banner')->orderBy('sort','desc')->get();
  92. $cates = S1GoodsCateModel::where('appid',$appid)->where('type_id',1)->
  93. with(['goods'=>function ($query) {
  94. $query->orderBy('sort','desc');
  95. }])->orderBy('sort','desc')->get();
  96. if (!empty($header_cate_id)) { //获取头部分类商品
  97. // where('appid',$appid)->where('cate_id',$header_cate_id)->where('type_id',1)
  98. $goods = S1GoodsCateModel::
  99. with(['goods'=>function ($query) {
  100. $query->orderBy('sort','desc');
  101. }])->orderBy('sort','desc')->where('id',$header_cate_id)->get();
  102. }else{
  103. $goods = S1GoodsCateModel::where('appid',$appid)->where('type_id',2)->
  104. with(['home_goods'=>function ($query) {
  105. $query->orderBy('sort','desc');
  106. }])->orderBy('sort','desc')->get();
  107. }
  108. return $this->api(compact('banner','cates','goods'));
  109. }
  110. /**
  111. * @api {get} /api/home/search 搜索
  112. * @apiDescription 搜索
  113. * @apiGroup Home
  114. * @apiParam {string} appid appid
  115. * @apiParam {string} keyword keyword
  116. * @apiPermission none
  117. * @apiVersion 0.1.0
  118. * @apiSuccessExample {json} Success-Response:
  119. * HTTP/1.1 200 OK
  120. {
  121. "status": true,
  122. "status_code": 0,
  123. "message": "",
  124. "data": {
  125. "banner": [
  126. {
  127. "value": "/upload/banner/20171012/71a9962b9b9de1876c4d76ed6d879afd.jpg",
  128. },
  129. ],
  130. "cates": [
  131. {
  132. "name": "商品分类2",
  133. "pic": "/upload/goods/cate/20171013/923996f99388fa90acfee1bf31a909fa.jpg",
  134. "goods": []
  135. }
  136. ],
  137. "goods": {
  138. "current_page": 1,
  139. "data": [
  140. {
  141. "id": 1,
  142. "name": "商品1",
  143. "pic": "/upload/s1/goods/face/20171013/effbecdc6d9de83d0128e3f08ec6d636.jpg",
  144. }
  145. ],
  146. "first_page_url": "http://www.s1.com/api/home/search?page=1",
  147. "from": 1,
  148. "last_page": 1,
  149. "last_page_url": "http://www.s1.com/api/home/search?page=1",
  150. "next_page_url": null,
  151. "path": "http://www.s1.com/api/home/search",
  152. "per_page": 10,
  153. "prev_page_url": null,
  154. "to": 1,
  155. "total": 1
  156. }
  157. }
  158. }
  159. }
  160. * @apiErrorExample {json} Error-Response:
  161. * HTTP/1.1 400 Bad Request
  162. */
  163. public function search(Request $request)
  164. {
  165. $validator = Validator::make($request->all(),
  166. [
  167. 'appid' => 'required',
  168. 'keyword' => 'required',
  169. ],
  170. [
  171. 'appid.required' => 'appid不存在',
  172. 'keyword.required' => '关键字不能为空',
  173. ]
  174. );
  175. if ($validator->fails()) {
  176. return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  177. }
  178. $appid = $request->input('appid');
  179. $banner = BaseSettingsModel::where('appid',$appid)->where('category','banner')->orderBy('sort','desc')->get();
  180. $cates = S1GoodsCateModel::where('appid',$appid)->where('type_id',1)->
  181. with(['goods'=>function ($query) {
  182. $query->orderBy('sort','desc');
  183. }])->orderBy('sort','desc')->get();
  184. $keyword = '%'.$request->input('keyword').'%';
  185. $goods = S1GoodsInfoModel::where('appid',$appid)->where('name','like',$keyword)->paginate();
  186. return $this->api(compact('banner','cates','goods'));
  187. }
  188. }