2024-09-08 20:13:32 +08:00
|
|
|
|
import { CONFIG } from '@/utils/configure'
|
|
|
|
|
|
import passwordConfig from '@/utils/passwordConfig'
|
2024-08-06 15:47:19 +08:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 弱:长度至少为8个字符。
|
|
|
|
|
|
* 一般:长度至少为8个字符,并包含至少一种字符类型。
|
|
|
|
|
|
* 强:长度至少为8个字符,并包含至少两种字符类型。
|
|
|
|
|
|
* 非常强:长度至少为8个字符,并包含所有四种字符类型。
|
|
|
|
|
|
* @param rule
|
|
|
|
|
|
* @param value
|
|
|
|
|
|
* @param callback
|
|
|
|
|
|
* @returns {*}
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
export function validatePassword(rule, value, callback) {
|
|
|
|
|
|
if (!value) {
|
2024-07-24 18:07:42 +08:00
|
|
|
|
return callback(new Error('请输入密码'))
|
2024-07-22 15:32:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-06 15:47:19 +08:00
|
|
|
|
const lengthRegex = /^.{8,20}$/
|
2024-07-24 18:07:42 +08:00
|
|
|
|
const uppercaseRegex = /[A-Z]/
|
|
|
|
|
|
const lowercaseRegex = /[a-z]/
|
|
|
|
|
|
const digitRegex = /\d/
|
|
|
|
|
|
const specialCharRegex = /[!@#$%^&*(),.?":{}|<>]/
|
|
|
|
|
|
|
|
|
|
|
|
if (!lengthRegex.test(value)) {
|
2024-08-06 15:47:19 +08:00
|
|
|
|
return callback(new Error('密码长度必须为8到20位'))
|
2024-07-24 18:07:42 +08:00
|
|
|
|
}
|
2024-07-22 15:32:27 +08:00
|
|
|
|
|
|
|
|
|
|
const checks = [
|
2024-07-24 18:07:42 +08:00
|
|
|
|
{ regex: uppercaseRegex, message: '必须包含至少一个大写字母' },
|
|
|
|
|
|
{ regex: lowercaseRegex, message: '必须包含至少一个小写字母' },
|
|
|
|
|
|
{ regex: digitRegex, message: '必须包含至少一个数字' },
|
|
|
|
|
|
{ regex: specialCharRegex, message: '必须包含至少一个特殊字符' }
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
let passedChecks = checks.filter(check => check.regex.test(value)).length
|
|
|
|
|
|
|
|
|
|
|
|
let requiredChecks
|
2024-08-08 15:11:05 +08:00
|
|
|
|
switch (CONFIG.STRENGTH) {
|
2024-07-22 15:32:27 +08:00
|
|
|
|
case 'weak':
|
2024-07-24 18:07:42 +08:00
|
|
|
|
requiredChecks = 1
|
|
|
|
|
|
break
|
2024-07-22 15:32:27 +08:00
|
|
|
|
case 'medium':
|
2024-07-24 18:07:42 +08:00
|
|
|
|
requiredChecks = 2
|
|
|
|
|
|
break
|
2024-07-22 15:32:27 +08:00
|
|
|
|
case 'strong':
|
2024-07-24 18:07:42 +08:00
|
|
|
|
requiredChecks = 3
|
|
|
|
|
|
break
|
2024-07-22 15:32:27 +08:00
|
|
|
|
case 'very-strong':
|
2024-07-24 18:07:42 +08:00
|
|
|
|
requiredChecks = 4
|
|
|
|
|
|
break
|
2024-07-22 15:32:27 +08:00
|
|
|
|
default:
|
2024-07-24 18:07:42 +08:00
|
|
|
|
return callback(new Error('请选择有效的密码强度'))
|
2024-07-22 15:32:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (passedChecks < requiredChecks) {
|
2024-07-24 18:07:42 +08:00
|
|
|
|
return callback(new Error(`密码至少包含 ${requiredChecks} 类字符(大写字母,小写字母,数字,特殊字符)`))
|
2024-07-22 15:32:27 +08:00
|
|
|
|
}
|
2024-07-24 18:07:42 +08:00
|
|
|
|
callback()
|
2024-07-22 15:32:27 +08:00
|
|
|
|
}
|
2024-09-08 20:13:32 +08:00
|
|
|
|
|
|
|
|
|
|
export function validateNewPassword(rule, value, callback) {
|
|
|
|
|
|
// 使用配置文件中的策略进行验证
|
|
|
|
|
|
|
|
|
|
|
|
// 1. 检查密码长度
|
|
|
|
|
|
if (value.length < passwordConfig.minLength || value.length > passwordConfig.maxLength) {
|
|
|
|
|
|
callback(new Error('密码长度应为' + passwordConfig.minLength + '至' + passwordConfig.maxLength + '位!'))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 检查密码复杂度
|
|
|
|
|
|
const hasUpperCase = /[A-Z]/.test(value)
|
|
|
|
|
|
const hasLowerCase = /[a-z]/.test(value)
|
|
|
|
|
|
const hasDigit = /\d/.test(value)
|
|
|
|
|
|
const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(value)
|
|
|
|
|
|
|
|
|
|
|
|
if (passwordConfig.requireUpperCase && !hasUpperCase) {
|
|
|
|
|
|
callback(new Error('密码必须包含大写字母!'))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if (passwordConfig.requireLowerCase && !hasLowerCase) {
|
|
|
|
|
|
callback(new Error('密码必须包含小写字母!'))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if (passwordConfig.requireDigit && !hasDigit) {
|
|
|
|
|
|
callback(new Error('密码必须包含数字!'))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if (passwordConfig.requireSpecialChar && !hasSpecialChar) {
|
|
|
|
|
|
callback(new Error('密码必须包含特殊字符!'))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
// 3. 检查是否包含弱密码
|
|
|
|
|
|
for (const weakPwd of 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. 检查是否包含超过规定数量的连续字符
|
|
|
|
|
|
if (passwordConfig.restrictConsecutiveChars && containsConsecutiveCharacters(value, passwordConfig.maxConsecutiveChars)) {
|
|
|
|
|
|
callback(new Error(`密码不能包含超过${passwordConfig.maxConsecutiveChars}位连续字符!`))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
callback() // 验证成功
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function containsConsecutiveCharacters(password, maxConsecutive) {
|
2024-09-13 16:36:46 +08:00
|
|
|
|
let count = 1 // 初始化计数器
|
|
|
|
|
|
let previousChar = '' // 保存上一个字符
|
|
|
|
|
|
maxConsecutive = maxConsecutive + 1
|
2024-09-13 16:06:50 +08:00
|
|
|
|
for (let i = 0; i < password.length; i++) {
|
|
|
|
|
|
// 检查当前字符与前一个字符是否相同
|
|
|
|
|
|
if (password[i] === previousChar) {
|
2024-09-13 16:36:46 +08:00
|
|
|
|
count++ // 计数器加1
|
2024-09-08 20:13:32 +08:00
|
|
|
|
} else {
|
2024-09-13 16:36:46 +08:00
|
|
|
|
count = 1 // 如果字符不同,重置计数器
|
2024-09-13 16:06:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否超过最大连续字符数
|
|
|
|
|
|
if (count > maxConsecutive) {
|
2024-09-13 16:36:46 +08:00
|
|
|
|
return true
|
2024-09-13 16:06:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前字符是否是数字
|
|
|
|
|
|
if (/\d/.test(password[i])) {
|
|
|
|
|
|
// 检查是否有超过指定数量的连续数字
|
|
|
|
|
|
if (i > 0 && password[i] === password[i - 1]) {
|
2024-09-13 16:36:46 +08:00
|
|
|
|
count++ // 计数器加1
|
2024-09-13 16:06:50 +08:00
|
|
|
|
if (count > maxConsecutive) {
|
2024-09-13 16:36:46 +08:00
|
|
|
|
return true
|
2024-09-13 16:06:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2024-09-13 16:36:46 +08:00
|
|
|
|
count = 1 // 重置计数器
|
2024-09-13 16:06:50 +08:00
|
|
|
|
}
|
2024-09-08 20:13:32 +08:00
|
|
|
|
}
|
2024-09-13 16:06:50 +08:00
|
|
|
|
|
|
|
|
|
|
// 检查当前字符是否是字母
|
|
|
|
|
|
if (/[a-zA-Z]/.test(password[i])) {
|
|
|
|
|
|
// 检查是否有超过指定数量的连续字母
|
|
|
|
|
|
if (i > 0 && password[i] === password[i - 1]) {
|
2024-09-13 16:36:46 +08:00
|
|
|
|
count++ // 计数器加1
|
2024-09-13 16:06:50 +08:00
|
|
|
|
if (count > maxConsecutive) {
|
2024-09-13 16:36:46 +08:00
|
|
|
|
return true
|
2024-09-13 16:06:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2024-09-13 16:36:46 +08:00
|
|
|
|
count = 1 // 重置计数器
|
2024-09-13 16:06:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-13 16:36:46 +08:00
|
|
|
|
previousChar = password[i] // 更新上一个字符
|
2024-09-08 20:13:32 +08:00
|
|
|
|
}
|
2024-09-13 16:06:50 +08:00
|
|
|
|
|
2024-09-13 16:36:46 +08:00
|
|
|
|
return false // 如果没有找到问题,则返回符合要求的提示
|
2024-09-08 20:13:32 +08:00
|
|
|
|
}
|
2024-09-13 16:06:50 +08:00
|
|
|
|
|