44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
import { Service } from 'egg';
|
|
import * as pubUtils from '@pubUtils/util';
|
|
const csprng = require('csprng');
|
|
|
|
/**
|
|
* Utils Service
|
|
*/
|
|
export default class Utils extends Service {
|
|
/**
|
|
* 生成 len 长度的随机字符串
|
|
* @param len 长度
|
|
* @param radix 基数
|
|
*/
|
|
public generateStr(len: number, radix = 36) {
|
|
return `${csprng(len, radix)}`;
|
|
}
|
|
|
|
public genCode(len: number) {
|
|
return pubUtils.genCode(len)
|
|
}
|
|
|
|
public resResult(status: {code: number, simStr: string}, data?, customMsg?: string) {
|
|
return pubUtils.resResult(status, data, customMsg);
|
|
}
|
|
|
|
public checkActivityData(data: any, type: number) {
|
|
return { data, type }
|
|
}
|
|
|
|
public log(level: 'DEBUG'|'INFO'|'WARN'| 'ERROR', message: string) {
|
|
const log = this.app.loggers.get('linkLogger');
|
|
|
|
if(level == 'DEBUG') {
|
|
log.error(`${message}`);
|
|
} else if (level == 'INFO') {
|
|
log.info(`${message}`);
|
|
} else if (level == 'WARN') {
|
|
log.warn(`${message}`);
|
|
} else if (level == "ERROR") {
|
|
log.error(`${message}`);
|
|
}
|
|
}
|
|
}
|