UnisKB/ui/src/layout/components/top-bar/avatar/ResetPassword.vue

219 lines
5.8 KiB
Vue
Raw Normal View History

2023-10-13 08:11:54 +00:00
<template>
2024-09-24 11:01:35 +00:00
<el-dialog
v-model="resetPasswordDialog"
:title="$t('layout.topbar.avatar.resetPassword')"
:close-on-click-modal="false"
:close-on-press-escape="false"
>
2023-10-13 08:11:54 +00:00
<el-form
2024-10-25 08:47:17 +00:00
class="reset-password-form"
ref="resetPasswordFormRef1"
2023-10-13 08:11:54 +00:00
:model="resetPasswordForm"
2024-10-25 08:47:17 +00:00
:rules="rules1"
2023-10-13 08:11:54 +00:00
>
<p class="mb-8 lighter">{{ $t('layout.topbar.avatar.dialog.newPassword') }}</p>
2024-02-23 03:37:37 +00:00
<el-form-item prop="password" style="margin-bottom: 8px">
2023-10-13 08:11:54 +00:00
<el-input
type="password"
class="input-item"
v-model="resetPasswordForm.password"
:placeholder="$t('layout.topbar.avatar.dialog.enterPassword')"
2023-12-08 10:19:31 +00:00
show-password
2023-10-13 08:11:54 +00:00
>
</el-input>
</el-form-item>
<el-form-item prop="re_password">
<el-input
type="password"
class="input-item"
v-model="resetPasswordForm.re_password"
:placeholder="$t('layout.topbar.avatar.dialog.confirmPassword')"
2023-12-08 10:19:31 +00:00
show-password
2023-10-13 08:11:54 +00:00
>
</el-input>
</el-form-item>
2024-10-25 08:47:17 +00:00
</el-form>
<el-form
class="reset-password-form mb-24"
ref="resetPasswordFormRef2"
:model="resetPasswordForm"
:rules="rules2"
>
<p class="mb-8 lighter">{{ $t('layout.topbar.avatar.dialog.useEmail') }}</p>
2024-02-23 03:37:37 +00:00
<el-form-item style="margin-bottom: 8px">
2023-10-13 08:11:54 +00:00
<el-input
class="input-item"
:disabled="true"
2023-10-19 10:18:45 +00:00
v-bind:modelValue="user.userInfo?.email"
:placeholder="$t('layout.topbar.avatar.dialog.enterEmail')"
2023-10-13 08:11:54 +00:00
>
</el-input>
</el-form-item>
<el-form-item prop="code">
<div class="flex-between w-full">
<el-input
class="code-input"
v-model="resetPasswordForm.code"
:placeholder="$t('layout.topbar.avatar.dialog.enterVerificationCode')"
>
2023-10-13 08:11:54 +00:00
</el-input>
2024-03-04 06:16:25 +00:00
<el-button
:disabled="isDisabled"
class="send-email-button ml-8"
@click="sendEmail"
:loading="loading"
>
{{
isDisabled
? $t('layout.topbar.avatar.dialog.resend', { time })
: $t('layout.topbar.avatar.dialog.getVerificationCode')
}}
</el-button>
2023-10-13 08:11:54 +00:00
</div>
</el-form-item>
</el-form>
<template #footer>
2024-02-23 08:17:16 +00:00
<div class="dialog-footer">
<el-button @click="resetPasswordDialog = false">{{
$t('layout.topbar.avatar.dialog.cancel')
}}</el-button>
<el-button type="primary" @click="resetPassword">
{{ $t('layout.topbar.avatar.dialog.save') }}
</el-button>
2024-02-23 08:17:16 +00:00
</div>
2023-10-13 08:11:54 +00:00
</template>
</el-dialog>
2023-09-15 09:40:35 +00:00
</template>
<script setup lang="ts">
2023-10-13 08:11:54 +00:00
import { ref } from 'vue'
2023-10-20 03:26:14 +00:00
import type { ResetCurrentUserPasswordRequest } from '@/api/type/user'
2023-09-15 09:40:35 +00:00
import type { FormInstance, FormRules } from 'element-plus'
2023-10-13 08:11:54 +00:00
import { MsgSuccess } from '@/utils/message'
import UserApi from '@/api/user'
2023-10-20 03:26:14 +00:00
import useStore from '@/stores'
2023-10-13 08:11:54 +00:00
import { useRouter } from 'vue-router'
import { t } from '@/locales'
2023-10-13 08:11:54 +00:00
const router = useRouter()
2023-10-20 03:26:14 +00:00
const { user } = useStore()
2023-09-15 09:40:35 +00:00
2023-10-13 08:11:54 +00:00
const resetPasswordDialog = ref<boolean>(false)
2023-09-15 09:40:35 +00:00
const resetPasswordForm = ref<ResetCurrentUserPasswordRequest>({
2023-10-13 08:11:54 +00:00
code: '',
password: '',
re_password: ''
})
2023-09-15 09:40:35 +00:00
2024-10-25 08:47:17 +00:00
const resetPasswordFormRef1 = ref<FormInstance>()
const resetPasswordFormRef2 = ref<FormInstance>()
2023-09-15 09:40:35 +00:00
2023-10-13 08:11:54 +00:00
const loading = ref<boolean>(false)
2024-03-04 06:16:25 +00:00
const isDisabled = ref<boolean>(false)
const time = ref<number>(60)
2023-09-15 09:40:35 +00:00
2024-10-25 08:47:17 +00:00
const rules1 = ref<FormRules<ResetCurrentUserPasswordRequest>>({
2023-10-13 08:11:54 +00:00
password: [
{
required: true,
message: t('layout.topbar.avatar.dialog.enterPassword'),
2023-10-13 08:11:54 +00:00
trigger: 'blur'
2023-09-15 09:40:35 +00:00
},
{
2023-10-13 08:11:54 +00:00
min: 6,
2023-12-08 10:19:31 +00:00
max: 20,
message: t('layout.topbar.avatar.dialog.passwordLength'),
2023-10-13 08:11:54 +00:00
trigger: 'blur'
}
],
re_password: [
{
required: true,
message: t('layout.topbar.avatar.dialog.confirmPassword'),
2023-10-13 08:11:54 +00:00
trigger: 'blur'
2023-09-15 09:40:35 +00:00
},
{
2023-10-13 08:11:54 +00:00
min: 6,
2023-12-08 10:19:31 +00:00
max: 20,
message: t('layout.topbar.avatar.dialog.passwordLength'),
2023-10-13 08:11:54 +00:00
trigger: 'blur'
},
{
validator: (rule, value, callback) => {
if (resetPasswordForm.value.password != resetPasswordForm.value.re_password) {
callback(new Error(t('layout.topbar.avatar.dialog.passwordMismatch')))
2023-10-13 08:11:54 +00:00
} else {
callback()
}
},
trigger: 'blur'
}
]
2023-09-15 09:40:35 +00:00
})
2024-10-25 08:47:17 +00:00
const rules2 = ref<FormRules<ResetCurrentUserPasswordRequest>>({
// @ts-ignore
code: [
{
required: true,
message: t('layout.topbar.avatar.dialog.enterVerificationCode'),
trigger: 'blur'
}
]
})
2023-09-15 09:40:35 +00:00
/**
* 发送验证码
*/
const sendEmail = () => {
2024-10-25 08:47:17 +00:00
resetPasswordFormRef1.value?.validate().then(() => {
UserApi.sendEmailToCurrent(loading).then(() => {
MsgSuccess(t('layout.topbar.avatar.dialog.verificationCodeSentSuccess'))
isDisabled.value = true
handleTimeChange()
})
2023-10-13 08:11:54 +00:00
})
2023-09-15 09:40:35 +00:00
}
2024-03-04 06:16:25 +00:00
const handleTimeChange = () => {
if (time.value <= 0) {
isDisabled.value = false
time.value = 60
} else {
setTimeout(() => {
time.value--
handleTimeChange()
}, 1000)
}
}
2023-09-15 09:40:35 +00:00
const open = () => {
2023-10-13 08:11:54 +00:00
resetPasswordForm.value = {
code: '',
password: '',
re_password: ''
}
resetPasswordDialog.value = true
2024-10-25 08:47:17 +00:00
resetPasswordFormRef1.value?.resetFields()
resetPasswordFormRef2.value?.resetFields()
2023-09-15 09:40:35 +00:00
}
const resetPassword = () => {
2024-10-25 08:47:17 +00:00
resetPasswordFormRef1.value?.validate().then(() => {
resetPasswordFormRef2.value
?.validate()
.then(() => {
return UserApi.resetCurrentUserPassword(resetPasswordForm.value)
})
.then(() => {
return user.logout()
})
.then(() => {
router.push({ name: 'login' })
})
})
2023-10-13 08:11:54 +00:00
}
const close = () => {
resetPasswordDialog.value = false
2023-09-15 09:40:35 +00:00
}
defineExpose({ open, close })
</script>
2023-10-20 03:26:14 +00:00
<style lang="scss" scope></style>