Commit b1101975 by 李伟康

refactor(store): 重构登录逻辑

- 优化了登录流程,减少了代码冗余
- 添加了默认参数,提高了代码健壮性
- 统一了错误处理方式,提升了用户体验
- 调整了代码格式,提高了可读性
1 parent 8adc19d3
......@@ -67,17 +67,17 @@ const actions = {
store.set(store.consts.USER_KEY, user)
},
login ({ dispatch }) {
let [redirectUrl] = window.location.href.split('?')
const wwcode = getParams('code')
if (wwcode) {
const { corpId, agentId } = util.getLocalStorage('ww_storage_info')
ssoApi
.login({
try {
let [redirectUrl] = window.location.href.split('?'),
{ defaultCorpId, defaultAgentId } = window.$fetchConfig
const wwcode = getParams('code')
if (wwcode) {
const { corpId, agentId } = util.getLocalStorage('ww_storage_info')
ssoApi.login({
code: wwcode,
corpId,
agentId
})
.then(res => {
}).then(res => {
let redirect_uri = util.getLocalStorage('ww_redirect_uri'),
url = `${redirectUrl.split('#')[0]}#/`
if (res.errCode == 0 && res.userId) {
......@@ -92,57 +92,53 @@ const actions = {
window.location.href = URL
}
else window.location.href = url
} else {
Notify(res.errMsg)
}
} else throw res.errMsg
})
} else {
util.showLoading()
let appid = getParams('corpId') //从链接获取
let agentid = getParams('agentId') //从链接获取
let storage_info = util.getLocalStorage('ww_storage_info')
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV)
if (!appid) {
if (storage_info) {
appid = storage_info.corpId
agentid = storage_info.agentId
} else {
if (IS_PROD) {
Notify({
message: '缺少corpId!',
duration: 15000
})
return
} else {
util.showLoading()
let appid = getParams('corpId') //从链接获取
let agentid = getParams('agentId') //从链接获取
let storage_info = util.getLocalStorage('ww_storage_info')
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV)
if (!appid) {
if (storage_info) {
appid = storage_info.corpId
agentid = storage_info.agentId
} else {
appid = 'wwa55ffac1225eb25e'
agentid = 1000015
if (IS_PROD) throw '缺少corpId!'
else {
appid = defaultCorpId // 默认参数
agentid = defaultAgentId //默认参数
}
}
}
}
let ww_storage_info = { corpId: appid },
env = detectEnvironment()
if (agentid && agentid != 'undefined') ww_storage_info.agentId = agentid
util.setLocalStorage('ww_storage_info', ww_storage_info)
if (env == 1) {
let wxUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize'
wxUrl = `${wxUrl}?appid=${appid}&redirect_uri=${encodeURIComponent(redirectUrl)}&response_type=code`
if (agentid && agentid != 'undefined') {
wxUrl = `${wxUrl}&scope=snsapi_privateinfo&agentid=${agentid}`
ww_storage_info.agentId = agentid
let ww_storage_info = { corpId: appid },
env = detectEnvironment()
if (agentid && agentid != 'undefined') ww_storage_info.agentId = agentid
util.setLocalStorage('ww_storage_info', ww_storage_info)
if (env == 1) {
let wxUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize'
wxUrl = `${wxUrl}?appid=${appid}&redirect_uri=${encodeURIComponent(redirectUrl)}&response_type=code`
if (agentid && agentid != 'undefined') {
wxUrl = `${wxUrl}&scope=snsapi_privateinfo&agentid=${agentid}`
ww_storage_info.agentId = agentid
}
else wxUrl = `${wxUrl}&scope=snsapi_base`
window.location.href = `${wxUrl}&state=STATE#wechat_redirect`
} else if (env == 2) {
Notify({
message: '请使用企业微信打开',
duration: 999999999
})
} else {
//扫码
router.replace({ name: 'common.wwLoginPanel' })
}
else wxUrl = `${wxUrl}&scope=snsapi_base`
window.location.href = `${wxUrl}&state=STATE#wechat_redirect`
} else if (env == 2) {
Notify({
message: '请使用企业微信打开',
duration: 999999999
})
} else {
//扫码
router.replace({ name: 'common.wwLoginPanel' })
}
} catch (error) {
Notify(typeof error === 'string' ? error : error.toString())
}
},
navigation ({ dispatch }, to) {
......
......@@ -36,21 +36,23 @@ export default {
redirect_type: 'callback',
},
async onLoginSuccess ({ code }) {
const res = await ssoApi.login({ code, corpId, agentId })
if (res.errCode == 0 && res.userId) {
_this.setLocalStorage('ww_userInfo', {
userId: res.userId,
entmicroId: corpId
})
if (redirect_uri) {
let [URL, query] = decodeURIComponent(redirect_uri).split('?')
URL = `${URL}?userId=${res.userId}`
if (query) URL = `${URL}&${query}`
window.location.href = URL
}
else window.location.href = url
} else {
Notify(res.errMsg)
try {
const res = await ssoApi.login({ code, corpId, agentId })
if (res.errCode == 0 && res.userId) {
_this.setLocalStorage('ww_userInfo', {
userId: res.userId,
entmicroId: corpId
})
if (redirect_uri) {
let [URL, query] = decodeURIComponent(redirect_uri).split('?')
URL = `${URL}?userId=${res.userId}`
if (query) URL = `${URL}&${query}`
window.location.href = URL
}
else window.location.href = url
} else throw res.errMsg
} catch (error) {
Notify(typeof error === 'string' ? error : error.toString())
}
},
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!