|
|
@@ -601,7 +601,6 @@ export class MessageTemplateComponent implements OnInit, OnDestroy {
|
|
|
},
|
|
|
{ emitEvent: false }
|
|
|
);
|
|
|
-
|
|
|
this.loadTemplates();
|
|
|
}
|
|
|
);
|
|
|
@@ -613,12 +612,10 @@ export class MessageTemplateComponent implements OnInit, OnDestroy {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //替换原来的方法
|
|
|
getActionOptions(): { value: string; label: string }[] {
|
|
|
return this.messageService.getActionOptions();
|
|
|
}
|
|
|
|
|
|
- // 替换原来的方法
|
|
|
getParamPlaceholder(): string {
|
|
|
const action = this.templateForm.get('action')?.value as ActionType;
|
|
|
return this.messageService.getActionParamPlaceholder(action);
|
|
|
@@ -639,7 +636,6 @@ export class MessageTemplateComponent implements OnInit, OnDestroy {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- // 应用筛选条件
|
|
|
applyFilters(): void {
|
|
|
const filterValue = this.filterForm.value;
|
|
|
this.filteredTemplates = this.templates.filter((template) => {
|
|
|
@@ -648,29 +644,21 @@ export class MessageTemplateComponent implements OnInit, OnDestroy {
|
|
|
.toLowerCase()
|
|
|
.includes(filterValue.templateName.toLowerCase())
|
|
|
: true;
|
|
|
-
|
|
|
const typeMatch =
|
|
|
filterValue.templateType !== null
|
|
|
? template.templateType === filterValue.templateType
|
|
|
: true;
|
|
|
-
|
|
|
return nameMatch && typeMatch;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- // 更新URL参数
|
|
|
private updateUrl(): void {
|
|
|
const queryParams: any = {};
|
|
|
const filterValue = this.filterForm.value;
|
|
|
-
|
|
|
- if (filterValue.templateName) {
|
|
|
+ if (filterValue.templateName)
|
|
|
queryParams.templateName = filterValue.templateName;
|
|
|
- }
|
|
|
-
|
|
|
- if (filterValue.templateType !== null) {
|
|
|
+ if (filterValue.templateType !== null)
|
|
|
queryParams.templateType = filterValue.templateType;
|
|
|
- }
|
|
|
-
|
|
|
this.router.navigate([], {
|
|
|
relativeTo: this.route,
|
|
|
queryParams,
|
|
|
@@ -678,19 +666,12 @@ export class MessageTemplateComponent implements OnInit, OnDestroy {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- // 点击类型标签筛选
|
|
|
filterByType(type: TemplateType): void {
|
|
|
- this.filterForm.patchValue({
|
|
|
- templateType: type,
|
|
|
- });
|
|
|
+ this.filterForm.patchValue({ templateType: type });
|
|
|
}
|
|
|
|
|
|
- // 重置筛选条件
|
|
|
resetFilters(): void {
|
|
|
- this.filterForm.reset({
|
|
|
- templateName: '',
|
|
|
- templateType: null,
|
|
|
- });
|
|
|
+ this.filterForm.reset({ templateName: '', templateType: null });
|
|
|
}
|
|
|
|
|
|
getTemplateTypeName(type: TemplateType): string {
|
|
|
@@ -699,18 +680,16 @@ export class MessageTemplateComponent implements OnInit, OnDestroy {
|
|
|
|
|
|
showCreateModal(): void {
|
|
|
this.isEditMode = false;
|
|
|
- // 关键修复:确保新建时模板名称控件可编辑
|
|
|
const templateNameControl = this.templateForm.get('templateName');
|
|
|
- if (templateNameControl) {
|
|
|
- templateNameControl.enable();
|
|
|
- }
|
|
|
- // 重置表单默认值
|
|
|
+ if (templateNameControl) templateNameControl.enable();
|
|
|
+ // 重置表单并启用所有核心字段
|
|
|
this.templateForm.reset({
|
|
|
templateType: TemplateType.OTHER,
|
|
|
bigger: false,
|
|
|
+ selectedLanguages: [],
|
|
|
});
|
|
|
+ this.enableCoreFields(); // 确保核心字段启用
|
|
|
this.clearDynamicControls();
|
|
|
- // 设置默认语言
|
|
|
const defaultLanguages = ['en', 'zh-cn'];
|
|
|
this.templateForm.get('selectedLanguages')?.setValue(defaultLanguages);
|
|
|
this.onLanguageSelectionChange();
|
|
|
@@ -721,11 +700,15 @@ export class MessageTemplateComponent implements OnInit, OnDestroy {
|
|
|
this.isEditMode = true;
|
|
|
this.selectedTemplate = template;
|
|
|
|
|
|
+ // 1. 先清理动态控件,避免影响表单重置
|
|
|
+ this.clearDynamicControls();
|
|
|
+
|
|
|
+ // 2. 重置表单(显式禁用事件触发),并加载模板数据
|
|
|
this.templateForm.reset(
|
|
|
{
|
|
|
templateName: template.templateName,
|
|
|
templateType: template.templateType,
|
|
|
- description: template.description,
|
|
|
+ description: template.description || '', // 处理null值,避免表单值缺失
|
|
|
image: template.image || null,
|
|
|
bigger: template.bigger || false,
|
|
|
action: template.action || null,
|
|
|
@@ -736,27 +719,26 @@ export class MessageTemplateComponent implements OnInit, OnDestroy {
|
|
|
{ emitEvent: false }
|
|
|
);
|
|
|
|
|
|
- // 编辑时禁用模板名称
|
|
|
+ // 3. 启用所有核心字段(确保可编辑),仅禁用templateName
|
|
|
+ this.enableCoreFields();
|
|
|
const templateNameControl = this.templateForm.get('templateName');
|
|
|
- if (templateNameControl) {
|
|
|
- templateNameControl.disable();
|
|
|
- }
|
|
|
+ if (templateNameControl) templateNameControl.disable();
|
|
|
|
|
|
- this.clearDynamicControls();
|
|
|
-
|
|
|
- // 加载多语言内容
|
|
|
+ // 4. 加载多语言内容
|
|
|
Object.keys(template.messageTitle).forEach((lang) => {
|
|
|
+ const normalizedLang = lang.toLowerCase();
|
|
|
this.templateForm.addControl(
|
|
|
- `title_${lang}`,
|
|
|
+ `title_${normalizedLang}`,
|
|
|
new FormControl(template.messageTitle[lang], Validators.required)
|
|
|
);
|
|
|
this.templateForm.addControl(
|
|
|
- `content_${lang}`,
|
|
|
+ `content_${normalizedLang}`,
|
|
|
new FormControl(template.messageContent[lang], Validators.required)
|
|
|
);
|
|
|
});
|
|
|
|
|
|
- this.cdr.detectChanges();
|
|
|
+ // 强制更新视图,确保表单值同步
|
|
|
+ this.cdr.markForCheck();
|
|
|
this.isModalVisible = true;
|
|
|
}
|
|
|
|
|
|
@@ -766,20 +748,14 @@ export class MessageTemplateComponent implements OnInit, OnDestroy {
|
|
|
}
|
|
|
|
|
|
onLanguageSelectionChange(): void {
|
|
|
- // 统一语言代码为小写,避免大小写不一致导致的问题
|
|
|
const selectedLanguages = (
|
|
|
this.templateForm.get('selectedLanguages')?.value || []
|
|
|
).map((lang: string) => lang.toLowerCase());
|
|
|
const currentLanguages = this.getCurrentLanguageControls();
|
|
|
|
|
|
- // 添加新选择的语言控件
|
|
|
selectedLanguages.forEach((lang: string) => {
|
|
|
- if (!currentLanguages.includes(lang)) {
|
|
|
- this.addLanguageControls(lang);
|
|
|
- }
|
|
|
+ if (!currentLanguages.includes(lang)) this.addLanguageControls(lang);
|
|
|
});
|
|
|
-
|
|
|
- // 移除取消选择的语言控件
|
|
|
currentLanguages.forEach((lang) => {
|
|
|
if (!selectedLanguages.includes(lang)) {
|
|
|
this.templateForm.removeControl(`title_${lang}`);
|
|
|
@@ -798,10 +774,14 @@ export class MessageTemplateComponent implements OnInit, OnDestroy {
|
|
|
const formValue = this.templateForm.getRawValue();
|
|
|
const selectedLanguages = formValue.selectedLanguages;
|
|
|
|
|
|
+ // 核心修复:显式提取核心字段值,避免依赖getRawValue()的不确定性
|
|
|
const messageData = {
|
|
|
- templateName: formValue.templateName,
|
|
|
- templateType: formValue.templateType,
|
|
|
- description: formValue.description,
|
|
|
+ templateName: this.isEditMode
|
|
|
+ ? this.selectedTemplate!.templateName
|
|
|
+ : formValue.templateName,
|
|
|
+ templateType: this.templateForm.get('templateType')
|
|
|
+ ?.value as TemplateType, // 直接获取控件值
|
|
|
+ description: this.templateForm.get('description')?.value || '', // 处理null
|
|
|
image: formValue.image,
|
|
|
bigger: formValue.bigger,
|
|
|
action: formValue.action,
|
|
|
@@ -811,17 +791,18 @@ export class MessageTemplateComponent implements OnInit, OnDestroy {
|
|
|
messageContent: {} as { [key: string]: string },
|
|
|
};
|
|
|
|
|
|
+ // 处理多语言内容
|
|
|
selectedLanguages.forEach((lang: string) => {
|
|
|
const normalizedLang = lang.toLowerCase();
|
|
|
const titleControl = this.templateForm.get(`title_${normalizedLang}`);
|
|
|
const contentControl = this.templateForm.get(`content_${normalizedLang}`);
|
|
|
-
|
|
|
if (titleControl && contentControl) {
|
|
|
messageData.messageTitle[normalizedLang] = titleControl.value;
|
|
|
messageData.messageContent[normalizedLang] = contentControl.value;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
+ // 提交更新/创建请求
|
|
|
if (this.isEditMode && this.selectedTemplate) {
|
|
|
this.updateTemplate(messageData);
|
|
|
} else {
|
|
|
@@ -834,7 +815,7 @@ export class MessageTemplateComponent implements OnInit, OnDestroy {
|
|
|
next: () => {
|
|
|
this.message.success('模板创建成功');
|
|
|
this.loadTemplates();
|
|
|
- this.isModalVisible = false;
|
|
|
+ this.handleCancel();
|
|
|
this.isSubmitting = false;
|
|
|
},
|
|
|
error: (err) => {
|
|
|
@@ -845,7 +826,10 @@ export class MessageTemplateComponent implements OnInit, OnDestroy {
|
|
|
}
|
|
|
|
|
|
updateTemplate(templateData: any): void {
|
|
|
- if (!this.selectedTemplate) return;
|
|
|
+ if (!this.selectedTemplate) {
|
|
|
+ this.isSubmitting = false;
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
this.messageService
|
|
|
.updateTemplate(this.selectedTemplate.templateName, templateData)
|
|
|
@@ -853,7 +837,7 @@ export class MessageTemplateComponent implements OnInit, OnDestroy {
|
|
|
next: () => {
|
|
|
this.message.success('模板更新成功');
|
|
|
this.loadTemplates();
|
|
|
- this.isModalVisible = false;
|
|
|
+ this.handleCancel();
|
|
|
this.isSubmitting = false;
|
|
|
},
|
|
|
error: (err) => {
|
|
|
@@ -886,16 +870,30 @@ export class MessageTemplateComponent implements OnInit, OnDestroy {
|
|
|
handleCancel(): void {
|
|
|
this.isModalVisible = false;
|
|
|
this.clearDynamicControls();
|
|
|
- // 重置表单状态,避免下次打开时显示错误提示
|
|
|
+ // 重置表单状态并启用所有字段,避免影响下次打开
|
|
|
Object.values(this.templateForm.controls).forEach((control) => {
|
|
|
control.markAsPristine();
|
|
|
control.markAsUntouched();
|
|
|
+ if (control.disabled) control.enable();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 新增:启用核心字段(确保编辑时可提交)
|
|
|
+ private enableCoreFields(): void {
|
|
|
+ const coreFields = [
|
|
|
+ 'templateType',
|
|
|
+ 'description',
|
|
|
+ 'image',
|
|
|
+ 'bigger',
|
|
|
+ 'action',
|
|
|
+ 'param',
|
|
|
+ 'extend',
|
|
|
+ 'selectedLanguages',
|
|
|
+ ];
|
|
|
+ coreFields.forEach((field) => {
|
|
|
+ const control = this.templateForm.get(field);
|
|
|
+ if (control && control.disabled) control.enable();
|
|
|
});
|
|
|
- // 确保下次打开新建时模板名称可编辑
|
|
|
- const templateNameControl = this.templateForm.get('templateName');
|
|
|
- if (templateNameControl) {
|
|
|
- templateNameControl.enable();
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
private addLanguageControls(lang: string): void {
|
|
|
@@ -939,17 +937,13 @@ export class MessageTemplateComponent implements OnInit, OnDestroy {
|
|
|
}
|
|
|
|
|
|
private handleApiError(err: any, action: string): void {
|
|
|
- if (err.status === 409) {
|
|
|
- this.message.error('模板名称已存在');
|
|
|
- } else if (err.status === 404) {
|
|
|
- this.message.error('未找到指定的模板');
|
|
|
- } else if (err.status === 400) {
|
|
|
+ if (err.status === 409) this.message.error('模板名称已存在');
|
|
|
+ else if (err.status === 404) this.message.error('未找到指定的模板');
|
|
|
+ else if (err.status === 400)
|
|
|
this.message.error(
|
|
|
'验证错误: ' + (err.error?.message || '请检查输入数据')
|
|
|
);
|
|
|
- } else {
|
|
|
- this.message.error(`${action}模板失败: ${err.message}`);
|
|
|
- }
|
|
|
+ else this.message.error(`${action}模板失败: ${err.message}`);
|
|
|
}
|
|
|
|
|
|
getLanguageName(code: string): string {
|