Banner.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. use App\Services\BannerVisibilityService;
  11. class Banner extends Controller
  12. {
  13. /**
  14. * @api {get} /admin/banner/index 轮播图列表
  15. * @apiGroup 轮播图管理
  16. * @apiUse result
  17. * @apiUse header
  18. * @apiVersion 1.0.0
  19. *
  20. * @apiParam {int} [page=1]
  21. * @apiParam {int} [limit=10]
  22. *
  23. */
  24. function index()
  25. {
  26. try {
  27. request()->validate([
  28. 'page' => ['sometimes', 'integer', 'min:1'],
  29. 'limit' => ['sometimes', 'integer', 'min:1', 'max:200'],
  30. 'type' => ['nullable', 'in:pc,app'],
  31. ]);
  32. $page = request()->input('page', 1);
  33. $limit = request()->input('limit', 15);
  34. $type = request()->input('type', '');
  35. $query = BannerModel::query();
  36. if (!empty($type)) {
  37. $query->where('type', $type);
  38. }
  39. $count = $query->count();
  40. $list = BannerVisibilityService::decorate($query->forPage($page, $limit)
  41. ->orderByDesc('sort')->orderByDesc('id')->get());
  42. } catch (ValidationException $e) {
  43. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  44. } catch (Exception $e) {
  45. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  46. }
  47. return $this->success(['total' => $count, 'data' => $list]);
  48. }
  49. /**
  50. * @api {post} /admin/banner/update 修改
  51. * @apiDescription 更新或者新增都用这个接口
  52. * @apiGroup 轮播图管理
  53. * @apiUse result
  54. * @apiUse header
  55. * @apiVersion 1.0.0
  56. *
  57. */
  58. public function update()
  59. {
  60. DB::beginTransaction();
  61. try {
  62. $params = request()->validate([
  63. 'id' => ['nullable', 'integer', 'min:1'],
  64. 'title' => ['required'],
  65. 'image' => ['required'],
  66. 'link' => ['nullable', function ($attribute, $value, $fail) {
  67. if ((!is_string($value) && !is_int($value)) || strlen((string) $value) > 191) {
  68. $fail('跳转ID或链接格式错误');
  69. }
  70. }],
  71. 'type' => ['required', 'in:pc,app'],
  72. 'sort' => ['nullable', 'integer'],
  73. 'lang' => ['required'],
  74. 'status' => ['sometimes', 'integer', 'in:0,1'],
  75. ]);
  76. $id = $params['id'] ?? null;
  77. $banner = $id === null ? new BannerModel() : BannerModel::where('id', $id)->lockForUpdate()->first();
  78. if (!$banner) throw new Exception('数据不存在', HttpStatus::CUSTOM_ERROR);
  79. $link = array_key_exists('link', $params) ? $params['link'] : $banner->link;
  80. $activityId = BannerVisibilityService::activityId($link);
  81. $previousActivityId = BannerVisibilityService::activityId($banner->link);
  82. if ($activityId !== null && (!$banner->exists || $activityId !== $previousActivityId)) {
  83. if (!DB::table('activity_rewards')->where('id', $activityId)->exists()) {
  84. throw ValidationException::withMessages(['link' => '绑定的活动不存在']);
  85. }
  86. }
  87. $banner->fill([
  88. 'title' => $params['title'],
  89. 'image' => Util::replacePartInUrl($params['image']),
  90. 'link' => $activityId ?? ($link === null ? null : (string) $link),
  91. 'type' => $params['type'],
  92. 'sort' => array_key_exists('sort', $params) ? ($params['sort'] ?? 0) : ($banner->sort ?? 0),
  93. 'lang' => $params['lang'],
  94. 'status' => $params['status'] ?? ($banner->status ?? 1),
  95. ])->save();
  96. DB::commit();
  97. } catch (ValidationException $e) {
  98. DB::rollBack();
  99. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  100. } catch (Exception $e) {
  101. DB::rollBack();
  102. return $this->error(intval($e->getCode()), $e->getMessage());
  103. }
  104. return $this->success();
  105. }
  106. /** Change manual publication without resubmitting the banner form. */
  107. public function status()
  108. {
  109. try {
  110. $params = request()->validate([
  111. 'id' => ['required', 'integer', 'min:1'],
  112. 'status' => ['required', 'integer', 'in:0,1'],
  113. ]);
  114. $banner = BannerModel::find($params['id']);
  115. if (!$banner) throw new Exception('数据不存在', HttpStatus::CUSTOM_ERROR);
  116. $banner->status = $params['status'];
  117. $banner->save();
  118. } catch (ValidationException $e) {
  119. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  120. } catch (Exception $e) {
  121. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  122. }
  123. return $this->success();
  124. }
  125. /**
  126. * @api {post} /admin/banner/delete 删除
  127. * @apiGroup 轮播图管理
  128. * @apiUse result
  129. * @apiUse header
  130. * @apiVersion 1.0.0
  131. *
  132. * @apiParam {int} id 要删除的ID
  133. *
  134. */
  135. function delete()
  136. {
  137. DB::beginTransaction();
  138. try {
  139. request()->validate([
  140. 'id' => ['required', 'integer', 'min:1', 'max:99999999'],
  141. ]);
  142. $id = request()->input('id', 0);
  143. $info = BannerModel::where('id', $id)->first();
  144. if (!$info) throw new Exception("数据不存在", HttpStatus::CUSTOM_ERROR);
  145. $info->delete();
  146. DB::commit();
  147. } catch (ValidationException $e) {
  148. DB::rollBack();
  149. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  150. } catch (Exception $e) {
  151. DB::rollBack();
  152. return $this->error(intval($e->getCode()));
  153. }
  154. return $this->success();
  155. }
  156. }