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

654 lines
16 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.
<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">
<!-- 账号设置选项 -->
<a-card :bordered="false" class="settings-card">
<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, Modal } 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'
},
{
key: 'deactivateAccount',
title: '注销账号',
icon: LockOutlined,
bgColor: '#fff1f0'
}
])
// 根据用户状态动态更新账号设置选项
const updateAccountSettings = () => {
const settings = []
// 手机号相关
if (!hasPhone.value) {
settings.push({
key: 'bindPhone',
title: '绑定手机号',
icon: PhoneOutlined,
bgColor: '#e6f7ff'
})
} else {
settings.push({
key: 'changePhone',
title: '修改手机号',
icon: PhoneOutlined,
bgColor: '#e6f7ff'
})
}
// 密码相关
if (!hasPassword.value) {
settings.push({
key: 'setPassword',
title: '设置密码',
icon: LockOutlined,
bgColor: '#fff7e6'
})
} else {
settings.push({
key: 'changePassword',
title: '修改密码',
icon: LockOutlined,
bgColor: '#f6ffed'
})
}
// 注销账号
settings.push({
key: 'deactivateAccount',
title: '注销账号',
icon: LockOutlined,
bgColor: '#fff1f0'
})
accountSettings.value = settings
}
// 获取用户信息
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'
// 更新账号设置选项
updateAccountSettings()
} catch (error) {
console.error('获取用户信息失败:', error)
message.error('获取用户信息失败')
}
}
// 处理返回
const handleBackClick = () => {
router.back()
}
// 处理设置项点击
const handleSettingClick = (key: string) => {
switch (key) {
case 'bindPhone':
bindPhoneModalVisible.value = true
break
case 'changePhone':
changePhoneModalVisible.value = true
break
case 'setPassword':
handleSetPassword()
break
case 'changePassword':
changePasswordModalVisible.value = true
break
case 'deactivateAccount':
handleDeactivateAccount()
break
default:
break
}
}
// 处理设置密码
const handleSetPassword = () => {
router.push('/settings/password')
}
// 发送绑定手机号验证码
const handleSendVerificationCode = async () => {
if (!bindPhoneForm.phone) {
message.error('请输入手机号')
return
}
const phoneRegex = /^1[3-9]\d{9}$/
if (!phoneRegex.test(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
}
const phoneRegex = /^1[3-9]\d{9}$/
if (!phoneRegex.test(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
}
const phoneRegex = /^1[3-9]\d{9}$/
if (!phoneRegex.test(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,
verificationCode: changePasswordForm.verificationCode
})
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 = ''
}
// 处理注销账号
const handleDeactivateAccount = async () => {
Modal.confirm({
title: '确认注销账号',
content: '注销账号后,您的所有数据将被删除且无法恢复。确定要注销账号吗?',
okText: '确认注销',
okType: 'danger',
cancelText: '取消',
onOk: async () => {
try {
await userApi.deactivateAccount()
message.success('账号注销成功')
} catch (error) {
console.error('注销账号失败:', error)
message.error('注销账号失败')
} finally {
// 无论成功或失败,都清除本地存储的用户信息
localStorage.removeItem('accessToken')
localStorage.removeItem('refreshToken')
localStorage.removeItem('userInfo')
localStorage.removeItem('adminAccessToken')
localStorage.removeItem('adminRefreshToken')
localStorage.removeItem('adminUserInfo')
localStorage.removeItem('needSetPassword')
localStorage.removeItem('userPermissions')
sessionStorage.removeItem('accessToken')
sessionStorage.removeItem('adminAccessToken')
// 跳转到登录页
router.push('/')
}
}
})
}
// 生命周期
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>