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

565 lines
14 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="account-settings">
<!-- 状态提示 -->
<div v-if="!hasPassword || !hasPhone" class="status-tips">
<a-alert
v-if="!hasPassword"
type="warning"
message="请设置密码"
description="为了账户安全,建议您设置一个安全的密码"
show-icon
:closable="false"
>
<template #action>
<a-button size="small" type="primary" @click="handleSetPassword">去设置</a-button>
</template>
</a-alert>
<a-alert
v-if="!hasPhone"
type="warning"
message="请绑定手机号"
description="绑定手机号可以提高账户安全性"
show-icon
:closable="false"
>
<template #action>
<a-button size="small" type="primary" @click="handleBindPhone">去绑定</a-button>
</template>
</a-alert>
</div>
<!-- 账号设置选项 -->
<a-card :bordered="false" class="settings-card" v-if="hasPassword && hasPhone">
<a-list :data-source="accountSettings" :split="false">
<template #renderItem="{ item }">
<a-list-item class="setting-item" @click="handleSettingClick(item.key)">
<template #actions>
<RightOutlined />
</template>
<a-list-item-meta>
<template #avatar>
<div class="setting-icon" :style="{ backgroundColor: item.bgColor }">
<component :is="item.icon" />
</div>
</template>
<template #title>
<span>{{ item.title }}</span>
</template>
<template #description v-if="item.desc">
<span>{{ item.desc }}</span>
</template>
</a-list-item-meta>
</a-list-item>
</template>
</a-list>
</a-card>
</div>
<!-- 绑定手机号弹窗 -->
<a-modal
v-model:visible="bindPhoneModalVisible"
title="绑定手机号"
@ok="handleBindPhoneSubmit"
@cancel="handleBindPhoneCancel"
:confirmLoading="bindPhoneLoading"
>
<a-form layout="vertical">
<a-form-item label="手机号" name="phone">
<a-input
v-model:value="bindPhoneForm.phone"
placeholder="请输入手机号"
:disabled="!!userInfo.phone"
/>
</a-form-item>
<a-form-item label="验证码" name="verificationCode">
<a-input
v-model:value="bindPhoneForm.verificationCode"
placeholder="请输入验证码"
>
<template #suffix>
<a-button
type="link"
:disabled="countdown > 0 || !bindPhoneForm.phone"
@click="handleSendVerificationCode"
>
{{ countdown > 0 ? `${countdown}秒后重发` : '发送验证码' }}
</a-button>
</template>
</a-input>
</a-form-item>
</a-form>
</a-modal>
<!-- 修改手机号弹窗 -->
<a-modal
v-model:visible="changePhoneModalVisible"
title="修改手机号"
@ok="handleChangePhoneSubmit"
@cancel="handleChangePhoneCancel"
:confirmLoading="changePhoneLoading"
>
<a-form layout="vertical">
<a-form-item label="旧手机号">
<a-input :value="userInfo.phone" disabled />
</a-form-item>
<a-form-item label="新手机号" name="newPhone">
<a-input
v-model:value="changePhoneForm.newPhone"
placeholder="请输入新手机号"
/>
</a-form-item>
<a-form-item label="验证码" name="verificationCode">
<a-input
v-model:value="changePhoneForm.verificationCode"
placeholder="请输入验证码"
>
<template #suffix>
<a-button
type="link"
:disabled="countdown > 0 || !changePhoneForm.newPhone"
@click="handleSendChangePhoneVerificationCode"
>
{{ countdown > 0 ? `${countdown}秒后重发` : '发送验证码' }}
</a-button>
</template>
</a-input>
</a-form-item>
</a-form>
</a-modal>
<!-- 修改密码弹窗 -->
<a-modal
v-model:visible="changePasswordModalVisible"
title="修改密码"
@ok="handleChangePasswordSubmit"
@cancel="handleChangePasswordCancel"
:confirmLoading="changePasswordLoading"
>
<a-form layout="vertical">
<a-form-item label="手机号">
<a-input :value="userInfo.phone" disabled />
</a-form-item>
<a-form-item label="验证码" name="verificationCode">
<a-input
v-model:value="changePasswordForm.verificationCode"
placeholder="请输入验证码"
>
<template #suffix>
<a-button
type="link"
:disabled="countdown > 0"
@click="handleSendChangePasswordVerificationCode"
>
{{ countdown > 0 ? `${countdown}秒后重发` : '发送验证码' }}
</a-button>
</template>
</a-input>
</a-form-item>
<a-form-item label="新密码" name="password">
<a-input-password
v-model:value="changePasswordForm.password"
placeholder="请输入新密码"
/>
<div class="password-tip">
密码长度至少8位,包含字母和数字
</div>
</a-form-item>
<a-form-item label="确认密码" name="confirmPassword">
<a-input-password
v-model:value="changePasswordForm.confirmPassword"
placeholder="请再次输入新密码"
/>
</a-form-item>
</a-form>
</a-modal>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { LeftOutlined, RightOutlined, LockOutlined, PhoneOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import { userApi } from '@/api/user'
import type { UserVO } from '@/api/user'
const router = useRouter()
// 用户信息
const userInfo = ref<UserVO>({
id: 0,
nickname: '',
account: '',
avatar: '',
type: 0,
status: 0,
createTime: '',
updateTime: ''
})
// 状态标识
const hasPassword = ref(false)
const hasPhone = ref(false)
// 绑定手机号表单
const bindPhoneModalVisible = ref(false)
const bindPhoneLoading = ref(false)
const bindPhoneForm = reactive({
phone: '',
verificationCode: ''
})
// 修改手机号表单
const changePhoneModalVisible = ref(false)
const changePhoneLoading = ref(false)
const changePhoneForm = reactive({
newPhone: '',
verificationCode: ''
})
// 修改密码表单
const changePasswordModalVisible = ref(false)
const changePasswordLoading = ref(false)
const changePasswordForm = reactive({
verificationCode: '',
password: '',
confirmPassword: ''
})
// 倒计时
const countdown = ref(0)
// 账号设置选项
const accountSettings = ref([
{
key: 'changePhone',
title: '修改手机号',
icon: PhoneOutlined,
bgColor: '#e6f7ff'
},
{
key: 'changePassword',
title: '修改密码',
icon: LockOutlined,
bgColor: '#f6ffed'
}
])
// 获取用户信息
const fetchUserInfo = async () => {
try {
const response = await userApi.getUserInfo() as any
userInfo.value = response
// 检查是否绑定手机号
hasPhone.value = !!userInfo.value.phone
// 检查是否设置密码(通过localStorage中的标识)
hasPassword.value = localStorage.getItem('needSetPassword') !== 'true'
} catch (error) {
console.error('获取用户信息失败:', error)
message.error('获取用户信息失败')
}
}
// 处理返回
const handleBackClick = () => {
router.back()
}
// 处理设置项点击
const handleSettingClick = (key: string) => {
switch (key) {
case 'changePhone':
changePhoneModalVisible.value = true
break
case 'changePassword':
changePasswordModalVisible.value = true
break
default:
break
}
}
// 处理设置密码
const handleSetPassword = () => {
router.push('/settings/password')
}
// 处理绑定手机号
const handleBindPhone = () => {
bindPhoneModalVisible.value = true
}
// 发送验证码
const handleSendVerificationCode = async () => {
if (!bindPhoneForm.phone) {
message.error('请输入手机号')
return
}
try {
await userApi.sendVerificationCode({ phone: bindPhoneForm.phone })
message.success('验证码发送成功')
startCountdown()
} catch (error) {
console.error('发送验证码失败:', error)
message.error('发送验证码失败')
}
}
// 发送修改手机号验证码
const handleSendChangePhoneVerificationCode = async () => {
if (!changePhoneForm.newPhone) {
message.error('请输入新手机号')
return
}
try {
await userApi.sendVerificationCode({ phone: changePhoneForm.newPhone })
message.success('验证码发送成功')
startCountdown()
} catch (error) {
console.error('发送验证码失败:', error)
message.error('发送验证码失败')
}
}
// 发送修改密码验证码
const handleSendChangePasswordVerificationCode = async () => {
if (!userInfo.value.phone) {
message.error('请先绑定手机号')
return
}
try {
await userApi.sendVerificationCode({ phone: userInfo.value.phone })
message.success('验证码发送成功')
startCountdown()
} catch (error) {
console.error('发送验证码失败:', error)
message.error('发送验证码失败')
}
}
// 开始倒计时
const startCountdown = () => {
countdown.value = 60
const timer = setInterval(() => {
countdown.value--
if (countdown.value <= 0) {
clearInterval(timer)
}
}, 1000)
}
// 处理绑定手机号提交
const handleBindPhoneSubmit = async () => {
if (!bindPhoneForm.phone || !bindPhoneForm.verificationCode) {
message.error('请填写完整信息')
return
}
bindPhoneLoading.value = true
try {
await userApi.bindPhone({
phone: bindPhoneForm.phone,
verificationCode: bindPhoneForm.verificationCode
})
message.success('手机号绑定成功')
bindPhoneModalVisible.value = false
await fetchUserInfo()
} catch (error: any) {
console.error('绑定手机号失败:', error)
message.error(error.message || '绑定手机号失败')
} finally {
bindPhoneLoading.value = false
}
}
// 处理绑定手机号取消
const handleBindPhoneCancel = () => {
bindPhoneModalVisible.value = false
bindPhoneForm.phone = ''
bindPhoneForm.verificationCode = ''
}
// 处理修改手机号提交
const handleChangePhoneSubmit = async () => {
if (!changePhoneForm.newPhone || !changePhoneForm.verificationCode) {
message.error('请填写完整信息')
return
}
changePhoneLoading.value = true
try {
await userApi.updatePhone({
oldPhone: userInfo.value.phone!,
newPhone: changePhoneForm.newPhone,
verificationCode: changePhoneForm.verificationCode
})
message.success('手机号修改成功')
changePhoneModalVisible.value = false
await fetchUserInfo()
} catch (error: any) {
console.error('修改手机号失败:', error)
message.error(error.message || '修改手机号失败')
} finally {
changePhoneLoading.value = false
}
}
// 处理修改手机号取消
const handleChangePhoneCancel = () => {
changePhoneModalVisible.value = false
changePhoneForm.newPhone = ''
changePhoneForm.verificationCode = ''
}
// 处理修改密码提交
const handleChangePasswordSubmit = async () => {
if (!changePasswordForm.verificationCode || !changePasswordForm.password || !changePasswordForm.confirmPassword) {
message.error('请填写完整信息')
return
}
if (changePasswordForm.password !== changePasswordForm.confirmPassword) {
message.error('两次输入的密码不一致')
return
}
changePasswordLoading.value = true
try {
await userApi.updatePassword({ password: changePasswordForm.password })
message.success('密码修改成功')
changePasswordModalVisible.value = false
// 清除需要设置密码的标识
localStorage.removeItem('needSetPassword')
hasPassword.value = true
} catch (error: any) {
console.error('修改密码失败:', error)
message.error(error.message || '修改密码失败')
} finally {
changePasswordLoading.value = false
}
}
// 处理修改密码取消
const handleChangePasswordCancel = () => {
changePasswordModalVisible.value = false
changePasswordForm.verificationCode = ''
changePasswordForm.password = ''
changePasswordForm.confirmPassword = ''
}
// 生命周期
onMounted(() => {
fetchUserInfo()
})
</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;
}
.account-settings {
padding: 15px;
}
.status-tips {
margin-bottom: 15px;
}
.settings-card {
border-radius: 16px;
overflow: hidden;
background: white;
}
.settings-card :deep(.ant-card-body) {
padding: 0;
}
.setting-item {
padding: 16px;
cursor: pointer;
transition: background 0.3s;
border-bottom: 1px solid #f0f0f0;
}
.setting-item:hover {
background: #fafafa;
}
.setting-item:last-child {
border-bottom: none;
}
.setting-item :deep(.ant-list-item-action) {
margin-left: 8px;
color: #ccc;
}
.setting-icon {
width: 40px;
height: 40px;
border-radius: 10px;
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
color: white;
}
.setting-item :deep(.ant-list-item-meta-title) {
font-size: 15px;
font-weight: 500;
color: #333;
}
.setting-item :deep(.ant-list-item-meta-description) {
font-size: 12px;
color: #999;
}
.password-tip {
font-size: 12px;
color: #999;
margin-top: 8px;
}
</style>