| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace App\Services\Manage;
- use App\Facade\Site;
- use App\Libs\Utils;
- use Illuminate\Support\Facades\DB;
- class ArtStyleService
- {
- /**
- * 画风管理列表(分页)
- *
- * @param array $params
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
- */
- public function getList(array $params)
- {
- $this->assertSuperadmin();
- $perPage = (int)getProp($params, 'per_page', 15);
- if ($perPage < 1) {
- $perPage = 15;
- }
- $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');
- // 是否启用筛选
- if (array_key_exists('is_enabled', $params) && $params['is_enabled'] !== '' && $params['is_enabled'] !== null) {
- $query->where('is_enabled', (int)$params['is_enabled']);
- }
- // 画风名称模糊筛选
- $keyword = trim((string)getProp($params, 'art_style', ''));
- if ($keyword !== '') {
- $query->where('art_style', 'like', "%{$keyword}%");
- }
- return $query->orderByDesc('id')->paginate($perPage);
- }
- /**
- * 新增画风
- *
- * @param array $params
- * @return array
- */
- public function addArtStyle(array $params): array
- {
- $this->assertSuperadmin();
- $artStyle = trim((string)getProp($params, 'art_style', ''));
- $picUrl = trim((string)getProp($params, 'pic_url', ''));
- $isEnabled = (int)getProp($params, 'is_enabled', 1);
- if ($artStyle === '') {
- Utils::throwError('1002:请传入画风名称');
- }
- if ($picUrl === '') {
- Utils::throwError('1002:请传入画风图片URL');
- }
- if (trim((string)getProp($params, 'prompt', '')) === '') {
- Utils::throwError('1002:请传入画风提示词prompt');
- }
- if (DB::table('mp_art_styles')->where('art_style', $artStyle)->where('is_deleted', 0)->exists()) {
- Utils::throwError('1002:画风名称已存在');
- }
- $id = DB::table('mp_art_styles')->insertGetId([
- 'art_style' => $artStyle,
- 'aliases' => $this->normalizeAliases($params, $artStyle),
- 'prompt' => trim((string)getProp($params, 'prompt', '')),
- 'character_prefix'=> (string)getProp($params, 'character_prefix', ''),
- 'scene_prefix' => (string)getProp($params, 'scene_prefix', ''),
- 'pic_url' => $picUrl,
- 'is_enabled' => $isEnabled,
- 'is_deleted' => 0,
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s'),
- ]);
- $row = DB::table('mp_art_styles')->where('id', $id)->first();
- $result = $this->buildStyleResult($row);
- $result['id'] = $id;
- return $result;
- }
- /**
- * 修改画风
- *
- * @param array $params
- * @return array
- */
- public function editArtStyle(array $params): array
- {
- $this->assertSuperadmin();
- $id = (int)getProp($params, 'id', 0);
- if ($id < 1) {
- Utils::throwError('1002:请传入画风ID');
- }
- $row = DB::table('mp_art_styles')->where('id', $id)->where('is_deleted', 0)->first();
- if (!$row) {
- Utils::throwError('20003:画风不存在');
- }
- $update = [];
- $artStyle = trim((string)getProp($params, 'art_style', ''));
- if ($artStyle !== '') {
- if (DB::table('mp_art_styles')->where('art_style', $artStyle)->where('id', '<>', $id)->where('is_deleted', 0)->exists()) {
- Utils::throwError('1002:画风名称已存在');
- }
- $update['art_style'] = $artStyle;
- }
- // 图片URL必填
- $picUrl = trim((string)getProp($params, 'pic_url', ''));
- if ($picUrl === '') {
- Utils::throwError('1002:请传入画风图片URL');
- }
- $update['pic_url'] = $picUrl;
- // prompt 必填(非空)
- $prompt = trim((string)getProp($params, 'prompt', ''));
- if ($prompt === '') {
- Utils::throwError('1002:请传入画风提示词prompt');
- }
- $update['prompt'] = $prompt;
- // aliases 不传时默认使用画风名(新画风名优先,未改则沿用原画风名)
- $defaultAliasStyle = $artStyle !== '' ? $artStyle : (string)$row->art_style;
- $update['aliases'] = $this->normalizeAliases($params, $defaultAliasStyle);
- if (array_key_exists('character_prefix', $params)) {
- $update['character_prefix'] = (string)$params['character_prefix'];
- }
- if (array_key_exists('scene_prefix', $params)) {
- $update['scene_prefix'] = (string)$params['scene_prefix'];
- }
- if (array_key_exists('is_enabled', $params) && $params['is_enabled'] !== '' && $params['is_enabled'] !== null) {
- $update['is_enabled'] = (int)$params['is_enabled'];
- }
- $update['updated_at'] = date('Y-m-d H:i:s');
- DB::table('mp_art_styles')->where('id', $id)->update($update);
- $row = DB::table('mp_art_styles')->where('id', $id)->first();
- return $this->buildStyleResult($row);
- }
- /**
- * 删除画风(软删除 is_deleted=1)
- *
- * @param array $params
- * @return array
- */
- public function deleteArtStyle(array $params)
- {
- $this->assertSuperadmin();
- $id = (int)getProp($params, 'id', 0);
- if ($id < 1) {
- Utils::throwError('1002:请传入画风ID');
- }
- $row = DB::table('mp_art_styles')->where('id', $id)->where('is_deleted', 0)->first();
- if (!$row) {
- Utils::throwError('20003:画风不存在');
- }
- return DB::table('mp_art_styles')->where('id', $id)->update([
- 'is_deleted' => 1,
- 'updated_at' => date('Y-m-d H:i:s'),
- ]);
-
- }
- /**
- * 规范化别名(支持数组或JSON字符串)
- *
- * @param array $params
- * @return string
- */
- private function normalizeAliases(array $params, string $defaultArtStyle = ''): string
- {
- // 未传 aliases(或为空)时,默认使用画风名本身作为别名
- if (!array_key_exists('aliases', $params) || $params['aliases'] === null || $params['aliases'] === '') {
- return json_encode([$defaultArtStyle], JSON_UNESCAPED_UNICODE);
- }
- $aliases = getProp($params, 'aliases', []);
- if (is_string($aliases)) {
- $aliases = json_decode($aliases, true) ?: [];
- }
- if (!is_array($aliases)) {
- $aliases = [];
- }
- $aliases = array_values(array_filter(array_map('trim', $aliases), function ($item) {
- return $item !== '';
- }));
- return json_encode($aliases, JSON_UNESCAPED_UNICODE);
- }
- /**
- * 仅超管可管理画风
- *
- * @return void
- */
- private function assertSuperadmin(): void
- {
- if (Site::getRole() !== 'superadmin') {
- Utils::throwError('1005:权限不足');
- }
- }
- /**
- * 构建画风返回信息(add/edit 成功后返回)
- *
- * @param object $row
- * @return array
- */
- private function buildStyleResult($row): array
- {
- return [
- 'id' => (int)$row->id,
- 'art_style' => (string)$row->art_style,
- 'prompt' => (string)$row->prompt,
- 'character_prefix' => (string)$row->character_prefix,
- 'scene_prefix' => (string)$row->scene_prefix,
- 'pic_url' => (string)$row->pic_url,
- 'is_enabled' => (int)$row->is_enabled,
- 'created_at' => transDate($row->created_at),
- ];
- }
- }
|