/** * 验证电子邮箱格式 */ function email(value) { return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(value) } /** * 验证手机格式 */ function mobile(value) { return /^1([3589]\d|4[5-9]|6[1-2,4-7]|7[0-8])\d{8}$/.test(value) } /** * 验证URL格式 */ function url(value) { return /^((https|http|ftp|rtsp|mms):\/\/)(([0-9a-zA-Z_!~*'().&=+$%-]+: )?[0-9a-zA-Z_!~*'().&=+$%-]+@)?(([0-9]{1,3}.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z].[a-zA-Z]{2,6})(:[0-9]{1,4})?((\/?)|(\/[0-9a-zA-Z_!~*'().;?:@&=+$,%#-]+)+\/?)$/ .test(value) } /** * 验证日期格式 */ function date(value) { if (!value) return false // 判断是否数值或者字符串数值(意味着为时间戳),转为数值,否则new Date无法识别字符串时间戳 if (number(value)) value = +value return !/Invalid|NaN/.test(new Date(value).toString()) } /** * 验证ISO类型的日期格式 */ function dateISO(value) { return /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/.test(value) } /** * 验证十进制数字 */ function number(value) { return /^[\+-]?(\d+\.?\d*|\.\d+|\d\.\d+e\+\d+)$/.test(value) } /** * 验证字符串 */ function string(value) { return typeof value === 'string' } /** * 验证整数 */ function digits(value) { return /^\d+$/.test(value) } /** * 验证身份证号码 */ function idCard(value) { return /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test( value ) } /** * 是否车牌号 */ function carNo(value) { value = value.trim(); // 新能源车牌 const xreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([A-HJ-K][A-HJ-NP-Z0-9][0-9]{4}$))/; // 旧车牌 const creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/; if (value.length === 7) { return creg.test(value) } if (value.length === 8) { return xreg.test(value) } return false } /** * 金额,只允许2位小数 */ function amount(value) { // 金额,只允许保留两位小数 return /^[1-9]\d*(,\d{3})*(\.\d{1,2})?$|^0\.\d{1,2}$/.test(value) } /** * 中文 */ function chinese(value) { const reg = /^[\u4e00-\u9fa5]+$/gi return reg.test(value) } /** * 只能输入字母 */ function letter(value) { return /^[a-zA-Z]*$/.test(value) } /** * 只能是字母或者数字 */ function enOrNum(value) { // 英文或者数字 const reg = /^[0-9a-zA-Z]*$/g return reg.test(value) } /** * 验证是否包含某个值 */ function contains(value, param) { return value.indexOf(param) >= 0 } /** * 验证一个值范围[min, max] */ function range(value, param) { return value >= param[0] && value <= param[1] } /** * 验证一个长度范围[min, max] */ function rangeLength(value, param) { return value.length >= param[0] && value.length <= param[1] } /** * 是否固定电话 */ function landline(value) { const reg = /^\d{3,4}-\d{7,8}(-\d{3,4})?$/ return reg.test(value) } /** * 判断是否为空 */ function empty(value) { switch (typeof value) { case 'undefined': return true case 'string': if (value.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true break case 'boolean': if (!value) return true break case 'number': if (value === 0 || isNaN(value)) return true break case 'object': if (value === null || value.length === 0) return true for (const i in value) { return false } return true } return false } /** * 是否json字符串 */ function jsonString(value) { if (typeof value === 'string') { try { const obj = JSON.parse(value) if (typeof obj === 'object' && obj) { return true } return false } catch (e) { return false } } return false } /** * 是否数组 */ function array(value) { if (typeof Array.isArray === 'function') { return Array.isArray(value) } return Object.prototype.toString.call(value) === '[object Array]' } /** * 是否对象 */ function object(value) { return Object.prototype.toString.call(value) === '[object Object]' } /** * 是否短信验证码 */ function code(value, len = 6) { return new RegExp(`^\\d{${len}}$`).test(value) } /** * 是否函数方法 * @param {Object} value */ function func(value) { return typeof value === 'function' } /** * 是否promise对象 * @param {Object} value */ function promise(value) { return object(value) && func(value.then) && func(value.catch) } /** 是否图片格式 * @param {Object} value */ function image(value) { const newValue = value.split('?')[0] const IMAGE_REGEXP = /\.(jpeg|jpg|gif|png|svg|webp|jfif|bmp|dpg)/i return IMAGE_REGEXP.test(newValue) } /** * 是否视频格式 * @param {Object} value */ function video(value) { const VIDEO_REGEXP = /\.(mp4|mpg|mpeg|dat|asf|avi|rm|rmvb|mov|wmv|flv|mkv|m3u8)/i return VIDEO_REGEXP.test(value) } /** * 是否为正则对象 * @param {Object} * @return {Boolean} */ function regExp(o) { return o && Object.prototype.toString.call(o) === '[object RegExp]' } /** * 通用脱敏函数 * @param {string} content - 需要脱敏的内容(邮箱/手机号/姓名) * @param {number} [type=1] - 脱敏类型:1-邮箱(默认),2-手机,3-姓名 * @returns {string} 脱敏后的内容,不符合规则返回原内容 */ function desensitizeContent(content, type = 1) { // 基础校验:内容为空或非字符串直接返回原内容 if (!content || typeof content !== 'string') { return content; } // 去除首尾空格 const trimContent = content.trim(); switch (type) { // 1 - 邮箱脱敏 case 1: { // 邮箱基本校验:包含@,且@前后有内容,总长度至少5位(如a@b.c) const emailReg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/; if (!emailReg.test(trimContent) || trimContent.length < 5) { return content; // 不符合邮箱格式返回原内容 } // 拆分邮箱:用户名@域名 const [username, domain] = trimContent.split('@'); // 用户名保留前2位,不足2位则保留1位,其余用*代替 const safeUsername = username.length > 2 ? username.substring(0, 2) + '*'.repeat(username.length - 2) : username.substring(0, 1) + '*'.repeat(username.length - 1); return `${safeUsername}@${domain}`; } // 2 - 手机号脱敏 case 2: { // 手机号基本校验:11位数字,以1开头 const phoneReg = /^1\d{10}$/; if (!phoneReg.test(trimContent) || trimContent.length !== 11) { return content; // 不符合手机号格式返回原内容 } // 保留前3位和后4位,中间4位用*代替 return trimContent.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'); } // 3 - 姓名脱敏 case 3: { const nameLength = trimContent.length; // 姓名长度校验:至少1位,最多6位(常规姓名长度) if (nameLength < 1 || nameLength > 6) { return content; } // 单字姓名:直接返回 if (nameLength === 1) { return trimContent; } // 双字姓名:保留首字,第二个字用*代替(如:李*) if (nameLength === 2) { return trimContent.substring(0, 1) + '*'; } // 三字及以上:保留首尾字,中间用*代替(如:张*三、王**明) return trimContent.substring(0, 1) + '*'.repeat(nameLength - 2) + trimContent.substring(nameLength - 1); } // 未知类型:返回原内容 default: return content; } } export default { email, mobile, url, date, dateISO, number, digits, idCard, carNo, amount, chinese, letter, enOrNum, contains, range, rangeLength, empty, isEmpty: empty, jsonString, landline, object, array, code, func, promise, video, image, regExp, string, desensitizeContent }