feat: 登录验证码接口联调

This commit is contained in:
duanshuwen
2025-12-17 22:58:04 +08:00
parent d6578463e3
commit 9bfcc49411
68 changed files with 360 additions and 8090 deletions

View File

@@ -0,0 +1,31 @@
/**
* @description 生成唯一 uuid
* @return string
*/
export function generateUUID() {
if (typeof crypto === 'object') {
if (typeof crypto.randomUUID === 'function') {
return crypto.randomUUID();
}
if (typeof crypto.getRandomValues === 'function' && typeof Uint8Array === 'function') {
const callback = (c: any) => {
const num = Number(c);
return (num ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (num / 4)))).toString(16);
};
return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, callback);
}
}
let timestamp = new Date().getTime();
let performanceNow = (typeof performance !== 'undefined' && performance.now && performance.now() * 1000) || 0;
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
let random = Math.random() * 16;
if (timestamp > 0) {
random = (timestamp + random) % 16 | 0;
timestamp = Math.floor(timestamp / 16);
} else {
random = (performanceNow + random) % 16 | 0;
performanceNow = Math.floor(performanceNow / 16);
}
return (c === 'x' ? random : (random & 0x3) | 0x8).toString(16);
});
}

View File

@@ -9,4 +9,209 @@ export function isPathMatch(pattern: string, path: string): boolean {
const regex = new RegExp(`^${regexPattern}$`)
return regex.test(path)
}
}
export const getRegExp = function (validatorName: string) {
const commonRegExp: any = {
number: '^[-]?\\d+(\\.\\d+)?$',
letter: '^[A-Za-z]+$',
letterAndNumber: '^[A-Za-z0-9]+$',
mobilePhone: '^[1][3-9][0-9]{9}$',
letterStartNumberIncluded: '^[A-Za-z]+[A-Za-z\\d]*$',
noChinese: '^[^\u4e00-\u9fa5]+$',
chinese: '^[\u4e00-\u9fa5]+$',
email: '^([-_A-Za-z0-9.]+)@([_A-Za-z0-9]+\\.)+[A-Za-z0-9]{2,3}$',
url: '(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]'
};
return commonRegExp[validatorName];
};
/**
* 判断是否为空
* @param val 数据
*/
export const validateNull = (val: any) => {
if (typeof val === 'boolean') {
return false;
}
if (typeof val === 'number') {
return false;
}
if (val instanceof Array) {
if (val.length === 0) return true;
} else if (val instanceof Object) {
if (JSON.stringify(val) === '{}') return true;
} else {
if (val === 'null' || val == null || val === 'undefined' || val === undefined || val === '') return true;
return false;
}
return false;
};
const validateFn = (validatorName: string, rule: any, value: any, callback: any, defaultErrorMsg: string) => {
if (validateNull(value) || value.length <= 0) {
callback();
return;
}
const reg = new RegExp(getRegExp(validatorName));
if (!reg.test(value)) {
const errTxt = rule.errorMsg || defaultErrorMsg;
callback(new Error(errTxt));
} else {
callback();
}
};
export const rule = {
overLength(_rule: any, value: any, callback: any) {
if (value?.length > 255) {
callback(new Error('输入内容过长,请重新输入'));
} else {
callback();
}
},
/**
* 校验 请输入中文、英文、数字包括下划线
* 名称校验
*/
validatorNameCn(_rule: any, value: any, callback: any) {
const acount = /^[\u4E00-\u9FA5A-Za-z0-9_]+$/;
if (value && !acount.test(value)) {
callback(new Error('请输入中文、英文、数字包括下划线'));
} else {
callback();
}
},
/**
* 校验 请输入大写英文、下划线
* 名称校验
*/
validatorCapital(_rule: any, value: any, callback: any) {
const acount = /^[A-Z_]+$/;
if (value && !acount.test(value)) {
callback(new Error('请输入大写英文、下划线'));
} else {
callback();
}
},
/**
* 校验 请输入小写英文、下划线
* 名称校验
*/
validatorLowercase(_rule: any, value: any, callback: any) {
const acount = /^[a-z_]+$/;
if (value && !acount.test(value)) {
callback(new Error('请输入小写英文、下划线'));
} else {
callback();
}
},
/**
* 校验 请输入小写英文
* 名称校验
*/
validatorLower(_rule: any, value: any, callback: any) {
const acount = /^[a-z]+$/;
if (value && !acount.test(value)) {
callback(new Error('请输入小写英文'));
} else {
callback();
}
},
/**
* 校验首尾空白字符的正则表达式
*
*/
checkSpace(_rule: any, value: any, callback: any) {
const longrg = /[^\s]+$/;
if (!longrg.test(value)) {
callback(new Error('请输入非空格信息'));
} else {
callback();
}
},
/**
* 校验手机号
*/
validatePhone(_rule: any, value: any, callback: any) {
var isPhone = /^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
if (value.indexOf('****') >= 0) {
return callback();
}
if (!isPhone.test(value)) {
callback(new Error('请输入合法手机号'));
} else {
callback();
}
},
/* 数字 */
number(_rule: any, value: any, callback: any) {
validateFn('number', _rule, value, callback, '包含非数字字符');
},
/* 字母 */
letter(_rule: any, value: any, callback: any) {
validateFn('letter', _rule, value, callback, '包含非字母字符');
},
/* 字母和数字 */
letterAndNumber(_rule: any, value: any, callback: any) {
validateFn('letterAndNumber', _rule, value, callback, '只能输入字母或数字');
},
/* 手机号码 */
mobilePhone(_rule: any, value: any, callback: any) {
validateFn('mobilePhone', _rule, value, callback, '手机号码格式有误');
},
/* 字母开头,仅可包含数字 */
letterStartNumberIncluded(_rule: any, value: any, callback: any) {
validateFn('letterStartNumberIncluded', _rule, value, callback, '必须以字母开头,可包含数字');
},
/* 禁止中文输入 */
noChinese(_rule: any, value: any, callback: any) {
validateFn('noChinese', _rule, value, callback, '不可输入中文字符');
},
/* 必须中文输入 */
chinese(_rule: any, value: any, callback: any) {
validateFn('chinese', _rule, value, callback, '只能输入中文字符');
},
/* 电子邮箱 */
email(_rule: any, value: any, callback: any) {
validateFn('email', _rule, value, callback, '邮箱格式有误');
},
/* URL网址 */
url(_rule: any, value: any, callback: any) {
validateFn('url', _rule, value, callback, 'URL格式有误');
},
regExp(_rule: any, value: any, callback: any) {
if (validateNull(value) || value.length <= 0) {
callback();
return;
}
const pattern = new RegExp(_rule.regExp);
if (!pattern.test(value)) {
const errTxt = _rule.errorMsg || 'invalid value';
callback(new Error(errTxt));
} else {
callback();
}
},
};