Files
devops-platform-web/src/views/LoginView.vue
T
湛兮 c264885ece feat: 展开运维导航并迁移通知接入文档
- 在顶部导航展示发布、项目接入、成员、设置和文档
- 提供舌吻运营后台返回入口和项目名称编辑
2026-11-13 23:27:51 +08:00

315 lines
7.7 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
/**
* 登录页对接后端真实鉴权,并处理普通成员首次登录强制改密。
*/
import { Lock, Monitor } from '@element-plus/icons-vue'
import { computed, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'
import { devopsApi } from '../services/devopsApi'
import { useSessionStore } from '../stores/session'
const SUPER_ADMIN_ACCOUNT = 'zhanxi'
const route = useRoute()
const router = useRouter()
const session = useSessionStore()
const loading = ref(false)
const forgotPasswordLoading = ref(false)
const loginForm = reactive({
account: '',
password: '',
})
const passwordForm = reactive({
currentPassword: '',
newPassword: '',
confirmPassword: '',
})
const redirect = computed(() => {
const value = route.query.redirect
return typeof value === 'string' && value.startsWith('/') && !value.startsWith('//')
? value
: '/overview'
})
const isSuperAdminLogin = computed(() => loginForm.account.trim() === SUPER_ADMIN_ACCOUNT)
const loginError = ref('')
async function submitLogin() {
if (loading.value) return
loginError.value = ''
loading.value = true
try {
const user = await session.login({
account: loginForm.account.trim(),
password: loginForm.password,
})
passwordForm.currentPassword = loginForm.password
if (user.mustChangePassword) {
ElMessage.warning('首次登录请先修改密码')
return
}
await router.push(redirect.value)
} catch (error) {
loginError.value = error instanceof Error ? error.message : '登录失败,请重试'
} finally {
loading.value = false
}
}
async function requestPasswordReset() {
if (isSuperAdminLogin.value) {
return
}
const account = loginForm.account.trim()
if (!account) {
ElMessage.warning('请先填写需要重置密码的成员账号')
return
}
try {
await ElMessageBox.confirm(`确认向超级管理员发送账号 ${account} 的密码重置请求?`, '忘记密码', {
confirmButtonText: '发送请求',
cancelButtonText: '取消',
type: 'warning',
})
} catch {
return
}
forgotPasswordLoading.value = true
try {
await devopsApi.forgotPassword({ account })
ElMessage.success('已通知超级管理员协助重置密码')
} catch (error) {
ElMessage.error(error instanceof Error ? error.message : '发送重置请求失败')
} finally {
forgotPasswordLoading.value = false
}
}
async function submitPasswordChange() {
if (passwordForm.newPassword !== passwordForm.confirmPassword) {
ElMessage.error('两次输入的新密码不一致')
return
}
loading.value = true
try {
await session.changePassword({
currentPassword: passwordForm.currentPassword,
newPassword: passwordForm.newPassword,
})
ElMessage.success('密码已更新')
await router.push(redirect.value)
} catch (error) {
ElMessage.error(error instanceof Error ? error.message : '修改密码失败')
} finally {
loading.value = false
}
}
async function logoutForAccountSwitch() {
await session.logout()
loginForm.password = ''
passwordForm.currentPassword = ''
passwordForm.newPassword = ''
passwordForm.confirmPassword = ''
}
function submit() {
if (session.mustChangePassword) {
void submitPasswordChange()
return
}
void submitLogin()
}
function accountLabel() {
return session.displayName || session.operator
}
</script>
<template>
<main class="login-page">
<a class="text-link system-return" href="https://admin.dev.mrzhan.top/">切回舌吻运营后台</a>
<section class="login-panel">
<div class="login-copy">
<ElIcon><Monitor /></ElIcon>
<h1>登录 DevOps</h1>
<p>项目发布,简单有序。</p>
</div>
<ElForm class="login-form" label-position="top" @submit.prevent="submit">
<template v-if="!session.mustChangePassword">
<ElFormItem label="账号">
<ElInput
v-model="loginForm.account"
size="large"
autocomplete="username"
name="username"
required
:spellcheck="false"
placeholder="输入平台账号…"
/>
</ElFormItem>
<ElFormItem label="密码">
<ElInput
v-model="loginForm.password"
size="large"
type="password"
autocomplete="current-password"
required
show-password
/>
</ElFormItem>
<p v-if="loginError" class="login-error" role="alert">{{ loginError }}</p>
<ElButton
type="primary"
size="large"
:icon="Lock"
:loading="loading"
native-type="submit"
>
登录
</ElButton>
<ElButton
v-if="!isSuperAdminLogin"
class="forgot-password"
text
:loading="forgotPasswordLoading"
@click="requestPasswordReset"
>
忘记密码
</ElButton>
</template>
<template v-else>
<div class="change-title">
<strong>{{ accountLabel() }}</strong>
<span>需要更新密码</span>
</div>
<ElFormItem label="当前密码">
<ElInput
v-model="passwordForm.currentPassword"
size="large"
type="password"
autocomplete="current-password"
show-password
/>
</ElFormItem>
<ElFormItem label="新密码">
<ElInput
v-model="passwordForm.newPassword"
size="large"
type="password"
autocomplete="new-password"
show-password
/>
</ElFormItem>
<ElFormItem label="确认新密码">
<ElInput
v-model="passwordForm.confirmPassword"
size="large"
type="password"
autocomplete="new-password"
show-password
/>
</ElFormItem>
<ElButton
type="primary"
size="large"
:icon="Lock"
:loading="loading"
native-type="submit"
>
更新并进入
</ElButton>
<ElButton class="switch-account" text @click="logoutForAccountSwitch">
切换账号
</ElButton>
</template>
</ElForm>
</section>
</main>
</template>
<style scoped>
.system-return { position: absolute; top: 20px; right: 24px; }
.login-page {
display: grid;
min-height: 100vh;
place-items: center;
padding: 28px 20px;
background: #f8f9fb;
}
.login-panel {
width: min(410px, 100%);
padding: 38px;
border: 1px solid var(--border);
border-radius: 12px;
background: #fff;
}
.login-copy {
margin-bottom: 32px;
}
.login-copy .el-icon {
display: grid;
place-items: center;
width: 38px;
height: 38px;
margin-bottom: 24px;
border-radius: 9px;
background: #272d3a;
color: #fff;
font-size: 23px;
}
.login-copy h1 {
margin: 0;
color: var(--text);
font-size: 23px;
font-weight: 600;
letter-spacing: -0.5px;
}
.login-copy p {
margin: 10px 0 0;
color: var(--text-muted);
font-size: 12px;
}
.login-form .el-button {
width: 100%;
margin: 8px 0 0;
}
.login-error {
padding: 10px 12px;
border-radius: 5px;
background: #fff4f4;
color: #b04747;
font-size: 12px;
line-height: 1.6;
}
.change-title {
display: grid;
gap: 6px;
margin-bottom: 22px;
font-size: 13px;
}
.change-title span {
color: var(--text-muted);
font-size: 12px;
}
@media (max-width: 480px) {
.login-panel {
padding: 30px 24px;
}
}
</style>