20251205优化数据请求逻辑
This commit is contained in:
61
src/App.vue
61
src/App.vue
@@ -1,71 +1,12 @@
|
||||
<template>
|
||||
<div id="app">
|
||||
<router-view/>
|
||||
|
||||
<!-- 全局数据初始化失败提示对话框 -->
|
||||
<el-dialog
|
||||
title="数据初始化失败"
|
||||
:visible.sync="initErrorVisible"
|
||||
:close-on-click-modal="false"
|
||||
:close-on-press-escape="false"
|
||||
:show-close="false"
|
||||
width="400px"
|
||||
>
|
||||
<div style="text-align: center; padding: 20px 0;">
|
||||
<i class="el-icon-warning" style="font-size: 48px; color: #E6A23C; margin-bottom: 16px;"></i>
|
||||
<p style="font-size: 16px; color: #606266; margin-bottom: 20px;">{{ initErrorMessage }}</p>
|
||||
</div>
|
||||
<div slot="footer" style="text-align: center;">
|
||||
<el-button type="primary" @click="retryInit" :loading="retrying">重试</el-button>
|
||||
<el-button @click="goToLogin">返回登录</el-button>
|
||||
</div>
|
||||
</el-dialog>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
name: 'App',
|
||||
data() {
|
||||
return {
|
||||
retrying: false
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
initErrorVisible: {
|
||||
get() {
|
||||
return !!this.$store.state.initError
|
||||
},
|
||||
set(value) {
|
||||
if (!value) {
|
||||
this.$store.commit('clearInitError')
|
||||
}
|
||||
}
|
||||
},
|
||||
initErrorMessage() {
|
||||
return this.$store.state.initError || '数据初始化失败,请重试'
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
async retryInit() {
|
||||
this.retrying = true
|
||||
this.$store.commit('clearInitError')
|
||||
const result = await this.$store.dispatch('initTaskData')
|
||||
this.retrying = false
|
||||
|
||||
if (result.success) {
|
||||
// 初始化成功,刷新当前路由
|
||||
this.$router.go(0)
|
||||
} else {
|
||||
// 初始化失败,显示错误
|
||||
this.$store.commit('setInitError', result.error)
|
||||
}
|
||||
},
|
||||
goToLogin() {
|
||||
this.$store.commit('clearInitError')
|
||||
this.$router.push({ path: '/login' })
|
||||
}
|
||||
}
|
||||
name: 'App'
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
background-color="#E8F5E9"
|
||||
text-color="#2E7D32"
|
||||
active-text-color="#1B5E20"
|
||||
router
|
||||
@select="handleMenuSelect"
|
||||
>
|
||||
<template v-for="item in menu">
|
||||
<el-submenu
|
||||
@@ -80,6 +80,22 @@ export default {
|
||||
myname: myname,
|
||||
};
|
||||
},
|
||||
methods: {
|
||||
// 处理菜单选择,避免重复导航
|
||||
handleMenuSelect(index) {
|
||||
// 如果点击的是当前路径,阻止导航
|
||||
if (this.$route.path === index) {
|
||||
return false;
|
||||
}
|
||||
// 否则正常导航
|
||||
this.$router.push({ path: index }).catch(err => {
|
||||
// 忽略 NavigationDuplicated 错误
|
||||
if (err.name !== 'NavigationDuplicated') {
|
||||
console.error('路由导航错误:', err);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
if (sessionStorage.getItem("access-user") == "Leader") {
|
||||
this.menu = [
|
||||
|
||||
@@ -2325,6 +2325,11 @@ export default {
|
||||
},
|
||||
// 获得历史轨迹
|
||||
chooseTask(id) {
|
||||
// 检查 taskId 是否有效,避免请求 taskId=NaN
|
||||
if (!this.listQuery.taskId || isNaN(this.listQuery.taskId)) {
|
||||
this.$message.info("请先选择任务");
|
||||
return;
|
||||
}
|
||||
api
|
||||
.getTaskWorker(this.listQuery)
|
||||
.then((res) => {
|
||||
@@ -4777,8 +4782,9 @@ export default {
|
||||
// 从 store 获取数据
|
||||
const taskData = this.$store.state.taskData.selectAll || [];
|
||||
that.taskList = taskData;
|
||||
if (that.taskList.length) that.listQuery.taskId = that.taskList[0].id;
|
||||
that.chooseTask(that.listQuery.taskId);
|
||||
// 不再自动调用 chooseTask,等待用户主动选择任务
|
||||
// if (that.taskList.length) that.listQuery.taskId = that.taskList[0].id;
|
||||
// that.chooseTask(that.listQuery.taskId);
|
||||
},
|
||||
watch: {
|
||||
activeItem: {
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
text-color="#2E7D32"
|
||||
active-text-color="#1B5E20"
|
||||
:default-active="this.$route.path"
|
||||
router
|
||||
@select="handleMenuSelect"
|
||||
>
|
||||
<el-submenu index="1">
|
||||
<template slot="title">
|
||||
@@ -44,7 +44,22 @@ export default {
|
||||
data() {
|
||||
return {};
|
||||
},
|
||||
method: {},
|
||||
methods: {
|
||||
// 处理菜单选择,避免重复导航
|
||||
handleMenuSelect(index) {
|
||||
// 如果点击的是当前路径,阻止导航
|
||||
if (this.$route.path === index) {
|
||||
return false;
|
||||
}
|
||||
// 否则正常导航
|
||||
this.$router.push({ path: index }).catch(err => {
|
||||
// 忽略 NavigationDuplicated 错误
|
||||
if (err.name !== 'NavigationDuplicated') {
|
||||
console.error('路由导航错误:', err);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
||||
</script>
|
||||
<style scoped>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<div id="TPcontainer">
|
||||
<el-container>
|
||||
<el-aside width="160px" style="text-align: left">
|
||||
<el-menu router :default-active="this.$route.path">
|
||||
<el-menu :default-active="this.$route.path" @select="handleMenuSelect">
|
||||
<el-submenu index="1">
|
||||
<template slot="title">
|
||||
<i class="el-icon-menu"></i>基础配置</template
|
||||
@@ -90,6 +90,20 @@ export default {
|
||||
};
|
||||
},
|
||||
methods: {
|
||||
// 处理菜单选择,避免重复导航
|
||||
handleMenuSelect(index) {
|
||||
// 如果点击的是当前路径,阻止导航
|
||||
if (this.$route.path === index) {
|
||||
return false;
|
||||
}
|
||||
// 否则正常导航
|
||||
this.$router.push({ path: index }).catch(err => {
|
||||
// 忽略 NavigationDuplicated 错误
|
||||
if (err.name !== 'NavigationDuplicated') {
|
||||
console.error('路由导航错误:', err);
|
||||
}
|
||||
});
|
||||
},
|
||||
initConfig() {
|
||||
let config1 = sessionStorage["Data"];
|
||||
if (config1) {
|
||||
|
||||
@@ -74,24 +74,6 @@
|
||||
<span style="position: fixed; bottom: 10px; left: 10px; color: lightgray"
|
||||
>v20251006</span
|
||||
>
|
||||
|
||||
<!-- 数据初始化失败提示对话框 -->
|
||||
<el-dialog
|
||||
title="数据初始化失败"
|
||||
:visible.sync="initErrorVisible"
|
||||
:close-on-click-modal="false"
|
||||
:close-on-press-escape="false"
|
||||
:show-close="false"
|
||||
width="400px"
|
||||
>
|
||||
<div style="text-align: center; padding: 20px 0;">
|
||||
<i class="el-icon-warning" style="font-size: 48px; color: #E6A23C; margin-bottom: 16px;"></i>
|
||||
<p style="font-size: 16px; color: #606266; margin-bottom: 20px;">{{ initErrorMessage }}</p>
|
||||
</div>
|
||||
<div slot="footer" style="text-align: center;">
|
||||
<el-button type="primary" @click="retryInit" :loading="loading">重试</el-button>
|
||||
</div>
|
||||
</el-dialog>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -126,8 +108,6 @@ export default {
|
||||
},
|
||||
loading: false,
|
||||
pwdType: "password",
|
||||
initErrorVisible: false,
|
||||
initErrorMessage: "",
|
||||
};
|
||||
},
|
||||
methods: {
|
||||
@@ -159,8 +139,38 @@ export default {
|
||||
sessionStorage.setItem("access-user", response.data.data.role);
|
||||
sessionStorage.setItem("access-id", response.data.data.id);
|
||||
|
||||
// 初始化任务数据
|
||||
that.initTaskData();
|
||||
// 根据用户角色跳转
|
||||
that.loading = false;
|
||||
if (
|
||||
sessionStorage.getItem("access-user") == "Admin" ||
|
||||
sessionStorage.getItem("access-user") == "SuperAdmin" ||
|
||||
sessionStorage.getItem("access-user") == "Leader"
|
||||
) {
|
||||
// 检查当前路径,避免重复导航
|
||||
if (that.$route.path !== "/view") {
|
||||
that.$router.push({ path: "/view" }).catch(err => {
|
||||
// 忽略 NavigationDuplicated 错误
|
||||
if (err.name !== 'NavigationDuplicated') {
|
||||
console.error('路由导航错误:', err)
|
||||
}
|
||||
})
|
||||
}
|
||||
} else if (sessionStorage.getItem("access-user") == "Operator") {
|
||||
// 检查当前路径,避免重复导航
|
||||
if (that.$route.path !== "/main") {
|
||||
that.$router.push({ path: "/main" }).catch(err => {
|
||||
// 忽略 NavigationDuplicated 错误
|
||||
if (err.name !== 'NavigationDuplicated') {
|
||||
console.error('路由导航错误:', err)
|
||||
}
|
||||
})
|
||||
}
|
||||
} else {
|
||||
that.$notify.error({
|
||||
title: "提示",
|
||||
message: "用户不可登录该系统!",
|
||||
});
|
||||
}
|
||||
} else if (response.data.status == "204") {
|
||||
that
|
||||
.$confirm("用户信息不完善,是否去完善信息?", "提示", {
|
||||
@@ -196,57 +206,6 @@ export default {
|
||||
toRegister() {
|
||||
this.$router.push({ path: "/register" });
|
||||
},
|
||||
// 初始化任务数据
|
||||
async initTaskData() {
|
||||
let that = this;
|
||||
const result = await store.dispatch('initTaskData');
|
||||
|
||||
if (result.success) {
|
||||
// 初始化成功,根据用户角色跳转
|
||||
that.loading = false;
|
||||
if (
|
||||
sessionStorage.getItem("access-user") == "Admin" ||
|
||||
sessionStorage.getItem("access-user") == "SuperAdmin" ||
|
||||
sessionStorage.getItem("access-user") == "Leader"
|
||||
) {
|
||||
// 检查当前路径,避免重复导航
|
||||
if (that.$route.path !== "/view") {
|
||||
that.$router.push({ path: "/view" }).catch(err => {
|
||||
// 忽略 NavigationDuplicated 错误
|
||||
if (err.name !== 'NavigationDuplicated') {
|
||||
console.error('路由导航错误:', err)
|
||||
}
|
||||
})
|
||||
}
|
||||
} else if (sessionStorage.getItem("access-user") == "Operator") {
|
||||
// 检查当前路径,避免重复导航
|
||||
if (that.$route.path !== "/main") {
|
||||
that.$router.push({ path: "/main" }).catch(err => {
|
||||
// 忽略 NavigationDuplicated 错误
|
||||
if (err.name !== 'NavigationDuplicated') {
|
||||
console.error('路由导航错误:', err)
|
||||
}
|
||||
})
|
||||
}
|
||||
} else {
|
||||
that.$notify.error({
|
||||
title: "提示",
|
||||
message: "用户不可登录该系统!",
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// 初始化失败,显示错误提示
|
||||
that.loading = false;
|
||||
that.initErrorMessage = result.error || "数据初始化失败,请重试";
|
||||
that.initErrorVisible = true;
|
||||
}
|
||||
},
|
||||
// 重试初始化
|
||||
retryInit() {
|
||||
this.initErrorVisible = false;
|
||||
this.loading = true;
|
||||
this.initTaskData();
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
@@ -159,69 +159,18 @@ router.beforeEach(async (to, from, next) => {
|
||||
if (to.path.startsWith('/login')) {
|
||||
window.sessionStorage.removeItem('access-user')
|
||||
window.sessionStorage.removeItem('access-id')
|
||||
// 清除初始化状态
|
||||
store.commit('setTaskDataInitialized', false)
|
||||
store.commit('clearInitError')
|
||||
next()
|
||||
} else if (to.path.startsWith('/register') || to.path.startsWith('/modifypassword') || to.path.startsWith('/forgetpassword') || to.path.startsWith('/addinfo')) {
|
||||
window.sessionStorage.removeItem('access-user')
|
||||
window.sessionStorage.removeItem('access-id')
|
||||
// 清除初始化状态
|
||||
store.commit('setTaskDataInitialized', false)
|
||||
store.commit('clearInitError')
|
||||
next()
|
||||
} else {
|
||||
let user = window.sessionStorage.getItem('access-user') === '' ? false : window.sessionStorage.getItem('access-user')
|
||||
if (!user) {
|
||||
next({ path: '/login' })
|
||||
} else {
|
||||
// 检查数据是否已初始化
|
||||
const isInitialized = store.state.taskDataInitialized
|
||||
const isInitializing = store.state.taskDataInitializing
|
||||
|
||||
// 如果数据未初始化且不在初始化中,则进行初始化
|
||||
if (!isInitialized && !isInitializing) {
|
||||
try {
|
||||
const result = await store.dispatch('initTaskData')
|
||||
if (result.success) {
|
||||
// 初始化成功,继续路由
|
||||
// 用户已登录,直接放行
|
||||
next()
|
||||
} else {
|
||||
// 初始化失败,显示错误提示并阻止路由
|
||||
store.commit('setInitError', result.error)
|
||||
// 阻止路由跳转,显示错误对话框
|
||||
next(false)
|
||||
}
|
||||
} catch (error) {
|
||||
// 初始化异常
|
||||
store.commit('setInitError', error.message || '初始化失败,请重试')
|
||||
next(false)
|
||||
}
|
||||
} else if (isInitializing) {
|
||||
// 正在初始化中,等待完成(最多等待5秒)
|
||||
let waited = 0
|
||||
const maxWait = 5000
|
||||
const checkInterval = setInterval(() => {
|
||||
waited += 100
|
||||
if (!store.state.taskDataInitializing || waited >= maxWait) {
|
||||
clearInterval(checkInterval)
|
||||
if (store.state.taskDataInitialized) {
|
||||
next()
|
||||
} else {
|
||||
if (waited >= maxWait) {
|
||||
store.commit('setTaskDataInitializing', false)
|
||||
store.commit('setInitError', '初始化超时,请重试')
|
||||
} else {
|
||||
store.commit('setInitError', '初始化失败,请重试')
|
||||
}
|
||||
next(false)
|
||||
}
|
||||
}
|
||||
}, 100)
|
||||
} else {
|
||||
// 已初始化,继续路由
|
||||
next()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user