ArtStyleController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Http\Controllers\Manage;
  3. use App\Libs\ApiResponse;
  4. use App\Libs\Utils;
  5. use App\Services\Manage\ArtStyleService;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Routing\Controller as BaseController;
  8. use Illuminate\Support\Facades\Validator;
  9. class ArtStyleController extends BaseController
  10. {
  11. use ApiResponse;
  12. protected $artStyleService;
  13. public function __construct(ArtStyleService $artStyleService)
  14. {
  15. $this->artStyleService = $artStyleService;
  16. }
  17. /**
  18. * 画风管理列表(分页)
  19. *
  20. * @param Request $request
  21. * @return mixed
  22. */
  23. public function adminList(Request $request)
  24. {
  25. $data = $request->all();
  26. $validator = Validator::make($data, [
  27. 'page' => 'nullable|integer|min:1',
  28. 'per_page' => 'nullable|integer|min:1|max:100',
  29. 'is_enabled' => 'nullable|integer|in:0,1',
  30. 'keyword' => 'nullable|string|max:100',
  31. ], [
  32. 'page.integer' => 'page必须为整数',
  33. 'page.min' => 'page不能小于1',
  34. 'per_page.integer' => 'per_page必须为整数',
  35. 'per_page.min' => 'per_page不能小于1',
  36. 'per_page.max' => 'per_page不能超过100',
  37. 'is_enabled.in' => 'is_enabled仅支持0或1',
  38. 'keyword.max' => 'keyword不能超过100个字符',
  39. ]);
  40. if ($validator->fails()) {
  41. Utils::throwError('1002:' . $validator->errors()->first());
  42. }
  43. $result = $this->artStyleService->getList($data);
  44. $list = collect($result->items())->map(function ($item) {
  45. // $item->aliases = is_string($item->aliases) ? json_decode($item->aliases, true) : [];
  46. return $item;
  47. });
  48. return $this->success([
  49. 'meta' => getMeta($result),
  50. 'list' => $list,
  51. ]);
  52. }
  53. /**
  54. * 新增画风
  55. *
  56. * @param Request $request
  57. * @return mixed
  58. */
  59. public function add(Request $request)
  60. {
  61. $data = $request->all();
  62. $validator = Validator::make($data, [
  63. 'art_style' => 'required|string|max:100',
  64. 'pic_url' => 'required|string|max:500',
  65. 'aliases' => 'nullable',
  66. 'prompt' => 'nullable|string',
  67. 'character_prefix' => 'nullable|string',
  68. 'scene_prefix' => 'nullable|string',
  69. 'is_enabled' => 'nullable|integer|in:0,1',
  70. ], [
  71. 'art_style.required' => '请传入画风名称',
  72. 'art_style.max' => '画风名称不能超过100个字符',
  73. 'pic_url.required' => '请传入画风图片URL',
  74. 'pic_url.max' => '画风图片URL不能超过500个字符',
  75. 'is_enabled.in' => 'is_enabled仅支持0或1',
  76. ]);
  77. if ($validator->fails()) {
  78. Utils::throwError('1002:' . $validator->errors()->first());
  79. }
  80. $result = $this->artStyleService->addArtStyle($data);
  81. return $this->success($result);
  82. }
  83. /**
  84. * 修改画风
  85. *
  86. * @param Request $request
  87. * @return mixed
  88. */
  89. public function edit(Request $request)
  90. {
  91. $data = $request->all();
  92. $validator = Validator::make($data, [
  93. 'id' => 'required|integer|min:1',
  94. 'pic_url' => 'required|string|max:500',
  95. 'art_style' => 'nullable|string|max:100',
  96. 'aliases' => 'nullable',
  97. 'prompt' => 'nullable|string',
  98. 'character_prefix' => 'nullable|string',
  99. 'scene_prefix' => 'nullable|string',
  100. 'is_enabled' => 'nullable|integer|in:0,1',
  101. ], [
  102. 'id.required' => '请传入画风ID',
  103. 'id.integer' => '画风ID必须为整数',
  104. 'id.min' => '画风ID不能小于1',
  105. 'pic_url.required' => '请传入画风图片URL',
  106. 'pic_url.max' => '画风图片URL不能超过500个字符',
  107. 'art_style.max' => '画风名称不能超过100个字符',
  108. 'is_enabled.in' => 'is_enabled仅支持0或1',
  109. ]);
  110. if ($validator->fails()) {
  111. Utils::throwError('1002:' . $validator->errors()->first());
  112. }
  113. $result = $this->artStyleService->editArtStyle($data);
  114. return $this->success($result);
  115. }
  116. /**
  117. * 删除画风(软删除)
  118. *
  119. * @param Request $request
  120. * @return mixed
  121. */
  122. public function delete(Request $request)
  123. {
  124. $data = $request->all();
  125. $validator = Validator::make($data, [
  126. 'id' => 'required|integer|min:1',
  127. ], [
  128. 'id.required' => '请传入画风ID',
  129. 'id.integer' => '画风ID必须为整数',
  130. 'id.min' => '画风ID不能小于1',
  131. ]);
  132. if ($validator->fails()) {
  133. Utils::throwError('1002:' . $validator->errors()->first());
  134. }
  135. $result = $this->artStyleService->deleteArtStyle($data);
  136. return $this->success(['success' => $result ? 1 : 0]);
  137. }
  138. }