CateController.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Models\S1GoodsCateModel;
  4. use App\Models\S1GoodsInfoModel;
  5. use App\Services\Base\ErrorCode;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Validator;
  8. class CateController extends Controller
  9. {
  10. /**
  11. * @api {get} /api/cate/index 分类
  12. * @apiDescription 分类
  13. * @apiGroup Cate
  14. * @apiParam {string} appid appid
  15. * @apiParam {int} [page=1] 页码(分页参数)
  16. * @apiParam {int} [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. "cates": [
  27. {
  28. "id": 1,
  29. "name": "首页分类1",
  30. "pic": "/upload/goods/cate/20171013/2454820f914e59dd4a90b575cd47e03c.jpg",
  31. },
  32. {
  33. "id": 3,
  34. "name": "首页分类2",
  35. "pic": "/upload/goods/cate/20171013/0e67c1d32c450c5ccd0a9aae5ec9ab96.jpg",
  36. }
  37. ],
  38. "goods": {
  39. "current_page": 1,
  40. "data": [
  41. {
  42. "id": 1,
  43. "name": "商品1",
  44. "pic": "/upload/s1/goods/face/20171013/effbecdc6d9de83d0128e3f08ec6d636.jpg",
  45. }
  46. ],
  47. "first_page_url": "http://www.s1.com/api/cate/index?page=1",
  48. "from": 1,
  49. "last_page": 1,
  50. "last_page_url": "http://www.s1.com/api/cate/index?page=1",
  51. "next_page_url": null,
  52. "path": "http://www.s1.com/api/cate/index",
  53. "per_page": 10,
  54. "prev_page_url": null,
  55. "to": 1,
  56. "total": 1
  57. }
  58. }
  59. }
  60. * @apiErrorExample {json} Error-Response:
  61. * HTTP/1.1 400 Bad Request
  62. */
  63. public function index(Request $request)
  64. {
  65. $validator = Validator::make($request->all(),
  66. [
  67. 'appid' => 'required',
  68. ],
  69. [
  70. 'appid.required' => 'appid不存在',
  71. ]
  72. );
  73. if ($validator->fails()) {
  74. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
  75. }
  76. $appid = $request->input('appid');
  77. $cate_id = $request->input('id');
  78. $model = new S1GoodsInfoModel();
  79. $cates = S1GoodsCateModel::where('type_id',2)->orderBy('sort')->get();
  80. $query = $model->where('appid',$appid)->where('is_home_cate',1)->orderBy('sort','desc');
  81. if($cate_id) $query = $query->where('home_cate_id',$cate_id);
  82. $goods = $query->paginate();
  83. return $this->api(compact('cates','goods'));
  84. }
  85. }