import { login, logout, getInfo } from "@/api/login"; import { getToken, setToken, removeToken } from "@/utils/auth"; import defAva from "@/assets/images/profile.jpg"; import Cookies from 'js-cookie' const useUserStore = defineStore("user", { state: () => ({ token: getToken(), name: "", avatar: "", roles: [], permissions: [], TitleIndex: Cookies.get('TitleIndex') || 2, // 当前顶部标题的索引值 XunHounNb: Cookies.get('XunHounNb') || {}, //防汛专题 选中为讯后总结后 出现的下拉选择项 fengxiantuNb: Cookies.get('fengxiantuNb') || {}, //防汛专题 选中为讯后总结后 出现的下拉选择项 iscockpit: Cookies.get("iscockpit") && JSON.parse(Cookies.get('iscockpit')) || false, //判断是否是驾驶舱页面 isHome: Cookies.get("isHome") && JSON.parse(Cookies.get("isHome")) || false, //判断当前路由是否是首页 isMenu: Cookies.get("isMenu") && JSON.parse(Cookies.get("isMenu")) || false, topMenu: Cookies.get('topMenu') || [], userInfo: {}, }), actions: { // 登录 login (userInfo) { const username = userInfo.username.trim(); const password = userInfo.password; const code = userInfo.code; const uuid = userInfo.uuid; const usertype = '00' return new Promise((resolve, reject) => { login(username, password, code, uuid, usertype) .then((res) => { let data = res.data; setToken(data.access_token); console.log(this, 'this'); this.token = data.access_token; resolve(); }) .catch((error) => { reject(error); }); }); }, // 获取用户信息 getInfo () { return new Promise((resolve, reject) => { getInfo() .then((res) => { const data = res.data; this.userInfo = data.user; const avatar = data.user.avatar == "" || data.user.avatar == null ? defAva : data.user.avatar; if (data.roleIds && data.roleIds.length > 0) { // 验证返回的roles是否是一个非空数组 this.roles = data.roleIds; this.permissions = data.permissions; } else { this.roles = ["ROLE_DEFAULT"]; } this.name = data.user.nickName; this.avatar = avatar; resolve(res); }) .catch((error) => { reject(error); }); }); }, // 退出系统 logOut () { return new Promise((resolve, reject) => { logout(this.token) .then(() => { this.token = ""; this.roles = []; this.permissions = []; removeToken(); resolve(); }) .catch((error) => { reject(error); }); }); }, // 当前专题选中的索引 SET_Top_TitleIndex (TitleIndex) { this.TitleIndex = TitleIndex; Cookies.set('TitleIndex', TitleIndex) }, SET_IS_MENU (isMenu) { console.log(this, 'this'); this.isMenu = isMenu; Cookies.set('isMenu', isMenu) }, SET_ISHOME (isHome) { this.isHome = isHome; Cookies.set('isHome', isHome) }, // 通过此数据改变大屏的高度属性以及隐藏头部 SET_Top_iscockpit (iscockpit) { this.iscockpit = iscockpit; Cookies.set('iscockpit', iscockpit) }, SET_TOPMENU (topMenu) { this.topMenu = topMenu; Cookies.set('topMenu', topMenu) }, }, }); export default useUserStore;