Banner.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. $type = request()->input('type', '');
  28. $query = new BannerModel();
  29. if (!empty($type)) {
  30. $query->where('type', $type);
  31. }
  32. $count = $query->count();
  33. $list = $query
  34. ->forPage($page, $limit)
  35. ->orderByDesc("sort")
  36. ->get()->toArray();
  37. } catch (ValidationException $e) {
  38. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  39. } catch (Exception $e) {
  40. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  41. }
  42. return $this->success(['total' => $count, 'data' => $list]);
  43. }
  44. /**
  45. * @api {post} /admin/banner/update 修改
  46. * @apiDescription 更新或者新增都用这个接口
  47. * @apiGroup 轮播图管理
  48. * @apiUse result
  49. * @apiUse header
  50. * @apiVersion 1.0.0
  51. *
  52. */
  53. public function update()
  54. {
  55. DB::beginTransaction();
  56. try {
  57. $params = request()->validate([
  58. 'id' => ['nullable'],
  59. 'title' => ['required'],
  60. 'image' => ['required'],
  61. 'link' => ['nullable'],
  62. 'type' => ['required'],
  63. 'sort' => ['nullable'],
  64. ]);
  65. $id = $params['id'] ?? null;
  66. BannerModel::updateOrCreate(
  67. ['id' => $id],
  68. [
  69. 'title' => $params['title'],
  70. 'image' => $params['image'],
  71. 'link' => $params['link'],
  72. 'type' => $params['type'],
  73. 'sort' => $params['sort']
  74. ],
  75. );
  76. DB::commit();
  77. } catch (ValidationException $e) {
  78. DB::rollBack();
  79. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  80. } catch (Exception $e) {
  81. DB::rollBack();
  82. return $this->error(intval($e->getCode()), $e->getMessage());
  83. }
  84. return $this->success();
  85. }
  86. /**
  87. * @api {post} /admin/banner/delete 删除
  88. * @apiGroup 轮播图管理
  89. * @apiUse result
  90. * @apiUse header
  91. * @apiVersion 1.0.0
  92. *
  93. * @apiParam {int} id 要删除的ID
  94. *
  95. */
  96. function delete()
  97. {
  98. DB::beginTransaction();
  99. try {
  100. request()->validate([
  101. 'id' => ['required', 'integer', 'min:1', 'max:99999999'],
  102. ]);
  103. $id = request()->input('id', 0);
  104. $info = BannerModel::where('id', $id)->first();
  105. if (!$info) throw new Exception("数据不存在", HttpStatus::CUSTOM_ERROR);
  106. $info->delete();
  107. DB::commit();
  108. } catch (ValidationException $e) {
  109. DB::rollBack();
  110. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  111. } catch (Exception $e) {
  112. DB::rollBack();
  113. return $this->error(intval($e->getCode()));
  114. }
  115. return $this->success();
  116. }
  117. }