Skip to content

style(login): 优化登陆部分代码,提升可读性 #1772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 46 additions & 33 deletions web/src/pinia/modules/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,47 +53,60 @@ export const useUserStore = defineStore('user', () => {
fullscreen: true,
text: '登录中,请稍候...',
})
try {
const res = await login(loginInfo)
if (res.code === 0) {
setUserInfo(res.data.user)
setToken(res.data.token)
const routerStore = useRouterStore()
await routerStore.SetAsyncRouter()
const asyncRouters = routerStore.asyncRouters
asyncRouters.forEach(asyncRouter => {
router.addRoute(asyncRouter)
})

if (!router.hasRoute(userInfo.value.authority.defaultRouter)) {
ElMessage.error('请联系管理员进行授权')
} else {
await router.replace({ name: userInfo.value.authority.defaultRouter })
}

loadingInstance.value.close()

const isWin = ref(/windows/i.test(navigator.userAgent))
if (isWin.value) {
window.localStorage.setItem('osType', 'WIN')
} else {
window.localStorage.setItem('osType', 'MAC')
}
return true
}
} catch (e) {

const res = await login(loginInfo)

// 登陆失败,直接返回
if (res.code !== 0) {
loadingInstance.value.close()
return false
}

// 登陆成功,设置用户信息和权限相关信息
setUserInfo(res.data.user)
setToken(res.data.token)

// 初始化路由信息
const routerStore = useRouterStore()
await routerStore.SetAsyncRouter()
const asyncRouters = routerStore.asyncRouters

// 注册到路由表里
asyncRouters.forEach(asyncRouter => {
router.addRoute(asyncRouter)
})

if (!router.hasRoute(userInfo.value.authority.defaultRouter)) {
ElMessage.error('请联系管理员进行授权')
} else {
await router.replace({ name: userInfo.value.authority.defaultRouter })
}

const isWin = ref(/windows/i.test(navigator.userAgent))
if (isWin.value) {
window.localStorage.setItem('osType', 'WIN')
} else {
window.localStorage.setItem('osType', 'MAC')
}

// 全部操作均结束,关闭loading并返回
loadingInstance.value.close()
return true
}
/* 登出*/
const LoginOut = async() => {
const res = await jsonInBlacklist()
if (res.code === 0) {
await ClearStorage()
router.push({ name: 'Login', replace: true })
window.location.reload()

// 登出失败
if (res.code !== 0) {
return
}

await ClearStorage()

// 把路由定向到登录页,无需等待直接reload
router.push({ name: 'Login', replace: true })
window.location.reload()
}
/* 清理数据 */
const ClearStorage = async() => {
Expand Down
20 changes: 14 additions & 6 deletions web/src/view/login/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,8 @@ const login = async() => {
}
const submitForm = () => {
loginForm.value.validate(async(v) => {
if (v) {
const flag = await login()
if (!flag) {
loginVerify()
}
} else {
if (!v) {
// 未通过前端静态验证
ElMessage({
type: 'error',
message: '请正确填写登录信息',
Expand All @@ -238,6 +234,18 @@ const submitForm = () => {
loginVerify()
return false
}

// 通过验证,请求登陆
const flag = await login()

// 登陆失败,刷新验证码
if (!flag) {
loginVerify()
return false
}

// 登陆成功
return true
})
}

Expand Down