refactor(biji-qianduan): 重构网络请求处理逻辑- 优化了多个组件中的 API 调用,使用 async/await 替代 Promise 链
- 改进了错误处理,统一使用 ElMessage 显示错误信息 - 简化了部分代码结构,提高了可读性和可维护性
This commit is contained in:
@@ -377,27 +377,31 @@ const buildTree = (items) => {
|
|||||||
|
|
||||||
const fetchGroupings = async () => {
|
const fetchGroupings = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await groupingAll("");
|
const allCategories = await groupingAll("");
|
||||||
const allCategories = response.data || [];
|
categoryTree.value = buildTree(allCategories || []);
|
||||||
categoryTree.value = buildTree(allCategories);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取分组失败:', error);
|
console.error('获取分组失败:', error);
|
||||||
ElMessage.error('获取分组失败: ' + (error.response?.data?.message || error.message));
|
ElMessage.error('获取分组失败: ' + error.message);
|
||||||
categoryTree.value = [];
|
categoryTree.value = [];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const selectFile = async (data) => {
|
const selectFile = async (data) => {
|
||||||
const promise = await markdownList(data.id);
|
try {
|
||||||
groupMarkdownFiles.value = promise.data;
|
const files = await markdownList(data.id);
|
||||||
currentGroupName.value = data.grouping;
|
groupMarkdownFiles.value = files || [];
|
||||||
selectedFile.value = null;
|
currentGroupName.value = data.grouping;
|
||||||
|
selectedFile.value = null;
|
||||||
|
} catch (error) {
|
||||||
|
ElMessage.error('获取笔记列表失败: ' + error.message);
|
||||||
|
groupMarkdownFiles.value = [];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchMarkdownFiles = async () => {
|
const fetchMarkdownFiles = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await markdownAll()
|
const files = await markdownAll();
|
||||||
markdownFiles.value = (response.data || []).map(file => ({
|
markdownFiles.value = (files || []).map(file => ({
|
||||||
...file,
|
...file,
|
||||||
id: String(file.id)
|
id: String(file.id)
|
||||||
}));
|
}));
|
||||||
@@ -932,18 +936,13 @@ const resetToHomeView = async () => {
|
|||||||
showEditor.value = false;
|
showEditor.value = false;
|
||||||
searchKeyword.value = '';
|
searchKeyword.value = '';
|
||||||
try {
|
try {
|
||||||
const response = await getRecentFiles();
|
groupMarkdownFiles.value = await getRecentFiles() || [];
|
||||||
groupMarkdownFiles.value = response.data;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ElMessage.error('获取最近文件失败: ' + error.message);
|
ElMessage.error('获取最近文件失败: ' + error.message);
|
||||||
groupMarkdownFiles.value = [];
|
groupMarkdownFiles.value = [];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
onMounted(async () => {
|
|
||||||
await fetchGroupings();
|
|
||||||
await resetToHomeView();
|
|
||||||
});
|
|
||||||
|
|
||||||
watch(activeMenu, (newVal) => {
|
watch(activeMenu, (newVal) => {
|
||||||
if (newVal === 'all') {
|
if (newVal === 'all') {
|
||||||
@@ -963,11 +962,11 @@ const handleToggleRegistration = async (value) => {
|
|||||||
|
|
||||||
const handleGenerateCode = async () => {
|
const handleGenerateCode = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await generateRegistrationCode();
|
const code = await generateRegistrationCode();
|
||||||
generatedCode.value = response.data;
|
generatedCode.value = code;
|
||||||
ElMessage.success('注册码生成成功');
|
ElMessage.success('注册码生成成功');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ElMessage.error('生成注册码失败');
|
ElMessage.error('生成注册码失败: ' + error.message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -980,10 +979,10 @@ const copyToClipboard = (text) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
// ... existing onMounted logic
|
await fetchGroupings();
|
||||||
|
await resetToHomeView();
|
||||||
try {
|
try {
|
||||||
const response = await getRegistrationStatus();
|
isRegistrationEnabled.value = await getRegistrationStatus();
|
||||||
isRegistrationEnabled.value = response.data;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to fetch registration status:", error);
|
console.error("Failed to fetch registration status:", error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ export const useUserStore = defineStore('user', {
|
|||||||
async login(username, password) {
|
async login(username, password) {
|
||||||
try {
|
try {
|
||||||
const response = await loginApi({ username, password });
|
const response = await loginApi({ username, password });
|
||||||
if (response.data && response.data.token) {
|
if (response && response.token) {
|
||||||
this.token = response.data.token;
|
this.token = response.token;
|
||||||
// 你可能还需要一个接口来获取用户信息
|
// 你可能还需要一个接口来获取用户信息
|
||||||
// this.userInfo = await getUserInfo();
|
// this.userInfo = await getUserInfo();
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
BIN
mydatabase.db
BIN
mydatabase.db
Binary file not shown.
Reference in New Issue
Block a user