Banner.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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("sort")
  32. ->get()->toArray();
  33. } catch (ValidationException $e) {
  34. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  35. } catch (Exception $e) {
  36. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  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' => ['nullable'],
  55. 'title' => ['required'],
  56. 'image' => ['required'],
  57. 'link' => ['nullable'],
  58. 'type' => ['nullable'],
  59. 'sort' => ['nullable'],
  60. ]);
  61. $id = $params['id'] ?? null;
  62. BannerModel::updateOrCreate(
  63. ['id' => $id],
  64. [
  65. 'title' => $params['title'],
  66. 'image' => $params['image'],
  67. 'link' => $params['link'],
  68. 'type' => $params['type'],
  69. 'sort' => $params['sort']
  70. ],
  71. );
  72. DB::commit();
  73. } catch (ValidationException $e) {
  74. DB::rollBack();
  75. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  76. } catch (Exception $e) {
  77. DB::rollBack();
  78. return $this->error(intval($e->getCode()), $e->getMessage());
  79. }
  80. return $this->success();
  81. }
  82. /**
  83. * @api {post} /admin/banner/delete 删除
  84. * @apiGroup 轮播图管理
  85. * @apiUse result
  86. * @apiUse header
  87. * @apiVersion 1.0.0
  88. *
  89. * @apiParam {int} id 要删除的ID
  90. *
  91. */
  92. function delete()
  93. {
  94. DB::beginTransaction();
  95. try {
  96. request()->validate([
  97. 'id' => ['required', 'integer', 'min:1', 'max:99999999'],
  98. ]);
  99. $id = request()->input('id', 0);
  100. $info = BannerModel::where('id', $id)->first();
  101. if (!$info) throw new Exception("数据不存在", HttpStatus::CUSTOM_ERROR);
  102. $info->delete();
  103. DB::commit();
  104. } catch (ValidationException $e) {
  105. DB::rollBack();
  106. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  107. } catch (Exception $e) {
  108. DB::rollBack();
  109. return $this->error(intval($e->getCode()));
  110. }
  111. return $this->success();
  112. }
  113. }