import { INFORMATION_ICON, SCHEME_ANDRIOD, SCHEME_IOS } from "@/common/config"; import * as FingerprintJS from "@fingerprintjs/fingerprintjs"; import CallApp from "callapp-lib"; import CryptoJS from "crypto-js"; // 验证密码等级 export const checkPassLevel = (pass) => { let level = 0; if (pass.length < 8 || pass.length > 12) return false; if (pass.match(/([a-z])+/)) level++; if (pass.match(/([A-Z])+/)) level++; if (pass.match(/([0-9])+/)) level++; return level >= 2; }; // 验证支付密码 export const checkPayPass = (pass) => pass.length === 6 && /^\d+$/.test(pass); /** * 参数校验器 * * @param {*} data * @param {*} validatorList */ export const validatorFun = (data, validatorList = []) => { // 默认校验器列表 const validatorDefault = { // 是否不为空 ["notNull"]: (val) => !(typeof val == "undefined" || val == null || val === ""), // 是否是手机号 ["isMobile"]: (val) => /^1[3456789]\d{9}$/.test(val), ["checkPassLevel"]: checkPassLevel, ["checkPayPass"]: checkPayPass, }; const errList = []; validatorList.forEach(([key, ...list]) => { // 字段值 const val = data[key]; list.some(([validator, errMsg]) => { const validatorType = typeof validator; // 如果校验器类型为string,查找默认验证器,默认验证器不存在则不校验 const stringResult = validatorType === "string" && !(validatorDefault[validator] ? validatorDefault[validator](val) : true); // 如果校验器类型为function,调用该方法 const funResult = validatorType === "function" && !validator(val, data); // 判断校验结果,true = 不通过,false = 通过,不通过则收集错误信息 if (stringResult || funResult) { errList.push({ key, val, errMsg, validator, }); } return stringResult || funResult; }); }); return errList; }; /** * 数字转中文数字 * @param {Object} num */ export function numToChinese(num) { if (!/^\d*(\.\d*)?$/.test(num)) { alert("Number is wrong!"); return "Number is wrong!"; } var AA = new Array( "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" ); var BB = new Array("", "十", "百", "千", "万", "亿", "点", ""); var a = ("" + num).replace(/(^0*)/g, "").split("."), k = 0, re = ""; for (var i = a[0].length - 1; i >= 0; i--) { switch (k) { case 0: re = BB[7] + re; break; case 4: if (!new RegExp("0{4}\\d{" + (a[0].length - i - 1) + "}$").test(a[0])) re = BB[4] + re; break; case 8: re = BB[5] + re; BB[7] = BB[5]; k = 0; break; } if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0) re = AA[0] + re; if (a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k % 4] + re; k++; } if (a.length > 1) { //加上小数部分(如果有小数部分) re += BB[6]; for (var i = 0; i < a[1].length; i++) re += AA[a[1].charAt(i)]; } return re; } /** * 监听滚动到底部 * @param {() => {}} callback 回调方法 * @param {number} bottom 触发距离,当前默认:350px */ let lstbOption = { callback: () => {}, isListenerScrollToBottom: false, lastScrollTop: 0, isCallBack: false, bottom: 100, listener: false, }; export const listenerScrollToBottom = (callbackTemp, bottomTemp = 250) => { lstbOption.callback = callbackTemp; lstbOption.bottom = bottomTemp; lstbOption.isListenerScrollToBottom = true; window.addEventListener("scroll", function () { const docEl = document.documentElement; const scrollTop = docEl.scrollTop || document.body.scrollTop; const scrollHeight = docEl.scrollHeight; const clientHeight = docEl.clientHeight; const newScrollTop = scrollTop + clientHeight; // 判断条件 --> 正向滚动 且 达到触发距离 const flatCallbackTrue = newScrollTop > lstbOption.lastScrollTop && newScrollTop >= scrollHeight - lstbOption.bottom; // 判断条件 --> 逆向滚动 且 达到触发距离加50px的距离 const flatCallbackFalse1 = newScrollTop < lstbOption.lastScrollTop && newScrollTop < scrollHeight - (lstbOption.bottom + 50); // 判断条件 --> 正向滚动 且 没有达到触发距离 const flatCallbackFalse2 = newScrollTop > lstbOption.lastScrollTop && newScrollTop < scrollHeight - lstbOption.bottom; // console.log('监听滚动到底部:', lastScrollTop, newScrollTop, scrollHeight, scrollHeight - bottom); if (flatCallbackTrue) { // 判断达到触发距离后是否触发过,触发过则不执行回调 if (!lstbOption.isCallBack) { lstbOption.isCallBack = true; lstbOption.callback && lstbOption.callback(); } } else if (flatCallbackFalse1 || flatCallbackFalse2) { lstbOption.isCallBack = false; } // 更新最后一次滚动 lstbOption.lastScrollTop = newScrollTop; }); }; /** * 清除滚动条监听后执行的方法 */ export const removeListenerScrollToBottom = () => { lstbOption = { callback: () => {}, isListenerScrollToBottom: false, lastScrollTop: 0, isCallBack: false, bottom: 100, listener: false, }; }; /** * 判断是否是微信中打开 * @returns {boolean} */ export const isWeChatOpen = () => { const ua = navigator.userAgent.toLowerCase(); return ua.indexOf("micromessenger") > -1; }; /** * 复制到粘贴板 * @param text * @param callback */ export const copy = (text, callback) => { // 禁止复制 document.oncopy = function () { return true; }; const tag = document.createElement("textarea"); tag.setAttribute("id", "cp_hgz_input"); tag.value = text; document.getElementsByTagName("body")[0].appendChild(tag); document.getElementById("cp_hgz_input").select(); document.execCommand("copy"); document.getElementById("cp_hgz_input").remove(); if (callback) { callback(text); } // 禁止复制 document.oncopy = function () { return false; }; }; /** * 获取文件类型 * @param filename * @returns {string} */ export const getFileType = (filename) => filename.substring(filename.lastIndexOf(".") + 1).toLocaleLowerCase(); /** * 获取文件图标 * @param fileType * @returns {*} */ export const getFileIcon = (fileType) => INFORMATION_ICON[fileType]; /** * 格式化富文本,防止图片过大出现横向滚动条 * @param str * @returns {*} */ export const formatRichText = (str) => { return str.replace(/<[^>]+>/g, ""); }; //获取设备唯一标识 export const getUniqueCode = () => { return FingerprintJS.load().then((fp) => { return fp.get().then((result) => result.visitorId); }); }; // 设备类型 1 安卓手机设备 2 ios手机设备 3 安卓ipad 4 iosipad 5 pc电脑设备号 export const getFacilityType = () => { var ua = navigator.userAgent, //获取浏览器UA isWindowsPhone = /(?:Windows Phone)/.test(ua), isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone, isAndroid = /(?:Android)/.test(ua) && /(?:Mobile)/.test(ua), isFireFox = /(?:Firefox)/.test(ua), isChrome = /(?:Chrome|CriOS)/.test(ua), isIosTablet = /(?:iPad|PlayBook)/.test(ua), isAndroidTablet = /(?:Android)/.test(ua) && !/(?:Mobile)/.test(ua), isTablet = isIosTablet || isAndroidTablet || (isFireFox && /(?:Tablet)/.test(ua)), isiPhone = /(?:iPhone)/.test(ua) && !isTablet, isPc = !isiPhone && !isAndroid && !isSymbian && !isTablet; if (isPc) return 5; //判断是否PC else if (isiPhone) return 2; //判断是否iPhone else if (isAndroid) return 1; //判断是否Android else if (isIosTablet) return 4; //判断是否ios平板 else if (isAndroidTablet) return 3; //判断是否Android平板 }; // 是否是Safari浏览器 export const isSafari = () => /Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent); // 判断是否是苹果设备 /iphone|ipod|ipad|macintosh/i export const isAppleMobileDevice = () => isSafari() || /iphone|ipod|ipad/i.test(navigator.userAgent.toLowerCase()); // 打开APP初始化 const callLib = new CallApp({ scheme: { protocol: isAppleMobileDevice() ? SCHEME_IOS : SCHEME_ANDRIOD, }, }); export const openApp = (path) => { setTimeout(() => loading.clear(), 5000); callLib.open({ path: "", callback: () => { loading.clear(); if (isAppleMobileDevice()) { location.href = "https://gaoyixia.hdlkeji.com/apidoc/?appKey=V1.0"; } else { location.href = `myinfo://gyx.net:8888${path}`; } }, }); }; /** * 设置是否可以复制 * @param isNoCopy */ export const setCopyFlag = (isNoCopy = false) => { // 禁止右键菜单 document.oncontextmenu = function () { return isNoCopy; }; // // 禁止文字选择 // document.onselectstart = function () { // return isNoCopy; // }; // 禁止复制 // document.oncopy = function () { // return isNoCopy; // }; // // 禁止剪切 // document.oncut = function () { // return isNoCopy; // }; }; // 获取富文本的纯文字内容 export const getPlainText = (richCont) => { const str = richCont; let value = richCont; if (richCont) { // 方法一: value = value.replace(/\s*/g, ""); //去掉空格 value = value.replace(/<[^>]+>/g, ""); //去掉所有的html标记 value = value.replace(/↵/g, ""); //去掉所有的↵符号 value = value.replace(/[\r\n]/g, ""); //去掉回车换行 value = value.replace(/ /g, ""); //去掉空格 value = convertIdeogramToNormalCharacter(value); return value; // 方法二: // value = value.replace(/(\n)/g, ""); // value = value.replace(/(\t)/g, ""); // value = value.replace(/(\r)/g, ""); // value = value.replace(/<\/?[^>]*>/g, ""); // value = value.replace(/\s*/g, ""); // value = convertIdeogramToNormalCharacter(value); // return value; } else { return null; } }; /** * TripleDES - 3DES解密 * @param base64Str * @param key * @returns {string} * @constructor */ export const TripleDESDecrypt = (base64Str, key = "123456") => { const keyHex = CryptoJS.enc.Utf8.parse(key); const decryted = CryptoJS.TripleDES.decrypt( { ciphertext: CryptoJS.enc.Base64.parse(base64Str), }, keyHex, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7, // 偏移量 // iv: CryptoJS.enc.Utf8.parse(ivstr) } ); return decryted.toString(CryptoJS.enc.Utf8); }; /** * 清除页面缓存 * @param fn * @returns {{beforeRouteLeave(*, *, *): void}} */ export const beforeRouteLeaveFn = (fn = () => 0) => ({ // 清除页面缓存 beforeRouteLeave(to, from, next) { fn(this); if (from.meta.toPagesKeep.includes(to.path)) { from.meta.keepAlive = true; } else { let vnode = this.$vnode; let parentVnode = vnode && vnode.parent; if ( parentVnode && parentVnode.componentInstance && parentVnode.componentInstance.cache ) { let key = vnode.key == null ? node.componentOptions.Ctor.cid + (vnode.componentOptions.tag ? `::${vnode.componentOptions.tag}` : "") : vnode.key; let cache = parentVnode.componentInstance.cache; let keys = parentVnode.componentInstance.keys; if (cache[key]) { this.$destroy(); // remove key if (keys.length) { let index = keys.indexOf(key); if (index > -1) { keys.splice(index, 1); } } cache[key] = null; } } } next(); }, }); /** * 获取字符串长度,两个英文字符算1个 * @param str * @returns {number} */ export const getStringSize = (str = "") => { const letterSize = (str.match(/[a-z0-9,;.!@#-+/\\$%^*()<>?:"'{}~]/gi) || "") .length; const chineseSize = str.length - letterSize; return Math.ceil(letterSize / 2) + chineseSize; }; /** * 根据字符串长度截取字符串,两个英文字符算1个 * @param str * @param maxSize * @returns {number} */ export const getStringByMaxSize = (str, maxSize) => { const strArr = str.split(""); let size = 0; let newStr = ""; for (let i = 0; i < strArr.length; i++) { if (/[a-z]|[0-9]|[,;.!@#-+/\\$%^*()<>?:"'{}~]/i.test(strArr[i])) { size = parseFloat((size + 0.5).toFixed(1)); } else { size += 1; } newStr += strArr[i]; if (size >= maxSize) { break; } } return newStr; };