ArtStyleService.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 (trim((string)getProp($params, 'prompt', '')) === '') {
  52. Utils::throwError('1002:请传入画风提示词prompt');
  53. }
  54. if (DB::table('mp_art_styles')->where('art_style', $artStyle)->where('is_deleted', 0)->exists()) {
  55. Utils::throwError('1002:画风名称已存在');
  56. }
  57. $id = DB::table('mp_art_styles')->insertGetId([
  58. 'art_style' => $artStyle,
  59. 'aliases' => $this->normalizeAliases($params, $artStyle),
  60. 'prompt' => trim((string)getProp($params, 'prompt', '')),
  61. 'character_prefix'=> (string)getProp($params, 'character_prefix', ''),
  62. 'scene_prefix' => (string)getProp($params, 'scene_prefix', ''),
  63. 'pic_url' => $picUrl,
  64. 'is_enabled' => $isEnabled,
  65. 'is_deleted' => 0,
  66. 'created_at' => date('Y-m-d H:i:s'),
  67. 'updated_at' => date('Y-m-d H:i:s'),
  68. ]);
  69. $row = DB::table('mp_art_styles')->where('id', $id)->first();
  70. $result = $this->buildStyleResult($row);
  71. $result['id'] = $id;
  72. return $result;
  73. }
  74. /**
  75. * 修改画风
  76. *
  77. * @param array $params
  78. * @return array
  79. */
  80. public function editArtStyle(array $params): array
  81. {
  82. $this->assertSuperadmin();
  83. $id = (int)getProp($params, 'id', 0);
  84. if ($id < 1) {
  85. Utils::throwError('1002:请传入画风ID');
  86. }
  87. $row = DB::table('mp_art_styles')->where('id', $id)->where('is_deleted', 0)->first();
  88. if (!$row) {
  89. Utils::throwError('20003:画风不存在');
  90. }
  91. $update = [];
  92. $artStyle = trim((string)getProp($params, 'art_style', ''));
  93. if ($artStyle !== '') {
  94. if (DB::table('mp_art_styles')->where('art_style', $artStyle)->where('id', '<>', $id)->where('is_deleted', 0)->exists()) {
  95. Utils::throwError('1002:画风名称已存在');
  96. }
  97. $update['art_style'] = $artStyle;
  98. }
  99. // 图片URL必填
  100. $picUrl = trim((string)getProp($params, 'pic_url', ''));
  101. if ($picUrl === '') {
  102. Utils::throwError('1002:请传入画风图片URL');
  103. }
  104. $update['pic_url'] = $picUrl;
  105. // prompt 必填(非空)
  106. $prompt = trim((string)getProp($params, 'prompt', ''));
  107. if ($prompt === '') {
  108. Utils::throwError('1002:请传入画风提示词prompt');
  109. }
  110. $update['prompt'] = $prompt;
  111. // aliases 不传时默认使用画风名(新画风名优先,未改则沿用原画风名)
  112. $defaultAliasStyle = $artStyle !== '' ? $artStyle : (string)$row->art_style;
  113. $update['aliases'] = $this->normalizeAliases($params, $defaultAliasStyle);
  114. if (array_key_exists('character_prefix', $params)) {
  115. $update['character_prefix'] = (string)$params['character_prefix'];
  116. }
  117. if (array_key_exists('scene_prefix', $params)) {
  118. $update['scene_prefix'] = (string)$params['scene_prefix'];
  119. }
  120. if (array_key_exists('is_enabled', $params) && $params['is_enabled'] !== '' && $params['is_enabled'] !== null) {
  121. $update['is_enabled'] = (int)$params['is_enabled'];
  122. }
  123. $update['updated_at'] = date('Y-m-d H:i:s');
  124. DB::table('mp_art_styles')->where('id', $id)->update($update);
  125. $row = DB::table('mp_art_styles')->where('id', $id)->first();
  126. return $this->buildStyleResult($row);
  127. }
  128. /**
  129. * 删除画风(软删除 is_deleted=1)
  130. *
  131. * @param array $params
  132. * @return array
  133. */
  134. public function deleteArtStyle(array $params)
  135. {
  136. $this->assertSuperadmin();
  137. $id = (int)getProp($params, 'id', 0);
  138. if ($id < 1) {
  139. Utils::throwError('1002:请传入画风ID');
  140. }
  141. $row = DB::table('mp_art_styles')->where('id', $id)->where('is_deleted', 0)->first();
  142. if (!$row) {
  143. Utils::throwError('20003:画风不存在');
  144. }
  145. return DB::table('mp_art_styles')->where('id', $id)->update([
  146. 'is_deleted' => 1,
  147. 'updated_at' => date('Y-m-d H:i:s'),
  148. ]);
  149. }
  150. /**
  151. * 规范化别名(支持数组或JSON字符串)
  152. *
  153. * @param array $params
  154. * @return string
  155. */
  156. private function normalizeAliases(array $params, string $defaultArtStyle = ''): string
  157. {
  158. // 未传 aliases(或为空)时,默认使用画风名本身作为别名
  159. if (!array_key_exists('aliases', $params) || $params['aliases'] === null || $params['aliases'] === '') {
  160. return json_encode([$defaultArtStyle], JSON_UNESCAPED_UNICODE);
  161. }
  162. $aliases = getProp($params, 'aliases', []);
  163. if (is_string($aliases)) {
  164. $aliases = json_decode($aliases, true) ?: [];
  165. }
  166. if (!is_array($aliases)) {
  167. $aliases = [];
  168. }
  169. $aliases = array_values(array_filter(array_map('trim', $aliases), function ($item) {
  170. return $item !== '';
  171. }));
  172. return json_encode($aliases, JSON_UNESCAPED_UNICODE);
  173. }
  174. /**
  175. * 仅超管可管理画风
  176. *
  177. * @return void
  178. */
  179. private function assertSuperadmin(): void
  180. {
  181. if (Site::getRole() !== 'superadmin') {
  182. Utils::throwError('1005:权限不足');
  183. }
  184. }
  185. /**
  186. * 构建画风返回信息(add/edit 成功后返回)
  187. *
  188. * @param object $row
  189. * @return array
  190. */
  191. private function buildStyleResult($row): array
  192. {
  193. return [
  194. 'id' => (int)$row->id,
  195. 'art_style' => (string)$row->art_style,
  196. 'prompt' => (string)$row->prompt,
  197. 'character_prefix' => (string)$row->character_prefix,
  198. 'scene_prefix' => (string)$row->scene_prefix,
  199. 'pic_url' => (string)$row->pic_url,
  200. 'is_enabled' => (int)$row->is_enabled,
  201. 'created_at' => transDate($row->created_at),
  202. ];
  203. }
  204. }