UnisKB/ui/src/layout/layout-header/avatar/ResetPassword.vue

164 lines
4.1 KiB
Vue
Raw Normal View History

2025-04-28 07:23:25 +00:00
<template>
<el-dialog
v-model="resetPasswordDialog"
:title="$t('views.login.resetPassword')"
2025-07-08 17:31:21 +00:00
destroy-on-close
append-to-body
align-center
2025-04-28 07:23:25 +00:00
:close-on-click-modal="false"
:close-on-press-escape="false"
>
<el-form
class="reset-password-form"
ref="resetPasswordFormRef1"
:model="resetPasswordForm"
:rules="rules1"
>
<p class="mb-8 lighter">{{ $t('views.login.newPassword') }}</p>
<el-form-item prop="password" style="margin-bottom: 8px">
<el-input
type="password"
class="input-item"
v-model="resetPasswordForm.password"
:placeholder="$t('views.login.enterPassword')"
show-password
>
</el-input>
</el-form-item>
<el-form-item prop="re_password">
<el-input
type="password"
class="input-item"
v-model="resetPasswordForm.re_password"
2025-06-12 11:23:11 +00:00
:placeholder="$t('views.login.enterPassword')"
2025-04-28 07:23:25 +00:00
show-password
>
</el-input>
</el-form-item>
</el-form>
<template #footer>
<div class="dialog-footer">
<el-button @click="resetPasswordDialog = false">{{ $t('common.cancel') }}</el-button>
<el-button type="primary" @click="resetPassword">
{{ $t('common.save') }}
</el-button>
</div>
</template>
</el-dialog>
</template>
<script setup lang="ts">
2025-06-13 03:00:15 +00:00
import { ref } from 'vue'
import type { ResetCurrentUserPasswordRequest } from '@/api/type/user'
import type { FormInstance, FormRules } from 'element-plus'
2025-07-01 12:52:00 +00:00
import UserApi from '@/api/user/user'
2025-04-28 07:23:25 +00:00
import useStore from '@/stores'
2025-06-13 03:00:15 +00:00
import { useRouter } from 'vue-router'
import { t } from '@/locales'
2025-06-12 11:23:11 +00:00
2025-06-25 08:49:17 +00:00
const props = defineProps<{
2025-07-08 17:31:21 +00:00
emitConfirm?: boolean // 在父级调接口
2025-06-25 08:49:17 +00:00
}>()
const emit = defineEmits<{
2025-07-08 17:31:21 +00:00
(e: 'confirm', value: ResetCurrentUserPasswordRequest): void
}>()
2025-06-25 08:49:17 +00:00
2025-04-28 07:23:25 +00:00
const router = useRouter()
2025-06-13 03:00:15 +00:00
const { login } = useStore()
2025-04-28 07:23:25 +00:00
const resetPasswordDialog = ref<boolean>(false)
const resetPasswordForm = ref<ResetCurrentUserPasswordRequest>({
code: '',
password: '',
2025-06-13 03:00:15 +00:00
re_password: '',
2025-04-28 07:23:25 +00:00
})
const resetPasswordFormRef1 = ref<FormInstance>()
const resetPasswordFormRef2 = ref<FormInstance>()
const loading = ref<boolean>(false)
const isDisabled = ref<boolean>(false)
const time = ref<number>(60)
const rules1 = ref<FormRules<ResetCurrentUserPasswordRequest>>({
password: [
{
required: true,
message: t('views.login.enterPassword'),
2025-06-13 03:00:15 +00:00
trigger: 'blur',
2025-04-28 07:23:25 +00:00
},
{
min: 6,
max: 20,
2025-06-13 06:05:23 +00:00
message: t('views.login.loginForm.password.lengthMessage'),
2025-06-13 03:00:15 +00:00
trigger: 'blur',
},
2025-04-28 07:23:25 +00:00
],
re_password: [
{
required: true,
2025-06-13 06:05:23 +00:00
message: t('views.login.loginForm.re_password.requiredMessage'),
2025-06-13 03:00:15 +00:00
trigger: 'blur',
2025-04-28 07:23:25 +00:00
},
{
min: 6,
max: 20,
2025-06-13 06:05:23 +00:00
message: t('views.login.loginForm.password.lengthMessage'),
2025-06-13 03:00:15 +00:00
trigger: 'blur',
2025-04-28 07:23:25 +00:00
},
{
validator: (rule, value, callback) => {
if (resetPasswordForm.value.password != resetPasswordForm.value.re_password) {
2025-06-13 06:05:23 +00:00
callback(new Error(t('views.login.loginForm.re_password.validatorMessage')))
2025-04-28 07:23:25 +00:00
} else {
callback()
}
},
2025-06-13 03:00:15 +00:00
trigger: 'blur',
},
],
2025-04-28 07:23:25 +00:00
})
const handleTimeChange = () => {
if (time.value <= 0) {
isDisabled.value = false
time.value = 60
} else {
setTimeout(() => {
time.value--
handleTimeChange()
}, 1000)
}
}
const open = () => {
resetPasswordForm.value = {
2025-06-12 11:23:11 +00:00
//code: '',
2025-04-28 07:23:25 +00:00
password: '',
2025-06-13 03:00:15 +00:00
re_password: '',
2025-04-28 07:23:25 +00:00
}
resetPasswordDialog.value = true
resetPasswordFormRef1.value?.resetFields()
resetPasswordFormRef2.value?.resetFields()
}
const resetPassword = () => {
resetPasswordFormRef1.value?.validate().then(() => {
2025-07-08 17:31:21 +00:00
if (props.emitConfirm) {
2025-06-25 08:49:17 +00:00
emit('confirm', resetPasswordForm.value)
} else {
return UserApi.resetCurrentPassword(resetPasswordForm.value).then(() => {
2025-07-08 17:31:21 +00:00
login.logout()
router.push({ name: 'login' })
})
2025-06-25 08:49:17 +00:00
}
2025-04-28 07:23:25 +00:00
})
}
const close = () => {
resetPasswordDialog.value = false
}
2025-06-13 03:00:15 +00:00
defineExpose({ open, close })
2025-04-28 07:23:25 +00:00
</script>
<style lang="scss" scope></style>