diff --git a/commont.js b/commont.js new file mode 100644 index 0000000..dc17a5b --- /dev/null +++ b/commont.js @@ -0,0 +1,541 @@ + +import request from './utils/request'; +/** + * 脱敏处理函数 + * @param {string} input - 需要脱敏处理的数据 + * @param {string} type - 数据类型,包括 'email', 'id', 'bankCard', 'address', 'birthdate', 'name', 'phone', 'custom'(不脱敏) + * @return {string} 脱敏后的数据 + */ +function maskData(input, type = 'phone') { + if (typeof input !== 'string') return input + // const { securityEncryption } = session.get('THEME_CONFIG') || {} + // 与服务端商定加解密方式 + // input = decrypt(input) + // 只解密不脱敏 + if (type === 'custom') return input + // 地址脱敏 中间的数字进行脱敏 明月小区*单元**号楼101 + if (type === 'address') return input.replace(/(\d+)(?=\D)/g, match => '*'.repeat(match.length)) + let start, end + switch (type) { + case 'email': { + // 邮箱,保留前两位和 "@" 后的所有字符 + const [localPart, domain] = input.split('@') + if (localPart.length > 2) { + start = 2 + end = domain.length + 1 } + break + } + case 'bankCard': { + // 身份证号或银行卡号,保留前三位和后四位 + start = 3 + end = 4 + break + } + case 'birthdate': { + // 出生日期,隐藏中间四位 + start = 2 + end = 2 + break + } + case 'name': { + // 姓名,3个字以内隐藏第1个字,4-6个字隐藏前2个字,大于6个字隐藏第3-6个字 + const length = input.length + if (length <= 3) { + start = 0 + end = length - 1 + } else if (length <= 6) { + start = 0 + end = length - 2 + } else { + start = 2 + end = length - 6 + } + break + } + default: // phone 包含手机号和固定电话 + // 手机号,保留前三位和后两位,电话号码保留区号和后三位 + // telRegExp-固定电话正则 phoneRegExp-手机号正则 + let telRegExp=/^(?:(?:\+|00)86)?1\d{10}$/; + if (telRegExp.test(input)) { + let matchRes = input.match(telRegExp); + if(matchRes){ + console.log(matchRes,"matchRes") + if(matchRes[1]&&matchRes[1].length){ + start = matchRes[1].length + }else{ + start = 3 + } + if(matchRes[2]&&matchRes[2].length){ + end = matchRes[2].length + }else{ + end = 2 + } + } + } else { + start = 3 + end = 2 + } + } + // 计算被脱敏处理后的字符的长度 + let maskLength = input.length - start - end + // 使用正则表达式进行脱敏处理 + const reg = new RegExp(`^(.{${start}}).*(.{${end}})$`) + return input.replace(reg, `$1${'*'.repeat(maskLength)}$2`) + } + + +//获取近几天 +function getDay (number,bool = false) { + let today = new Date(); + let day = today.getTime() + 24 * 60 * 60 * 1000 * number; + today.setTime(day); + let val = formatDate(parseInt(today.getTime() / 1000), 'Y-m-d') + if(bool){ + val = parseInt(today.getTime() / 1000) + } + + return val +} + + +// 添加本地存储 + function addSessionStorage(key,value) { + sessionStorage.setItem(key,JSON.stringify(value)); +} + function getSessionStorage(key) { + let result=sessionStorage.getItem(key); + try{ + result= JSON.parse(result); + }catch(err){ + } + return result; +} + + function delSessionStorage(key) { +sessionStorage.removeItem(key); +} + +function getMonthFirst (data) { + let date = new Date(data); + date.setDate(1); + let month = parseInt(date.getMonth() + 1); + let day = date.getDate(); + if( month < 10) { + month = '0' + month + } + if(day < 10 ) { + day = '0' + day + } + let val = date.getFullYear() + '-' + month +'-' + day + + return val +} + +function getMonthLast (data) { + let date = new Date(data); + let currentMonth = date.getMonth(); + let nextMonth = ++currentMonth; + let nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1); + let oneDay = 24 * 60 * 60 * 1000 + let lastTime = new Date(nextMonthFirstDay - oneDay); + let month = parseInt(lastTime.getMonth() + 1); + let day = lastTime.getDate(); + if( month < 10) { + month = '0' + month + } + if(day < 10 ) { + day = '0' + day + } + let val = date.getFullYear() + '-' + month +'-' + day + + + return val + +} +//格式化时间 +function uuid(){ + var s = []; + var hexDigits = "0123456789abcdef"; + for (var i = 0; i < 36; i++) { + s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); + } + s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 + s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 + s[8] = s[13] = s[18] = s[23] = "-"; + var uuid = s.join(""); + return uuid; +} +//获取url上的参数值 e为参数名称 +function getCookie(e) { + return new Promise((r,j)=>{ + // 获取所有的cookie + var cookies = document.cookie; + + // 将cookie字符串拆分为一个数组 + // 注意:这个方法仅用于演示目的,实际的解析可能需要更复杂的逻辑来处理等号、引号、分号等 + var cookieArray = cookies.split(";"); + let find=false; + // 遍历cookie数组并处理每个cookie + for (var i = 0; i < cookieArray.length; i++) { + // 使用trim()方法去除前后的空格 + var cookiePair = cookieArray[i]?.split("="); + var cookieName = cookiePair[0]?.trim(); + var cookieValue = cookiePair[1]?.trim(); + + // 如果你只对某个特定的cookie感兴趣,可以添加条件判断 + if (cookieName === e) { + find=true; + r(cookieValue); + } + if(i==cookieArray.length-1){ + if(!find){ + r(false); + } + } + } + }); +} + +function setCookie(name, value, days) { + var date = new Date(); + date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); // 有效期为days天 + var expires = "; expires=" + date.toUTCString(); + document.cookie = name + "=" + (value || "") + expires + "; path=/"; // 默认情况下,path为'/' +} + + + +//获取url上的参数值 e为参数名称 + function getQueryString(e) { + var t = new RegExp("(/\?|&)" + e + "=([^&]*)(&|$)"); + var a = window.location.href.substr(1).match(t); + if (a != null) return a[2]; + return "" +} + +// 添加本地存储 + function addStorage(key,value) { + localStorage.setItem(key,JSON.stringify(value)); +} +// 获取本地存储 + function getStorage(key) { + let result=localStorage.getItem(key); + try{ + result= JSON.parse(result); + }catch(err){ + } + return result; +} +//