77 lines
2.7 KiB
Vue
77 lines
2.7 KiB
Vue
<template>
|
|
<el-dialog v-model="visible" title="修改密码" width="380px" align-center @close="emit('close')">
|
|
<el-form label-position="top" :model="form" :rules="rules" ref="formRef" class="pwd-form">
|
|
<el-form-item label="旧密码" prop="oldPwd">
|
|
<el-input v-model="form.oldPwd" type="password" show-password placeholder="请输入旧密码" @keyup.enter="changePwd" />
|
|
</el-form-item>
|
|
<el-form-item label="新密码" prop="newPwd">
|
|
<el-input v-model="form.newPwd" type="password" show-password placeholder="至少 6 位,不与原密码相同" @keyup.enter="changePwd" />
|
|
</el-form-item>
|
|
<el-form-item label="确认新密码" prop="newPwd2">
|
|
<el-input v-model="form.newPwd2" type="password" show-password placeholder="再次输入新密码" @keyup.enter="changePwd" />
|
|
</el-form-item>
|
|
</el-form>
|
|
<template #footer>
|
|
<el-button @click="visible = false">取消</el-button>
|
|
<el-button type="primary" :loading="pwdBusy" @click="changePwd">
|
|
{{ pwdBusy ? '提交中…' : '保存新密码' }}
|
|
</el-button>
|
|
</template>
|
|
</el-dialog>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { ref, reactive } from 'vue'
|
|
import { ElMessage } from 'element-plus'
|
|
import { store } from '../store'
|
|
import { adminPassword } from '../api'
|
|
|
|
const emit = defineEmits(['close'])
|
|
const visible = ref(true)
|
|
|
|
const formRef = ref()
|
|
const form = reactive({ oldPwd: '', newPwd: '', newPwd2: '' })
|
|
const rules = {
|
|
oldPwd: [{ required: true, message: '请输入原密码', trigger: 'blur' }],
|
|
newPwd: [
|
|
{ required: true, message: '请输入新密码', trigger: 'blur' },
|
|
{ min: 6, message: '新密码至少 6 位', trigger: 'blur' },
|
|
{
|
|
validator: (_, v, cb) => (v && v === form.oldPwd
|
|
? cb(new Error('新密码不能与原密码相同')) : cb()),
|
|
trigger: 'blur'
|
|
},
|
|
],
|
|
newPwd2: [
|
|
{ required: true, message: '请再次输入新密码', trigger: 'blur' },
|
|
{
|
|
validator: (_, v, cb) => (v !== form.newPwd
|
|
? cb(new Error('两次输入的密码不一致')) : cb()),
|
|
trigger: 'blur'
|
|
},
|
|
],
|
|
}
|
|
const pwdBusy = ref(false)
|
|
async function changePwd() {
|
|
await formRef.value.validate().catch(() => Promise.reject())
|
|
pwdBusy.value = true
|
|
try {
|
|
await adminPassword({
|
|
account: store.current,
|
|
old_password: form.oldPwd,
|
|
new_password: form.newPwd,
|
|
})
|
|
ElMessage.success('密码已修改,下次登录请使用新密码')
|
|
visible.value = false
|
|
} catch (e) {
|
|
ElMessage.error('修改失败:' + (e.message || e))
|
|
} finally {
|
|
pwdBusy.value = false
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<style scoped>
|
|
.pwd-form :deep(.el-form-item__label) { color: var(--text-sub); font-weight: 500; }
|
|
</style>
|