20251128API:selectAllWithTableName/selectAll/selectTaskHasCreated提前缓存

This commit is contained in:
wxlong
2025-11-28 14:46:02 +08:00
parent d3e0678ef0
commit 014e6dc299
17 changed files with 415 additions and 229 deletions

View File

@@ -131,7 +131,7 @@
<script type="text/javascript" src="/static/js/sockjs.min.js"></script> <script type="text/javascript" src="/static/js/sockjs.min.js"></script>
<script> <script>
// 平台名称配置 // 平台名称配置
const myname = '天津市林草资源专项调查管理平台 v20251126beta'; const myname = '天津市林草资源专项调查管理平台 v20251006';
document.getElementsByTagName('title')[0].innerText = myname; document.getElementsByTagName('title')[0].innerText = myname;
// 页面加载完成后隐藏加载动画 // 页面加载完成后隐藏加载动画

View File

@@ -1,12 +1,71 @@
<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

@@ -554,7 +554,7 @@
overflow-y: scroll; overflow-y: scroll;
height: calc(100% - 70px); height: calc(100% - 70px);
padding: 20px 24px; padding: 8px 8px;
width: 420px; width: 420px;
box-shadow: 0 15px 35px rgba(76, 175, 80, 0.14), inset 0 1px 0 rgba(255, 255, 255, 0.6); box-shadow: 0 15px 35px rgba(76, 175, 80, 0.14), inset 0 1px 0 rgba(255, 255, 255, 0.6);
backdrop-filter: blur(4px); backdrop-filter: blur(4px);

View File

@@ -135,6 +135,7 @@ ul {
float: right; float: right;
text-align: left; text-align: left;
font-size: 15px; font-size: 15px;
font-weight: bold !important;
border-left: 0px solid #000; border-left: 0px solid #000;
z-index: 999; z-index: 999;
margin-right: 0px; margin-right: 0px;
@@ -157,6 +158,7 @@ ul {
width: 140px; width: 140px;
text-align: center; text-align: center;
color: #2E7D32 !important; color: #2E7D32 !important;
font-weight: bold !important;
transition: all 0.3s ease; transition: all 0.3s ease;
} }
@@ -447,6 +449,29 @@ el-aside.tac.el-menu-item,
border-radius: 0 0 14px 14px !important; border-radius: 0 0 14px 14px !important;
} }
/* el-scrollbar 最大宽度设置,超出部分隐藏 */
.el-scrollbar {
max-width: 480px !important;
overflow-x: hidden !important;
overflow-y: auto !important;
}
.el-scrollbar__wrap {
max-width: 480px !important;
overflow-x: hidden !important;
overflow-y: auto !important;
}
.el-scrollbar__view {
max-width: 480px !important;
overflow-x: hidden !important;
}
/* 隐藏横向滚动条 */
.el-scrollbar__bar.is-horizontal {
display: none !important;
}
.el-input__inner { .el-input__inner {
height: 35px !important; height: 35px !important;
} }
@@ -549,6 +574,7 @@ el-aside.tac.el-menu-item,
.container .topbar-wrap .topbar-title { .container .topbar-wrap .topbar-title {
margin-right: 0px; margin-right: 0px;
height: 70px; height: 70px;
font-weight: bold !important;
} }
} }

View File

@@ -770,32 +770,22 @@ export default {
this.wTaskOptions = []; this.wTaskOptions = [];
let that = this; let that = this;
request({ // 从 store 获取数据
url: "/task/selectTaskHasCreated", const taskData = that.$store.state.taskData.selectTaskHasCreated || [];
method: "get", for (var i = 0; i < taskData.length; i++) {
}).then(function (response) { that.reportoptions.push({
if (response.data.success == true) { value: taskData[i].id,
var jsondata = JSON.parse(response.data.data); label: taskData[i].taskName,
var options = jsondata.data; });
for (var i = 0; i < options.length; i++) { that.taskoptions.push({
that.reportoptions.push({ value: taskData[i].id,
value: options[i].id, label: taskData[i].taskName,
label: options[i].taskName, });
}); that.wTaskOptions.push({
that.taskoptions.push({ value: taskData[i].id,
value: options[i].id, label: taskData[i].taskName,
label: options[i].taskName, });
}); }
that.wTaskOptions.push({
value: options[i].id,
label: options[i].taskName,
});
}
} else if (response.data.status == "401") {
that.$alert(response.data.message);
that.$router.push({ path: "/login" });
}
});
}, },
getWarningTables() { getWarningTables() {
if (this.warningform.task != "") { if (this.warningform.task != "") {

View File

@@ -363,24 +363,14 @@ export default {
getTasks () { getTasks () {
this.taskoptions = [] this.taskoptions = []
let that = this let that = this
request({ // 从 store 获取数据
url: '/task/selectTaskHasCreated', const taskData = that.$store.state.taskData.selectTaskHasCreated || []
method: 'get' for (var i = 0; i < taskData.length; i++) {
}).then(function (response) { that.taskoptions.push({
if (response.data.success === true) { value: taskData[i].id,
var jsondata = JSON.parse(response.data.data) label: taskData[i].taskName
var options = jsondata.data })
for (var i = 0; i < options.length; i++) { }
that.taskoptions.push({
value: options[i].id,
label: options[i].taskName
})
}
} else if (response.data.status === '401') {
that.$alert(response.data.message)
that.$router.push({path: '/login'})
}
})
}, },
getTables () { getTables () {
if (this.task === '') { if (this.task === '') {

View File

@@ -551,8 +551,8 @@ export default {
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
min-height: 400px; height: 270px;
margin-bottom: 20px; margin-bottom: 0px;
width: 100%; width: 100%;
overflow: visible; overflow: visible;
} }
@@ -564,7 +564,7 @@ export default {
align-items: center; align-items: center;
justify-content: center; justify-content: center;
max-width: 100%; max-width: 100%;
min-height: 400px; height: 270px;
z-index: 1; z-index: 1;
} }
@@ -633,16 +633,17 @@ export default {
.thumbnail-nav { .thumbnail-nav {
display: flex; display: flex;
gap: 12px; gap: 8px;
justify-content: center; justify-content: center;
flex-wrap: wrap; flex-wrap: wrap;
padding: 10px 0; padding: 4px 0;
height: 60px;
border-top: 1px solid rgba(129, 199, 132, 0.3); border-top: 1px solid rgba(129, 199, 132, 0.3);
} }
.thumbnail-item { .thumbnail-item {
width: 80px; width: 72px;
height: 80px; height: 48px;
border-radius: 8px; border-radius: 8px;
overflow: hidden; overflow: hidden;
border: 2px solid transparent; border: 2px solid transparent;

View File

@@ -129,6 +129,7 @@ export default {
} }
.topbar-title .el-menu--horizontal > .el-menu-item { .topbar-title .el-menu--horizontal > .el-menu-item {
font-weight: bold !important;
height: 50px; height: 50px;
line-height: 70px; line-height: 70px;
width: 114px; width: 114px;

View File

@@ -523,24 +523,14 @@ export default {
getTasks1() { getTasks1() {
this.stattaskoptions = []; this.stattaskoptions = [];
let that = this; let that = this;
request({ // 从 store 获取数据
url: "/task/selectTaskHasCreated", const taskData = that.$store.state.taskData.selectTaskHasCreated || [];
method: "get", for (var i = 0; i < taskData.length; i++) {
}).then(function (response) { that.stattaskoptions.push({
if (response.data.success == true) { value: taskData[i].id,
var jsondata = JSON.parse(response.data.data); label: taskData[i].taskName,
var options = jsondata.data; });
for (var i = 0; i < options.length; i++) { }
that.stattaskoptions.push({
value: options[i].id,
label: options[i].taskName,
});
}
} else if (response.data.status == "401") {
that.$alert(response.data.message);
that.$router.push({ path: "/login" });
}
});
}, },
getTables1() { getTables1() {
if (this.stattask === "") { if (this.stattask === "") {

View File

@@ -1476,37 +1476,24 @@ export default {
}); });
this.map.addOverlay(this.trackLayerOverLay); this.map.addOverlay(this.trackLayerOverLay);
}, },
// 初始化任务清单 // 初始化任务清单 - 从 store 获取数据
initList() { initList() {
that.routelist = []; that.routelist = [];
that.routelist2 = []; that.routelist2 = [];
// 连接后台测试: // 从 store 获取数据
request({ const taskData = that.$store.state.taskData.selectAllWithTableName || [];
url: "/task/selectAllWithTableName", // 把数据装入下拉菜单中
method: "get", for (let i = 0; i < taskData.length; i++) {
}) let newData = {
.then(function (response) { value: taskData[i].databaseName,
if (response.data.status == "401") { label: taskData[i].taskName,
that.$alert(response.data.message); id: taskData[i].id,
that.$router.push({ path: "/login" }); tableNames: taskData[i].tableNames,
} else { };
// 把数据装入下拉菜单中 that.routelist.push(newData);
for (let i = 0; i < response.data.data.length; i++) { that.list.push(newData);
let newData = { that.routelist2.push(newData);
value: response.data.data[i].databaseName, }
label: response.data.data[i].taskName,
id: response.data.data[i].id,
tableNames: response.data.data[i].tableNames,
};
that.routelist.push(newData);
that.list.push(newData);
that.routelist2.push(newData);
}
}
})
.catch(function (err) {
console.log(err);
});
}, },
// 左侧面板收缩动画 // 左侧面板收缩动画
collapse() { collapse() {
@@ -4785,20 +4772,11 @@ export default {
}, },
}, },
mounted() { mounted() {
api // 从 store 获取数据
.getAllTask() const taskData = this.$store.state.taskData.selectAll || [];
.then((res) => { that.taskList = taskData;
if (res.data.status == "401") { if (that.taskList.length) that.listQuery.taskId = that.taskList[0].id;
that.$alert(response.data.message); that.chooseTask(that.listQuery.taskId);
that.$router.push({ path: "/login" });
}
that.taskList = JSON.parse(res.data).data;
if (that.taskList.length) that.listQuery.taskId = that.taskList[0].id;
that.chooseTask(that.listQuery.taskId);
})
.catch((error) => {
this.$message.error(error);
});
}, },
watch: { watch: {
activeItem: { activeItem: {

View File

@@ -792,25 +792,12 @@ export default {
// }, // },
init() { init() {
let that = this; let that = this;
// 请求任务列表 // 从 store 获取数据
that.tasksData = []; const taskData = that.$store.state.taskData.selectTaskHasCreated || [];
that.srcTaskOptions = []; that.tasksData = taskData;
that.destTaskOptions = []; that.filteredData = [...that.tasksData]; // 初始化过滤数据
request({ that.srcTaskOptions = taskData;
url: "/task/selectTaskHasCreated", that.destTaskOptions = taskData;
method: "get",
}).then(function (response) {
if (response.data.success == true) {
var jsondata = JSON.parse(response.data.data);
that.tasksData = jsondata.data;
that.filteredData = [...that.tasksData]; // 初始化过滤数据
that.srcTaskOptions = jsondata.data;
that.destTaskOptions = jsondata.data;
} else if (response.data.status == "401") {
that.$alert(response.data.message);
that.$router.push({ path: "/login" });
}
});
}, },
beforeTpkUpload(file) { beforeTpkUpload(file) {
if (file.type != "tpk") { if (file.type != "tpk") {

View File

@@ -765,27 +765,19 @@ export default {
} else { } else {
let geojsonObject = result.data.data; let geojsonObject = result.data.data;
this.drawgridLayer(geojsonObject, flag, isRedraw); this.drawgridLayer(geojsonObject, flag, isRedraw);
request({ // 从 store 获取数据
url: "/task/selectAllWithTableName", const taskData = this.$store.state.taskData.selectAllWithTableName || [];
method: "get", let current = taskData.find((item) => {
}).then((response) => { return item.id == this.taskId;
if (response.data.status == "401") {
this.$alert(response.data.message);
this.$router.push({ path: "/login" });
} else {
// 把数据装入查看表的下拉菜单中
let temp = response.data.data;
let current = temp.find((item) => {
return item.id == this.taskId;
});
this.taskValue = {
value: current.databaseName,
label: current.taskName,
id: current.id,
tableNames: current.tableNames,
};
}
}); });
if (current) {
this.taskValue = {
value: current.databaseName,
label: current.taskName,
id: current.id,
tableNames: current.tableNames,
};
}
} }
}) })
.catch((error) => { .catch((error) => {

View File

@@ -370,34 +370,24 @@ export default {
}, },
init() { init() {
let that = this; let that = this;
// 请求任务列表 // 从 store 获取数据
request({ const taskData = that.$store.state.taskData.selectTaskHasCreated || [];
url: "/task/selectTaskHasCreated", that.tasksData = taskData;
method: "get", //0表示需要分配
}).then(function (response) { for (var i = 0; i < that.tasksData.length; i++) {
if (response.data.success == true) { if (that.tasksData[i].isAllocationOptional == 0) {
var jsondata = JSON.parse(response.data.data); that.tasksData[i].isAllocationOptional = true;
that.tasksData = jsondata.data; } else {
//0表示需要分配 that.tasksData[i].isAllocationOptional = false;
for (var i = 0; i < that.tasksData.length; i++) {
if (that.tasksData[i].isAllocationOptional == 0) {
that.tasksData[i].isAllocationOptional = true;
} else {
that.tasksData[i].isAllocationOptional = false;
}
if (that.tasksData[i].allocated == "true") {
that.tasksData[i].allocated = true;
} else {
that.tasksData[i].allocated = false;
}
}
// 应用当前筛选和搜索
that.applyFilter();
} else if (response.data.status == "401") {
that.$alert(response.data.message);
that.$router.push({ path: "/login" });
} }
}); if (that.tasksData[i].allocated == "true") {
that.tasksData[i].allocated = true;
} else {
that.tasksData[i].allocated = false;
}
}
// 应用当前筛选和搜索
that.applyFilter();
request({ request({
url: "/user/all_worker", url: "/user/all_worker",
method: "get", method: "get",

View File

@@ -295,37 +295,28 @@ export default {
} }
}, },
//读取数据库 初始化xml //读取数据库 初始化xml - 从 store 获取数据
init() { init() {
let that = this; let that = this;
request({ // 从 store 获取数据
url: "/task/selectAll", const taskData = that.$store.state.taskData.selectAll || [];
method: "get", that.xmlData = taskData;
}).then(function (response) { that.filteredData = [...that.xmlData]; // 初始化过滤数据
if (response.data.success == true) { for (var i = 0; i < that.xmlData.length; i++) {
var jsondata = JSON.parse(response.data.data); if (that.xmlData[i].hasCreateTables == 1) {
that.xmlData = jsondata.data; that.xmlData[i].hasCreateTables = true;
that.filteredData = [...that.xmlData]; // 初始化过滤数据 } else {
for (var i = 0; i < that.xmlData.length; i++) { that.xmlData[i].hasCreateTables = false;
if (that.xmlData[i].hasCreateTables == 1) {
that.xmlData[i].hasCreateTables = true;
} else {
that.xmlData[i].hasCreateTables = false;
}
if (
that.xmlData[i].isReviewerRestricted == 1 ||
that.xmlData[i].isReviewerRestricted == true
) {
that.xmlData[i].isReviewerRestricted = true;
} else {
that.xmlData[i].isReviewerRestricted = false;
}
}
} else if (response.data.status == "401") {
that.$alert(response.data.message);
that.$router.push({ path: "/login" });
} }
}); if (
that.xmlData[i].isReviewerRestricted == 1 ||
that.xmlData[i].isReviewerRestricted == true
) {
that.xmlData[i].isReviewerRestricted = true;
} else {
that.xmlData[i].isReviewerRestricted = false;
}
}
}, },
// 修改方法参数从index改为id // 修改方法参数从index改为id
changeRestricted(id, val) { changeRestricted(id, val) {

View File

@@ -72,14 +72,33 @@
</div> </div>
</el-form> </el-form>
<span style="position: fixed; bottom: 10px; left: 10px; color: lightgray" <span style="position: fixed; bottom: 10px; left: 10px; color: lightgray"
>v20251113beta</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>
<script> <script>
import { ajax as request } from "@/request.js"; import { ajax as request } from "@/request.js";
import md5 from "js-md5"; import md5 from "js-md5";
import { store } from "@/components/vuex/store.js";
export default { export default {
name: "login", name: "login",
data() { data() {
@@ -107,6 +126,8 @@ export default {
}, },
loading: false, loading: false,
pwdType: "password", pwdType: "password",
initErrorVisible: false,
initErrorMessage: "",
}; };
}, },
methods: { methods: {
@@ -138,23 +159,8 @@ 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);
if ( // 初始化任务数据
sessionStorage.getItem("access-user") == "Admin" || that.initTaskData();
sessionStorage.getItem("access-user") == "SuperAdmin" ||
sessionStorage.getItem("access-user") == "Leader"
) {
that.loading = false;
that.$router.push({ path: "/view" });
} else if (sessionStorage.getItem("access-user") == "Operator") {
that.loading = false;
that.$router.push({ path: "/main" });
} else {
that.$notify.error({
title: "提示",
message: "用户不可登录该系统!",
});
that.loading = false;
}
} else if (response.data.status == "204") { } else if (response.data.status == "204") {
that that
.$confirm("用户信息不完善,是否去完善信息?", "提示", { .$confirm("用户信息不完善,是否去完善信息?", "提示", {
@@ -190,6 +196,41 @@ 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"
) {
that.$router.push({ path: "/view" });
} else if (sessionStorage.getItem("access-user") == "Operator") {
that.$router.push({ path: "/main" });
} 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

@@ -1,5 +1,6 @@
import Vue from 'vue' import Vue from 'vue'
import Vuex from 'vuex' import Vuex from 'vuex'
import { ajax as request } from "@/request.js";
Vue.use(Vuex) Vue.use(Vuex)
export const store = new Vuex.Store({ export const store = new Vuex.Store({
@@ -15,7 +16,17 @@ export const store = new Vuex.Store({
LocalLocation: { LocalLocation: {
currentPosition: '', currentPosition: '',
currentLocation: '' currentLocation: ''
} },
// 登录后初始化的任务数据
taskData: {
selectAllWithTableName: [], // /api/task/selectAllWithTableName 的数据
selectAll: [], // /api/task/selectAll/ 的数据
selectTaskHasCreated: [] // /api/task/selectTaskHasCreated 的数据
},
// 初始化状态
taskDataInitialized: false,
taskDataInitializing: false,
initError: null // 初始化错误信息
}, },
mutations: { mutations: {
updateDrawtool(state, drawtool) { updateDrawtool(state, drawtool) {
@@ -63,6 +74,28 @@ export const store = new Vuex.Store({
}, },
updatePosition(state, position) { updatePosition(state, position) {
state.LocalLocation.currentPosition = position state.LocalLocation.currentPosition = position
},
// 设置任务数据
setTaskDataSelectAllWithTableName(state, data) {
state.taskData.selectAllWithTableName = data
},
setTaskDataSelectAll(state, data) {
state.taskData.selectAll = data
},
setTaskDataSelectTaskHasCreated(state, data) {
state.taskData.selectTaskHasCreated = data
},
setTaskDataInitialized(state, value) {
state.taskDataInitialized = value
},
setTaskDataInitializing(state, value) {
state.taskDataInitializing = value
},
setInitError(state, error) {
state.initError = error
},
clearInitError(state) {
state.initError = null
} }
}, },
@@ -87,6 +120,70 @@ export const store = new Vuex.Store({
}, },
updatePosition({ commit, state }, obj) { updatePosition({ commit, state }, obj) {
commit("updatePosition", obj.position) commit("updatePosition", obj.position)
},
// 初始化任务数据
async initTaskData({ commit, state }) {
commit('setTaskDataInitializing', true)
commit('setTaskDataInitialized', false)
try {
// 调用3个接口
const [response1, response2, response3] = await Promise.all([
request({
url: "/task/selectAllWithTableName",
method: "get",
}),
request({
url: "/task/selectAll/",
method: "get",
}),
request({
url: "/task/selectTaskHasCreated",
method: "get",
})
])
// 检查是否有401错误
if (response1.data.status == "401" || response2.data.status == "401" || response3.data.status == "401") {
throw new Error("认证失败,请重新登录")
}
// 处理第一个接口的数据
let data1 = response1.data.data || []
commit('setTaskDataSelectAllWithTableName', data1)
// 处理第二个接口的数据
let data2 = []
if (response2.data.success == true && response2.data.data) {
try {
const parsed = JSON.parse(response2.data.data)
data2 = parsed.data || []
} catch (e) {
data2 = response2.data.data || []
}
}
commit('setTaskDataSelectAll', data2)
// 处理第三个接口的数据
let data3 = []
if (response3.data.success == true && response3.data.data) {
try {
const parsed = JSON.parse(response3.data.data)
data3 = parsed.data || []
} catch (e) {
data3 = response3.data.data || []
}
}
commit('setTaskDataSelectTaskHasCreated', data3)
commit('setTaskDataInitialized', true)
commit('setTaskDataInitializing', false)
return { success: true }
} catch (error) {
commit('setTaskDataInitialized', false)
commit('setTaskDataInitializing', false)
return { success: false, error: error.message || '初始化失败,请重试' }
}
} }
}, },

View File

@@ -1,5 +1,6 @@
import Vue from 'vue' import Vue from 'vue'
import VueRouter from 'vue-router' import VueRouter from 'vue-router'
import { store } from '@/components/vuex/store'
const Home = () => import('@/components/Home') const Home = () => import('@/components/Home')
const ViewTask = () => import('@/components/Supervision/ViewTask') const ViewTask = () => import('@/components/Supervision/ViewTask')
const statPage = () => import('@/components/Statistic/statPage') const statPage = () => import('@/components/Statistic/statPage')
@@ -148,21 +149,73 @@ const routes = [
const router = new VueRouter({ const router = new VueRouter({
routes routes
}) })
router.beforeEach((to, from, next) => { 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 {
next() // 检查数据是否已初始化
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()
}
} }
} }
}) })