feat(auth): 支持企业微信重置密码
This commit is contained in:
1 parent
9bc4d1dd18
commit
6baeffba64
55 files changed
+2852
-546
No files matched your search
@@ -35,6 +35,13 @@ export const submitLeaveWorkflow = (form: Record<string, any>) =>
|
||||
body: JSON.stringify(form),
|
||||
});
|
||||
|
||||
export const resetPasswordByWecom = (employeeId: string, username: string) =>
|
||||
requestJson('/api/user/reset-password-by-wecom', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ employee_id: employeeId, username }),
|
||||
});
|
||||
|
||||
export const resetPasswordConfirm = (token: string, newPassword: string) =>
|
||||
requestJson('/api/user/reset-password-confirm', {
|
||||
method: 'POST',
|
||||
|
||||
@@ -1,24 +1,45 @@
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* features/auth/components/LoginModals.vue
|
||||
* 登录相关弹窗:找回密码、公告详情、企微登录/绑定提示、注册。
|
||||
* 用途:登录相关弹窗(找回密码、公告、企微提示、注册)。
|
||||
* 依赖:login.context、useDialogBackdropClose、wecom 工具函数。
|
||||
* 逻辑:LoginPage 注入 LOGIN_KEY;遮罩关闭用 pointer 判定避免拖选误关。
|
||||
*/
|
||||
import { inject } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { Lock, User, X } from 'lucide-vue-next';
|
||||
import { formatWecomFieldLabel } from '@/shared/utils/wecom';
|
||||
import { createDialogBackdropCloseHandler } from '@/shared/composables/useDialogBackdropClose';
|
||||
import { LOGIN_KEY } from '../composables/login.context';
|
||||
|
||||
const { t } = useI18n();
|
||||
const lk = inject(LOGIN_KEY)! as any;
|
||||
|
||||
const forgotPasswordBackdrop = createDialogBackdropCloseHandler(() => {
|
||||
lk.showForgotPasswordModal = false;
|
||||
});
|
||||
const announcementBackdrop = createDialogBackdropCloseHandler(() => {
|
||||
lk.showAnnouncementModal = false;
|
||||
});
|
||||
const wecomLoginBackdrop = createDialogBackdropCloseHandler(() => {
|
||||
lk.wecomLoginDialog = null;
|
||||
});
|
||||
const wecomBindBackdrop = createDialogBackdropCloseHandler(() => {
|
||||
lk.wecomBindPrompt = null;
|
||||
});
|
||||
const registerBackdrop = createDialogBackdropCloseHandler(() => {
|
||||
lk.showRegisterModal = false;
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<div
|
||||
v-if="lk.showForgotPasswordModal"
|
||||
class="dialog-overlay z-50 items-center animate-fade-in"
|
||||
@click.self="lk.showForgotPasswordModal = false"
|
||||
@pointerdown.self="forgotPasswordBackdrop.onBackdropPointerDown"
|
||||
@pointerup.self="forgotPasswordBackdrop.onBackdropPointerUp"
|
||||
>
|
||||
<div class="dialog-panel max-w-md animate-slide-up">
|
||||
<div class="dialog-header dialog-header--compact relative border-0 pb-0">
|
||||
@@ -33,9 +54,15 @@ const lk = inject(LOGIN_KEY)! as any;
|
||||
</div>
|
||||
<h3 class="text-xl font-bold text-primary">{{ $t('login.forgotPassword') }}</h3>
|
||||
<p class="mt-2 text-sm text-muted">{{ $t('login.forgotPasswordDesc') }}</p>
|
||||
<div class="mt-6 space-y-2 text-left">
|
||||
<label class="label-field ml-1">{{ $t('register.email') }}</label>
|
||||
<input v-model="lk.forgotPasswordForm.email" :placeholder="$t('register.email')" class="input-field" @keyup.enter="lk.handleForgotPassword" />
|
||||
<div class="mt-6 space-y-4 text-left">
|
||||
<div class="space-y-2">
|
||||
<label class="label-field ml-1">{{ $t('register.fields.employeeId') }}</label>
|
||||
<input v-model="lk.forgotPasswordForm.employee_id" :placeholder="$t('register.fields.employeeId')" class="input-field" @keyup.enter="lk.handleForgotPassword" />
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<label class="label-field ml-1">{{ $t('login.username') }}</label>
|
||||
<input v-model="lk.forgotPasswordForm.username" :placeholder="$t('login.placeholder.username')" class="input-field" @keyup.enter="lk.handleForgotPassword" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="dialog-footer">
|
||||
@@ -48,7 +75,8 @@ const lk = inject(LOGIN_KEY)! as any;
|
||||
<div
|
||||
v-if="lk.showAnnouncementModal && lk.selectedAnnouncement"
|
||||
class="dialog-overlay z-50 items-center animate-fade-in"
|
||||
@click.self="lk.showAnnouncementModal = false"
|
||||
@pointerdown.self="announcementBackdrop.onBackdropPointerDown"
|
||||
@pointerup.self="announcementBackdrop.onBackdropPointerUp"
|
||||
>
|
||||
<div class="dialog-panel max-w-2xl max-h-[85vh] animate-slide-up">
|
||||
<div class="dialog-header">
|
||||
@@ -74,7 +102,8 @@ const lk = inject(LOGIN_KEY)! as any;
|
||||
<div
|
||||
v-if="lk.wecomLoginDialog"
|
||||
class="dialog-overlay z-50 items-center animate-fade-in"
|
||||
@click.self="lk.wecomLoginDialog = null"
|
||||
@pointerdown.self="wecomLoginBackdrop.onBackdropPointerDown"
|
||||
@pointerup.self="wecomLoginBackdrop.onBackdropPointerUp"
|
||||
>
|
||||
<div class="dialog-panel max-w-lg animate-slide-up">
|
||||
<div class="dialog-header dialog-header--compact">
|
||||
@@ -116,7 +145,8 @@ const lk = inject(LOGIN_KEY)! as any;
|
||||
<div
|
||||
v-if="lk.wecomBindPrompt"
|
||||
class="dialog-overlay z-50 items-center animate-fade-in"
|
||||
@click.self="lk.wecomBindPrompt = null"
|
||||
@pointerdown.self="wecomBindBackdrop.onBackdropPointerDown"
|
||||
@pointerup.self="wecomBindBackdrop.onBackdropPointerUp"
|
||||
>
|
||||
<div class="dialog-panel max-w-lg animate-slide-up">
|
||||
<div class="dialog-header dialog-header--compact">
|
||||
@@ -168,7 +198,8 @@ const lk = inject(LOGIN_KEY)! as any;
|
||||
<div
|
||||
v-if="lk.showRegisterModal"
|
||||
class="dialog-overlay z-50 items-center animate-fade-in"
|
||||
@click.self="lk.showRegisterModal = false"
|
||||
@pointerdown.self="registerBackdrop.onBackdropPointerDown"
|
||||
@pointerup.self="registerBackdrop.onBackdropPointerUp"
|
||||
>
|
||||
<div class="dialog-panel max-w-md animate-slide-up">
|
||||
<div class="dialog-header">
|
||||
|
||||
@@ -90,7 +90,7 @@ const lk = inject(LOGIN_KEY)! as any;
|
||||
</button>
|
||||
|
||||
<div class="flex justify-between items-center pt-4 mt-2 border-t border-subtle/50">
|
||||
<button type="button" @click="lk.showForgotPasswordModal = true" class="text-sm text-secondary hover:text-brand-700 transition-colors">
|
||||
<button type="button" @click="lk.openForgotPasswordModal" class="text-sm text-secondary hover:text-brand-700 transition-colors">
|
||||
{{ $t('login.forgotPassword') }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
@@ -45,7 +45,7 @@ export function useLoginAuth(deps: LoginDeps, options: LoginAuthOptions) {
|
||||
employee_id: '',
|
||||
phone_number: '',
|
||||
});
|
||||
const forgotPasswordForm = ref({ email: '' });
|
||||
const forgotPasswordForm = ref({ employee_id: '', username: '' });
|
||||
const isWecomLoggingIn = ref(false);
|
||||
const wecomLoginDialog = ref<WecomLoginDialogContent | null>(null);
|
||||
const wecomBindPrompt = ref<WecomBindPromptState | null>(null);
|
||||
@@ -59,22 +59,43 @@ export function useLoginAuth(deps: LoginDeps, options: LoginAuthOptions) {
|
||||
saveAuthUser({ ...(user || {}) }, rememberMe.value);
|
||||
};
|
||||
|
||||
const openForgotPasswordModal = () => {
|
||||
forgotPasswordForm.value = {
|
||||
employee_id: '',
|
||||
username: loginUsername.value.trim(),
|
||||
};
|
||||
showForgotPasswordModal.value = true;
|
||||
};
|
||||
|
||||
const handleForgotPassword = async () => {
|
||||
if (!forgotPasswordForm.value.email) {
|
||||
msg.warning(t('register.emailRequired'));
|
||||
const employeeId = forgotPasswordForm.value.employee_id.trim();
|
||||
const username = forgotPasswordForm.value.username.trim();
|
||||
if (!employeeId) {
|
||||
msg.warning(t('login.employeeIdRequired'));
|
||||
return;
|
||||
}
|
||||
if (!username) {
|
||||
msg.warning(t('login.usernameRequired'));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await requestJson('/api/user/reset-password-by-email', {
|
||||
await requestJson('/api/user/reset-password-by-wecom', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(forgotPasswordForm.value),
|
||||
body: JSON.stringify({ employee_id: employeeId, username }),
|
||||
});
|
||||
msg.success(t('login.passwordResetEmailSent'));
|
||||
msg.success(t('login.passwordResetWecomSent'));
|
||||
showForgotPasswordModal.value = false;
|
||||
forgotPasswordForm.value = { email: '' };
|
||||
forgotPasswordForm.value = { employee_id: '', username: '' };
|
||||
} catch (e) {
|
||||
console.error('Password reset error', e);
|
||||
if (e instanceof BusinessError && e.code === 'AUTH.PASSWORD_RESET_RATE_LIMITED') {
|
||||
const minutes =
|
||||
Number(e.details?.retry_after_minutes)
|
||||
|| Math.max(1, Math.ceil(Number(e.details?.retry_after_seconds || 300) / 60));
|
||||
msg.error(t('login.passwordResetRateLimited', { minutes }));
|
||||
return;
|
||||
}
|
||||
msg.error((e as Error)?.message || t('login.passwordResetRequestFailed'));
|
||||
}
|
||||
};
|
||||
@@ -375,6 +396,7 @@ export function useLoginAuth(deps: LoginDeps, options: LoginAuthOptions) {
|
||||
rememberMe,
|
||||
showRegisterModal,
|
||||
showForgotPasswordModal,
|
||||
openForgotPasswordModal,
|
||||
registerForm,
|
||||
forgotPasswordForm,
|
||||
isWecomLoggingIn,
|
||||
|
||||
@@ -8,6 +8,7 @@ export {
|
||||
getUserProfile,
|
||||
getLeaveBalance,
|
||||
submitLeaveWorkflow,
|
||||
resetPasswordByWecom,
|
||||
resetPasswordConfirm,
|
||||
getUserAgent,
|
||||
listUserAgents,
|
||||
|
||||
@@ -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