refactor: 优化数据加载逻辑.

This commit is contained in:
lijiahangmax
2023-12-02 23:36:02 +08:00
parent 9f35fb855d
commit a22f30a8b4
19 changed files with 155 additions and 117 deletions

View File

@@ -31,7 +31,6 @@
import type { TabRouterItem } from '@/components/view/tab-router/types';
import { computed, onMounted, ref } from 'vue';
import { useCacheStore } from '@/store';
import { getRoleList } from '@/api/user/role';
import { Message } from '@arco-design/web-vue';
import useLoading from '@/hooks/loading';
@@ -59,9 +58,14 @@
const loadRoleList = async () => {
setLoading(true);
try {
const { data } = await getRoleList();
// 设置到缓存
cacheStore.set('roles', data);
const roles = await cacheStore.loadRoles();
rolesRouter.value = roles.map(s => {
return {
key: s.id,
text: `${s.name} (${s.code})`,
code: s.code
};
});
} catch (e) {
Message.error('角色列表加载失败');
} finally {
@@ -69,18 +73,9 @@
}
};
// 加载主机
onMounted(async () => {
if (!cacheStore.roles.length) {
await loadRoleList();
}
rolesRouter.value = cacheStore.roles.map(s => {
return {
key: s.id,
text: `${s.name} (${s.code})`,
code: s.code
};
});
// 加载角色列表
onMounted(() => {
loadRoleList();
});
</script>

View File

@@ -31,7 +31,6 @@
import type { TabRouterItem } from '@/components/view/tab-router/types';
import { computed, onMounted, ref } from 'vue';
import { useCacheStore } from '@/store';
import { getUserList } from '@/api/user/user';
import { Message } from '@arco-design/web-vue';
import useLoading from '@/hooks/loading';
@@ -59,9 +58,13 @@
const loadUserList = async () => {
setLoading(true);
try {
const { data } = await getUserList();
// 设置到缓存
cacheStore.set('users', data);
const users = await cacheStore.loadUsers();
usersRouter.value = users.map(s => {
return {
key: s.id,
text: `${s.nickname} (${s.username})`
};
});
} catch (e) {
Message.error('用户列表加载失败');
} finally {
@@ -69,17 +72,9 @@
}
};
// 加载主机
onMounted(async () => {
if (!cacheStore.users.length) {
await loadUserList();
}
usersRouter.value = cacheStore.users.map(s => {
return {
key: s.id,
text: `${s.nickname} (${s.username})`
};
});
// 加载用户列表
onMounted(() => {
loadUserList();
});
</script>