From 903fe9aa9d699137ac198ba8f547c49591e62919 Mon Sep 17 00:00:00 2001 From: nicheng_he Date: Thu, 24 Jul 2025 01:05:45 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=89=BE=E5=9B=9E=E5=AF=86=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/locales/langs/en-US/authentication.ts | 1 + .../src/locales/langs/zh-CN/authentication.ts | 1 + .../certd-client/src/router/source/outside.ts | 8 + .../certd-client/src/store/user/api.user.ts | 18 ++ .../ui/certd-client/src/store/user/index.ts | 9 +- .../views/framework/forgot-password/index.vue | 163 ++++++++++++++++++ .../src/views/framework/login/image-code.vue | 7 +- .../src/views/framework/login/index.vue | 6 +- .../user/login/forgot-password-controller.ts | 45 +++++ .../sys/authority/service/user-service.ts | 25 ++- 10 files changed, 277 insertions(+), 6 deletions(-) create mode 100644 packages/ui/certd-client/src/views/framework/forgot-password/index.vue create mode 100644 packages/ui/certd-server/src/controller/user/login/forgot-password-controller.ts diff --git a/packages/ui/certd-client/src/locales/langs/en-US/authentication.ts b/packages/ui/certd-client/src/locales/langs/en-US/authentication.ts index 33b150b3..3109985a 100644 --- a/packages/ui/certd-client/src/locales/langs/en-US/authentication.ts +++ b/packages/ui/certd-client/src/locales/langs/en-US/authentication.ts @@ -57,6 +57,7 @@ export default { passwordPlaceholder: "Please enter your password", mobilePlaceholder: "Please enter your mobile number", loginButton: "Log In", + forgotPassword: "Forgot password?", forgotAdminPassword: "Forgot admin password?", registerLink: "Register", diff --git a/packages/ui/certd-client/src/locales/langs/zh-CN/authentication.ts b/packages/ui/certd-client/src/locales/langs/zh-CN/authentication.ts index c71bb400..6df63d0b 100644 --- a/packages/ui/certd-client/src/locales/langs/zh-CN/authentication.ts +++ b/packages/ui/certd-client/src/locales/langs/zh-CN/authentication.ts @@ -57,6 +57,7 @@ export default { passwordPlaceholder: "请输入密码", mobilePlaceholder: "请输入手机号", loginButton: "登录", + forgotPassword: "忘记密码?", forgotAdminPassword: "忘记管理员密码?", registerLink: "注册", diff --git a/packages/ui/certd-client/src/router/source/outside.ts b/packages/ui/certd-client/src/router/source/outside.ts index b86ddbdb..5c9bdfa4 100644 --- a/packages/ui/certd-client/src/router/source/outside.ts +++ b/packages/ui/certd-client/src/router/source/outside.ts @@ -24,6 +24,14 @@ export const outsideResource = [ path: "/register", component: "/framework/register/index.vue", }, + { + meta: { + title: "找回密码", + }, + name: "forgotPassword", + path: "/forgotPassword", + component: "/framework/forgot-password/index.vue", + }, ], }, ...errorPage, diff --git a/packages/ui/certd-client/src/store/user/api.user.ts b/packages/ui/certd-client/src/store/user/api.user.ts index bfd61d41..c37202a5 100644 --- a/packages/ui/certd-client/src/store/user/api.user.ts +++ b/packages/ui/certd-client/src/store/user/api.user.ts @@ -20,6 +20,17 @@ export interface SmsLoginReq { randomStr: string; } +export interface ForgotPasswordReq { + forgotPasswordType: string; + input: string; + randomStr: string; + imgCode: string; + validateCode: string; + + password: string; + confirmPassword: string; +} + export interface UserInfoRes { id: string | number; username: string; @@ -43,6 +54,13 @@ export async function register(user: RegisterReq): Promise { data: user, }); } +export async function forgotPassword(data: ForgotPasswordReq): Promise { + return await request({ + url: "/forgotPassword", + method: "post", + data: data, + }); +} export async function logout() { return await request({ url: "/logout", diff --git a/packages/ui/certd-client/src/store/user/index.ts b/packages/ui/certd-client/src/store/user/index.ts index 9010cebc..cca8e848 100644 --- a/packages/ui/certd-client/src/store/user/index.ts +++ b/packages/ui/certd-client/src/store/user/index.ts @@ -4,7 +4,7 @@ import router from "../../router"; import { LocalStorage } from "/src/utils/util.storage"; // @ts-ignore import * as UserApi from "./api.user"; -import { RegisterReq, SmsLoginReq } from "./api.user"; +import { ForgotPasswordReq, RegisterReq, SmsLoginReq } from "./api.user"; // @ts-ignore import { LoginReq, UserInfoRes } from "/@/store/user/api.user"; import { message, Modal, notification } from "ant-design-vue"; @@ -67,6 +67,13 @@ export const useUserStore = defineStore({ }); await router.replace("/login"); }, + async forgotPassword(params: ForgotPasswordReq): Promise { + await UserApi.forgotPassword(params); + notification.success({ + message: "密码已重置,请登录", + }); + await router.replace("/login"); + }, /** * @description: login */ diff --git a/packages/ui/certd-client/src/views/framework/forgot-password/index.vue b/packages/ui/certd-client/src/views/framework/forgot-password/index.vue new file mode 100644 index 00000000..9e94db2a --- /dev/null +++ b/packages/ui/certd-client/src/views/framework/forgot-password/index.vue @@ -0,0 +1,163 @@ + + + + diff --git a/packages/ui/certd-client/src/views/framework/login/image-code.vue b/packages/ui/certd-client/src/views/framework/login/image-code.vue index 9ac1dae8..8b03ce8a 100644 --- a/packages/ui/certd-client/src/views/framework/login/image-code.vue +++ b/packages/ui/certd-client/src/views/framework/login/image-code.vue @@ -11,7 +11,7 @@ diff --git a/packages/ui/certd-client/src/views/framework/login/index.vue b/packages/ui/certd-client/src/views/framework/login/index.vue index bad1d72f..cfc3ca01 100644 --- a/packages/ui/certd-client/src/views/framework/login/index.vue +++ b/packages/ui/certd-client/src/views/framework/login/index.vue @@ -48,9 +48,9 @@
- - {{ t("authentication.forgotAdminPassword") }} - + + {{ t("authentication.forgotPassword") }} +
diff --git a/packages/ui/certd-server/src/controller/user/login/forgot-password-controller.ts b/packages/ui/certd-server/src/controller/user/login/forgot-password-controller.ts new file mode 100644 index 00000000..62ff9a46 --- /dev/null +++ b/packages/ui/certd-server/src/controller/user/login/forgot-password-controller.ts @@ -0,0 +1,45 @@ +import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core'; +import { BaseController, CommonException, Constants, SysSettingsService } from "@certd/lib-server"; +import { CodeService } from '../../../modules/basic/service/code-service.js'; +import { UserService } from '../../../modules/sys/authority/service/user-service.js'; + +/** + */ +@Provide() +@Controller('/api') +export class LoginController extends BaseController { + @Inject() + userService: UserService; + @Inject() + codeService: CodeService; + + @Inject() + sysSettingsService: SysSettingsService; + + @Post('/forgotPassword', { summary: Constants.per.guest }) + public async forgotPassword( + @Body(ALL) + body: any, + ) { + if(body.type === 'email') { + this.codeService.checkEmailCode({ + email: body.input, + randomStr: body.randomStr, + validateCode: body.validateCode, + throwError: true, + }); + } else if(body.type === 'mobile') { + await this.codeService.checkSmsCode({ + mobile: body.input, + randomStr: body.randomStr, + phoneCode: body.phoneCode, + smsCode: body.validateCode, + throwError: true, + }); + } else { + throw new CommonException('暂不支持的找回类型,请联系管理员找回'); + } + await this.userService.forgotPassword(body); + return this.ok(); + } +} diff --git a/packages/ui/certd-server/src/modules/sys/authority/service/user-service.ts b/packages/ui/certd-server/src/modules/sys/authority/service/user-service.ts index fec66ac4..94359243 100644 --- a/packages/ui/certd-server/src/modules/sys/authority/service/user-service.ts +++ b/packages/ui/certd-server/src/modules/sys/authority/service/user-service.ts @@ -15,6 +15,7 @@ import { DbAdapter } from '../../../db/index.js'; import { simpleNanoId, utils } from '@certd/basic'; export type RegisterType = 'username' | 'mobile' | 'email'; +export type ForgotPasswordType = 'mobile' | 'email'; export const AdminRoleId = 1 /** @@ -23,7 +24,7 @@ export const AdminRoleId = 1 @Provide() @Scope(ScopeEnum.Request, { allowDowngrade: true }) export class UserService extends BaseService { - + @InjectEntityModel(UserEntity) repository: Repository; @Inject() @@ -229,6 +230,28 @@ export class UserService extends BaseService { return newUser; } + async forgotPassword( + data: { + type: ForgotPasswordType; input?: string, phoneCode?: string, + randomStr: string, imgCode:string, validateCode: string, + password: string, confirmPassword: string, + } + ) { + if(!data.type) { + throw new CommonException('找回类型不能为空'); + } + if(data.password !== data.confirmPassword) { + throw new CommonException('两次输入的密码不一致'); + } + const user = await this.findOne([{ [data.type]: data.input }]); + console.log('user', user) + if(!user) { + throw new CommonException('用户不存在'); + // return; + } + await this.resetPassword(user.id, data.password) + } + async changePassword(userId: any, form: any) { const user = await this.info(userId); const passwordChecked = await this.checkPassword(form.password, user.password, user.passwordVersion);