mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2025-12-11 16:52:49 +00:00
兼容二进制打包
This commit is contained in:
parent
eadf113c6d
commit
fd8aaad60a
@ -18,6 +18,7 @@
|
|||||||
"@midwayjs/upload": "^3.19.3",
|
"@midwayjs/upload": "^3.19.3",
|
||||||
"@midwayjs/validate": "^3.19.2",
|
"@midwayjs/validate": "^3.19.2",
|
||||||
"@midwayjs/view-ejs": "^3.19.2",
|
"@midwayjs/view-ejs": "^3.19.2",
|
||||||
|
"axios": "^1.7.9",
|
||||||
"jsonwebtoken": "^9.0.2",
|
"jsonwebtoken": "^9.0.2",
|
||||||
"lodash": "^4.17.21",
|
"lodash": "^4.17.21",
|
||||||
"md5": "^2.3.0",
|
"md5": "^2.3.0",
|
||||||
@ -39,9 +40,9 @@
|
|||||||
"mwts": "^1.3.0",
|
"mwts": "^1.3.0",
|
||||||
"mwtsc": "^1.15.1",
|
"mwtsc": "^1.15.1",
|
||||||
"pkg": "^5.8.1",
|
"pkg": "^5.8.1",
|
||||||
|
"rimraf": "^5.0.5",
|
||||||
"ts-jest": "^29.2.5",
|
"ts-jest": "^29.2.5",
|
||||||
"typescript": "~5.7.3",
|
"typescript": "~5.7.3"
|
||||||
"rimraf": "^5.0.5"
|
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=18.0.0"
|
"node": ">=18.0.0"
|
||||||
@ -49,7 +50,7 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "NODE_ENV=production node ./bootstrap.js",
|
"start": "NODE_ENV=production node ./bootstrap.js",
|
||||||
"entity": "cool entity",
|
"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",
|
"test": "cross-env NODE_ENV=unittest jest",
|
||||||
"cov": "jest --coverage",
|
"cov": "jest --coverage",
|
||||||
"lint": "mwts check",
|
"lint": "mwts check",
|
||||||
|
|||||||
3
pnpm-lock.yaml
generated
3
pnpm-lock.yaml
generated
@ -50,6 +50,9 @@ importers:
|
|||||||
'@midwayjs/view-ejs':
|
'@midwayjs/view-ejs':
|
||||||
specifier: ^3.19.2
|
specifier: ^3.19.2
|
||||||
version: 3.19.2
|
version: 3.19.2
|
||||||
|
axios:
|
||||||
|
specifier: ^1.7.9
|
||||||
|
version: 1.7.9
|
||||||
jsonwebtoken:
|
jsonwebtoken:
|
||||||
specifier: ^9.0.2
|
specifier: ^9.0.2
|
||||||
version: 9.0.2
|
version: 9.0.2
|
||||||
|
|||||||
@ -1,15 +1,20 @@
|
|||||||
import { Inject, Provide } from '@midwayjs/core';
|
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
||||||
import { Context } from '@midwayjs/koa';
|
import { Context } from '@midwayjs/koa';
|
||||||
import * as moment from 'moment';
|
import * as moment from 'moment';
|
||||||
|
import { LocationUtil } from '@cool-midway/core';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 帮助类
|
* 帮助类
|
||||||
*/
|
*/
|
||||||
@Provide()
|
@Provide()
|
||||||
|
@Scope(ScopeEnum.Singleton)
|
||||||
export class Utils {
|
export class Utils {
|
||||||
@Inject()
|
@Inject()
|
||||||
baseDir;
|
baseDir;
|
||||||
|
|
||||||
|
@Inject()
|
||||||
|
locationUtil: LocationUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得请求IP
|
* 获得请求IP
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -69,13 +69,12 @@ export class BaseOpenController extends BaseController {
|
|||||||
@CoolTag(TagTypes.IGNORE_TOKEN)
|
@CoolTag(TagTypes.IGNORE_TOKEN)
|
||||||
@Get('/captcha', { summary: '验证码' })
|
@Get('/captcha', { summary: '验证码' })
|
||||||
async captcha(
|
async captcha(
|
||||||
@Query('type') type: string,
|
|
||||||
@Query('width') width: number,
|
@Query('width') width: number,
|
||||||
@Query('height') height: number,
|
@Query('height') height: number,
|
||||||
@Query('color') color: string
|
@Query('color') color: string
|
||||||
) {
|
) {
|
||||||
return this.ok(
|
return this.ok(
|
||||||
await this.baseSysLoginService.captcha(type, width, height, color)
|
await this.baseSysLoginService.captcha(width, height, color)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
import { Inject, Provide, Config, InjectClient } from '@midwayjs/core';
|
import { Inject, Provide, Config, InjectClient } from '@midwayjs/core';
|
||||||
import { BaseService, CoolCommException } from '@cool-midway/core';
|
import { BaseService, CoolCommException } from '@cool-midway/core';
|
||||||
import { LoginDTO } from '../../dto/login';
|
import { LoginDTO } from '../../dto/login';
|
||||||
import * as svgCaptcha from 'svg-captcha';
|
|
||||||
import { v1 as uuid } from 'uuid';
|
import { v1 as uuid } from 'uuid';
|
||||||
import { BaseSysUserEntity } from '../../entity/sys/user';
|
import { BaseSysUserEntity } from '../../entity/sys/user';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
@ -14,7 +13,8 @@ import { BaseSysDepartmentService } from './department';
|
|||||||
import * as jwt from 'jsonwebtoken';
|
import * as jwt from 'jsonwebtoken';
|
||||||
import { Context } from '@midwayjs/koa';
|
import { Context } from '@midwayjs/koa';
|
||||||
import { CachingFactory, MidwayCache } from '@midwayjs/cache-manager';
|
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()
|
@Inject()
|
||||||
ctx: Context;
|
ctx: Context;
|
||||||
|
|
||||||
|
@Inject()
|
||||||
|
utils: Utils;
|
||||||
|
|
||||||
@Config('module.base')
|
@Config('module.base')
|
||||||
coolConfig;
|
coolConfig;
|
||||||
|
|
||||||
@ -103,15 +106,15 @@ export class BaseSysLoginService extends BaseService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证码
|
* 验证码
|
||||||
* @param type 图片验证码类型 svg
|
|
||||||
* @param width 宽
|
* @param width 宽
|
||||||
* @param height 高
|
* @param height 高
|
||||||
*/
|
*/
|
||||||
async captcha(type: string, width = 150, height = 50, color = '#fff') {
|
async captcha(width = 150, height = 50, color = '#fff') {
|
||||||
const svg = svgCaptcha.create({
|
const svg = svgCaptcha.create({
|
||||||
ignoreChars: 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM',
|
// ignoreChars: 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM',
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
|
noise: 3,
|
||||||
});
|
});
|
||||||
const result = {
|
const result = {
|
||||||
captchaId: uuid(),
|
captchaId: uuid(),
|
||||||
@ -132,12 +135,11 @@ export class BaseSysLoginService extends BaseService {
|
|||||||
rpList.forEach(rp => {
|
rpList.forEach(rp => {
|
||||||
result.data = result.data['replaceAll'](rp, color);
|
result.data = result.data['replaceAll'](rp, color);
|
||||||
});
|
});
|
||||||
if (type === 'png' || type === 'base64') {
|
|
||||||
result.data = await this.svgToBase64Png(result.data, {
|
// Convert SVG to base64
|
||||||
width,
|
const base64Data = Buffer.from(result.data).toString('base64');
|
||||||
height,
|
result.data = `data:image/svg+xml;base64,${base64Data}`;
|
||||||
});
|
|
||||||
}
|
|
||||||
// 半小时过期
|
// 半小时过期
|
||||||
await this.midwayCache.set(
|
await this.midwayCache.set(
|
||||||
`verify:img:${result.captchaId}`,
|
`verify:img:${result.captchaId}`,
|
||||||
@ -147,38 +149,6 @@ export class BaseSysLoginService extends BaseService {
|
|||||||
return result;
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 退出登录
|
* 退出登录
|
||||||
*/
|
*/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user