29 lines
922 B
TypeScript
29 lines
922 B
TypeScript
import { STATUS } from '../app/consts/statusCode';
|
|
|
|
export function genCode(len) {
|
|
const chars = '123456789ABCDEFGHJKLMNPQRSTWXYZabcdefghijklmnopqrstuvwxyz';
|
|
const charArr = chars.split('');
|
|
let code = '';
|
|
for (let i = 0; i < len; i++) {
|
|
code += charArr[Math.floor(Math.random() * charArr.length)];
|
|
}
|
|
return code;
|
|
}
|
|
|
|
/**
|
|
* 从一个数组中随机返回1元素
|
|
* @param source
|
|
*/
|
|
export function getRandSingleEelm<T>(source: Array<T>): T {
|
|
let len = source.length;
|
|
return source[Math.floor(Math.random() * len)]
|
|
}
|
|
|
|
export function resResult<T>(status: { code: number, simStr: string }, data: T = <T>{}, customMsg = ''): { code: number, msg: string, data: T } {
|
|
const { code, simStr } = status;
|
|
if (code !== STATUS.SUCCESS.code) {
|
|
console.log(`normal err, code: ${code}, des: ${customMsg || simStr}`);
|
|
}
|
|
return { code, msg: customMsg || simStr, data };
|
|
}
|