兼容二进制打包

This commit is contained in:
COOL 2025-01-14 19:46:01 +08:00
parent eadf113c6d
commit fd8aaad60a
5 changed files with 27 additions and 49 deletions

View File

@ -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",

3
pnpm-lock.yaml generated
View File

@ -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

View File

@ -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
*/

View File

@ -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)
);
}

View File

@ -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;
}
}
/**
* 退
*/