diff --git a/src/api/user.js b/src/api/user.js index 28368b6..e2d0a30 100644 --- a/src/api/user.js +++ b/src/api/user.js @@ -159,14 +159,6 @@ export function GetObjectStoreToken(params) { method: 'get' }) } - -export function loginSelectUserRole(params) { - return request({ - url: `/user/loginSelectUserType`, - method: 'get', - params - }) -} // 置换token export function TJUserLoginInfo(params) { return request({ @@ -175,4 +167,20 @@ export function TJUserLoginInfo(params) { params }) } +// 登陆获取角色 +export function getUserLoginRoleList(data) { + return request({ + url: `/User/getUserLoginRoleList`, + method: 'post', + data, + }) +} +// 登陆角色id获取token +export function loginSelectUserRole(params) { + return request({ + url: `/User/loginSelectUserRole`, + method: 'get', + params, + }) +} diff --git a/src/components/toggleRole/index.vue b/src/components/toggleRole/index.vue index 3921ca6..0a0741b 100644 --- a/src/components/toggleRole/index.vue +++ b/src/components/toggleRole/index.vue @@ -70,10 +70,7 @@ export default { if (!this.form.userRoleId) return this.$message.warning(this.$t('toggleRole:ruleMessage:select')) zzSessionStorage.removeItem('lastWorkbench') - this.$emit('save', { - UserId: this.form.userRoleId, - UserTypeId: this.roles.find(item => item.Id === this.form.userRoleId).UserTypeId - }) + this.$emit('save', this.form.userRoleId) } catch (err) { console.log(err) } diff --git a/src/store/modules/user.js b/src/store/modules/user.js index ef6b130..1c68ca7 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -1,5 +1,5 @@ import { getToken, setToken, removeToken, setName, removeName } from '@/utils/auth' -import { login, getUserMenuTree, getUserPermissions, loginSelectUserRole, TJUserLoginInfo } from '@/api/user' +import { login, loginOut, getUserMenuTree, getUserPermissions, loginSelectUserRole, getUserLoginRoleList, TJUserLoginInfo } from '@/api/user' import { getUser } from '@/api/admin' import { resetRouter } from '@/router' import md5 from 'js-md5' @@ -87,20 +87,29 @@ const actions = { }, // user login login({ commit }, userInfo) { - const { username, password } = userInfo + const { username, password, UserId } = userInfo return new Promise((resolve, reject) => { - login({ UserName: username.trim(), Password: md5(password) }).then(async response => { + let data = { + UserName: username.trim(), Password: md5(password) + } + if (UserId) { + data.UserId = UserId; + } + getUserLoginRoleList(data).then(async response => { if (response.IsSuccess) { zzSessionStorage.removeItem('lastWorkbench') zzSessionStorage.setItem('my_username', username.trim()) zzSessionStorage.setItem('my_password', md5(password)) + zzSessionStorage.setItem('my_EMail', response.Result.BasicInfo.EMail) + localStorage.setItem('CompanyInfo', JSON.stringify(response.Result.CompanyInfo)) const data = response.Result - if (data.BasicInfo.IsFirstAdd) { + commit('SET_ROLES', data.BasicInfo.AccountList) + if (data.BasicInfo.IsFirstAdd || data.BasicInfo.LoginState === 1) { try { zzSessionStorage.setItem('userId', data.BasicInfo.Id) commit('SET_TOKEN', data.JWTStr) setToken(data.JWTStr) - resolve(false) + resolve(response.Result) } catch (e) { console.log(e) } @@ -110,25 +119,24 @@ const actions = { zzSessionStorage.setItem('IsReviewer', data.BasicInfo.IsReviewer) zzSessionStorage.setItem('userName', data.BasicInfo.UserName) commit('SET_TOKEN', data.JWTStr) - commit('SET_NAME', data.BasicInfo.RealName) - zzSessionStorage.setItem('realName', data.BasicInfo.RealName) + // commit('SET_NAME', data.BasicInfo.UserName) zzSessionStorage.setItem('isTestUser', data.BasicInfo.IsTestUser) commit('SET_ISTESTUSER', data.BasicInfo.IsTestUser) commit('SET_USERNAME', data.BasicInfo.UserName) - commit('SET_USERID', data.BasicInfo.Id) setToken(data.JWTStr) - setName(data.BasicInfo.RealName) - const userString = decodeURIComponent(escape(window.atob(data.JWTStr.split('.')[1].replace(/-/g, '+').replace(/_/g, '/')))) - const user = JSON.parse(userString) - zzSessionStorage.setItem('userTypeShortName', user.userTypeShortName) - zzSessionStorage.setItem('userId', user.id) - zzSessionStorage.setItem('userTypeEnumInt', user.userTypeEnumInt) - var permissions = await getUserPermissions() - var menuTree = await getUserMenuTree() - commit('SET_TREE', menuTree.Result) - commit('SET_PERMISSIONS', permissions.Result) - zzSessionStorage.setItem('newTree', JSON.stringify(menuTree.Result)) - zzSessionStorage.setItem('permissions', JSON.stringify(permissions.Result)) + // setName(data.BasicInfo.RealName) + // const userString = decodeURIComponent(escape(window.atob(data.JWTStr.split('.')[1].replace(/-/g, '+').replace(/_/g, '/')))) + // const user = JSON.parse(userString) + // zzSessionStorage.setItem('userId', user.identityUserId) + // commit('SET_IDENTITYUSERID', user.identityUserId) + // zzSessionStorage.setItem('identityUserId', user.identityUserId) + // zzSessionStorage.setItem('userTypeEnumInt', user.userTypeEnumInt) + // var permissions = await getUserPermissions() + // var menuTree = await getUserMenuTree() + // commit('SET_TREE', menuTree.Result) + // commit('SET_PERMISSIONS', permissions.Result) + // zzSessionStorage.setItem('newTree', JSON.stringify(menuTree.Result)) + // zzSessionStorage.setItem('permissions', JSON.stringify(permissions.Result)) } catch (e) { console.log(e) } @@ -161,16 +169,15 @@ const actions = { }) }, loginByRole({ commit }, userInfo) { - const { UserId, UserTypeId } = userInfo + const { userRoleId } = userInfo let params = { - UserId, UserTypeId + userRoleId } return new Promise((resolve, reject) => { loginSelectUserRole(params).then(async response => { if (response.IsSuccess) { const data = response.Result.JWTStr - try { commit('SET_TOKEN', data) setToken(data) @@ -247,12 +254,22 @@ const actions = { commit('SET_NAME', zzSessionStorage.getItem('realName')) }, // user logout - logout({ commit, state }) { - removeToken() // must remove token first - resetRouter() - removeName() - zzSessionStorage.clear() - commit('RESET_STATE') + async logout({ commit, state }) { + try { + removeToken() // must remove token first + await loginOut({ + UserRoleId: zzSessionStorage.getItem('userId'), + IdentityUserId: zzSessionStorage.getItem('identityUserId'), + }) + resetRouter() + removeName() + let lang = zzSessionStorage.getItem('lang') + zzSessionStorage.clear() + zzSessionStorage.setItem('lang', lang) + commit('RESET_STATE') + } catch (e) { + console.log(e) + } }, setToken({ commit }, token) { commit('SET_TOKEN', token) diff --git a/src/utils/request.js b/src/utils/request.js index b75c3d0..3469054 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -87,9 +87,9 @@ service.interceptors.response.use( console.log(err) } router.push(`/login`) - this.$i18n.locale = 'zh' - this.setLanguage('zh') - this.$updateDictionary() + // this.$i18n.locale = 'zh' + // this.setLanguage('zh') + // this.$updateDictionary() }) } switch (status) { @@ -119,9 +119,9 @@ service.interceptors.response.use( } store.dispatch('user/logout').then(() => { router.push(`/login`) - this.$i18n.locale = 'zh' - this.setLanguage('zh') - this.$updateDictionary() + // this.$i18n.locale = 'zh' + // this.setLanguage('zh') + // this.$updateDictionary() }) } else { Message({ diff --git a/src/views/login/index.vue b/src/views/login/index.vue index 0ca5806..919920e 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -125,7 +125,7 @@ + @save="loginByRole" @cancel="cancel" /> @@ -237,62 +237,11 @@ export default { }, methods: { ...mapMutations({ setLanguage: 'lang/setLanguage' }), - loginByRole(row) { - let { UserId, UserTypeId } = row - if (this.$store.state.user.userId === UserId) { - this.toggleRoleVisible = false - this.toggleRoleLoading = false - return false - } - this.toggleRoleLoading = true - this.$store - .dispatch('user/loginByRole', { UserId, UserTypeId }) - .then((res) => { - if (res) { - this.$store - .dispatch('permission/generateRoutes') - .then(async (res) => { - if (res && res.length > 0) { - resetRouter() - await this.$store.dispatch('global/getNoticeList') - this.$router.addRoutes(res) - this.toggleRoleLoading = false - if (this.loginType === 'DevOps') { - this.$router.replace({ path: res[0].path }) - return - } - if (this.hasPermi(['role:radmin'])) { - this.$router.replace({ path: res[0].path }) - return - } - if ( - this.hasPermi(['role:pm', 'role:oa', 'role:admin', 'role:dev']) - ) { - history.replaceState(null, null, '/trials/trials-inspection') - history.go(0) - } else { - history.replaceState(null, null, '/trials/trials-list') - history.go(0) - } - this.toggleRoleVisible = false - this.toggleRoleLoading = false - this.$EventBus.$emit('reload') - } else { - // 此账户暂未配置菜单权限,请联系管理员处理后再登录。 - this.toggleRoleLoading = false - this.$message.warning(this.$t('login:message:login2')) - } - }) - .catch((err) => { - this.toggleRoleLoading = false - }) - } else { - this.toggleRoleLoading = false - } - }) - .catch(() => { - this.toggleRoleLoading = false - }) + cancel() { + this.showCode = true + this.loading = false + this.toggleRoleLoading = false + // this.toggleRoleVisible = false }, // 置换token async TJUserLoginInfo(Token) { @@ -385,7 +334,10 @@ export default { this.$store .dispatch('user/login', this.loginForm) .then((res) => { - if (!res) { + if (res.BasicInfo.LoginState === 2) { + this.$message.warning(this.$t('login:message:login4')) + } + if (res.BasicInfo.IsFirstAdd) { // 当前用户为首次登录,请先修改密码之后再次登录 this.$message.success(this.$t('login:message:login1')) setTimeout(() => { @@ -394,39 +346,108 @@ export default { }) }, 500) return + } else if (res.BasicInfo.NeedChangePassWord) { + // 请先修改密码后再登录! + this.$alert( + this.$t('login:message:login3'), + this.$t('common:title:warning'), + { + callback: (action) => { + this.$router.push({ + path: `/recompose?userName=${this.loginForm.username}`, + }) + return + }, + } + ) + return } - this.$store.dispatch('permission/generateRoutes').then((res) => { - this.loading = false - if (res && res.length > 0) { - this.$store.dispatch('global/getNoticeList') - this.$router.addRoutes(res) - if (this.loginType === 'DevOps') { - this.$router.replace({ path: res[0].path }) - return - } - if (this.hasPermi(['role:radmin'])) { - this.$router.replace({ path: res[0].path }) - return - } - if ( - this.hasPermi(['role:pm', 'role:oa', 'role:admin', 'role:dev']) - ) { - this.$router.replace({ path: '/trials/trials-inspection' }) - } else if (this.hasPermi(['role:oa'])) { - this.$router.replace({ path: '/system' }) - } else { - this.$router.replace({ path: '/trials' }) - } - } else { - // 此账户暂未配置菜单权限,请联系管理员处理后再登录。 - this.$message.warning(this.$t('login:message:login2')) - } - }) + zzSessionStorage.removeItem('userId') + zzSessionStorage.removeItem('identityUserId') + this.changeRoleLogin() }) .catch(() => { this.loading = false }) }, + changeRoleLogin() { + if ( + Array.isArray(this.$store.state.user.roles) && + this.$store.state.user.roles.length === 1 + ) { + this.loginByRole(this.$store.state.user.roles[0].Id) + return + } + if ( + Array.isArray(this.$store.state.user.roles) && + this.$store.state.user.roles.filter((item) => item.IsUserRoleDisabled) + .length === + this.$store.state.user.roles.length - 1 + ) { + let role = this.$store.state.user.roles.find( + (item) => !item.IsUserRoleDisabled + ) + this.loginByRole(role.Id) + return + } + return (this.toggleRoleVisible = true) + }, + loginByRole(userRoleId) { + this.toggleRoleLoading = true + this.$store + .dispatch('user/loginByRole', { userRoleId }) + .then((res) => { + this.toggleRoleLoading = false + if (res) { + this.$store + .dispatch('permission/generateRoutes') + .then((res) => { + this.loading = false + if (res && res.length > 0) { + this.$store.dispatch('global/getNoticeList') + this.$router.addRoutes(res) + // if (this.LoginState === 2) { + // this.$message.warning(this.$t('login:message:login4')) + // } + if (this.loginType === 'DevOps') { + this.$router.replace({ path: res[0].path }) + return + } + if (this.hasPermi(['role:radmin'])) { + this.$router.replace({ path: res[0].path }) + return + } + if ( + this.hasPermi([ + 'role:air', + 'role:rpm', + 'role:rcrc', + 'role:rir', + ]) + ) { + this.$router.replace({ path: '/trials/trials-list' }) + } else { + this.$router.replace({ path: '/trials' }) + } + } else { + this.toggleRoleLoading = false + // 此账户暂未配置菜单权限,请联系管理员处理后再登录。 + this.$message.warning(this.$t('login:message:login2')) + } + }) + .catch((err) => { + this.toggleRoleLoading = false + }) + } else { + this.toggleRoleLoading = false + } + }) + .catch((err) => { + this.showCode = true + this.loading = false + this.toggleRoleLoading = false + }) + }, onSuccess() { this.isShow = false this.loginIn()