diff --git a/framework/sdk-parent/frontend/src/components/form/CustomFiledFormItem.vue b/framework/sdk-parent/frontend/src/components/form/CustomFiledFormItem.vue index 335146c785..2c4101ce36 100644 --- a/framework/sdk-parent/frontend/src/components/form/CustomFiledFormItem.vue +++ b/framework/sdk-parent/frontend/src/components/form/CustomFiledFormItem.vue @@ -4,7 +4,7 @@ - @@ -21,12 +22,13 @@ @@ -59,7 +61,11 @@ export default { type: Boolean, default() { return false; - } + }, + }, + formProp: { + type: String, + default: 'name' }, defaultOpen: { type: String, diff --git a/framework/sdk-parent/frontend/src/components/template/CustomFiledComponent.vue b/framework/sdk-parent/frontend/src/components/template/CustomFiledComponent.vue index 4308d032d2..1c3c43c97d 100644 --- a/framework/sdk-parent/frontend/src/components/template/CustomFiledComponent.vue +++ b/framework/sdk-parent/frontend/src/components/template/CustomFiledComponent.vue @@ -161,14 +161,18 @@ import {OPTION_LABEL_PREFIX} from "../../utils/tableUtils"; export default { name: "CustomFiledComponent", components: {MsMarkDownText, MsInputTag, MsTableColumn}, - props: [ - 'data', - 'prop', - 'form', - 'disabled', - 'defaultOpen', - 'isTemplateEdit' - ], + props: { + data: Object, + prop: String, + form: Object, + disabled: Boolean, + defaultOpen: String, + isTemplateEdit: Boolean, + formProp: { + type: String, + default: 'name' + } + }, data() { return { memberOptions: [], @@ -223,10 +227,10 @@ export default { return item.system ? this.$t(item.text) : item.text; }, handleChange() { - if (this.form) { - this.$set(this.form, this.data.name, this.data[this.prop]); - } - this.$emit('change', this.data.name); + + this.setFormData(); + + this.$emit('change', this.data[this.formProp]); this.$forceUpdate(); if (this.data.inputSearch) { // 将选项的选项名保存在 optionLabel 中 @@ -285,7 +289,7 @@ export default { }, setFormData() { if (this.form && this.data && this.data[this.prop]) { - this.$set(this.form, this.data.name, this.data[this.prop]); + this.$set(this.form, this.data[this.formProp], this.data[this.prop]); } } } diff --git a/framework/sdk-parent/frontend/src/utils/custom_field.js b/framework/sdk-parent/frontend/src/utils/custom_field.js index c7b16b09f9..7bddf9aa0f 100644 --- a/framework/sdk-parent/frontend/src/utils/custom_field.js +++ b/framework/sdk-parent/frontend/src/utils/custom_field.js @@ -111,6 +111,111 @@ export function parseCustomField(data, template, rules, oldFields) { return customFieldForm; } +/** + * + * 原本的 parseCustomField form 的 key 是字段名,这里改成 ID,jira 存在相同字段名的情况 + * 设置默认值,添加自定义校验 + * @param data 原表单值 + * @param template 模板 + * @param rules 自定义表单的校验规则 + * @param oldFields 用于兼容旧版本数据 + */ +export function parseCustomFieldForId(data, template, rules, oldFields) { + let hasOldData = false; + if (!data.fields) { + // 旧数据 + hasOldData = true; + data.fields = []; + } + + let customFieldForm = {}; + + // 设置页面显示的默认值 + template.customFields.forEach((item) => { + if (item.defaultValue && !item.hasParse) { + let val = item.defaultValue; + try { + val = JSON.parse(item.defaultValue); + } catch (e) { + // + } + if ( + item.name === "责任人" && + item.system && + val && + val === "CURRENT_USER" + ) { + val = ''; + const {id, userGroups} = getCurrentUser(); + if (userGroups) { + // CURRENT_USER是否是当前项目下的成员 + let index = userGroups.findIndex(ug => ug.sourceId === getCurrentProjectID()); + if (index !== -1) { + val = id; + } + } + } + setDefaultValue(item, val); + } + + // 添加自定义字段必填校验 + if (item.required) { + let msg = + (item.system ? i18n.t(SYSTEM_FIELD_NAME_MAP[item.name]) : item.name) + + i18n.t("commons.cannot_be_null"); + if (rules) { + rules[item.id] = [{ required: true, message: msg, trigger: "blur" }]; + } + } + + if (hasOldData && oldFields) { + // 兼容旧数据 + for (const key of oldFields.keys()) { + if (item.name === key) { + if (oldFields.get(key)) { + setDefaultValue(item, oldFields.get(key)); + } + } + } + } + + // 将保存的值赋值给template + if (data.fields instanceof Array) { + for (let i = 0; i < data.fields.length; i++) { + let customField = data.fields[i]; + if (customField.id === item.id) { + try { + if (item.type === "textarea" || item.type === "richText") { + setDefaultValue(item, customField.textValue); + } else { + setDefaultValue(item, customField.value); + } + parseCustomFieldOptionLabel(customField, item); + item.isEdit = true; + } catch (e) { + console.error("JSON parse custom field value error.", e); + } + break; + } + } + } else if (data.fields instanceof Object) { + // todo + // 兼容旧的存储方式 + for (const key in data.fields) { + if (item.name === key) { + if (data.fields[key]) { + setDefaultValue(item, JSON.parse(data.fields[key])); + } + } + } + } + + customFieldForm[item.id] = item.defaultValue; + }); + + return customFieldForm; +} + export function isMultipleField(type) { if (type.startsWith('multiple') || type === 'checkbox') { return true; diff --git a/test-track/frontend/src/business/issue/IssueEditDetail.vue b/test-track/frontend/src/business/issue/IssueEditDetail.vue index 4ce3f0a6c9..732f588f64 100644 --- a/test-track/frontend/src/business/issue/IssueEditDetail.vue +++ b/test-track/frontend/src/business/issue/IssueEditDetail.vue @@ -43,6 +43,7 @@ :default-open="richTextDefaultOpen" :form-label-width="formLabelWidth" :issue-template="issueTemplate" + form-prop="id" @inputSearch="handleInputSearch" ref="customFieldItem" /> @@ -188,7 +189,7 @@ import TemplateComponentEditHeader from "@/business/plan/view/comonents/report/TemplateComponentEditHeader"; import MsFormDivider from "metersphere-frontend/src/components/MsFormDivider"; import FormRichTextItem from "metersphere-frontend/src/components/FormRichTextItem"; -import {buildCustomFields, parseCustomField} from "metersphere-frontend/src/utils/custom_field"; +import {buildCustomFields, parseCustomField, parseCustomFieldForId} from "metersphere-frontend/src/utils/custom_field"; import CustomFiledComponent from "metersphere-frontend/src/components/template/CustomFiledComponent"; import TestCaseIssueList from "@/business/issue/TestCaseIssueList"; import IssueEditDetail from "@/business/issue/IssueEditDetail"; @@ -495,7 +496,7 @@ export default { this.form.creator = getCurrentUserId(); } } - this.customFieldForm = parseCustomField(this.form, this.issueTemplate, this.customFieldRules); + this.customFieldForm = parseCustomFieldForId(this.form, this.issueTemplate, this.customFieldRules); this.comments = []; this.$nextTick(() => { if (this.$refs.testCaseIssueList) {