Banner.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Validation\ValidationException;
  7. use Exception;
  8. use App\Models\Banner as BannerModel;
  9. class Banner extends Controller
  10. {
  11. /**
  12. * @api {get} /admin/banner 轮播图列表
  13. * @apiGroup 轮播图管理
  14. * @apiUse result
  15. * @apiUse header
  16. * @apiVersion 1.0.0
  17. *
  18. * @apiParam {int} [page=1]
  19. * @apiParam {int} [limit=10]
  20. *
  21. */
  22. function index()
  23. {
  24. try {
  25. $page = request()->input('page', 1);
  26. $limit = request()->input('limit', 15);
  27. $query = new BannerModel();
  28. $count = $query->count();
  29. $list = $query
  30. ->forPage($page, $limit)
  31. ->orderByDesc('create_time')
  32. ->get();
  33. } catch (ValidationException $e) {
  34. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  35. } catch (Exception $e) {
  36. return $this->error(intval($e->getCode()));
  37. }
  38. return $this->success(['total' => $count, 'data' => $list]);
  39. }
  40. /**
  41. * @api {post} /admin/banner/update 修改
  42. * @apiDescription 更新或者新增都用这个接口
  43. * @apiGroup 轮播图管理
  44. * @apiUse result
  45. * @apiUse header
  46. * @apiVersion 1.0.0
  47. *
  48. */
  49. public function update()
  50. {
  51. DB::beginTransaction();
  52. try {
  53. $params = request()->validate([
  54. 'id' => ['required', 'integer', 'min:0', 'max:99999999'],
  55. 'title' => ['required'],
  56. 'image' => ['required'],
  57. 'link' => ['nullable'],
  58. 'type' => ['nullable'],
  59. 'sort' => ['nullable', 'integer', 'min:0', 'max:99999999'],
  60. ]);
  61. $id = request()->input('id', 0);
  62. BannerModel::updateOrCreate(
  63. ['id' => $id],
  64. ['title' => $params['title']],
  65. ['image' => $params['image']],
  66. ['link' => $params['link']],
  67. ['type' => $params['type']],
  68. ['sort' => $params['sort']],
  69. );
  70. DB::commit();
  71. } catch (ValidationException $e) {
  72. DB::rollBack();
  73. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  74. } catch (Exception $e) {
  75. DB::rollBack();
  76. return $this->error(intval($e->getCode()), $e->getMessage());
  77. }
  78. return $this->success();
  79. }
  80. /**
  81. * @api {post} /admin/banner/delete 删除
  82. * @apiGroup 轮播图管理
  83. * @apiUse result
  84. * @apiUse header
  85. * @apiVersion 1.0.0
  86. *
  87. * @apiParam {int} id 要删除的ID
  88. *
  89. */
  90. function destroy()
  91. {
  92. DB::beginTransaction();
  93. try {
  94. request()->validate([
  95. 'id' => ['required', 'integer', 'min:1', 'max:99999999'],
  96. ]);
  97. $id = request()->input('id', 0);
  98. $info = BannerModel::where('id', $id)->first();
  99. if (!$info) throw new Exception("数据不存在", HttpStatus::CUSTOM_ERROR);
  100. $info->delete();
  101. DB::commit();
  102. } catch (ValidationException $e) {
  103. DB::rollBack();
  104. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  105. } catch (Exception $e) {
  106. DB::rollBack();
  107. return $this->error(intval($e->getCode()));
  108. }
  109. return $this->success();
  110. }
  111. }