feat(user): 添加用户信息修改功能及对应验证码校验
- 在管理员页面新增修改用户信息表单,支持姓名、手机号、密码修改 - 实现验证码发送倒计时与发送状态管理 - 新增接口支持用户信息更新,包含密码和手机号校验 - 后端校验验证码有效性,编码密码后更新用户信息 - 修改用户信息后强制登出,确保安全性 - 优化登录状态判断,登出后跳转至登录页 - 取消部分日志打印,调整发送验证码缓存过期时间为5分钟
This commit is contained in:
@@ -15,3 +15,7 @@ export function getVerificationCode(data) {
|
||||
export function getUserInfo() {
|
||||
return axios.post("/user/info")
|
||||
}
|
||||
|
||||
export function updateUserInfo(data) {
|
||||
return axios.post("/user/update-user-info", data)
|
||||
}
|
||||
|
||||
@@ -83,9 +83,14 @@ async function refreshUser() {
|
||||
try {
|
||||
const r = await getUserInfo()
|
||||
const d = r?.data
|
||||
userName.value = d?.success ? (d?.data?.name || '') : ''
|
||||
console.log("header" + d.success)
|
||||
if (d?.success) {
|
||||
userName.value = d?.data?.name || ''
|
||||
} else {
|
||||
handleLogout()
|
||||
}
|
||||
} catch {
|
||||
userName.value = ''
|
||||
handleLogout()
|
||||
}
|
||||
}
|
||||
async function handleLogout() {
|
||||
@@ -96,7 +101,7 @@ async function handleLogout() {
|
||||
userName.value = ''
|
||||
menuOpen.value = false
|
||||
showMessage('已退出登录', 'success')
|
||||
router.push('/')
|
||||
router.push('/login')
|
||||
}
|
||||
}
|
||||
function onDocClick(e) {
|
||||
|
||||
@@ -48,6 +48,40 @@
|
||||
<el-alert type="success" :closable="false" :title="`邀请码:${inviteCode}`" show-icon />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel-shell p-6">
|
||||
<div class="flex items-center justify-between mb-4">
|
||||
<span class="text-lg font-semibold">修改用户信息</span>
|
||||
</div>
|
||||
<el-form :model="pwForm" :rules="pwRules" ref="pwFormRef" label-width="120px">
|
||||
<el-form-item label="姓名" prop="name">
|
||||
<el-input v-model="pwForm.name" />
|
||||
</el-form-item>
|
||||
<el-form-item label="新密码" prop="newPassword">
|
||||
<el-input v-model="pwForm.newPassword" type="password" show-password />
|
||||
</el-form-item>
|
||||
<el-form-item label="确认密码" prop="confirmPassword">
|
||||
<el-input v-model="pwForm.confirmPassword" type="password" show-password />
|
||||
</el-form-item>
|
||||
<el-form-item label="手机号" prop="phone">
|
||||
<el-input v-model="pwForm.phone" />
|
||||
</el-form-item>
|
||||
<el-form-item label="验证码" prop="code">
|
||||
<el-input v-model="pwForm.code">
|
||||
<template #append>
|
||||
<el-button :disabled="codeCountdown > 0 || codeSending || !pwForm.phone"
|
||||
@click="sendCode">
|
||||
{{ codeCountdown > 0 ? `${codeCountdown}s` : '获取验证码' }}
|
||||
</el-button>
|
||||
</template>
|
||||
</el-input>
|
||||
</el-form-item>
|
||||
<el-form-item>
|
||||
<el-button type="primary" :loading="pwLoading" @click="submitPw">修改用户信息</el-button>
|
||||
<el-button class="ml-2" @click="resetPw">重置</el-button>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<el-dialog v-model="createVisible" title="新增用户" width="420px">
|
||||
@@ -76,8 +110,9 @@
|
||||
|
||||
<script setup>
|
||||
import Header from '@/layouts/components/Header.vue'
|
||||
import { ref, reactive, onMounted } from 'vue'
|
||||
import { ref, reactive, onMounted, onUnmounted } from 'vue'
|
||||
import { getUserList, createUser, createInvitationCode } from '@/api/admin'
|
||||
import { updateUserInfo, getVerificationCode } from '@/api/user'
|
||||
import { showMessage } from '@/composables/util.js'
|
||||
import Sidebar from '@/layouts/components/Sidebar.vue'
|
||||
|
||||
@@ -203,4 +238,109 @@ function submitInvite() {
|
||||
})
|
||||
}
|
||||
|
||||
const pwForm = reactive({ name: '', phone: '', newPassword: '', confirmPassword: '', code: '' })
|
||||
const pwFormRef = ref()
|
||||
const pwLoading = ref(false)
|
||||
const pwRules = {
|
||||
phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
|
||||
newPassword: [],
|
||||
confirmPassword: [
|
||||
{
|
||||
validator: (rule, value, callback) => {
|
||||
if (pwForm.newPassword) {
|
||||
if (!value) {
|
||||
callback(new Error('请确认密码'))
|
||||
return
|
||||
}
|
||||
if (value !== pwForm.newPassword) {
|
||||
callback(new Error('两次输入的密码不一致'))
|
||||
return
|
||||
}
|
||||
}
|
||||
callback()
|
||||
},
|
||||
trigger: 'blur'
|
||||
}
|
||||
],
|
||||
code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
|
||||
}
|
||||
|
||||
function resetPw() {
|
||||
if (codeTimer) {
|
||||
clearInterval(codeTimer)
|
||||
codeTimer = null
|
||||
}
|
||||
codeCountdown.value = 0
|
||||
codeSending.value = false
|
||||
pwForm.name = ''
|
||||
pwForm.phone = ''
|
||||
pwForm.newPassword = ''
|
||||
pwForm.confirmPassword = ''
|
||||
pwForm.code = ''
|
||||
}
|
||||
|
||||
const codeCountdown = ref(0)
|
||||
const codeSending = ref(false)
|
||||
let codeTimer = null
|
||||
|
||||
async function sendCode() {
|
||||
if (!pwForm.phone) {
|
||||
showMessage('请输入手机号', 'warning')
|
||||
return
|
||||
}
|
||||
if (codeSending.value || codeCountdown.value > 0) return
|
||||
codeSending.value = true
|
||||
try {
|
||||
const r = await getVerificationCode({ phone: pwForm.phone })
|
||||
const d = r?.data
|
||||
if (d?.success) {
|
||||
showMessage('验证码已发送', 'success')
|
||||
codeCountdown.value = 60
|
||||
codeTimer = setInterval(() => {
|
||||
if (codeCountdown.value > 0) {
|
||||
codeCountdown.value -= 1
|
||||
} else {
|
||||
clearInterval(codeTimer)
|
||||
codeTimer = null
|
||||
}
|
||||
}, 1000)
|
||||
} else {
|
||||
showMessage(d?.message || '发送验证码失败', 'error')
|
||||
}
|
||||
} finally {
|
||||
codeSending.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onUnmounted(() => {
|
||||
if (codeTimer) {
|
||||
clearInterval(codeTimer)
|
||||
codeTimer = null
|
||||
}
|
||||
})
|
||||
|
||||
function submitPw() {
|
||||
pwFormRef.value?.validate(async (valid) => {
|
||||
if (!valid) return
|
||||
pwLoading.value = true
|
||||
try {
|
||||
const r = await updateUserInfo({
|
||||
newPassword: pwForm.newPassword || '',
|
||||
name: pwForm.name || '',
|
||||
phone: pwForm.phone,
|
||||
code: pwForm.code
|
||||
})
|
||||
const d = r?.data
|
||||
if (d?.success) {
|
||||
showMessage('用户信息修改成功', 'success')
|
||||
resetPw()
|
||||
} else {
|
||||
showMessage(d?.message || '用户信息修改失败', 'error')
|
||||
}
|
||||
} finally {
|
||||
pwLoading.value = false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user