163 lines
5.2 KiB
Vue
163 lines
5.2 KiB
Vue
<script setup lang="ts">
|
|
/**
|
|
* features/auth/views/ResetPasswordView.vue
|
|
* 企微 textcard / 邮件链接重置密码页(/reset-password?token=...)。
|
|
* 依赖:auth.api confirmPasswordReset、vue-i18n、MinimalShell。
|
|
* 逻辑:校验 token 与两次密码一致后调 confirm 接口,成功后跳转 /login。
|
|
*/
|
|
import { onMounted, ref } from 'vue';
|
|
import { useRoute, useRouter } from 'vue-router';
|
|
import { useI18n } from 'vue-i18n';
|
|
import { Lock, AlertCircle, CheckCircle } from 'lucide-vue-next';
|
|
import MinimalShell from '@/shared/layouts/MinimalShell.vue';
|
|
import { confirmPasswordReset } from '@/features/auth/api/auth.api';
|
|
import { BusinessError } from '@/shared/api/client';
|
|
import { resolveCasdoorErrorMessage } from '@/shared/utils/casdoor';
|
|
|
|
const { t } = useI18n();
|
|
const route = useRoute();
|
|
const router = useRouter();
|
|
|
|
/**
|
|
* 从路由 query 解析 reset token,兼容重复参数与首尾空白。
|
|
* @returns 明文 token;缺失时返回空字符串
|
|
*/
|
|
function readResetTokenFromRoute(): string {
|
|
const raw = route.query.token;
|
|
const value = Array.isArray(raw) ? raw[0] : raw;
|
|
return String(value || '').trim();
|
|
}
|
|
|
|
const token = ref(readResetTokenFromRoute());
|
|
const password = ref('');
|
|
const confirmPassword = ref('');
|
|
const isLoading = ref(false);
|
|
const status = ref<'idle' | 'success' | 'error'>('idle');
|
|
const message = ref('');
|
|
|
|
/**
|
|
* 将后端/网络错误映射为页面可读文案。
|
|
* @param err - 捕获的异常
|
|
*/
|
|
function resolveResetErrorMessage(err: unknown): string {
|
|
if (err instanceof BusinessError) {
|
|
if (err.code === 'AUTH.RESET_TOKEN_INVALID') {
|
|
return t('resetPassword.tokenInvalid');
|
|
}
|
|
const raw = String(err.message || '').toLowerCase();
|
|
if (
|
|
err.code === 'VALIDATION.INVALID_PARAM'
|
|
&& (raw.includes('invalid or expired token') || raw.includes('reset_token_invalid'))
|
|
) {
|
|
return t('resetPassword.tokenInvalid');
|
|
}
|
|
return resolveCasdoorErrorMessage(err, t) || err.message || t('resetPassword.failed');
|
|
}
|
|
return (err as Error)?.message || t('resetPassword.failed');
|
|
}
|
|
|
|
onMounted(() => {
|
|
if (!token.value) {
|
|
status.value = 'error';
|
|
message.value = t('resetPassword.invalidLink');
|
|
}
|
|
});
|
|
|
|
/**
|
|
* 提交新密码并重置 token。
|
|
* 核心逻辑:前端校验 → confirmPasswordReset → 成功 3 秒后跳转登录页。
|
|
*/
|
|
const handleReset = async () => {
|
|
if (!token.value) {
|
|
status.value = 'error';
|
|
message.value = t('resetPassword.invalidLink');
|
|
return;
|
|
}
|
|
|
|
if (!password.value || !confirmPassword.value) {
|
|
message.value = t('resetPassword.fillAllFields');
|
|
status.value = 'error';
|
|
return;
|
|
}
|
|
|
|
if (password.value !== confirmPassword.value) {
|
|
message.value = t('resetPassword.passwordMismatch');
|
|
status.value = 'error';
|
|
return;
|
|
}
|
|
|
|
isLoading.value = true;
|
|
status.value = 'idle';
|
|
message.value = '';
|
|
|
|
try {
|
|
await confirmPasswordReset(token.value, password.value);
|
|
status.value = 'success';
|
|
message.value = t('resetPassword.successRedirect');
|
|
setTimeout(() => {
|
|
router.push('/login');
|
|
}, 3000);
|
|
} catch (e) {
|
|
status.value = 'error';
|
|
message.value = resolveResetErrorMessage(e);
|
|
} finally {
|
|
isLoading.value = false;
|
|
}
|
|
};
|
|
</script>
|
|
|
|
<template>
|
|
<MinimalShell>
|
|
<div class="text-center mb-8">
|
|
<div class="w-12 h-12 bg-brand-100 rounded-xl mx-auto flex items-center justify-center text-brand-600 mb-4">
|
|
<Lock class="w-6 h-6" />
|
|
</div>
|
|
<h2 class="page-title">{{ $t('resetPassword.title') }}</h2>
|
|
<p class="section-desc mt-2">{{ $t('resetPassword.desc') }}</p>
|
|
</div>
|
|
|
|
<div v-if="status === 'success'" class="alert-ds alert-success mb-6">
|
|
<CheckCircle class="w-5 h-5 shrink-0" />
|
|
<div>{{ message }}</div>
|
|
</div>
|
|
|
|
<div v-if="status === 'error'" class="alert-ds alert-error mb-6">
|
|
<AlertCircle class="w-5 h-5 shrink-0" />
|
|
<div>{{ message }}</div>
|
|
</div>
|
|
|
|
<form v-if="status !== 'success' && token" @submit.prevent="handleReset" class="space-y-5">
|
|
<div>
|
|
<label class="label-field mb-1.5 block">{{ $t('resetPassword.newPassword') }}</label>
|
|
<input
|
|
v-model="password"
|
|
type="password"
|
|
class="input-field"
|
|
:placeholder="$t('resetPassword.newPasswordPlaceholder')"
|
|
required
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label class="label-field mb-1.5 block">{{ $t('resetPassword.confirmPassword') }}</label>
|
|
<input
|
|
v-model="confirmPassword"
|
|
type="password"
|
|
class="input-field"
|
|
:placeholder="$t('resetPassword.confirmPasswordPlaceholder')"
|
|
required
|
|
/>
|
|
</div>
|
|
|
|
<button type="submit" :disabled="isLoading" class="btn-primary w-full py-3 flex justify-center items-center gap-2">
|
|
<div v-if="isLoading" class="animate-spin rounded-full h-4 w-4 border-2 border-white/30 border-t-white"></div>
|
|
<span>{{ isLoading ? $t('resetPassword.submitting') : $t('resetPassword.submit') }}</span>
|
|
</button>
|
|
</form>
|
|
|
|
<div class="mt-6 text-center">
|
|
<router-link to="/login" class="text-sm font-medium text-muted hover:text-secondary">{{ $t('resetPassword.backToLogin') }}</router-link>
|
|
</div>
|
|
</MinimalShell>
|
|
</template>
|