ArtStyleService.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace App\Services\Manage;
  3. use App\Facade\Site;
  4. use App\Libs\Utils;
  5. use Illuminate\Support\Facades\DB;
  6. class ArtStyleService
  7. {
  8. /**
  9. * 画风管理列表(分页)
  10. *
  11. * @param array $params
  12. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  13. */
  14. public function getList(array $params)
  15. {
  16. $this->assertSuperadmin();
  17. $perPage = (int)getProp($params, 'per_page', 15);
  18. if ($perPage < 1) {
  19. $perPage = 15;
  20. }
  21. $query = DB::table('mp_art_styles')->where('is_deleted', 0)->select('id', 'art_style', 'prompt', 'character_prefix', 'scene_prefix', 'pic_url', 'is_enabled', 'created_at');
  22. // 是否启用筛选
  23. if (array_key_exists('is_enabled', $params) && $params['is_enabled'] !== '' && $params['is_enabled'] !== null) {
  24. $query->where('is_enabled', (int)$params['is_enabled']);
  25. }
  26. // 画风名称模糊筛选
  27. $keyword = trim((string)getProp($params, 'art_style', ''));
  28. if ($keyword !== '') {
  29. $query->where('art_style', 'like', "%{$keyword}%");
  30. }
  31. return $query->orderByDesc('id')->paginate($perPage);
  32. }
  33. /**
  34. * 新增画风
  35. *
  36. * @param array $params
  37. * @return array
  38. */
  39. public function addArtStyle(array $params): array
  40. {
  41. $this->assertSuperadmin();
  42. $artStyle = trim((string)getProp($params, 'art_style', ''));
  43. $picUrl = trim((string)getProp($params, 'pic_url', ''));
  44. $isEnabled = (int)getProp($params, 'is_enabled', 1);
  45. if ($artStyle === '') {
  46. Utils::throwError('1002:请传入画风名称');
  47. }
  48. if ($picUrl === '') {
  49. Utils::throwError('1002:请传入画风图片URL');
  50. }
  51. if (DB::table('mp_art_styles')->where('art_style', $artStyle)->where('is_deleted', 0)->exists()) {
  52. Utils::throwError('1002:画风名称已存在');
  53. }
  54. $id = DB::table('mp_art_styles')->insertGetId([
  55. 'art_style' => $artStyle,
  56. 'aliases' => $this->normalizeAliases($params, $artStyle),
  57. 'prompt' => trim((string)getProp($params, 'prompt', '')),
  58. 'character_prefix'=> (string)getProp($params, 'character_prefix', ''),
  59. 'scene_prefix' => (string)getProp($params, 'scene_prefix', ''),
  60. 'pic_url' => $picUrl,
  61. 'is_enabled' => $isEnabled,
  62. 'is_deleted' => 0,
  63. 'created_at' => date('Y-m-d H:i:s'),
  64. 'updated_at' => date('Y-m-d H:i:s'),
  65. ]);
  66. $row = DB::table('mp_art_styles')->where('id', $id)->first();
  67. $result = $this->buildStyleResult($row);
  68. $result['id'] = $id;
  69. return $result;
  70. }
  71. /**
  72. * 修改画风
  73. *
  74. * @param array $params
  75. * @return array
  76. */
  77. public function editArtStyle(array $params): array
  78. {
  79. $this->assertSuperadmin();
  80. $id = (int)getProp($params, 'id', 0);
  81. if ($id < 1) {
  82. Utils::throwError('1002:请传入画风ID');
  83. }
  84. $row = DB::table('mp_art_styles')->where('id', $id)->where('is_deleted', 0)->first();
  85. if (!$row) {
  86. Utils::throwError('20003:画风不存在');
  87. }
  88. $update = [];
  89. $artStyle = trim((string)getProp($params, 'art_style', ''));
  90. if ($artStyle !== '') {
  91. if (DB::table('mp_art_styles')->where('art_style', $artStyle)->where('id', '<>', $id)->where('is_deleted', 0)->exists()) {
  92. Utils::throwError('1002:画风名称已存在');
  93. }
  94. $update['art_style'] = $artStyle;
  95. }
  96. // 图片URL必填
  97. $picUrl = trim((string)getProp($params, 'pic_url', ''));
  98. if ($picUrl === '') {
  99. Utils::throwError('1002:请传入画风图片URL');
  100. }
  101. $update['pic_url'] = $picUrl;
  102. $prompt = trim((string)getProp($params, 'prompt', ''));
  103. $update['prompt'] = $prompt;
  104. // aliases 不传时默认使用画风名(新画风名优先,未改则沿用原画风名)
  105. $defaultAliasStyle = $artStyle !== '' ? $artStyle : (string)$row->art_style;
  106. $update['aliases'] = $this->normalizeAliases($params, $defaultAliasStyle);
  107. if (array_key_exists('character_prefix', $params)) {
  108. $update['character_prefix'] = (string)$params['character_prefix'];
  109. }
  110. if (array_key_exists('scene_prefix', $params)) {
  111. $update['scene_prefix'] = (string)$params['scene_prefix'];
  112. }
  113. if (array_key_exists('is_enabled', $params) && $params['is_enabled'] !== '' && $params['is_enabled'] !== null) {
  114. $update['is_enabled'] = (int)$params['is_enabled'];
  115. }
  116. $update['updated_at'] = date('Y-m-d H:i:s');
  117. DB::table('mp_art_styles')->where('id', $id)->update($update);
  118. $row = DB::table('mp_art_styles')->where('id', $id)->first();
  119. return $this->buildStyleResult($row);
  120. }
  121. /**
  122. * 删除画风(软删除 is_deleted=1)
  123. *
  124. * @param array $params
  125. * @return array
  126. */
  127. public function deleteArtStyle(array $params)
  128. {
  129. $this->assertSuperadmin();
  130. $id = (int)getProp($params, 'id', 0);
  131. if ($id < 1) {
  132. Utils::throwError('1002:请传入画风ID');
  133. }
  134. $row = DB::table('mp_art_styles')->where('id', $id)->where('is_deleted', 0)->first();
  135. if (!$row) {
  136. Utils::throwError('20003:画风不存在');
  137. }
  138. return DB::table('mp_art_styles')->where('id', $id)->update([
  139. 'is_deleted' => 1,
  140. 'updated_at' => date('Y-m-d H:i:s'),
  141. ]);
  142. }
  143. /**
  144. * 规范化别名(支持数组或JSON字符串)
  145. *
  146. * @param array $params
  147. * @return string
  148. */
  149. private function normalizeAliases(array $params, string $defaultArtStyle = ''): string
  150. {
  151. // 未传 aliases(或为空)时,默认使用画风名本身作为别名
  152. if (!array_key_exists('aliases', $params) || $params['aliases'] === null || $params['aliases'] === '') {
  153. return json_encode([$defaultArtStyle], JSON_UNESCAPED_UNICODE);
  154. }
  155. $aliases = getProp($params, 'aliases', []);
  156. if (is_string($aliases)) {
  157. $aliases = json_decode($aliases, true) ?: [];
  158. }
  159. if (!is_array($aliases)) {
  160. $aliases = [];
  161. }
  162. $aliases = array_values(array_filter(array_map('trim', $aliases), function ($item) {
  163. return $item !== '';
  164. }));
  165. return json_encode($aliases, JSON_UNESCAPED_UNICODE);
  166. }
  167. /**
  168. * 仅超管可管理画风
  169. *
  170. * @return void
  171. */
  172. private function assertSuperadmin(): void
  173. {
  174. if (Site::getRole() !== 'superadmin') {
  175. Utils::throwError('1005:权限不足');
  176. }
  177. }
  178. /**
  179. * 构建画风返回信息(add/edit 成功后返回)
  180. *
  181. * @param object $row
  182. * @return array
  183. */
  184. private function buildStyleResult($row): array
  185. {
  186. return [
  187. 'id' => (int)$row->id,
  188. 'art_style' => (string)$row->art_style,
  189. 'prompt' => (string)$row->prompt,
  190. 'character_prefix' => (string)$row->character_prefix,
  191. 'scene_prefix' => (string)$row->scene_prefix,
  192. 'pic_url' => (string)$row->pic_url,
  193. 'is_enabled' => (int)$row->is_enabled,
  194. 'created_at' => transDate($row->created_at),
  195. ];
  196. }
  197. }