123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600 |
- 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
|