import Setting from "@/setting"; import util from "@/libs/util"; import { post, get, del, put } from "@/plugins/requests/index.js"; import api from "@/api"; import addRoutes from "@/libs/route/addRoutes"; /** * 用户信息 * */ export default { namespaced: true, state: { customer: false, // true:为客户,没有个人中心 customerName: "", // 客户名称 title: "", logoUrl: "", avatar: "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png", userId: 3, userName: "", roleId: "", dataTime: "" }, mutations: { SET_INFO: (state, info) => { if (info.avatar) state.avatar = info.userAvatars; state.userId = info.userId; state.userName = info.userName; state.roleId = info.roleId; state.dataTime = info.dataTime; }, SET_CUSTOMER: (state, customer) => { state.customer = customer; }, SET_CUSTOMER_NAME: (state, customerName) => { state.customerName = customerName; }, SET_TITLE: (state, title) => { state.title = title; }, SET_LOGO_URL: (state, logoUrl) => { state.logoUrl = logoUrl; }, SET_AVATAR: (state, avatar) => { state.avatar = avatar; }, SET_USERNAME: (state, userName) => { state.userName = userName; } }, actions: { login({ state, commit }, userInfo) { return new Promise((resolve, reject) => { post(api.logins, userInfo).then(res => { console.log(JSON.stringify(res)); if (res.status == 200) { let { data } = res; // 生成路由权限 // 每个系统都是用这套角色权限代码的,后端也是基本一样,除了个别字段可能会不一样 // 总体来说就两个步骤: 1是生成能够访问的路由的数组集合,2是生成能看到的按钮的数组集合 // res.message.listValue && Setting.dynamicRoute && addRoutes(res.message.listValue) // 2021-10-13重做权限,因权限系统封装 util.local.set(Setting.tokenKey, data.token, Setting.tokenExpires); commit("SET_CUSTOMER", data.customer); commit("SET_CUSTOMER_NAME", data.customerName); util.successMsg("登录成功"); console.log(data, "登录保存的数据"); commit("SET_INFO", data); resolve(); } else { util.errorMsg(res.errmessage || res.message); reject(res); } }).catch(error => { reject(error); }); }); }, logout({ commit, state, dispatch }) { return new Promise((resolve, reject) => { util.local.remove(Setting.storeKey); util.local.remove(Setting.tokenKey); location.reload(); resolve(); }); }, setInfo({ state, commit }, info) { commit("SET_INFO", info); }, setTitle({ state, commit }, title) { commit("SET_TITLE", title); }, setLogoUrl({ state, commit }, logoUrl) { commit("SET_LOGO_URL", logoUrl); }, setAvatar({ state, commit }, avatar) { commit("SET_AVATAR", avatar); }, setUserName({ state, commit }, userName) { commit("SET_USERNAME", userName); } } };