This commit is contained in:
670788339
2025-11-04 14:48:10 +08:00
parent bbafde31b5
commit 2fd3ac0de2

View File

@@ -26,12 +26,6 @@
:disabled="isTagDisabled(item)"
/>
</el-select>
<div style="margin-top: 10px; font-size: 12px; color: #666;">
<div>调试信息 - selectedTags:</div>
<div v-for="(tag, index) in selectedTags" :key="tag.id">
{{ index }}: {{ tag.tagName }} ({{ tag.id }})
</div>
</div>
<!-- 添加标签计数显示 -->
<div class="tag-count">
{{ selectedTags.length }}/5
@@ -75,27 +69,15 @@ export default {
params: {},
tag: {},
// 添加临时存储用于回滚
previousTags: [],
allTags: []
previousTags: []
}
},
computed: {
...mapGetters(['userInfo']),
displayTags() {
const tags = this.selectedTags.map(tag => {
if (typeof tag === 'object' && tag.tagName) {
return tag; // 已经是完整对象
} else if (typeof tag === 'object' && tag.id) {
// 有id但可能缺少tagName从tagMap获取
return this.tagMap.get(tag.id) || tag;
} else {
// 其他情况尝试从tagMap获取
return this.tagMap.get(tag) || tag;
}
}).filter(Boolean);
console.log('displayTags计算结果:', tags);
return tags;
return this.selectedTags.map(tag =>
typeof tag === 'object' ? tag : this.tagMap.get(tag)
).filter(Boolean)
}
},
created() {
@@ -106,19 +88,14 @@ export default {
// 添加:挂载时初始化标签数据
mounted() {
if (this.initialTags && this.initialTags.length > 0) {
// 验证初始标签数据
this.selectedTags = this.initialTags.filter(tag =>
tag && tag.id && tag.tagName
);
this.searchResults = this.selectedTags;
this.selectedTags.forEach(tag => {
this.selectedTags = this.initialTags;
this.searchResults = this.initialTags;
// 将初始标签添加到tagMap中确保删除功能正常
this.initialTags.forEach(tag => {
if (tag.id) {
this.tagMap.set(tag.id, tag);
}
});
console.log('初始化后的标签数据:', this.selectedTags);
}
},
watch: {
@@ -139,14 +116,8 @@ export default {
sysTypeList: {
handler() {
// 只有在已选择分类且有焦点时才重新加载
// if (this.sysTypeList.length > 0 && this.$refs.tagSelect && this.$refs.tagSelect.visible) {
// this.doSearch('');
// }
if (this.sysTypeList.length > 0) {
this.shouldReloadAllTags = true; // 设置重新加载标志
if (this.$refs.tagSelect && this.$refs.tagSelect.visible) {
this.doSearch(''); // 触发重新加载
}
if (this.sysTypeList.length > 0 && this.$refs.tagSelect && this.$refs.tagSelect.visible) {
this.doSearch('');
}
},
deep: true
@@ -197,7 +168,7 @@ export default {
},
//按回车键,创建新标签
async handleEnterKey(event) {
handleEnterKey(event) {
const inputVal = event.target.value?.trim()
if (!inputVal) return;
// 检查是否与已选择的标签重复
@@ -208,7 +179,7 @@ export default {
return;
}
if (!isDuplicate && inputVal && this.selectedTags.length < this.maxTags) {
await this.createNewTag(inputVal)
this.createNewTag(event.target.value.trim())
this.clearInput();
} else if (this.selectedTags.length >= this.maxTags) {
this.$message.warning('最多只能添加5个标签')
@@ -220,7 +191,6 @@ export default {
// 新增:处理选择变化事件
handleSelectionChange(newValues) {
console.log("----------handleSelectionChange---------->", newValues);
// 检查每个标签对象是否完整
newValues.forEach((tag, index) => {
@@ -248,8 +218,7 @@ export default {
if (this.$refs.tagSelect) {
const input = this.$refs.tagSelect.$refs.input;
if (input) {
console.log("----------调用了注释的清除文字方法 clearInput---------->",input.value)
// input.value = '';
input.value = '';
}
}
},
@@ -292,33 +261,14 @@ export default {
if (this.sysTypeList.length > 2) {
this.params.sysType3 = this.sysTypeList[2]; //三级的id
}
console.log('添加前的selectedTags:', this.selectedTags);
console.log('添加前的searchResults:', this.searchResults);
const {result:newTag} = await apiCourseTag.createTag(this.params)
console.log('API返回的新标签对象:', newTag);
console.log('标签ID:', newTag?.id);
console.log('标签名称:', newTag?.tagName);
console.log('标签完整结构:', JSON.stringify(newTag));
this.$message.success('标签创建成功',newTag);
this.selectedTags = [...this.selectedTags, newTag];
// 更新搜索结果的逻辑保持不变
this.searchResults = [newTag, ...this.searchResults];
this.tagMap.set(newTag.id, newTag);
console.log('添加后的selectedTags:', this.selectedTags);
// 触发change事件
this.$emit('change', this.displayTags);
// 强制更新el-select组件
this.$nextTick(() => {
if (this.$refs.tagSelect) {
// 强制重新渲染选择器
this.$refs.tagSelect.$forceUpdate();
}
});
this.tagMap.set(newTag.id, newTag)
this.$emit('change', this.displayTags)
this.$nextTick(() => {
// 强制重新设置selectedTags来触发更新
@@ -328,45 +278,38 @@ export default {
this.selectedTags = tempTags;
});
});
} finally {
this.loading = false
}
},
// 修改doSearch方法添加搜索结果为空时的提示
async doSearch(query) {
// 如果是初始化query为空且allTags为空或者分类变更后
if ((!query && this.allTags.length === 0) || this.shouldReloadAllTags) {
this.loading = true;
try {
const typeId = this.sysTypeList.length > 2 ? this.sysTypeList[2] :
this.sysTypeList.length > 1 ? this.sysTypeList[1] :
this.sysTypeList.length > 0 ? this.sysTypeList[0] : null;
// 不再在空查询时清空搜索结果
// if (!query.trim()) {
// this.searchResults = []
// return
// }
console.log("---- doSearch ------ query = " + query )
this.loading = true
try {
// 获取 typeId取 sysTypeList 最后一个有效的值
const typeId = this.sysTypeList.length > 2 ? this.sysTypeList[2] :
this.sysTypeList.length > 1 ? this.sysTypeList[1] :
this.sysTypeList.length > 0 ? this.sysTypeList[0] : null;
console.log("---- doSearch searchTags ------ query = " + query + " , typeId = " + typeId )
const {result:tags} = await apiCourseTag.searchTags({tagName:query,typeId:typeId})
console.log("-- searchTags 查询结果 tags = " + tags )
const { result: tags } = await apiCourseTag.searchTags({ tagName: '', typeId: typeId });
tags.forEach(item => {
this.tagMap.set(item.id, item);
});
this.allTags = tags; // 保存所有标签
this.searchResults = tags; // 初始显示所有标签
this.shouldReloadAllTags = false; // 重置标志位
} finally {
this.loading = false;
}
} else {
// 本地过滤
const filteredTags = this.allTags.filter(tag =>
tag.tagName.toLowerCase().includes(query.toLowerCase())
);
this.searchResults = filteredTags;
// 当本地过滤结果为空时提示创建
if (filteredTags.length === 0 && query) {
this.$message.info('无此标签,按回车键创建');
tags.forEach(item => {
this.tagMap.set(item.id, item)
})
this.searchResults = tags
// 当搜索结果为空时,提示用户可以按回车键创建标签
if (tags.length === 0) {
this.$message.info('无此标签,按回车键创建')
}
} finally {
this.loading = false
}
}
}