217 lines
5.7 KiB
JavaScript
217 lines
5.7 KiB
JavaScript
import { webLoginType, ssoLogin, ssoLogout } from '@/sysConfig'
|
|
import { getMenu, loginIdm } from '@/api/process'
|
|
import { getTokenToWeLink } from '@/api/phoneWeLink'
|
|
import isMobile from '@/store/isMobile'
|
|
import store from '@/store'
|
|
import router from '@/router'
|
|
import hwh5 from '@/api/hwh5-cloudonline.js'
|
|
import { Message, MessageBox, Loading } from 'element-ui'
|
|
import { Dialog } from 'vant'
|
|
|
|
function getCode () {
|
|
const url = window.location.href
|
|
if (url && url !== '') {
|
|
if (url.split('code=').length > 1) {
|
|
return url.split('code=')[1]
|
|
}
|
|
}
|
|
}
|
|
|
|
function toIdmLoginPage (url) {
|
|
if (webLoginType === 'idm') {
|
|
let href = url
|
|
if (href.indexOf('localhost:9090' > -1)) {
|
|
href = href.replace('localhost:9090', '39.98.140.126:9090') // 开发环境
|
|
}
|
|
console.log(ssoLogin + encodeURIComponent(href))
|
|
window.location.href = ssoLogin + encodeURIComponent(href)
|
|
}
|
|
}
|
|
|
|
// 重新登录
|
|
function loginAgain () {
|
|
store.dispatch('logout').then(() => {
|
|
if (webLoginType === 'idm') {
|
|
if (isMobile()) {
|
|
loginInPhone().then(() => {
|
|
Message.success('登录成功')
|
|
// router.go(0)
|
|
window.location.reload(false)
|
|
})
|
|
} else {
|
|
window.location.href = ssoLogout + encodeURIComponent(window.location.href)
|
|
}
|
|
}
|
|
if (webLoginType === 'dev') {
|
|
router.push('/login')
|
|
}
|
|
})
|
|
}
|
|
|
|
// 退出登录
|
|
function loginOut () {
|
|
store.dispatch('logout').then(() => {
|
|
if (webLoginType === 'idm') {
|
|
window.location.href = ssoLogout + encodeURIComponent(window.location.origin)
|
|
}
|
|
if (webLoginType === 'dev') {
|
|
router.push('/login')
|
|
}
|
|
})
|
|
}
|
|
|
|
// idm登录
|
|
function loginInIdm (code) {
|
|
return new Promise((resolve, reject) => {
|
|
const loading = Loading.service({
|
|
lock: true,
|
|
text: '登录中...',
|
|
spinner: 'el-icon-loading'
|
|
})
|
|
const _formData = new FormData()
|
|
_formData.append('code', code)
|
|
// 获取用户信息
|
|
loginIdm(_formData).then(res => {
|
|
if (res.ok) {
|
|
loginNew(res)
|
|
.then(() => {
|
|
resolve()
|
|
})
|
|
.finally(() => {
|
|
loading.close()
|
|
})
|
|
} else {
|
|
if (res.respCode && res.respCode === 'r0011') {
|
|
quit()
|
|
loading.close()
|
|
}
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
// welink登录
|
|
function loginInPhone () {
|
|
return new Promise( (resolve, reject) => {
|
|
const loading = Loading.service({
|
|
lock: true,
|
|
text: '登录中...',
|
|
spinner: 'el-icon-loading'
|
|
})
|
|
hwh5.getAuthCode().then(data => {
|
|
console.log('获取到了welinkCode', data)
|
|
if (data) {
|
|
const code = data.code
|
|
getTokenToWeLink({
|
|
code: code,
|
|
name: encodeURIComponent('standards' + '-' + 'regulations'),
|
|
type: '',
|
|
corpCode: ''
|
|
}).then(res => {
|
|
if (res.ok) {
|
|
loginNew(res)
|
|
.then(() => {
|
|
resolve()
|
|
}).catch(e => {
|
|
Message.error('loginNew--warning')
|
|
})
|
|
.finally(() => {
|
|
loading.close()
|
|
})
|
|
} else {
|
|
if (res.respCode && res.respCode === 'r0011') {
|
|
quit()
|
|
loading.close()
|
|
} else {
|
|
Message.warning('getTokenToWeLink--warning')
|
|
loading.close()
|
|
}
|
|
}
|
|
}).catch(e => {
|
|
Message.error('getTokenToWeLink--error')
|
|
loading.close()
|
|
})
|
|
}else {
|
|
Message.warning('hwh5.getAuthCode--warning')
|
|
loading.close()
|
|
}
|
|
}).catch(e => {
|
|
Message.error('hwh5.getAuthCode--error', e)
|
|
loading.close()
|
|
})
|
|
})
|
|
}
|
|
|
|
function loginNew (res) {
|
|
return new Promise((resolve, reject) => {
|
|
if (res.data.orgId === null || res.data.orgId === '') {
|
|
Message.warning('未分配组织机构,请联系管理员分配后重新登录')
|
|
return
|
|
}
|
|
const Base64 = require('js-base64').Base64
|
|
// 解析用户信息
|
|
const userInfoStr = Base64.decode(res.data)
|
|
const userInfoJSON = decodeURIComponent(userInfoStr)
|
|
const userInfo = JSON.parse(userInfoJSON)
|
|
userInfo.userId = userInfo.usid
|
|
userInfo.uName = userInfo.uName || userInfo.uname
|
|
store.commit('setToken', userInfo.token)
|
|
store.commit('setUserInfo', JSON.stringify(userInfo))
|
|
getMenu({ userId: store.getters.userInfo.userId })
|
|
.then(menuRes => {
|
|
if (menuRes.ok) {
|
|
let hasHomeMenu = false
|
|
const menuList = menuRes.data
|
|
menuList.forEach(menu => {
|
|
if (menu.id === 'asdfadf') {
|
|
hasHomeMenu = true
|
|
}
|
|
})
|
|
if (hasHomeMenu) {
|
|
store.dispatch('setMenu', JSON.stringify(menuList)).then(() => {
|
|
resolve()
|
|
})
|
|
}
|
|
} else {
|
|
reject(menuRes)
|
|
}
|
|
})
|
|
.catch(e => {
|
|
Message.error('getMenu--error')
|
|
reject(e)
|
|
})
|
|
})
|
|
}
|
|
|
|
function quit () {
|
|
Message.warning('系统无权限访问')
|
|
if (isMobile()) {
|
|
Dialog.confirm({
|
|
message: '您没有系统访问权限,请联系管理员进行授权'
|
|
}).then(() => {
|
|
store.dispatch('logout').then(() => {
|
|
close()
|
|
})
|
|
})
|
|
} else {
|
|
MessageBox.confirm('您没有系统访问权限,请联系管理员进行授权', '提示', {
|
|
confirmButtonText: '确定',
|
|
type: 'warning',
|
|
showClose: false,
|
|
showCancelButton: false
|
|
}).then(() => {
|
|
store.dispatch('logout').then(() => {
|
|
window.location.href = ssoLogout + encodeURIComponent(window.location.origin)
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
function close () {
|
|
if (isMobile()) {
|
|
hwh5.close()
|
|
}
|
|
}
|
|
|
|
export { toIdmLoginPage, getCode, loginInIdm, loginInPhone, loginAgain, loginOut, close }
|