import clone from './clone.js' /** * @desc 函数防抖 * @param func 函数 * @param wait 延迟执行毫秒数 * @param immediate true 表立即执行,false 表非立即执行 */ export let debounce = (func, wait, immediate) => { let timeout; return function() { let context = this; let args = arguments; if (timeout) clearTimeout(timeout); if (immediate) { let callNow = !timeout; timeout = setTimeout(() => { timeout = null; }, wait); if (callNow) func.apply(context, args); } else { timeout = setTimeout(() => { func.apply(context, args); }, wait); } }; }; /** * @desc 函数节流 * @param func 函数 * @param delay 毫秒数 */ export let throttle = (func, delay) => { let timeout; return function() { let args = arguments; if (!timeout) { timeout = setTimeout(() => { func.apply(this, args); timeout = null; }, delay); } }; }; /** * 函数节流 * @param fn * @param interval * @returns {Function} * @constructor */ let startThrottleTimer = true; // 是否第一次执行 第一次点击时立即执行一次,后续不再立即执行 let isThrottleTimer = false; // 第一次是否执行 let ThrottleTimer = true; // 节流函数开关 // 节流函数 export let Throttle = function(fn, delay) { var arg = Array.from(arguments).splice(2); if (ThrottleTimer) { if (startThrottleTimer) { fn.apply(this, arg); startThrottleTimer = false; } ThrottleTimer = false; setTimeout(() => { ThrottleTimer = true; if (isThrottleTimer) { fn.apply(this, arg); } startThrottleTimer = true; isThrottleTimer = !startThrottleTimer; }, delay); } }; // 校验手机号 export function phoneValidate(phone) { const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/ return reg.test(phone) } // 校验+86手机号 \ 座机电话 export function is86Phone(phone) { const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/ if (reg.test(phone)) { return true } const str2 = phone.substring(0, 2) if (str2 == '86') { const str3 = phone.substring(2, phone.length - 1) if (reg.test(str3)) { return true } } const regMob = /^((0\d{2,3})-)?(\d{7,8})$/ if (regMob.test(phone)) { return true } return false } // 复制文本 export function copy(data, msg) { uni.setClipboardData({ data: data, success: () => { uni.showToast({ title: msg || '复制成功', icon: 'none', mask: true }) } }) } // 拨打电话 export function callPhone(phone) { uni.makePhoneCall({ phoneNumber: phone }) } // 拆分字符串--汉字 | 数字 export function splitStr(str) { if (!str) { return } let show_text = str const arr = Array.from(new Set(show_text.match(/\d+(.\d+)?/g))) let replace_text = arr.map(item => { if (item.length >= 8 && item.length <= 11) { return item } }) for (var i = 0; i < replace_text.length; i++) { var replaceString = '**' + replace_text[i] + "**" show_text = show_text.replace(RegExp(replace_text[i]), replaceString) } return show_text.split('**') } // 预览pdf export function exportPDF(pdfUrl) { // #ifdef H5 window.open( pdfUrl ) // #endif // 微信下载文件需要在微信公众平台>开发>开发管理>服务器域名>downloadFile合法域名>配置白名单域名 // #ifdef MP-WEIXIN uni.downloadFile({ url: pdfUrl, success: res => { console.log(res) if (res.statusCode === 200) { // 预览pdf文件 uni.openDocument({ filePath: res.tempFilePath, showMenu: true, // 右上角菜单,可以进行分享保存pdf success: function(file) { console.log("file-success", file) } }) } } }) // #endif // #ifdef APP-PLUS uni.downloadFile({ url: pdfUrl, success: res => { console.log(res) if (res.statusCode === 200) { // 保存pdf文件至手机,一般安卓端存储路径为:手机存储/dcim/camera文件夹下 uni.showLoading({ title: '请稍等...' }) setTimeout(function() { uni.hideLoading() // 预览pdf文件 uni.openDocument({ filePath: res.tempFilePath, showMenu: true, success: function(file) { console.log("file-success", file) } }) }, 1000) } } }) // #endif } /** * 抹除api请求的对象的null和undefined */ export const wipeNulish = (obj) => { obj = Object(obj) // 不是对象类型就转成对象类型 obj = clone(obj, true) // 不改变原来的值 Object.keys(obj) .forEach((key) => { // 删除null和undefined obj[key] == null && delete obj[key] if ( typeof obj[key] === 'object' && Object.prototype.toString.call(obj) === '[object Object]' ) { // 不对Array或Date或RegExp这样的对象处理 // 递归删除 wipeNulish(obj[key]) } }) return obj } // 调用历史栈页面方法 export function pageInit(obj = {}) { if (!obj.route || !obj.funName) { return } return new Promise(resolve => { let params = { route: obj.route, funName: obj.funName } const pages = getCurrentPages() const listPage = pages.find(item => item.route === params.route) if (listPage) { console.log(listPage) const fun = listPage.$vm[params.funName] fun && fun() setTimeout(_ => { resolve() }, 300) } else { resolve() } }) } // 获取url参数--普通 export const getUrlParamsNormal = (param) => { let query = '' let hash = window.location.hash const index = hash.indexOf('#') query = hash.substring(index, hash.length) const indexQuery = hash.indexOf('?') query = query.substring(indexQuery + 1, hash.length) let vars = query.split("&") for (let i = 0; i < vars.length; i++) { let pair = vars[i].split("=") if (pair[0] == param) { return pair[1] } } return (false) } // 检测微信是否安装 export function checkApp() { if (plus.runtime.isApplicationExist({ pname: 'com.tencent.mm', action: 'weixin://' })) { return true console.log("微信应用已安装"); } else { return false console.log("微信应用未安装"); } } // 数字保留两位小数或保留整数 export function numFixtwo(num) { let number = Number(num) if (parseInt(number.toString()) == parseFloat(number.toString())) { return number } else { return number.toFixed(2) } } // 手机号3-4-4结构 export function formatPhone(phone) { let phoneCopy = '' if (phone.length === 3 || phone.length === 8) { phone += " " } if (phone.length === 11) { // 处理复制的号码 const mobileReg = /(?=(\d{4})+$)/g phoneCopy = phone.replace(mobileReg, " ") } return phoneCopy ? phoneCopy : phone } // 精确计算加法 export function acc(arg1, arg2) { let r1 = deal(arg1) let r2 = deal(arg2) let m = Math.pow(10, Math.max(r1, r2)) return (arg1 * m + arg2 * m) / m } function deal(arg) { var t = 0; try { t = arg.toString().split(".")[1].length } catch (e) { } return t } // 精确计算减法 export function rce(arg1, arg2) { let r1 = deal(arg1) let r2 = deal(arg2) let m = Math.pow(10, Math.max(r1, r2)) return (arg1 * m - arg2 * m) / m } // function deal(arg) { // var t = 0; // try { // t = arg.toString().split(".")[1].length // } catch (e) { } // return t // } // 手机号码分割过滤 export const filterPhone = (phone, formart = " ") => { let phoneCopy = phone if (phone.length === 3 || phone.length === 8) { phoneCopy += formart } if (phone.length === 11) { // 处理复制的号码 const mobileReg = /(?=(\d{4})+$)/g phoneCopy = phone.replace(mobileReg, formart) } return phoneCopy } // 解除手机分割-只支持空格拆分 export const openFilterPhone = (phone) => { return phone.replace(/\s/g, "") }