Files
JayuCloud/Frontend/src/views/PasswordSettingsView.vue
T

263 lines
6.7 KiB
Vue
Raw Normal View History

<template>
<div class="mobile-container">
<header class="header">
<a-button class="back-btn" type="text" @click="handleBackClick">
<LeftOutlined :style="{ fontSize: '20px' }" />
</a-button>
<h1>设置密码</h1>
<div class="empty"></div>
</header>
<div class="password-form">
<a-card :bordered="false" class="form-card">
<a-form
ref="formRef"
:model="formState"
:rules="formRules"
layout="vertical"
@finish="handleSubmit"
>
2026-03-19 20:51:07 +08:00
<a-form-item label="手机号">
<a-input :value="userPhone || '未绑定手机号'" disabled size="large" />
</a-form-item>
<a-form-item label="验证码" name="verificationCode">
<a-input
v-model:value="formState.verificationCode"
placeholder="请输入验证码"
size="large"
>
<template #suffix>
<a-button
type="link"
:disabled="countdown > 0 || !userPhone"
@click="handleSendVerificationCode"
>
{{ countdown > 0 ? `${countdown}秒后重发` : '发送验证码' }}
</a-button>
</template>
</a-input>
</a-form-item>
<a-form-item label="新密码" name="password">
<a-input-password
v-model:value="formState.password"
placeholder="请输入新密码"
size="large"
@pressEnter="handleSubmit"
>
<template #prefix>
<LockOutlined style="color: #999" />
</template>
</a-input-password>
<div class="password-tip">
2026-03-19 20:51:07 +08:00
密码长度至少 8 位,且需包含字母和数字
</div>
</a-form-item>
<a-form-item label="确认密码" name="confirmPassword">
<a-input-password
v-model:value="formState.confirmPassword"
placeholder="请再次输入新密码"
size="large"
@pressEnter="handleSubmit"
>
<template #prefix>
<LockOutlined style="color: #999" />
</template>
</a-input-password>
</a-form-item>
<a-form-item>
<a-button
type="primary"
html-type="submit"
size="large"
block
:loading="loading"
>
确认设置
</a-button>
</a-form-item>
</a-form>
</a-card>
</div>
</div>
</template>
<script setup lang="ts">
2026-03-19 20:51:07 +08:00
import { computed, onMounted, onUnmounted, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { LeftOutlined, LockOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import type { FormInstance } from 'ant-design-vue'
2026-03-10 23:23:15 +08:00
import type { Rule } from 'ant-design-vue/es/form'
import { userApi } from '@/api/user'
const router = useRouter()
const formRef = ref<FormInstance>()
const loading = ref(false)
2026-03-19 20:51:07 +08:00
const countdown = ref(0)
const userInfo = ref<{ phone?: string } | null>(null)
let countdownTimer: ReturnType<typeof setInterval> | null = null
const formState = reactive({
2026-03-19 20:51:07 +08:00
verificationCode: '',
password: '',
confirmPassword: ''
})
2026-03-19 20:51:07 +08:00
const userPhone = computed(() => userInfo.value?.phone || '')
2026-03-10 23:23:15 +08:00
const validateConfirmPassword = (_rule: any, value: string) => {
if (value !== formState.password) {
return Promise.reject(new Error('两次输入的密码不一致'))
}
return Promise.resolve()
}
2026-03-10 23:23:15 +08:00
const formRules: Record<string, Rule[]> = {
2026-03-19 20:51:07 +08:00
verificationCode: [
{ required: true, message: '请输入验证码', trigger: 'blur' },
{ pattern: /^\d{6}$/, message: '验证码必须是6位数字', trigger: 'blur' }
],
password: [
{ required: true, message: '请输入新密码', trigger: 'blur' },
{ min: 8, message: '密码长度至少8位', trigger: 'blur' },
{ pattern: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d@$!%*#?&]{8,}$/, message: '密码必须包含字母和数字', trigger: 'blur' }
],
confirmPassword: [
{ required: true, message: '请确认新密码', trigger: 'blur' },
{ validator: validateConfirmPassword, trigger: 'blur' }
]
}
2026-03-19 20:51:07 +08:00
const startCountdown = () => {
countdown.value = 60
if (countdownTimer) {
clearInterval(countdownTimer)
}
countdownTimer = setInterval(() => {
countdown.value -= 1
if (countdown.value <= 0 && countdownTimer) {
clearInterval(countdownTimer)
countdownTimer = null
}
}, 1000)
}
const fetchUserInfo = async () => {
try {
const res = await userApi.getUserInfo()
userInfo.value = res as unknown as { phone?: string }
} catch (error: any) {
console.error('获取用户信息失败:', error)
message.error(error.message || '获取用户信息失败')
}
}
const handleSendVerificationCode = async () => {
if (!userPhone.value) {
message.error('请先绑定手机号')
return
}
try {
await userApi.sendVerificationCode({ phone: userPhone.value })
message.success('验证码发送成功')
startCountdown()
} catch (error: any) {
console.error('发送验证码失败:', error)
message.error(error.message || '发送验证码失败')
}
}
const handleSubmit = async () => {
if (!formRef.value) return
2026-03-19 20:51:07 +08:00
try {
await formRef.value.validate()
2026-03-19 20:51:07 +08:00
if (!userPhone.value) {
message.error('请先绑定手机号')
return
}
loading.value = true
await userApi.updatePassword({
2026-03-19 20:51:07 +08:00
password: formState.password,
verificationCode: formState.verificationCode
})
2026-03-19 20:51:07 +08:00
message.success('密码设置成功')
localStorage.removeItem('needSetPassword')
router.push('/profile')
} catch (error: any) {
console.error('设置密码失败:', error)
message.error(error.message || '设置密码失败')
} finally {
loading.value = false
}
}
const handleBackClick = () => {
router.back()
}
2026-03-19 20:51:07 +08:00
onMounted(() => {
fetchUserInfo()
})
onUnmounted(() => {
if (countdownTimer) {
clearInterval(countdownTimer)
}
})
</script>
<style scoped>
.mobile-container {
min-height: 100vh;
background: #f5f5f5;
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 15px;
background: white;
border-bottom: 1px solid #f0f0f0;
}
.header h1 {
font-size: 18px;
font-weight: bold;
margin: 0;
}
.back-btn,
.empty {
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
}
.password-form {
padding: 15px;
}
.form-card {
border-radius: 16px;
overflow: hidden;
background: white;
}
.password-tip {
margin-top: 8px;
2026-03-19 20:51:07 +08:00
color: #999;
font-size: 12px;
}
2026-03-19 20:51:07 +08:00
</style>