| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <template>
- <div class="login-container">
- <div class="login-form">
- <img :src="exitIcon" alt="关闭" class="exit-icon" @click="emit('close')" />
- <header class="login-form__header">
- <span>修改密码</span>
- </header>
- <main class="login-form__main">
- <el-form
- ref="formRef"
- :model="formValue"
- :rules="rules"
- label-width="auto"
- class="login-form__form"
- @keydown.enter="confirmUpdatePwd"
- >
- <el-form-item prop="oldPwd">
- <el-input
- placeholder="请输入原密码"
- v-model="formValue.oldPwd"
- type="password"
- autocomplete="off"
- show-password
- clearable
- class="el-input--default"
- />
- </el-form-item>
- <el-form-item prop="newPwd">
- <el-input
- placeholder="新密码(至少6位)"
- v-model="formValue.newPwd"
- type="password"
- autocomplete="off"
- show-password
- clearable
- class="el-input--default"
- />
- </el-form-item>
- <el-form-item prop="confirmPwd">
- <el-input
- placeholder="确认密码(至少6位)"
- v-model="formValue.confirmPwd"
- type="password"
- autocomplete="off"
- show-password
- clearable
- class="el-input--default"
- />
- </el-form-item>
- </el-form>
- </main>
- <footer class="login-form__footer">
- <el-button class="login-form__button" @click="confirmUpdatePwd">确定修改</el-button>
- </footer>
- </div>
- <div v-if="showOverlay" class="overlay">
- <div class="loading-text">{{ loadingText }}</div>
- </div>
- </div>
- </template>
- <script lang="ts" setup>
- import { ref, reactive, nextTick } from 'vue';
- import { ElMessage, ElLoading } from 'element-plus';
- import type { FormInstance } from 'element-plus';
- import exitIcon from 'assets/svg/exit.svg';
- import { useUserStore } from '@/store/modules/user';
- import router from '@/router';
- const userStore = useUserStore();
- const emit = defineEmits(['close']);
- const formValue = reactive({
- oldPwd: '',
- newPwd: '',
- confirmPwd: '',
- });
- const formRef = ref<FormInstance>();
- const showOverlay = ref(false);
- const loadingBaseText = '密码修改成功,正在退出登录';
- const loadingText = ref(loadingBaseText + '.');
- let dotCount = 1;
- let intervalId: number | null = null;
- // 启动省略号动画
- const startLoadingAnimation = async () => {
- showOverlay.value = true;
- intervalId = window.setInterval(() => {
- dotCount = (dotCount % 3) + 1;
- loadingText.value = loadingBaseText + '.'.repeat(dotCount);
- }, 200);
- };
- // 停止动画并跳转
- const stopLoadingAndRedirect = () => {
- if (intervalId !== null) {
- clearInterval(intervalId);
- intervalId = null;
- }
- showOverlay.value = false;
- };
- const validateConfirmPassword = (rule: any, value: string, callback: any) => {
- if (value !== formValue.newPwd) {
- callback(new Error('两次输入密码不一致'));
- } else {
- callback();
- }
- };
- const rules = {
- oldPwd: [{ required: true, message: '原密码不能为空', trigger: 'blur' }],
- newPwd: [
- { required: true, message: '新密码不能为空', trigger: 'blur' },
- { min: 6, message: '密码长度不能小于6位', trigger: 'blur' },
- ],
- confirmPwd: [
- { required: true, message: '请确认新密码', trigger: 'blur' },
- { validator: validateConfirmPassword, trigger: 'blur' },
- ],
- };
- const confirmUpdatePwd = () => {
- if (!formRef.value) return;
- formRef.value.validate((valid: boolean, ...rest) => {
- if (valid) {
- startLoadingAnimation();
- userStore
- .changePassword(formValue)
- .then(() => {
- emit('close');
- setTimeout(() => {
- stopLoadingAndRedirect();
- }, 20000);
- userStore.logout();
- router.push({ path: '/home' });
- })
- .catch(() => {
- ElMessage.error('修改密码失败');
- stopLoadingAndRedirect();
- });
- }
- });
- };
- </script>
- <style lang="scss" scoped>
- .login-container {
- @include flex-center;
- position: fixed;
- left: 0;
- top: 0;
- width: 100vw;
- height: 100vh;
- background-color: rgba(0, 0, 0, 0.42);
- z-index: 1000;
- }
- .login-form {
- position: relative;
- width: 700cpx;
- padding: 0 33cpx;
- background: $white-color;
- border-radius: 24cpx;
- &__header {
- width: 100%;
- height: 100cpx;
- font-size: 28cpx;
- font-weight: 550;
- color: #333;
- text-align: center;
- line-height: 100cpx;
- letter-spacing: 5cpx;
- }
- &__main {
- @include flex-center;
- flex-direction: column;
- gap: 20cpx;
- }
- &__code {
- @include flex-center;
- justify-content: space-between;
- width: 100%;
- height: 72cpx;
- .el-input--default {
- width: 50%;
- }
- }
- &__footer {
- width: 100%;
- height: 100cpx;
- margin-top: 18cpx;
- .login-form__button {
- width: 100%;
- height: 74cpx;
- font-size: 24cpx;
- color: $white-color;
- background-color: $primary-color;
- border-radius: 8cpx;
- cursor: pointer;
- }
- }
- .exit-icon {
- position: absolute;
- top: 15cpx;
- right: 16cpx;
- width: 40cpx;
- height: 40cpx;
- cursor: pointer;
- }
- }
- .login-form__form {
- display: flex;
- flex-direction: column;
- gap: 2cpx;
- width: 100%;
- }
- .el-input--default {
- width: 100%;
- height: 72cpx;
- :deep(.el-input__inner) {
- font-size: 20cpx;
- color: #999;
- }
- }
- .overlay {
- position: fixed;
- top: 0;
- left: 0;
- width: 100vw;
- height: 100vh;
- background-color: rgba(0, 0, 0, 0.6);
- display: flex;
- justify-content: center;
- align-items: center;
- z-index: 9999;
- }
- .loading-text {
- color: white;
- font-size: 20px;
- font-weight: bold;
- }
- </style>
|