refactor: 修改缓存加载逻辑.

This commit is contained in:
lijiahang
2023-12-04 14:35:18 +08:00
parent a22f30a8b4
commit f4b5ba168a
39 changed files with 278 additions and 332 deletions

View File

@@ -44,15 +44,10 @@
const treeData = ref<Array<TreeNodeData>>([]);
// 初始化选项
onBeforeMount(async () => {
if (cacheStore.hostGroups.length) {
treeData.value = cacheStore.hostGroups;
} else {
// 加载数据
const { data } = await getHostGroupTree();
treeData.value = data;
cacheStore.set('hostGroups', data);
}
onBeforeMount(() => {
cacheStore.loadHostGroups().then(s => {
treeData.value = s;
});
});
</script>

View File

@@ -5,7 +5,7 @@
ref="tree"
class="tree-container"
:blockNode="true"
:draggable="draggable"
:draggable="editable"
:data="treeData"
:checkable="checkable"
v-model:checked-keys="checkedKeys"
@@ -20,7 +20,6 @@
v-model="currName"
style="width: 138px;"
placeholder="名称"
autofocus
:max-length="32"
:disabled="node.loading"
@blur="() => saveNode(node.key)"
@@ -77,7 +76,7 @@
<!-- 无数据 -->
<div v-else-if="!loading" class="empty-container">
<span>暂无数据</span>
<span>点击上方 '<icon-plus />' 添加一个分组吧~</span>
<span v-if="editable">点击上方 '<icon-plus />' 添加一个分组吧~</span>
</div>
</a-scrollbar>
</template>
@@ -99,7 +98,7 @@
const props = defineProps({
loading: Boolean,
draggable: {
editable: {
type: Boolean,
default: true
},
@@ -313,20 +312,13 @@
// 加载数据
const fetchTreeData = async (force = false) => {
if (cacheStore.hostGroups.length && !force) {
// 缓存有数据并且非强制加载 直接从缓存中加载
treeData.value = cacheStore.hostGroups;
} else {
// 无数据/强制加载
try {
emits('loading', true);
const { data } = await getHostGroupTree();
treeData.value = data;
cacheStore.hostGroups = data;
} catch (e) {
} finally {
emits('loading', false);
}
try {
const groups = await cacheStore.loadHostGroups(force);
emits('loading', true);
treeData.value = groups;
} catch (e) {
} finally {
emits('loading', false);
}
// 未选择则选择首个
if (!tree.value?.getSelectedNodes()?.length && treeData.value.length) {

View File

@@ -40,11 +40,13 @@
// 初始化选项
onBeforeMount(() => {
optionData.value = cacheStore.hostIdentities.map(s => {
return {
label: `${s.name} (${s.username})`,
value: s.id,
};
cacheStore.loadHostIdentities().then(hostIdentities => {
optionData.value = hostIdentities.map(s => {
return {
label: `${s.name} (${s.username})`,
value: s.id,
};
});
});
});

View File

@@ -40,11 +40,13 @@
// 初始化选项
onBeforeMount(() => {
optionData.value = cacheStore.hostKeys.map(s => {
return {
label: s.name,
value: s.id,
};
cacheStore.loadHostKeys().then(hostKeys => {
optionData.value = hostKeys.map(s => {
return {
label: s.name,
value: s.id,
};
});
});
});
</script>