This commit is contained in:
zhoutianchi
2026-02-28 17:38:37 +08:00
parent 4a9350211d
commit 9e0407331a
9 changed files with 47 additions and 22 deletions

View File

@@ -17,7 +17,7 @@
<script>
const HEATH='heath'
const TEACHER_CATE='teacher_cate'
const TEACHER_CATE='professional_teacher_cate'
const EXAM_STATUS="EXAM_STATUS"
//技能提升

View File

@@ -5,6 +5,7 @@ import pinia from '/@/stores/index';
import { storeToRefs } from 'pinia';
import { useKeepALiveNames } from '/@/stores/keepAliveNames';
import { useRoutesList } from '/@/stores/routesList';
import { useUserInfo } from '/@/stores/userInfo';
import { Session } from '/@/utils/storage';
import { staticRoutes, notFoundAndNoPower } from '/@/router/route';
import { initBackEndControlRoutes } from '/@/router/backEnd';
@@ -91,6 +92,18 @@ export function formatTwoStageRoutes(arr: any) {
router.beforeEach(async (to, from, next) => {
NProgress.configure({ showSpinner: false });
if (to.name) NProgress.start();
// 全局监听 URL 参数 token若存在则写入缓存与登录成功同一方法不在此处 redirect避免先跳转再 init 导致 tagsView 首次丢失
const urlToken = to.query?.token as string | undefined;
if (urlToken) {
useUserInfo().setTokenCache(urlToken, to.query?.refresh_token as string | undefined);
}
// 若上面刚写了 token后续用去掉 token 的 query 做一次 replace在 init 之后统一做保证只一次导航、tagsView 能正确加 tag
const stripTokenQuery =
urlToken && to.query
? Object.fromEntries(Object.entries(to.query).filter(([k]) => k !== 'token' && k !== 'refresh_token'))
: null;
const token = Session.getToken();
if (to.meta.isAuth !== undefined && !to.meta.isAuth) {
next();
@@ -117,9 +130,16 @@ router.beforeEach(async (to, from, next) => {
if (routesList.value.length === 0) {
// 后端控制路由:路由数据初始化,防止刷新时丢失
await initBackEndControlRoutes();
next({ path: to.path, query: to.query });
// 只传 path/params/query让路由器用刚添加的动态路由重新解析避免沿用进入守卫时解析到的 404
const query = stripTokenQuery ?? to.query;
next({ path: to.path, params: to.params, query, replace: true });
} else {
next();
// 已有路由列表,若本次是带 token 的 URL做一次 replace 去掉地址栏 token
if (stripTokenQuery) {
next({ path: to.path, params: to.params, query: stripTokenQuery, replace: true });
} else {
next();
}
}
}
}

View File

@@ -20,6 +20,18 @@ export const useUserInfo = defineStore('userInfo', {
}),
actions: {
/**
* 统一写入 token 到缓存登录成功、URL 参数 token 等共用)
* @param token 访问令牌
* @param refreshToken 刷新令牌,可选
*/
setTokenCache(token: string, refreshToken?: string) {
Session.set('token', token);
if (refreshToken != null && refreshToken !== '') {
Session.set('refresh_token', refreshToken);
}
},
/**
* 登录方法
* @function login
@@ -35,9 +47,7 @@ export const useUserInfo = defineStore('userInfo', {
login(data)
.then((res) => {
debugger
// 存储token 信息
Session.set('token', res.access_token);
Session.set('refresh_token', res.refresh_token);
this.setTokenCache(res.access_token, res.refresh_token);
Local.remove('roleCode');
Local.remove('roleName');
Local.remove('roleId');
@@ -61,9 +71,7 @@ export const useUserInfo = defineStore('userInfo', {
return new Promise((resolve, reject) => {
loginByMobile(data.mobile, data.code)
.then((res) => {
// 存储token 信息
Session.set('token', res.access_token);
Session.set('refresh_token', res.refresh_token);
this.setTokenCache(res.access_token, res.refresh_token);
resolve(res);
})
.catch((err) => {
@@ -85,9 +93,7 @@ export const useUserInfo = defineStore('userInfo', {
return new Promise((resolve, reject) => {
loginBySocial(state, code)
.then((res) => {
// 存储token 信息
Session.set('token', res.access_token);
Session.set('refresh_token', res.refresh_token);
this.setTokenCache(res.access_token, res.refresh_token);
resolve(res);
})
.catch((err) => {
@@ -108,9 +114,7 @@ export const useUserInfo = defineStore('userInfo', {
const refreshToken = Session.get('refresh_token');
refreshTokenApi(refreshToken)
.then((res) => {
// 存储token 信息
Session.set('token', res.access_token);
Session.set('refresh_token', res.refresh_token);
this.setTokenCache(res.access_token, res.refresh_token);
resolve(res);
})
.catch((err) => {

View File

@@ -2248,10 +2248,10 @@
fetchSecondTree()
await Promise.all([
// 批量获取字典数据 religious_belief宗教信仰 health健康状况 teacher_cate授课类型 teacher_classify职位类别 yes_no_type是否类型
getDictsByTypes(['religious_belief', 'heath', 'teacher_cate', 'teacher_classify', 'yes_no_type']).then((response: any) => {
getDictsByTypes(['religious_belief', 'heath', 'professional_teacher_cate', 'teacher_classify', 'yes_no_type']).then((response: any) => {
religiousBeliefDic.value = response.data.religious_belief;
healthList.value = response.data.heath;
teacherCateList.value = response.data.teacher_cate;
teacherCateList.value = response.data.professional_teacher_cate;
teacherClassifyData.value = response.data.teacher_classify || [];
inoutFlagOptions.value = response.data.yes_no_type || [];
}),

View File

@@ -1099,10 +1099,10 @@ const init = (id: string | null, typeParam: number, groupId?: string) => {
areaHomeAreaList.value = []
// 批量获取数据字典(新增和编辑都需要) 文化程度 学费 代办费 户口性质 学校归属地
getDictsByTypes(['id_type','finance_student_source', 'agency_fee', 'tuition_fee','house_hold_properties','recruit_school_form']).then((res: any) => {
getDictsByTypes(['id_type','finance_student_source', 'recruit_agency_fee', 'tuition_fee','house_hold_properties','recruit_school_form']).then((res: any) => {
idCardTypeList.value = res.data.id_type || []
eduList.value = res.data.finance_student_source || []
agencyFeeList.value = res.data.agency_fee || []
agencyFeeList.value = res.data.recruit_agency_fee || []
tuitionFeeList.value = res.data.tuition_fee || []
residenceTypeList.value = res.data.house_hold_properties || []
schoolFromList.value = res.data.recruit_school_form || []

View File

@@ -103,7 +103,7 @@ const init = () => {
canSubmit.value = true
circleShow.value = true
nextTick(() => {
getDicts("dorm_jw").then((data: any) => {
getDicts("recruit_dorm_jw").then((data: any) => {
const arr = data.data
arr.forEach((e: any) => {
if (e.label == 'bj') {

View File

@@ -311,7 +311,7 @@ const init = (id: string | null) => {
dataFormRef.value?.resetFields()
if (dataForm.id) {
// 获取数据字典代办费
getDicts('agency_fee').then((res: any) => {
getDicts('recruit_agency_fee').then((res: any) => {
agencyFeeList.value = res.data
getObj(dataForm.id).then((response: any) => {
Object.assign(dataForm, response.data)

View File

@@ -56,7 +56,7 @@ const init = (row: any) => {
form.homeLng = row.homeLng || row.homeLongitude || 0
form.homeLat = row.homeLat || row.homeLatitude || 0
nextTick(() => {
getDicts("dorm_jw").then((data: any) => {
getDicts("recruit_dorm_jw").then((data: any) => {
const arr = data.data
arr.forEach((e: any) => {
if (e.label == 'bj') {

View File

@@ -62,6 +62,7 @@ const viteConfig = defineConfig((mode: ConfigEnv) => {
port: env.VITE_PORT as unknown as number, // 服务器端口号
open: env.VITE_OPEN === 'true', // 是否自动打开浏览器
hmr: true, // 启用热更新
allowedHosts: ['mycomputer.top'],
proxy: {
'/api': {
target: env.VITE_ADMIN_PROXY_PATH, // 目标服务器地址