bonus-ui/src/utils/validate.js

218 lines
6.5 KiB
JavaScript
Raw Normal View History

2024-10-29 14:19:41 +08:00
const systemConfig = JSON.parse(localStorage.getItem('systemConfig'));
2024-06-26 15:11:05 +08:00
/**
* @param {string} path
* @returns {Boolean}
*/
export function isExternal(path) {
return /^(https?:|mailto:|tel:)/.test(path)
}
/**
* @param {string} str
* @returns {Boolean}
*/
export function validUsername(str) {
const valid_map = ['admin', 'editor']
return valid_map.indexOf(str.trim()) >= 0
}
/**
* @param {string} url
* @returns {Boolean}
*/
export function validURL(url) {
const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
return reg.test(url)
}
/**
* @param {string} str
* @returns {Boolean}
*/
export function validLowerCase(str) {
const reg = /^[a-z]+$/
return reg.test(str)
}
/**
* @param {string} str
* @returns {Boolean}
*/
export function validUpperCase(str) {
const reg = /^[A-Z]+$/
return reg.test(str)
}
/**
* @param {string} str
* @returns {Boolean}
*/
export function validAlphabets(str) {
const reg = /^[A-Za-z]+$/
return reg.test(str)
}
/**
* @param {string} email
* @returns {Boolean}
*/
export function validEmail(email) {
const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return reg.test(email)
}
/**
* @param {string} str
* @returns {Boolean}
*/
export function isString(str) {
2024-07-22 15:32:27 +08:00
return typeof str === 'string' || str instanceof String
2024-06-26 15:11:05 +08:00
}
/**
* @param {Array} arg
* @returns {Boolean}
*/
export function isArray(arg) {
if (typeof Array.isArray === 'undefined') {
return Object.prototype.toString.call(arg) === '[object Array]'
}
return Array.isArray(arg)
}
2024-07-22 15:32:27 +08:00
2024-07-04 10:35:24 +08:00
/**
* 密码的正则表达式 最少8个字符最多20个字符至少一个字母一个数字和一个特殊字符
* @param {string} password
* @returns {Boolean}
*/
export function validPwd(value) {
const reg = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,20}$/
return reg.test(value)
2024-07-22 15:32:27 +08:00
}
2024-09-08 20:13:32 +08:00
export function validateNewPassword(rule, value, callback) {
// 使用配置文件中的策略进行验证
// 1. 检查密码长度
2024-10-29 14:19:41 +08:00
if (value.length < systemConfig.passwordConfig.minLength || value.length > systemConfig.passwordConfig.maxLength) {
callback(new Error('密码长度应为' + systemConfig.passwordConfig.minLength + '至' + systemConfig.passwordConfig.maxLength + '位!'))
2024-09-08 20:13:32 +08:00
return
}
// 2. 检查密码复杂度
const hasUpperCase = /[A-Z]/.test(value)
const hasLowerCase = /[a-z]/.test(value)
const hasDigit = /\d/.test(value)
const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(value)
2024-10-29 14:19:41 +08:00
if (systemConfig.passwordConfig.requireUpperCase && !hasUpperCase) {
2024-09-08 20:13:32 +08:00
callback(new Error('密码必须包含大写字母!'))
return
}
2024-10-29 14:19:41 +08:00
if (systemConfig.passwordConfig.requireLowerCase && !hasLowerCase) {
2024-09-08 20:13:32 +08:00
callback(new Error('密码必须包含小写字母!'))
return
}
2024-10-29 14:19:41 +08:00
if (systemConfig.passwordConfig.requireDigit && !hasDigit) {
2024-09-08 20:13:32 +08:00
callback(new Error('密码必须包含数字!'))
return
}
2024-10-29 14:19:41 +08:00
if (systemConfig.passwordConfig.requireSpecialChar && !hasSpecialChar) {
2024-09-08 20:13:32 +08:00
callback(new Error('密码必须包含特殊字符!'))
return
}
// 3. 检查是否包含弱密码
2024-10-29 14:19:41 +08:00
for (const weakPwd of systemConfig.passwordConfig.weakPasswords) {
2024-09-13 16:06:50 +08:00
// 将密码和弱密码都转换为小写进行比较
if (value.toLowerCase().includes(weakPwd.toLowerCase())) {
2024-09-08 20:13:32 +08:00
callback(new Error(`密码包含常见的弱密码片段: ${weakPwd}`))
return
}
}
// 4. 检查是否包含超过规定数量的连续字符
2024-10-29 14:19:41 +08:00
if (systemConfig.passwordConfig.restrictConsecutiveChars && containsConsecutiveCharacters(value, systemConfig.passwordConfig.maxConsecutiveChars)) {
callback(new Error(`密码不能包含超过${systemConfig.passwordConfig.maxConsecutiveChars}位连续字符!`))
2024-09-08 20:13:32 +08:00
return
}
callback() // 验证成功
}
2024-09-18 15:51:58 +08:00
/**
* 检查密码中是否包含超过 n 个连续相同字符连续递增/递减的数字或字母不区分大小写
*/
function containsConsecutiveCharacters(password, n) {
// 检查连续相同字符
n = n + 1; // 允许最多 n 个连续字符
for (let i = 0; i <= password.length - n; i++) {
let consecutiveSameChar = true;
for (let j = 1; j < n; j++) {
if (password[i + j] !== password[i]) {
consecutiveSameChar = false;
break;
}
2024-09-13 16:06:50 +08:00
}
2024-09-18 15:51:58 +08:00
if (consecutiveSameChar) {
return true; // 包含超过 n 个连续相同字符
2024-09-13 16:06:50 +08:00
}
2024-09-18 15:51:58 +08:00
}
// 检查连续递增或递减的数字
for (let i = 0; i <= password.length - n; i++) {
let consecutiveIncreasing = true;
let consecutiveDecreasing = true;
for (let j = 1; j < n; j++) {
const currentChar = password[i];
const nextChar = password[i + j];
2024-09-13 16:06:50 +08:00
2024-09-18 15:51:58 +08:00
// 检查数字递增或递减
if (/\d/.test(currentChar) && /\d/.test(nextChar)) {
if (nextChar.charCodeAt(0) !== currentChar.charCodeAt(0) + j) {
consecutiveIncreasing = false;
}
if (nextChar.charCodeAt(0) !== currentChar.charCodeAt(0) - j) {
consecutiveDecreasing = false;
2024-09-13 16:06:50 +08:00
}
} else {
2024-09-18 15:51:58 +08:00
consecutiveIncreasing = false;
consecutiveDecreasing = false;
break;
2024-09-13 16:06:50 +08:00
}
2024-09-08 20:13:32 +08:00
}
2024-09-18 15:51:58 +08:00
if (consecutiveIncreasing || consecutiveDecreasing) {
return true; // 包含超过 n 个递增或递减的连续数字
}
}
2024-09-13 16:06:50 +08:00
2024-09-18 15:51:58 +08:00
// 检查连续递增或递减的字母(不区分大小写)
for (let i = 0; i <= password.length - n; i++) {
let consecutiveIncreasing = true;
let consecutiveDecreasing = true;
for (let j = 1; j < n; j++) {
const currentChar = password[i].toLowerCase(); // 转为小写
const nextChar = password[i + j].toLowerCase(); // 转为小写
// 检查字母递增或递减
if (/[a-zA-Z]/.test(currentChar) && /[a-zA-Z]/.test(nextChar)) {
if (nextChar.charCodeAt(0) !== currentChar.charCodeAt(0) + j) {
consecutiveIncreasing = false;
}
if (nextChar.charCodeAt(0) !== currentChar.charCodeAt(0) - j) {
consecutiveDecreasing = false;
2024-09-13 16:06:50 +08:00
}
} else {
2024-09-18 15:51:58 +08:00
consecutiveIncreasing = false;
consecutiveDecreasing = false;
break;
2024-09-13 16:06:50 +08:00
}
}
2024-09-18 15:51:58 +08:00
if (consecutiveIncreasing || consecutiveDecreasing) {
return true; // 包含超过 n 个递增或递减的连续字母
}
2024-09-08 20:13:32 +08:00
}
2024-09-13 16:06:50 +08:00
2024-09-18 15:51:58 +08:00
// 不包含连续相同字符、数字或字母序列
return false;
2024-09-08 20:13:32 +08:00
}
2024-09-13 16:06:50 +08:00
2024-09-18 15:51:58 +08:00