|
|
|
|
import store from '@/store'
|
|
|
|
|
import router from '@/router'
|
|
|
|
|
import cache from '@/plugins/cache'
|
|
|
|
|
import { MessageBox } from 'element-ui'
|
|
|
|
|
import { login, logout, getInfo, getCommonConfigOptions } from '@/api/login'
|
|
|
|
|
import { getToken, getLoginInfo, setToken, setLoginInfo, removeToken, removeLoginInfo } from '@/utils/auth'
|
|
|
|
|
import { isHttp, isEmpty } from "@/utils/validate"
|
|
|
|
|
import defAva from '@/assets/images/profile.jpg'
|
|
|
|
|
import { initMinioClient, parseMinioFilePath } from '@/utils/requestMinio'
|
|
|
|
|
|
|
|
|
|
const user = {
|
|
|
|
|
state: {
|
|
|
|
|
loginInfo: getLoginInfo(),
|
|
|
|
|
userInfo: {},
|
|
|
|
|
token: getToken(),
|
|
|
|
|
roles: [],
|
|
|
|
|
permissions: [],
|
|
|
|
|
netConfig: {}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
mutations: {
|
|
|
|
|
SET_TOKEN: (state, token) => {
|
|
|
|
|
state.token = token
|
|
|
|
|
},
|
|
|
|
|
SET_ROLES: (state, roles) => {
|
|
|
|
|
state.roles = roles
|
|
|
|
|
},
|
|
|
|
|
SET_PERMISSIONS: (state, permissions) => {
|
|
|
|
|
state.permissions = permissions
|
|
|
|
|
},
|
|
|
|
|
SET_LOGIN_INFO: (state, loginInfo) => {
|
|
|
|
|
state.loginInfo = loginInfo
|
|
|
|
|
},
|
|
|
|
|
SET_USER_INFO: (state, userInfo) => {
|
|
|
|
|
state.userInfo = userInfo
|
|
|
|
|
},
|
|
|
|
|
SET_NET_CONFIG: (state, netConfig) => {
|
|
|
|
|
state.netConfig = netConfig
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
actions: {
|
|
|
|
|
// 登录
|
|
|
|
|
Login({ commit }, userInfo) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
login(userInfo).then(res => {
|
|
|
|
|
setToken(res.data.token)
|
|
|
|
|
setLoginInfo(res.data)
|
|
|
|
|
commit('SET_TOKEN', res.data.token)
|
|
|
|
|
commit('SET_LOGIN_INFO', res.data)
|
|
|
|
|
// 设置 userInfo,包含完整的用户信息
|
|
|
|
|
store.dispatch('lock/unlockScreen')
|
|
|
|
|
resolve()
|
|
|
|
|
}).catch(error => {
|
|
|
|
|
reject(error)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 获取用户信息
|
|
|
|
|
GetInfo({ commit, state }) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
getInfo().then(res => {
|
|
|
|
|
const user = res.data
|
|
|
|
|
let avatar = user.avatar || ""
|
|
|
|
|
if (!isHttp(avatar)) {
|
|
|
|
|
avatar = (isEmpty(avatar)) ? defAva : state.netConfig.MINIO_ENDPOINT_HTTPS + avatar
|
|
|
|
|
}
|
|
|
|
|
if (res.roles && res.roles.length > 0) { // 验证返回的 roles 是否是一个非空数组
|
|
|
|
|
commit('SET_ROLES', res.roles)
|
|
|
|
|
commit('SET_PERMISSIONS', res.permissions)
|
|
|
|
|
} else {
|
|
|
|
|
commit('SET_ROLES', ['ROLE_DEFAULT'])
|
|
|
|
|
}
|
|
|
|
|
commit('SET_USER_INFO', user)
|
|
|
|
|
cache.session.set('pwrChrtype', res.pwdChrtype)
|
|
|
|
|
/* 初始密码提示 */
|
|
|
|
|
if (res.isDefaultModifyPwd) {
|
|
|
|
|
MessageBox.confirm('您的密码还是初始密码,请修改密码!', '安全提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
|
|
|
|
|
router.push({ name: 'Profile', params: { activeTab: 'resetPwd' } })
|
|
|
|
|
}).catch(() => { })
|
|
|
|
|
}
|
|
|
|
|
/* 过期密码提示 */
|
|
|
|
|
if (!res.isDefaultModifyPwd && res.isPasswordExpired) {
|
|
|
|
|
MessageBox.confirm('您的密码已过期,请尽快修改密码!', '安全提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
|
|
|
|
|
router.push({ name: 'Profile', params: { activeTab: 'resetPwd' } })
|
|
|
|
|
}).catch(() => { })
|
|
|
|
|
}
|
|
|
|
|
resolve(res)
|
|
|
|
|
}).catch(error => {
|
|
|
|
|
reject(error)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
// 获取网络配置 - 数组转 key-value 对象
|
|
|
|
|
GetNetConfig({ commit }) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
getCommonConfigOptions().then(res => {
|
|
|
|
|
const arr = res.data
|
|
|
|
|
// 数组转为 key:value 对象
|
|
|
|
|
const netConfig = Object.fromEntries(arr.map(item => [item.code, item.value]))
|
|
|
|
|
commit('SET_NET_CONFIG', netConfig)
|
|
|
|
|
// 初始化 MinIO 客户端
|
|
|
|
|
// user.js - GetNetConfig 方法
|
|
|
|
|
if (netConfig.MINIO_ENDPOINT) {
|
|
|
|
|
const endpointParts = netConfig.MINIO_ENDPOINT.split(":")
|
|
|
|
|
initMinioClient({
|
|
|
|
|
endPoint: endpointParts[0],
|
|
|
|
|
port: parseInt(endpointParts[1]) || 9000,
|
|
|
|
|
accessKey: netConfig.MINIO_ACCESS_KEY,
|
|
|
|
|
secretKey: netConfig.MINIO_SECRET_KEY,
|
|
|
|
|
useSSL: netConfig.MINIO_SECURE === '1' || netConfig.MINIO_SECURE === true,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
resolve(res)
|
|
|
|
|
}).catch(error => {
|
|
|
|
|
reject(error)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
// 退出系统
|
|
|
|
|
LogOut({ commit, state }) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
logout(state.token).then(() => {
|
|
|
|
|
commit('SET_TOKEN', '')
|
|
|
|
|
commit('SET_ROLES', [])
|
|
|
|
|
commit('SET_PERMISSIONS', [])
|
|
|
|
|
removeToken()
|
|
|
|
|
removeLoginInfo()
|
|
|
|
|
resolve()
|
|
|
|
|
}).catch(error => {
|
|
|
|
|
reject(error)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 前端 登出
|
|
|
|
|
FedLogOut({ commit }) {
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
commit('SET_TOKEN', '')
|
|
|
|
|
removeToken()
|
|
|
|
|
removeLoginInfo()
|
|
|
|
|
resolve()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default user
|