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 {
const wwcode = getParams('code') let [redirectUrl] = window.location.href.split('?'),
if (wwcode) { { defaultCorpId, defaultAgentId } = window.$fetchConfig
const { corpId, agentId } = util.getLocalStorage('ww_storage_info') const wwcode = getParams('code')
ssoApi if (wwcode) {
.login({ const { corpId, agentId } = util.getLocalStorage('ww_storage_info')
ssoApi.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,57 +92,53 @@ const actions = { ...@@ -92,57 +92,53 @@ 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()
let appid = getParams('corpId') //从链接获取 let appid = getParams('corpId') //从链接获取
let agentid = getParams('agentId') //从链接获取 let agentid = getParams('agentId') //从链接获取
let storage_info = util.getLocalStorage('ww_storage_info') let storage_info = util.getLocalStorage('ww_storage_info')
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV) const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV)
if (!appid) { if (!appid) {
if (storage_info) { if (storage_info) {
appid = storage_info.corpId appid = storage_info.corpId
agentid = storage_info.agentId agentid = storage_info.agentId
} else {
if (IS_PROD) {
Notify({
message: '缺少corpId!',
duration: 15000
})
return
} else { } else {
appid = 'wwa55ffac1225eb25e' if (IS_PROD) throw '缺少corpId!'
agentid = 1000015 else {
appid = defaultCorpId // 默认参数
agentid = defaultAgentId //默认参数
}
} }
} }
} let ww_storage_info = { corpId: appid },
let ww_storage_info = { corpId: appid }, env = detectEnvironment()
env = detectEnvironment() if (agentid && agentid != 'undefined') ww_storage_info.agentId = agentid
if (agentid && agentid != 'undefined') ww_storage_info.agentId = agentid util.setLocalStorage('ww_storage_info', ww_storage_info)
util.setLocalStorage('ww_storage_info', ww_storage_info) if (env == 1) {
if (env == 1) { let wxUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize'
let wxUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize' wxUrl = `${wxUrl}?appid=${appid}&redirect_uri=${encodeURIComponent(redirectUrl)}&response_type=code`
wxUrl = `${wxUrl}?appid=${appid}&redirect_uri=${encodeURIComponent(redirectUrl)}&response_type=code` if (agentid && agentid != 'undefined') {
if (agentid && agentid != 'undefined') { wxUrl = `${wxUrl}&scope=snsapi_privateinfo&agentid=${agentid}`
wxUrl = `${wxUrl}&scope=snsapi_privateinfo&agentid=${agentid}` ww_storage_info.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) { navigation ({ dispatch }, to) {
......
...@@ -36,21 +36,23 @@ export default { ...@@ -36,21 +36,23 @@ export default {
redirect_type: 'callback', redirect_type: 'callback',
}, },
async onLoginSuccess ({ code }) { async onLoginSuccess ({ code }) {
const res = await ssoApi.login({ code, corpId, agentId }) try {
if (res.errCode == 0 && res.userId) { const res = await ssoApi.login({ code, corpId, agentId })
_this.setLocalStorage('ww_userInfo', { if (res.errCode == 0 && res.userId) {
userId: res.userId, _this.setLocalStorage('ww_userInfo', {
entmicroId: corpId userId: res.userId,
}) entmicroId: corpId
if (redirect_uri) { })
let [URL, query] = decodeURIComponent(redirect_uri).split('?') if (redirect_uri) {
URL = `${URL}?userId=${res.userId}` let [URL, query] = decodeURIComponent(redirect_uri).split('?')
if (query) URL = `${URL}&${query}` URL = `${URL}?userId=${res.userId}`
window.location.href = URL if (query) URL = `${URL}&${query}`
} window.location.href = URL
else window.location.href = url }
} else { else window.location.href = url
Notify(res.errMsg) } 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!