feat(auth): 支持企业微信重置密码
This commit is contained in:
1 parent
9bc4d1dd18
commit
6baeffba64
55 files changed
+2852
-546
No files matched your search
@@ -1,33 +1,87 @@
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* features/auth/views/ResetPasswordView.vue
|
||||
* 邮件链接重置密码页(/reset-password?token=...)。
|
||||
* 企微 textcard / 邮件链接重置密码页(/reset-password?token=...)。
|
||||
* 依赖:user.api resetPasswordConfirm、vue-i18n、MinimalShell。
|
||||
* 逻辑:校验 token 与两次密码一致后调 confirm 接口,成功后跳转 /login。
|
||||
*/
|
||||
import { ref } from 'vue';
|
||||
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 { resetPasswordConfirm } from '@/features/auth/api/user.api';
|
||||
import { BusinessError } from '@/shared/api/client';
|
||||
import { resolveCasdoorErrorMessage } from '@/shared/utils/casdoor';
|
||||
|
||||
const { t } = useI18n();
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
|
||||
const token = ref(route.query.token as string || '');
|
||||
/**
|
||||
* 从路由 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。
|
||||
* 核心逻辑:前端校验 → resetPasswordConfirm → 成功 3 秒后跳转登录页。
|
||||
*/
|
||||
const handleReset = async () => {
|
||||
if (!token.value) {
|
||||
status.value = 'error';
|
||||
message.value = t('resetPassword.invalidLink');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!password.value || !confirmPassword.value) {
|
||||
message.value = 'Please fill in all fields';
|
||||
message.value = t('resetPassword.fillAllFields');
|
||||
status.value = 'error';
|
||||
return;
|
||||
}
|
||||
|
||||
if (password.value !== confirmPassword.value) {
|
||||
message.value = 'Passwords do not match';
|
||||
message.value = t('resetPassword.passwordMismatch');
|
||||
status.value = 'error';
|
||||
return;
|
||||
}
|
||||
@@ -39,13 +93,13 @@ const handleReset = async () => {
|
||||
try {
|
||||
await resetPasswordConfirm(token.value, password.value);
|
||||
status.value = 'success';
|
||||
message.value = 'Password reset successfully! Redirecting to login...';
|
||||
message.value = t('resetPassword.successRedirect');
|
||||
setTimeout(() => {
|
||||
router.push('/');
|
||||
router.push('/login');
|
||||
}, 3000);
|
||||
} catch (e) {
|
||||
status.value = 'error';
|
||||
message.value = (e as Error).message || 'Failed to reset password';
|
||||
message.value = resolveResetErrorMessage(e);
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
@@ -58,8 +112,8 @@ const handleReset = async () => {
|
||||
<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">Reset Password</h2>
|
||||
<p class="section-desc mt-2">Enter your new password below</p>
|
||||
<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">
|
||||
@@ -72,25 +126,37 @@ const handleReset = async () => {
|
||||
<div>{{ message }}</div>
|
||||
</div>
|
||||
|
||||
<form v-if="status !== 'success'" @submit.prevent="handleReset" class="space-y-5">
|
||||
<form v-if="status !== 'success' && token" @submit.prevent="handleReset" class="space-y-5">
|
||||
<div>
|
||||
<label class="label-field mb-1.5 block">New Password</label>
|
||||
<input v-model="password" type="password" class="input-field" placeholder="Enter new password" required />
|
||||
<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">Confirm Password</label>
|
||||
<input v-model="confirmPassword" type="password" class="input-field" placeholder="Confirm new password" required />
|
||||
<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 ? 'Resetting...' : 'Reset Password' }}</span>
|
||||
<span>{{ isLoading ? $t('resetPassword.submitting') : $t('resetPassword.submit') }}</span>
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div class="mt-6 text-center">
|
||||
<router-link to="/" class="text-sm font-medium text-muted hover:text-secondary">Back to Login</router-link>
|
||||
<router-link to="/login" class="text-sm font-medium text-muted hover:text-secondary">{{ $t('resetPassword.backToLogin') }}</router-link>
|
||||
</div>
|
||||
</MinimalShell>
|
||||
</template>
|
||||
Reference in new issue
Block a user