feat: 初始化版本!

This commit is contained in:
tianyongbao
2024-05-31 13:08:46 +08:00
parent 884a84802d
commit b3fe699735
587 changed files with 103758 additions and 27 deletions

55
src/store/modules/dict.ts Normal file
View 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
View 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;