Banner.php 3.8 KB

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