mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-11-09 13:53:38 +08:00
refactor: axios methods and env
This commit is contained in:
@@ -33,3 +33,8 @@ export type setType = {
|
||||
fixedHeader: boolean;
|
||||
hiddenSideBar: boolean;
|
||||
};
|
||||
|
||||
export type userType = {
|
||||
token: string;
|
||||
name?: string;
|
||||
};
|
||||
|
||||
84
src/store/modules/user.ts
Normal file
84
src/store/modules/user.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { store } from "/@/store";
|
||||
import { userType } from "./types";
|
||||
import { useRouter } from "vue-router";
|
||||
import { getLogin, refreshToken } from "/@/api/user";
|
||||
import { storageLocal, storageSession } from "/@/utils/storage";
|
||||
import { getToken, setToken, removeToken } from "/@/utils/auth";
|
||||
import { useMultiTagsStoreHook } from "/@/store/modules/multiTags";
|
||||
|
||||
const data = getToken();
|
||||
let token = "";
|
||||
let name = "";
|
||||
if (data) {
|
||||
const dataJson = JSON.parse(data);
|
||||
if (dataJson) {
|
||||
token = dataJson?.accessToken;
|
||||
name = dataJson?.name ?? "admin";
|
||||
}
|
||||
}
|
||||
|
||||
export const useUserStore = defineStore({
|
||||
id: "pure-user",
|
||||
state: (): userType => ({
|
||||
token,
|
||||
name
|
||||
}),
|
||||
actions: {
|
||||
SET_TOKEN(token) {
|
||||
this.token = token;
|
||||
},
|
||||
SET_NAME(name) {
|
||||
this.name = name;
|
||||
},
|
||||
// 登入
|
||||
async loginByUsername(data) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
getLogin(data)
|
||||
.then(data => {
|
||||
if (data) {
|
||||
setToken(data);
|
||||
resolve();
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
},
|
||||
// 登出 清空缓存
|
||||
logOut() {
|
||||
this.token = "";
|
||||
this.name = "";
|
||||
removeToken();
|
||||
storageLocal.clear();
|
||||
storageSession.clear();
|
||||
useMultiTagsStoreHook().handleTags("equal", [
|
||||
{
|
||||
path: "/welcome",
|
||||
parentPath: "/",
|
||||
meta: {
|
||||
title: "message.hshome",
|
||||
icon: "el-icon-s-home",
|
||||
i18n: true,
|
||||
showLink: true
|
||||
}
|
||||
}
|
||||
]);
|
||||
useRouter().push("/login");
|
||||
},
|
||||
// 刷新token
|
||||
async refreshToken(data) {
|
||||
return refreshToken(data).then(data => {
|
||||
if (data) {
|
||||
setToken(data);
|
||||
return data;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export function useUserStoreHook() {
|
||||
return useUserStore(store);
|
||||
}
|
||||
Reference in New Issue
Block a user