优化拦截器

优化错误捕获
优化全局状态管理
配置全局公用类型
This commit is contained in:
tao
2025-12-19 15:54:26 +08:00
parent 71158afc35
commit 73dd0ae6b6
7 changed files with 141 additions and 53 deletions

View File

@@ -1,16 +1,16 @@
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import Cookies from 'js-cookie';
import { message } from 'ant-design-vue';
import Cookies from "js-cookie";
import { defineStore } from "pinia";
import { ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { message } from "ant-design-vue";
import { useUserStore } from './user';
import { login } from '@/api/system';
import type { LoginInfo } from '@/api/system/model';
import { useUserStore } from "./user";
import { login, logout as logoutApi } from "@/api/system";
import { TokenKey as TOKEN_KEY } from "@/utils/auth";
import type { LoginInfo } from "@/api/system/model";
const TOKEN_KEY = 'token';
export const useAuthStore = defineStore('auth', () => {
export const useAuthStore = defineStore("auth", () => {
const route = useRoute();
const router = useRouter();
const userStore = useUserStore();
@@ -34,12 +34,13 @@ export const useAuthStore = defineStore('auth', () => {
if (res.code === 200) {
setToken(res.token as string);
await userStore.fetchUserInfo();
message.success('登录成功');
await router.push('/');
message.success("登录成功");
const redirect = route.query.redirect || "/";
router.replace(redirect as string);
return res;
} else {
// 抛出错误,让调用方处理
throw new Error(res.msg || '登录失败');
throw new Error(res.msg || "登录失败");
}
} catch (error) {
throw error;
@@ -50,11 +51,11 @@ export const useAuthStore = defineStore('auth', () => {
async function logout() {
// 在实际应用中,这里可以调用后端的退出登录接口
// await doLogoutApi();
await logoutApi();
clearToken();
userStore.clearUserInfo();
await router.push('/login');
message.success('已成功退出');
await router.push("/login");
message.success("已成功退出");
}
return {

View File

@@ -1,2 +1,3 @@
export * from './auth';
export * from './user';
export * from './pwo';

28
src/store/pwo.ts Normal file
View File

@@ -0,0 +1,28 @@
import { defineStore } from "pinia";
import { ref, reactive } from "vue";
import { message } from "ant-design-vue";
export const usePwoStore = defineStore("pwo", () => {
const code = ref('');
const currentJob = ref({});
function setCode(traceOrderCode: string) {
code.value = traceOrderCode;
}
function setCurrentJob(job: any) {
currentJob.value = job;
}
function getCurrentJob() {
return currentJob.value;
}
return {
code,
currentJob,
setCode,
setCurrentJob,
getCurrentJob,
}
});