From fd8aaad60afb35083383ec373817bf26dfbafbc6 Mon Sep 17 00:00:00 2001 From: COOL Date: Tue, 14 Jan 2025 19:46:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E4=BA=8C=E8=BF=9B=E5=88=B6?= =?UTF-8?q?=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 7 +-- pnpm-lock.yaml | 3 ++ src/comm/utils.ts | 7 ++- src/modules/base/controller/admin/open.ts | 3 +- src/modules/base/service/sys/login.ts | 56 ++++++----------------- 5 files changed, 27 insertions(+), 49 deletions(-) diff --git a/package.json b/package.json index 89450b1..fb2887b 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@midwayjs/upload": "^3.19.3", "@midwayjs/validate": "^3.19.2", "@midwayjs/view-ejs": "^3.19.2", + "axios": "^1.7.9", "jsonwebtoken": "^9.0.2", "lodash": "^4.17.21", "md5": "^2.3.0", @@ -39,9 +40,9 @@ "mwts": "^1.3.0", "mwtsc": "^1.15.1", "pkg": "^5.8.1", + "rimraf": "^5.0.5", "ts-jest": "^29.2.5", - "typescript": "~5.7.3", - "rimraf": "^5.0.5" + "typescript": "~5.7.3" }, "engines": { "node": ">=18.0.0" @@ -49,7 +50,7 @@ "scripts": { "start": "NODE_ENV=production node ./bootstrap.js", "entity": "cool entity", - "dev": "cool check entity && cross-env NODE_ENV=local mwtsc --watch --run @midwayjs/mock/app.js", + "dev": "cool check entity && cross-env NODE_ENV=local mwtsc --cleanOutDir --watch --run @midwayjs/mock/app.js", "test": "cross-env NODE_ENV=unittest jest", "cov": "jest --coverage", "lint": "mwts check", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a6d3c9e..7b36600 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -50,6 +50,9 @@ importers: '@midwayjs/view-ejs': specifier: ^3.19.2 version: 3.19.2 + axios: + specifier: ^1.7.9 + version: 1.7.9 jsonwebtoken: specifier: ^9.0.2 version: 9.0.2 diff --git a/src/comm/utils.ts b/src/comm/utils.ts index a57d1e8..f213e95 100644 --- a/src/comm/utils.ts +++ b/src/comm/utils.ts @@ -1,15 +1,20 @@ -import { Inject, Provide } from '@midwayjs/core'; +import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core'; import { Context } from '@midwayjs/koa'; import * as moment from 'moment'; +import { LocationUtil } from '@cool-midway/core'; /** * 帮助类 */ @Provide() +@Scope(ScopeEnum.Singleton) export class Utils { @Inject() baseDir; + @Inject() + locationUtil: LocationUtil; + /** * 获得请求IP */ diff --git a/src/modules/base/controller/admin/open.ts b/src/modules/base/controller/admin/open.ts index 217433b..c7242ed 100644 --- a/src/modules/base/controller/admin/open.ts +++ b/src/modules/base/controller/admin/open.ts @@ -69,13 +69,12 @@ export class BaseOpenController extends BaseController { @CoolTag(TagTypes.IGNORE_TOKEN) @Get('/captcha', { summary: '验证码' }) async captcha( - @Query('type') type: string, @Query('width') width: number, @Query('height') height: number, @Query('color') color: string ) { return this.ok( - await this.baseSysLoginService.captcha(type, width, height, color) + await this.baseSysLoginService.captcha(width, height, color) ); } diff --git a/src/modules/base/service/sys/login.ts b/src/modules/base/service/sys/login.ts index bc3dfae..23a9a55 100644 --- a/src/modules/base/service/sys/login.ts +++ b/src/modules/base/service/sys/login.ts @@ -1,7 +1,6 @@ import { Inject, Provide, Config, InjectClient } from '@midwayjs/core'; import { BaseService, CoolCommException } from '@cool-midway/core'; import { LoginDTO } from '../../dto/login'; -import * as svgCaptcha from 'svg-captcha'; import { v1 as uuid } from 'uuid'; import { BaseSysUserEntity } from '../../entity/sys/user'; import { Repository } from 'typeorm'; @@ -14,7 +13,8 @@ import { BaseSysDepartmentService } from './department'; import * as jwt from 'jsonwebtoken'; import { Context } from '@midwayjs/koa'; import { CachingFactory, MidwayCache } from '@midwayjs/cache-manager'; -// import * as sharp from 'sharp'; +import { Utils } from '../../../../comm/utils'; +import * as svgCaptcha from 'svg-captcha'; /** * 登录 @@ -39,6 +39,9 @@ export class BaseSysLoginService extends BaseService { @Inject() ctx: Context; + @Inject() + utils: Utils; + @Config('module.base') coolConfig; @@ -103,15 +106,15 @@ export class BaseSysLoginService extends BaseService { /** * 验证码 - * @param type 图片验证码类型 svg * @param width 宽 * @param height 高 */ - async captcha(type: string, width = 150, height = 50, color = '#fff') { + async captcha(width = 150, height = 50, color = '#fff') { const svg = svgCaptcha.create({ - ignoreChars: 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM', + // ignoreChars: 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM', width, height, + noise: 3, }); const result = { captchaId: uuid(), @@ -132,12 +135,11 @@ export class BaseSysLoginService extends BaseService { rpList.forEach(rp => { result.data = result.data['replaceAll'](rp, color); }); - if (type === 'png' || type === 'base64') { - result.data = await this.svgToBase64Png(result.data, { - width, - height, - }); - } + + // Convert SVG to base64 + const base64Data = Buffer.from(result.data).toString('base64'); + result.data = `data:image/svg+xml;base64,${base64Data}`; + // 半小时过期 await this.midwayCache.set( `verify:img:${result.captchaId}`, @@ -147,38 +149,6 @@ export class BaseSysLoginService extends BaseService { return result; } - /** - * svg转base64 - * @param svgBuffer - * @param options - */ - async svgToBase64Png(svgBuffer: string, options = {} as any) { - try { - // const svgBufferData = Buffer.from(svgBuffer); - - // // 处理图片 - // const pngBuffer = await sharp(svgBufferData) - // .png({ - // quality: options.quality || 100, - // compressionLevel: options.compression || 6, - // }) - // .resize(options.width, options.height, { - // fit: 'contain', - // background: { r: 255, g: 255, b: 255, alpha: 1 }, - // }) - // .toBuffer(); - - // // 转换为base64 - // const base64String = `data:image/png;base64,${pngBuffer.toString( - // 'base64' - // )}`; - return ''; - } catch (error) { - console.error('转换失败:', error); - throw error; - } - } - /** * 退出登录 */