初始提交
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
import { createPinia } from "pinia";
|
||||
import { useUserStore } from "./modules/user";
|
||||
import { useConfigStore } from "./modules/config";
|
||||
|
||||
const pinia = createPinia();
|
||||
|
||||
export default pinia;
|
||||
|
||||
export { useUserStore, useConfigStore };
|
||||
@@ -0,0 +1,13 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { ref } from "vue";
|
||||
|
||||
export const useConfigStore = defineStore("config", () => {
|
||||
const config = ref();
|
||||
const setConfig = (val) => {
|
||||
config.value = val;
|
||||
};
|
||||
return {
|
||||
config,
|
||||
setConfig,
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
import { defineStore } from "pinia";
|
||||
|
||||
const useDictStore = defineStore("dict", {
|
||||
state: () => ({
|
||||
dict: new Array(),
|
||||
}),
|
||||
actions: {
|
||||
// 获取字典
|
||||
getDict(_key) {
|
||||
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, value) {
|
||||
if (_key !== null && _key !== "") {
|
||||
this.dict.push({
|
||||
key: _key,
|
||||
value: value,
|
||||
});
|
||||
}
|
||||
},
|
||||
// 删除字典
|
||||
removeDict(_key) {
|
||||
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;
|
||||
@@ -0,0 +1,125 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { ref } from "vue";
|
||||
import config from "@/config";
|
||||
import storage from "@/utils/storage";
|
||||
import constant from "@/utils/constant";
|
||||
import { isHttp, isEmpty } from "@/utils/validate";
|
||||
import { getInfo, login, logout } from "@/api/login";
|
||||
import { getToken, removeToken, setToken } from "@/utils/auth";
|
||||
import defAva from "@/static/images/profile.jpg";
|
||||
|
||||
const baseUrl = config.baseUrl;
|
||||
|
||||
export const useUserStore = defineStore("user", () => {
|
||||
const token = ref(getToken());
|
||||
const id = ref(storage.get(constant.id));
|
||||
const name = ref(storage.get(constant.name));
|
||||
const avatar = ref(storage.get(constant.avatar));
|
||||
const roles = ref(storage.get(constant.roles));
|
||||
const permissions = ref(storage.get(constant.permissions));
|
||||
|
||||
const SET_TOKEN = (val) => {
|
||||
token.value = val;
|
||||
};
|
||||
const SET_ID = (val) => {
|
||||
id.value = val;
|
||||
storage.set(constant.id, val);
|
||||
};
|
||||
const SET_NAME = (val) => {
|
||||
name.value = val;
|
||||
storage.set(constant.name, val);
|
||||
};
|
||||
const SET_AVATAR = (val) => {
|
||||
avatar.value = val;
|
||||
storage.set(constant.avatar, val);
|
||||
};
|
||||
const SET_ROLES = (val) => {
|
||||
roles.value = val;
|
||||
storage.set(constant.roles, val);
|
||||
};
|
||||
const SET_PERMISSIONS = (val) => {
|
||||
permissions.value = val;
|
||||
storage.set(constant.permissions, val);
|
||||
};
|
||||
|
||||
// 登录
|
||||
const loginAction = (userInfo) => {
|
||||
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) => {
|
||||
setToken(res.token);
|
||||
SET_TOKEN(res.token);
|
||||
resolve();
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// 获取用户信息
|
||||
const getInfoAction = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
getInfo()
|
||||
.then((res) => {
|
||||
const user = res.user;
|
||||
let avatar = user.avatar || "";
|
||||
if (!isHttp(avatar)) {
|
||||
avatar = isEmpty(avatar) ? defAva : baseUrl + avatar;
|
||||
}
|
||||
const userid =
|
||||
isEmpty(user) || isEmpty(user.userId) ? "" : user.userId;
|
||||
const username =
|
||||
isEmpty(user) || isEmpty(user.userName) ? "" : user.userName;
|
||||
if (res.roles && res.roles.length > 0) {
|
||||
SET_ROLES(res.roles);
|
||||
SET_PERMISSIONS(res.permissions);
|
||||
} else {
|
||||
SET_ROLES(["ROLE_DEFAULT"]);
|
||||
}
|
||||
SET_ID(userid);
|
||||
SET_NAME(username);
|
||||
SET_AVATAR(avatar);
|
||||
resolve(res);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// 退出系统
|
||||
const logOutAction = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
logout(token.value)
|
||||
.then(() => {
|
||||
SET_TOKEN("");
|
||||
SET_ROLES([]);
|
||||
SET_PERMISSIONS([]);
|
||||
removeToken();
|
||||
storage.clean();
|
||||
resolve();
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
token,
|
||||
id,
|
||||
name,
|
||||
avatar,
|
||||
roles,
|
||||
permissions,
|
||||
SET_AVATAR,
|
||||
login: loginAction,
|
||||
getInfo: getInfoAction,
|
||||
logOut: logOutAction,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user