20251205优化数据请求逻辑

This commit is contained in:
wxlong
2025-12-05 11:12:10 +08:00
parent 7140e63b3f
commit c4c36028d3
7 changed files with 92 additions and 192 deletions

View File

@@ -1,71 +1,12 @@
<template> <template>
<div id="app"> <div id="app">
<router-view/> <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> </div>
</template> </template>
<script> <script>
export default { export default {
name: 'App', 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' })
}
}
} }
</script> </script>

View File

@@ -34,7 +34,7 @@
background-color="#E8F5E9" background-color="#E8F5E9"
text-color="#2E7D32" text-color="#2E7D32"
active-text-color="#1B5E20" active-text-color="#1B5E20"
router @select="handleMenuSelect"
> >
<template v-for="item in menu"> <template v-for="item in menu">
<el-submenu <el-submenu
@@ -80,6 +80,22 @@ export default {
myname: myname, 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() { mounted() {
if (sessionStorage.getItem("access-user") == "Leader") { if (sessionStorage.getItem("access-user") == "Leader") {
this.menu = [ this.menu = [

View File

@@ -2325,6 +2325,11 @@ export default {
}, },
// 获得历史轨迹 // 获得历史轨迹
chooseTask(id) { chooseTask(id) {
// 检查 taskId 是否有效,避免请求 taskId=NaN
if (!this.listQuery.taskId || isNaN(this.listQuery.taskId)) {
this.$message.info("请先选择任务");
return;
}
api api
.getTaskWorker(this.listQuery) .getTaskWorker(this.listQuery)
.then((res) => { .then((res) => {
@@ -4777,8 +4782,9 @@ export default {
// 从 store 获取数据 // 从 store 获取数据
const taskData = this.$store.state.taskData.selectAll || []; const taskData = this.$store.state.taskData.selectAll || [];
that.taskList = taskData; that.taskList = taskData;
if (that.taskList.length) that.listQuery.taskId = that.taskList[0].id; // 不再自动调用 chooseTask等待用户主动选择任务
that.chooseTask(that.listQuery.taskId); // if (that.taskList.length) that.listQuery.taskId = that.taskList[0].id;
// that.chooseTask(that.listQuery.taskId);
}, },
watch: { watch: {
activeItem: { activeItem: {

View File

@@ -11,7 +11,7 @@
text-color="#2E7D32" text-color="#2E7D32"
active-text-color="#1B5E20" active-text-color="#1B5E20"
:default-active="this.$route.path" :default-active="this.$route.path"
router @select="handleMenuSelect"
> >
<el-submenu index="1"> <el-submenu index="1">
<template slot="title"> <template slot="title">
@@ -44,7 +44,22 @@ export default {
data() { data() {
return {}; 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> </script>
<style scoped> <style scoped>

View File

@@ -2,7 +2,7 @@
<div id="TPcontainer"> <div id="TPcontainer">
<el-container> <el-container>
<el-aside width="160px" style="text-align: left"> <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"> <el-submenu index="1">
<template slot="title"> <template slot="title">
<i class="el-icon-menu"></i>基础配置</template <i class="el-icon-menu"></i>基础配置</template
@@ -90,6 +90,20 @@ export default {
}; };
}, },
methods: { 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() { initConfig() {
let config1 = sessionStorage["Data"]; let config1 = sessionStorage["Data"];
if (config1) { if (config1) {

View File

@@ -74,24 +74,6 @@
<span style="position: fixed; bottom: 10px; left: 10px; color: lightgray" <span style="position: fixed; bottom: 10px; left: 10px; color: lightgray"
>v20251006</span >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> </div>
</template> </template>
@@ -126,8 +108,6 @@ export default {
}, },
loading: false, loading: false,
pwdType: "password", pwdType: "password",
initErrorVisible: false,
initErrorMessage: "",
}; };
}, },
methods: { methods: {
@@ -159,8 +139,38 @@ export default {
sessionStorage.setItem("access-user", response.data.data.role); sessionStorage.setItem("access-user", response.data.data.role);
sessionStorage.setItem("access-id", response.data.data.id); 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") { } else if (response.data.status == "204") {
that that
.$confirm("用户信息不完善,是否去完善信息?", "提示", { .$confirm("用户信息不完善,是否去完善信息?", "提示", {
@@ -196,57 +206,6 @@ export default {
toRegister() { toRegister() {
this.$router.push({ path: "/register" }); 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> </script>

View File

@@ -159,69 +159,18 @@ router.beforeEach(async (to, from, next) => {
if (to.path.startsWith('/login')) { if (to.path.startsWith('/login')) {
window.sessionStorage.removeItem('access-user') window.sessionStorage.removeItem('access-user')
window.sessionStorage.removeItem('access-id') window.sessionStorage.removeItem('access-id')
// 清除初始化状态
store.commit('setTaskDataInitialized', false)
store.commit('clearInitError')
next() next()
} else if (to.path.startsWith('/register') || to.path.startsWith('/modifypassword') || to.path.startsWith('/forgetpassword') || to.path.startsWith('/addinfo')) { } 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-user')
window.sessionStorage.removeItem('access-id') window.sessionStorage.removeItem('access-id')
// 清除初始化状态
store.commit('setTaskDataInitialized', false)
store.commit('clearInitError')
next() next()
} else { } else {
let user = window.sessionStorage.getItem('access-user') === '' ? false : window.sessionStorage.getItem('access-user') let user = window.sessionStorage.getItem('access-user') === '' ? false : window.sessionStorage.getItem('access-user')
if (!user) { if (!user) {
next({ path: '/login' }) next({ path: '/login' })
} else { } else {
// 检查数据是否已初始化 // 用户已登录,直接放行
const isInitialized = store.state.taskDataInitialized next()
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()
}
} }
} }
}) })