192 lines
4.6 KiB
Vue
192 lines
4.6 KiB
Vue
<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"
|
||
>
|
||
<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">
|
||
密码长度至少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">
|
||
import { ref, reactive } 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'
|
||
import { userApi } from '@/api/user'
|
||
|
||
const router = useRouter()
|
||
const formRef = ref<FormInstance>()
|
||
const loading = ref(false)
|
||
|
||
const formState = reactive({
|
||
password: '',
|
||
confirmPassword: ''
|
||
})
|
||
|
||
const validateConfirmPassword = (_, value: string) => {
|
||
if (value !== formState.password) {
|
||
return Promise.reject(new Error('两次输入的密码不一致'))
|
||
}
|
||
return Promise.resolve()
|
||
}
|
||
|
||
const formRules = {
|
||
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' }
|
||
]
|
||
}
|
||
|
||
const handleSubmit = async () => {
|
||
if (!formRef.value) return
|
||
|
||
try {
|
||
await formRef.value.validate()
|
||
loading.value = true
|
||
|
||
await userApi.updatePassword({
|
||
password: formState.password
|
||
})
|
||
|
||
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()
|
||
}
|
||
</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;
|
||
}
|
||
|
||
.form-card :deep(.ant-card-body) {
|
||
padding: 24px;
|
||
}
|
||
|
||
.password-tip {
|
||
font-size: 12px;
|
||
color: #999;
|
||
margin-top: 8px;
|
||
}
|
||
|
||
.form-card :deep(.ant-btn-primary) {
|
||
height: 48px;
|
||
border-radius: 12px;
|
||
font-size: 15px;
|
||
font-weight: 600;
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
border: none;
|
||
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
|
||
}
|
||
</style> |