Commit b1101975 by 李伟康

refactor(store): 重构登录逻辑

- 优化了登录流程,减少了代码冗余
- 添加了默认参数,提高了代码健壮性
- 统一了错误处理方式,提升了用户体验
- 调整了代码格式,提高了可读性
1 parent 8adc19d3
...@@ -67,17 +67,17 @@ const actions = { ...@@ -67,17 +67,17 @@ const actions = {
store.set(store.consts.USER_KEY, user) store.set(store.consts.USER_KEY, user)
}, },
login ({ dispatch }) { login ({ dispatch }) {
let [redirectUrl] = window.location.href.split('?') try {
let [redirectUrl] = window.location.href.split('?'),
{ defaultCorpId, defaultAgentId } = window.$fetchConfig
const wwcode = getParams('code') const wwcode = getParams('code')
if (wwcode) { if (wwcode) {
const { corpId, agentId } = util.getLocalStorage('ww_storage_info') const { corpId, agentId } = util.getLocalStorage('ww_storage_info')
ssoApi ssoApi.login({
.login({
code: wwcode, code: wwcode,
corpId, corpId,
agentId agentId
}) }).then(res => {
.then(res => {
let redirect_uri = util.getLocalStorage('ww_redirect_uri'), let redirect_uri = util.getLocalStorage('ww_redirect_uri'),
url = `${redirectUrl.split('#')[0]}#/` url = `${redirectUrl.split('#')[0]}#/`
if (res.errCode == 0 && res.userId) { if (res.errCode == 0 && res.userId) {
...@@ -92,9 +92,7 @@ const actions = { ...@@ -92,9 +92,7 @@ const actions = {
window.location.href = URL window.location.href = URL
} }
else window.location.href = url else window.location.href = url
} else { } else throw res.errMsg
Notify(res.errMsg)
}
}) })
} else { } else {
util.showLoading() util.showLoading()
...@@ -107,15 +105,10 @@ const actions = { ...@@ -107,15 +105,10 @@ const actions = {
appid = storage_info.corpId appid = storage_info.corpId
agentid = storage_info.agentId agentid = storage_info.agentId
} else { } else {
if (IS_PROD) { if (IS_PROD) throw '缺少corpId!'
Notify({ else {
message: '缺少corpId!', appid = defaultCorpId // 默认参数
duration: 15000 agentid = defaultAgentId //默认参数
})
return
} else {
appid = 'wwa55ffac1225eb25e'
agentid = 1000015
} }
} }
} }
...@@ -144,6 +137,9 @@ const actions = { ...@@ -144,6 +137,9 @@ const actions = {
} }
} }
} catch (error) {
Notify(typeof error === 'string' ? error : error.toString())
}
}, },
navigation ({ dispatch }, to) { navigation ({ dispatch }, to) {
let { redirect_uri, corpId, agentId } = getParams() let { redirect_uri, corpId, agentId } = getParams()
......
...@@ -36,6 +36,7 @@ export default { ...@@ -36,6 +36,7 @@ export default {
redirect_type: 'callback', redirect_type: 'callback',
}, },
async onLoginSuccess ({ code }) { async onLoginSuccess ({ code }) {
try {
const res = await ssoApi.login({ code, corpId, agentId }) const res = await ssoApi.login({ code, corpId, agentId })
if (res.errCode == 0 && res.userId) { if (res.errCode == 0 && res.userId) {
_this.setLocalStorage('ww_userInfo', { _this.setLocalStorage('ww_userInfo', {
...@@ -49,8 +50,9 @@ export default { ...@@ -49,8 +50,9 @@ export default {
window.location.href = URL window.location.href = URL
} }
else window.location.href = url else window.location.href = url
} else { } else throw res.errMsg
Notify(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!