feat: 初始化版本!
This commit is contained in:
4
src/store/index.ts
Normal file
4
src/store/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { createPinia } from "pinia"
|
||||
|
||||
const store = createPinia()
|
||||
export default store
|
||||
55
src/store/modules/dict.ts
Normal file
55
src/store/modules/dict.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { defineStore } from "pinia";
|
||||
const useDictStore = defineStore("dict", {
|
||||
state: () => ({
|
||||
dict: new Array(),
|
||||
}),
|
||||
actions: {
|
||||
// 获取字典
|
||||
getDict(_key: string) {
|
||||
if (_key == null && _key == "") {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
for (let i = 0; i < this.dict.length; i++) {
|
||||
if (this.dict[i].key == _key) {
|
||||
return this.dict[i].value;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
// 设置字典
|
||||
setDict(_key: string, value: any) {
|
||||
if (_key !== null && _key !== "") {
|
||||
this.dict.push({
|
||||
key: _key,
|
||||
value: value,
|
||||
});
|
||||
}
|
||||
},
|
||||
// 删除字典
|
||||
removeDict(_key: string) {
|
||||
var bln = false;
|
||||
try {
|
||||
for (let i = 0; i < this.dict.length; i++) {
|
||||
if (this.dict[i].key == _key) {
|
||||
this.dict.splice(i, 1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
bln = false;
|
||||
}
|
||||
return bln;
|
||||
},
|
||||
// 清空字典
|
||||
cleanDict() {
|
||||
this.dict = new Array();
|
||||
},
|
||||
// 初始字典
|
||||
initDict() {},
|
||||
},
|
||||
});
|
||||
|
||||
export default useDictStore;
|
||||
88
src/store/modules/user.ts
Normal file
88
src/store/modules/user.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { login, logout, getInfo } from "@/api/login";
|
||||
import { getToken, setToken, removeToken } from "@/utils/auth";
|
||||
import defAva from "@/static/images/profile.jpg";
|
||||
import { defineStore } from "pinia";
|
||||
|
||||
export interface LoginForm {
|
||||
username: string;
|
||||
password: string;
|
||||
code: string;
|
||||
uuid: string;
|
||||
}
|
||||
|
||||
const useUserStore = defineStore("user", {
|
||||
state: () => ({
|
||||
token: getToken(),
|
||||
name: "",
|
||||
avatar: "",
|
||||
roles: Array(),
|
||||
permissions: [],
|
||||
}),
|
||||
actions: {
|
||||
// 登录
|
||||
login(userInfo: LoginForm) {
|
||||
const username = userInfo.username.trim();
|
||||
const password = userInfo.password;
|
||||
const code = userInfo.code;
|
||||
const uuid = userInfo.uuid;
|
||||
return new Promise((resolve, reject) => {
|
||||
login(username, password, code, uuid)
|
||||
.then((res: any) => {
|
||||
setToken(res.data.access_token);
|
||||
this.token = res.data.access_token;
|
||||
resolve(null);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
},
|
||||
// 获取用户信息
|
||||
getInfo() {
|
||||
return new Promise((resolve, reject) => {
|
||||
getInfo()
|
||||
.then((res: any) => {
|
||||
const user = res.user;
|
||||
const avatar =
|
||||
user.avatar == "" || user.avatar == null
|
||||
? defAva
|
||||
: import.meta.env.VITE_APP_BASE_API + user.avatar;
|
||||
|
||||
if (res.roles && res.roles.length > 0) {
|
||||
// 验证返回的roles是否是一个非空数组
|
||||
this.roles = res.roles;
|
||||
this.permissions = res.permissions;
|
||||
} else {
|
||||
this.roles = ["ROLE_DEFAULT"];
|
||||
}
|
||||
this.name = user.userName;
|
||||
this.avatar = avatar;
|
||||
resolve(res);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
},
|
||||
// 退出系统
|
||||
logOut() {
|
||||
return new Promise<null>((resolve, reject) => {
|
||||
logout()
|
||||
.then(() => {
|
||||
this.token = "";
|
||||
this.roles = [];
|
||||
this.permissions = [];
|
||||
this.name = "";
|
||||
this.avatar = "";
|
||||
removeToken();
|
||||
resolve(null);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export default useUserStore;
|
||||
Reference in New Issue
Block a user