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, loginBySocial, getUserInfo, logout, refreshToken, getButtons } from '@/api/user' import { getByRoute, getTenantMenuByTenantIdAndRoleId, allByRole } from '@/api/lesseeLogin' import { getRoutes } from '@/api/system/menu' import { getList, getDictToWeb } from '@/api/system/dict' import md5 from 'js-md5' import { getUserAll } from '@/api/activiti/taskSetting' function addPath(ele, first) { const menu = website.menu const propsConfig = menu.props const propsDefault = { label: propsConfig.label || 'name', path: propsConfig.path || 'path', icon: propsConfig.icon || 'icon', children: propsConfig.children || 'children', } const icon = ele[propsDefault.icon] ele[propsDefault.icon] = validatenull(icon) ? menu.iconDefault : icon const isChild = ele[propsDefault.children] && ele[propsDefault.children].length !== 0 if (!isChild) ele[propsDefault.children] = [] if (!isChild && first && !isURL(ele[propsDefault.path])) { ele[propsDefault.path] = ele[propsDefault.path] // ele[propsDefault.path] = ele[propsDefault.path] + '/index' } else { ele[propsDefault.children].forEach((child) => { addPath(child) }) } } function toTree(data) { let result = [] if (!Array.isArray(data)) { return result } let map = {} data.forEach((item) => { map[item.id] = item }) data.forEach((item) => { let parent = map[item.parentId] if (parent) { if (!parent.children) { parent.children = [] } parent.children.push(item) } else { result.push(item) } }) return result } const user = { state: { tenantId: getStore({ name: 'tenantId' }) || '', userInfo: getStore({ name: 'userInfo' }) || [], permission: getStore({ name: 'permission' }) || {}, roles: [], menuId: {}, menu: getStore({ name: 'menu' }) || [], menuAll: getStore({ name: 'menuAll' }) || [], menuTop: getStore({ name: 'menuTop' }) || [], pitchOn: getStore({ name: 'pitchOn' }) || '', dictData: getStore({ name: 'dictData' }) || [], dictTenantData: getStore({ name: 'dictTenantData' }) || [], constantData: getStore({ name: 'constantData' }) || [], token: getStore({ name: 'token' }) || '', refreshToken: getStore({ name: 'refreshToken' }) || '', }, actions: { //获取所有字典 GetDictAll({ commit }) { return new Promise((resolve, reject) => { getList() .then((res) => { res.data.data = res.data.data.map((item) => { return { ...item, dictLabel: item.dictValue, dictValue: item.dictKey, } }) commit('SET_DICT_ALL', res.data.data) }) .catch((error) => { reject(error) }) getDictToWeb() .then((res) => { commit('SET_TENANT_DICT_ALL', res.data.data) }) .catch((error) => { reject(error) }) }) }, // 获取所有用户 getAllUserInformation({ commit }) { return new Promise(() => { getUserAll({}).then((res) => { commit('SET_USER_ALL', res.data.data.list) }) }) }, //租户业务员登录 SalesmanLogin({ commit, dispatch }, userInfo) { return new Promise((resolve, reject) => { var pwd = '' var noPwd = '' if (userInfo.pwd) { pwd = md5(userInfo.pwd) } else { noPwd = 'noPwd' } loginByUsername(userInfo.tenantId, userInfo.deptId, userInfo.roleId, userInfo.account, pwd, 'tenant_salesman', userInfo.key, userInfo.code, noPwd) .then((res) => { const data = res.data if (data.error_description) { Message({ message: data.error_description, type: 'error', }) } else { commit('SET_TOKEN', data.access_token) commit('SET_REFRESH_TOKEN', data.access_token) commit('SET_TENANT_ID', data.tenant_id) commit('SET_USER_INFO', data) commit('DEL_ALL_TAG') commit('CLEAR_LOCK') getTenantMenuByTenantIdAndRoleId().then((resS) => { let menuAll = deepClone(resS.data.data.list) menuAll.forEach((ele) => { addPath(ele, true) }) let pitchOn = '' if (data.applications.length > 0) { pitchOn = data.applications[0].id } let menu = deepClone(menuAll).filter(function (item) { return item.applicationId === pitchOn && item.category === 1 }) commit('SET_MENU_TOP', data.applications) commit('SET_SELECT', pitchOn) commit('SET_MENU_ALL', menuAll) commit('SET_MENU', toTree(menu)) dispatch('GetButtons') resolve() }) } }) .catch((error) => { reject(error) }) }) }, getSalesmanMenuAll({ commit, dispatch }, pitchOn) { return new Promise((resolve, reject) => { getTenantMenuByTenantIdAndRoleId() .then((res) => { let menuAll = deepClone(res.data.data.list) menuAll.forEach((ele) => { addPath(ele, true) }) let menu = deepClone(menuAll).filter(function (item) { return item.applicationId === pitchOn && item.category === 1 }) commit('SET_MENU_ALL', menuAll) commit('SET_MENU', toTree(menu)) dispatch('GetButtons') resolve() }) .catch((error) => { reject(error) }) }) }, //租户管理员登录 LesseeLogin({ commit, dispatch }, userInfo) { return new Promise((resolve, reject) => { var pwd = '' var noPwd = '' if (userInfo.pwd) { pwd = md5(userInfo.pwd) } else { noPwd = 'noPwd' } loginByUsername(userInfo.tenantId, userInfo.deptId, userInfo.roleId, userInfo.account, pwd, 'tenant_admin', userInfo.key, userInfo.code, noPwd) .then((res) => { const data = res.data if (data.error_description) { Message({ message: data.error_description, type: 'error', }) } else { commit('SET_TOKEN', data.access_token) commit('SET_REFRESH_TOKEN', data.access_token) commit('SET_TENANT_ID', data.tenant_id) commit('SET_USER_INFO', data) commit('DEL_ALL_TAG') commit('CLEAR_LOCK') getByRoute({ path: '/tenantControl' }).then((res) => { let menuAll = deepClone(res.data.data.list) menuAll.forEach((ele) => { addPath(ele, true) }) let menu = deepClone(menuAll).filter(function (item) { return item.id }) commit('SET_MENU_TOP', [{ name: '控制台', source: 'iconfont iconicon_safety', id: '' }]) commit('SET_SELECT', '') commit('SET_MENU_ALL', menuAll) commit('SET_MENU', toTree(menu)) dispatch('GetButtons') resolve() }) } }) .catch((error) => { reject(error) }) }) }, getLesseeMenuAll({ commit, dispatch }) { return new Promise((resolve, reject) => { getByRoute({ path: '/tenantControl' }) .then((res) => { let menuAll = deepClone(res.data.data.list) menuAll.forEach((ele) => { addPath(ele, true) }) let menu = deepClone(menuAll).filter(function (item) { return item.id }) commit('SET_MENU_ALL', menuAll) commit('SET_MENU', toTree(menu)) dispatch('GetButtons') resolve() }) .catch((error) => { reject(error) }) }) }, //服务商管理员登录 LoginByUsername({ commit, dispatch }, userInfo) { return new Promise((resolve, reject) => { loginByUsername('000000', userInfo.deptId, userInfo.roleId, userInfo.username, md5(userInfo.password), 'manage', userInfo.key, userInfo.code, '') .then((res) => { const data = res.data if (data.error_description) { Message({ message: data.error_description, type: 'error', }) } else { commit('SET_TOKEN', data.access_token) commit('SET_REFRESH_TOKEN', data.access_token) commit('SET_TENANT_ID', data.tenant_id) commit('SET_USER_INFO', data) commit('DEL_ALL_TAG') commit('CLEAR_LOCK') } allByRole().then((res) => { const data = res.data.data.details.topMenus || [] let pitchOn = '' if (data.length > 0) { pitchOn = data[0].id } let menuAll = deepClone(res.data.data.details.menus) menuAll.forEach((ele) => { addPath(ele, true) }) let menu = deepClone(menuAll).filter(function (item) { return item.topMenuId === pitchOn && item.category === 1 }) commit('SET_SELECT', pitchOn) commit('SET_MENU_TOP', data) commit('SET_MENU_ALL', menuAll) commit('SET_MENU', toTree(menu)) dispatch('GetButtons') resolve() }) }) .catch((error) => { reject(error) }) }) }, getAdminMenuAll({ commit, dispatch }, pitchOn) { return new Promise((resolve, reject) => { allByRole() .then((res) => { let menuAll = deepClone(res.data.data.details.menus) menuAll.forEach((ele) => { addPath(ele, true) }) let menu = deepClone(menuAll).filter(function (item) { return item.topMenuId === pitchOn && item.category === 1 }) commit('SET_MENU_ALL', menuAll) commit('SET_MENU', toTree(menu)) dispatch('GetButtons') resolve() }) .catch((error) => { reject(error) }) }) }, //根据手机号登录 LoginByPhone({ commit }, userInfo) { return new Promise((resolve) => { loginByUsername(userInfo.phone, userInfo.code).then((res) => { const data = res.data.data commit('SET_TOKEN', data) commit('DEL_ALL_TAG') commit('CLEAR_LOCK') resolve() }) }) }, //根据第三方信息登录 LoginBySocial({ commit }, userInfo) { return new Promise((resolve) => { loginBySocial(userInfo.tenantId, userInfo.source, userInfo.code, userInfo.state).then((res) => { const data = res.data if (data.error_description) { Message({ message: data.error_description, type: 'error', }) } else { commit('SET_TOKEN', data.access_token) commit('SET_REFRESH_TOKEN', data.access_token) commit('SET_USER_INFO', data) commit('DEL_ALL_TAG') commit('CLEAR_LOCK') } resolve() }) }) }, //获取用户信息 GetUserInfo({ commit }) { return new Promise((resolve, reject) => { getUserInfo() .then((res) => { const data = res.data.data commit('SET_ROLES', data.roles) resolve(data) }) .catch((err) => { reject(err) }) }) }, //刷新token refreshToken({ state, commit }) { window.console.log('handle refresh token') return new Promise((resolve, reject) => { refreshToken(state.refreshToken, state.tenantId) .then((res) => { const data = res.data commit('SET_TOKEN', data.access_token) commit('SET_REFRESH_TOKEN', data.access_token) resolve() }) .catch((error) => { reject(error) }) }) }, // 登出 LogOut({ commit }) { return new Promise((resolve, reject) => { logout() .then(() => { commit('SET_TOKEN', '') commit('SET_MENU', []) commit('SET_MENU_ALL_NULL', []) commit('SET_USER_ALL_NULL', []) commit('SET_ROLES', []) commit('SET_TAG_LIST', []) commit('DEL_ALL_TAG') commit('CLEAR_LOCK') removeToken() removeRefreshToken() localStorage.clear() resolve() }) .catch((error) => { reject(error) }) }) }, //注销session FedLogOut({ commit }) { return new Promise((resolve) => { commit('SET_TOKEN', '') commit('SET_MENU_ALL_NULL', []) commit('SET_USER_ALL_NULL', []) commit('SET_MENU', []) commit('SET_ROLES', []) commit('SET_TAG_LIST', []) commit('DEL_ALL_TAG') commit('CLEAR_LOCK') removeToken() removeRefreshToken() resolve() }) }, //获取顶部菜单 GetTopMenu({ commit, dispatch }) { return new Promise((resolve) => { allByRole().then((res) => { const data = res.data.data.details.topMenus || [] let pitchOn = '' if (data.length > 0) { pitchOn = data[0].id } let menuAll = deepClone(res.data.data.details.menus) menuAll.forEach((ele) => { addPath(ele, true) }) let menu = deepClone(menuAll).filter(function (item) { return item.topMenuId === pitchOn && item.category === 1 }) commit('SET_SELECT', pitchOn) commit('SET_MENU_ALL', menuAll) commit('SET_MENU', toTree(menu)) dispatch('GetButtons') resolve(data) }) }) }, //切换菜单 SwitchToMenu({ commit, dispatch }, data) { return new Promise((resolve) => { var menu = [] if (data.id) { menu = deepClone(data.menus).filter(function (item) { return (item.id === data.id || item.applicationId === data.id) && item.category === 1 }) } else { menu = deepClone(data.menus) } commit('SET_SELECT', data.id) commit('SET_MENU', toTree(menu)) dispatch('GetButtons') resolve(menu) }) }, //服务商切换菜单 AdminSwitchToMenu({ commit, dispatch }, data) { return new Promise((resolve) => { var menu = [] if (data.id) { menu = deepClone(data.menus).filter(function (item) { return item.topMenuId === data.id && item.category === 1 }) } else { menu = deepClone(data.menus) } commit('SET_SELECT', data.id) commit('SET_MENU', toTree(menu)) dispatch('GetButtons') resolve(menu) }) }, //获取系统菜单 GetMenu({ commit, dispatch }, topMenuId) { return new Promise((resolve) => { getRoutes(topMenuId).then((res) => { const data = res.data.data let menu = deepClone(data) menu.forEach((ele) => { addPath(ele, true) }) commit('SET_MENU', menu) dispatch('GetButtons') resolve(menu) }) }) }, //获取系统按钮 GetButtons({ commit }) { return new Promise((resolve) => { getButtons().then((res) => { const data = res.data.data commit('SET_PERMISSION', data) resolve() }) }) }, }, mutations: { SET_TOKEN: (state, token) => { setToken(token) state.token = token setStore({ name: 'token', content: state.token }) }, SET_MENU_ID(state, menuId) { state.menuId = menuId }, SET_MENU_ALL: (state, menuAll) => { let menu = state.menuAll menuAll.forEach((ele) => { if (!menu.find((item) => item.label === ele.label && item.path === ele.path && item.applicationId === ele.applicationId)) { menu.push(ele) } }) state.menuAll = menuAll setStore({ name: 'menuAll', content: state.menuAll }) }, SET_MENU_ALL_NULL: (state) => { state.menuAll = [] setStore({ name: 'menuAll', content: state.menuAll }) }, SET_USER_ALL: (state, userAll) => { state.userAll = userAll setStore({ name: 'userAll', content: state.userAll }) }, SET_USER_ALL_NULL: (state) => { state.userAll = [] setStore({ name: 'userAll', content: state.userAll }) }, SET_MENU: (state, menu) => { state.menu = menu setStore({ name: 'menu', content: state.menu }) }, SET_SELECT: (state, pitchOn) => { state.pitchOn = pitchOn setStore({ name: 'pitchOn', content: state.pitchOn }) }, SET_DICT_ALL: (state, dictData) => { state.dictData = dictData setStore({ name: 'dictData', content: state.dictData }) }, SET_TENANT_DICT_ALL: (state, dictTenantData) => { state.dictTenantData = dictTenantData setStore({ name: 'dictTenantData', content: state.dictTenantData }) }, SET_MENU_TOP: (state, menuTop) => { state.menuTop = menuTop setStore({ name: 'menuTop', content: state.menuTop }) }, SET_REFRESH_TOKEN: (state, refreshToken) => { setRefreshToken(refreshToken) state.refreshToken = refreshToken setStore({ name: 'refreshToken', content: state.refreshToken }) }, SET_TENANT_ID: (state, tenantId) => { state.tenantId = tenantId setStore({ name: 'tenantId', content: state.tenantId }) }, SET_USER_INFO: (state, userInfo) => { if (validatenull(userInfo.avatar)) { userInfo.avatar = '/img/bg/img-logo.png' } state.userInfo = userInfo setStore({ name: 'userInfo', content: state.userInfo }) }, SET_ROLES: (state, roles) => { state.roles = roles }, SET_PERMISSION: (state, permission) => { let result = [] function getCode(list) { list.forEach((ele) => { if (typeof ele === 'object') { const chiildren = ele.children const code = ele.code if (chiildren) { getCode(chiildren) } else { result.push(code) } } }) } getCode(permission) state.permission = {} result.forEach((ele) => { state.permission[ele] = true }) setStore({ name: 'permission', content: state.permission }) }, }, } export default user