Files
cmanager/src/store/modules/user.js

296 lines
8.6 KiB
JavaScript
Raw Normal View History

2024-02-20 10:30:29 +08:00
import {
setToken,
setRefreshToken,
removeToken,
removeRefreshToken,
} from "@/util/auth";
import { Message } from "element-ui";
import { setStore, getStore } from "@/util/store";
import { isURL, validatenull } from "@/util/validate";
import { deepClone } from "@/util/util";
import website from "@/config/website";
import {
loginByUsername,
getUserInfo,
logout,
refreshToken,
getButtons,
} from "@/api/user";
import { getTopMenu, getRoutes } from "@/api/system/menu";
import md5 from "js-md5";
2024-02-02 15:04:47 +08:00
function addPath(ele, first) {
const menu = website.menu;
const propsConfig = menu.props;
const propsDefault = {
2024-02-20 10:30:29 +08:00
label: propsConfig.label || "name",
path: propsConfig.path || "path",
icon: propsConfig.icon || "icon",
children: propsConfig.children || "children",
};
2024-02-02 15:04:47 +08:00
const icon = ele[propsDefault.icon];
ele[propsDefault.icon] = validatenull(icon) ? menu.iconDefault : icon;
2024-02-20 10:30:29 +08:00
const isChild =
ele[propsDefault.children] && ele[propsDefault.children].length !== 0;
2024-02-02 15:04:47 +08:00
if (!isChild) ele[propsDefault.children] = [];
if (!isChild && first && !isURL(ele[propsDefault.path])) {
2024-02-20 10:30:29 +08:00
ele[propsDefault.path] = ele[propsDefault.path] + "/index";
2024-02-02 15:04:47 +08:00
} else {
2024-02-20 10:30:29 +08:00
ele[propsDefault.children].forEach((child) => {
2024-02-02 15:04:47 +08:00
addPath(child);
2024-02-20 10:30:29 +08:00
});
2024-02-02 15:04:47 +08:00
}
}
const user = {
state: {
2024-02-20 10:30:29 +08:00
tenantId: getStore({ name: "tenantId" }) || "",
userInfo: getStore({ name: "userInfo" }) || [],
permission: getStore({ name: "permission" }) || {},
2024-02-02 15:04:47 +08:00
roles: [],
2024-02-20 10:30:29 +08:00
menu: getStore({ name: "menu" }) || [],
menuId: getStore({ name: "menuId" }) || [],
menuAll: getStore({ name: "menuAll" }) || [],
token: getStore({ name: "token" }) || "",
refreshToken: getStore({ name: "refreshToken" }) || "",
auth: getStore({ name: "auth" }) || "",
2024-02-02 15:04:47 +08:00
},
actions: {
//根据用户名登录
2024-02-20 10:30:29 +08:00
LoginByUsername({ commit }, userInfo) {
2024-02-02 15:04:47 +08:00
return new Promise((resolve, reject) => {
2024-02-20 10:30:29 +08:00
loginByUsername(
userInfo.username,
md5(userInfo.password),
userInfo.type,
userInfo.key,
userInfo.code,
userInfo.loginType
)
.then((res) => {
const data = res.data;
if (data.error_description) {
Message({
message: data.error_description,
type: "error",
});
} else {
// 判断是否为客服
data.is_admin =
data.role_name === "operate" || data.role_name === "muser";
// 判断是否为管理员账号
data.is_master = ["administrator", "admin", "operate"].includes(
data.role_name
);
commit("SET_AUTH", data.auth);
commit("SET_TOKEN", data.access_token);
commit("SET_REFRESH_TOKEN", data.refresh_token);
commit("SET_TENANT_ID", data.tenant_id);
commit("SET_USER_INFO", data);
commit("DEL_ALL_TAG");
commit("CLEAR_LOCK");
}
resolve();
})
.catch((error) => {
reject(error);
});
});
2024-02-02 15:04:47 +08:00
},
2024-02-20 10:30:29 +08:00
GetButtons({ commit }) {
2024-02-02 15:04:47 +08:00
return new Promise((resolve) => {
2024-02-20 10:30:29 +08:00
getButtons().then((res) => {
2024-02-02 15:04:47 +08:00
const data = res.data.data;
2024-02-20 10:30:29 +08:00
commit("SET_PERMISSION", data);
2024-02-02 15:04:47 +08:00
resolve();
2024-02-20 10:30:29 +08:00
});
});
2024-02-02 15:04:47 +08:00
},
//根据手机号登录
2024-02-20 10:30:29 +08:00
LoginByPhone({ commit }, userInfo) {
2024-02-02 15:04:47 +08:00
return new Promise((resolve) => {
2024-02-20 10:30:29 +08:00
loginByUsername(userInfo.phone, userInfo.code).then((res) => {
2024-02-02 15:04:47 +08:00
const data = res.data.data;
2024-02-20 10:30:29 +08:00
commit("SET_TOKEN", data);
commit("DEL_ALL_TAG");
commit("CLEAR_LOCK");
2024-02-02 15:04:47 +08:00
resolve();
2024-02-20 10:30:29 +08:00
});
});
2024-02-02 15:04:47 +08:00
},
2024-02-20 10:30:29 +08:00
GetUserInfo({ commit }) {
2024-02-02 15:04:47 +08:00
return new Promise((resolve, reject) => {
2024-02-20 10:30:29 +08:00
getUserInfo()
.then((res) => {
const data = res.data.data;
commit("SET_ROLES", data.roles);
resolve(data);
})
.catch((err) => {
reject(err);
});
});
2024-02-02 15:04:47 +08:00
},
//刷新token
2024-02-20 10:30:29 +08:00
refreshToken({ state, commit }) {
window.console.log("handle refresh token");
2024-02-02 15:04:47 +08:00
return new Promise((resolve, reject) => {
var authType = state.userInfo.login_type;
2024-02-20 10:30:29 +08:00
refreshToken(state.refreshToken, state.tenantId, authType)
.then((res) => {
const data = res.data;
commit("SET_TOKEN", data.access_token);
commit("SET_REFRESH_TOKEN", data.refresh_token);
resolve();
})
.catch((error) => {
reject(error);
});
});
2024-02-02 15:04:47 +08:00
},
// 登出
2024-02-20 10:30:29 +08:00
LogOut({ commit }) {
2024-02-02 15:04:47 +08:00
return new Promise((resolve, reject) => {
2024-02-20 10:30:29 +08:00
logout()
.then(() => {
commit("SET_AUTH", "");
commit("SET_TOKEN", "");
commit("SET_MENU", []);
commit("SET_MENU_ID", {});
commit("SET_MENU_ALL", []);
commit("SET_ROLES", []);
commit("DEL_ALL_TAG");
commit("CLEAR_LOCK");
commit("RESET_CONFIG");
removeToken();
removeRefreshToken();
resolve();
})
.catch((error) => {
reject(error);
});
});
2024-02-02 15:04:47 +08:00
},
//注销session
2024-02-20 10:30:29 +08:00
FedLogOut({ commit }) {
return new Promise((resolve) => {
commit("SET_TOKEN", "");
commit("SET_MENU_ID", {});
commit("SET_MENU_ALL", []);
commit("SET_MENU", []);
commit("SET_ROLES", []);
commit("DEL_ALL_TAG");
commit("CLEAR_LOCK");
2024-02-02 15:04:47 +08:00
removeToken();
removeRefreshToken();
resolve();
2024-02-20 10:30:29 +08:00
});
2024-02-02 15:04:47 +08:00
},
//获取顶部菜单
GetTopMenu() {
2024-02-20 10:30:29 +08:00
return new Promise((resolve) => {
2024-02-02 15:04:47 +08:00
getTopMenu().then((res) => {
const data = res.data.data || [];
2024-02-20 10:30:29 +08:00
resolve(data);
});
});
2024-02-02 15:04:47 +08:00
},
//获取系统菜单
2024-02-20 10:30:29 +08:00
GetMenu({ commit, dispatch }, topMenuId) {
return new Promise((resolve) => {
2024-02-02 15:04:47 +08:00
getRoutes(topMenuId).then((res) => {
2024-02-20 10:30:29 +08:00
const data = res.data.data;
2024-02-02 15:04:47 +08:00
let menu = deepClone(data);
2024-02-20 10:30:29 +08:00
menu.forEach((ele) => {
2024-02-02 15:04:47 +08:00
addPath(ele, true);
});
2024-02-20 10:30:29 +08:00
commit("SET_MENU", menu);
dispatch("GetButtons");
resolve(menu);
});
});
2024-02-02 15:04:47 +08:00
},
},
mutations: {
SET_TOKEN: (state, token) => {
setToken(token);
state.token = token;
2024-02-20 10:30:29 +08:00
setStore({ name: "token", content: state.token, type: "session" });
2024-02-02 15:04:47 +08:00
},
SET_MENU_ID(state, menuId) {
state.menuId = menuId;
2024-02-20 10:30:29 +08:00
setStore({ name: "menuId", content: state.menuId, type: "session" });
2024-02-02 15:04:47 +08:00
},
SET_MENU_ALL: (state, menuAll) => {
2024-02-20 10:30:29 +08:00
state.menuAll = menuAll;
setStore({ name: "menuAll", content: state.menuAll, type: "session" });
2024-02-02 15:04:47 +08:00
},
SET_REFRESH_TOKEN: (state, refreshToken) => {
2024-02-20 10:30:29 +08:00
setRefreshToken(refreshToken);
2024-02-02 15:04:47 +08:00
state.refreshToken = refreshToken;
2024-02-20 10:30:29 +08:00
setStore({
name: "refreshToken",
content: state.refreshToken,
type: "session",
});
2024-02-02 15:04:47 +08:00
},
SET_TENANT_ID: (state, tenantId) => {
state.tenantId = tenantId;
2024-02-20 10:30:29 +08:00
setStore({ name: "tenantId", content: state.tenantId, type: "session" });
2024-02-02 15:04:47 +08:00
},
SET_USER_INFO: (state, userInfo) => {
state.userInfo = userInfo;
2024-02-20 10:30:29 +08:00
setStore({ name: "userInfo", content: state.userInfo });
2024-02-02 15:04:47 +08:00
},
SET_MENU: (state, menu) => {
state.menu = menu;
let menuAll = state.menuAll;
if (!validatenull(menu)) {
2024-02-20 10:30:29 +08:00
const obj = menuAll.filter((ele) => ele.path === menu[0].path)[0];
2024-02-02 15:04:47 +08:00
if (!obj) {
menuAll = menuAll.concat(menu);
2024-02-20 10:30:29 +08:00
state.menuAll = menuAll;
2024-02-02 15:04:47 +08:00
}
2024-02-20 10:30:29 +08:00
setStore({ name: "menuAll", content: menuAll, type: "session" });
2024-02-02 15:04:47 +08:00
}
2024-02-20 10:30:29 +08:00
setStore({ name: "menu", content: state.menu, type: "session" });
2024-02-02 15:04:47 +08:00
},
SET_ROLES: (state, roles) => {
state.roles = roles;
},
SET_PERMISSION: (state, permission) => {
let result = [];
function getCode(list) {
2024-02-20 10:30:29 +08:00
list.forEach((ele) => {
if (typeof ele === "object") {
2024-02-02 15:04:47 +08:00
const chiildren = ele.children;
const code = ele.code;
if (chiildren) {
2024-02-20 10:30:29 +08:00
getCode(chiildren);
2024-02-02 15:04:47 +08:00
} else {
result.push(code);
}
}
2024-02-20 10:30:29 +08:00
});
2024-02-02 15:04:47 +08:00
}
getCode(permission);
state.permission = {};
2024-02-20 10:30:29 +08:00
result.forEach((ele) => {
2024-02-02 15:04:47 +08:00
state.permission[ele] = true;
});
2024-02-20 10:30:29 +08:00
setStore({
name: "permission",
content: state.permission,
type: "session",
});
2024-02-02 15:04:47 +08:00
},
2024-02-20 10:30:29 +08:00
SET_AUTH(state, auth) {
state.auth = auth;
setStore({ name: "auth", content: auth, type: "session" });
},
},
};
export default user;