import qrcode from "@/util/qrcode.js" /* 获取当前日期并格式化 */ function getNowTime() { var nowtime = new Date() var year = nowtime.getFullYear() var month = nowtime.getMonth() + 1 < 10 ? "0" + (nowtime.getMonth() + 1) : nowtime.getMonth() + 1 var day = nowtime.getDate() < 10 ? "0" + nowtime.getDate() : nowtime.getDate() var hour = nowtime.getHours() < 10 ? "0" + nowtime.getHours() : nowtime.getHours() var minute = nowtime.getMinutes() < 10 ? "0" + nowtime.getMinutes() : nowtime.getMinutes() return year + "-" + month + "-" + day + " " + hour + ":" + minute } // 格式化日期格式为 xxxx-xx-xx function getFullDate(targetDate) { var nowDate = new Date() var fullYear = nowDate.getFullYear() var month = nowDate.getMonth() + 1 // getMonth 方法返回 0-11,代表1-12月 var D, y, m, d if (targetDate) { D = new Date(targetDate) y = D.getFullYear() m = D.getMonth() + 1 d = D.getDate() } else { // 不传参数 则获取今天的日期 y = fullYear m = month d = new Date() d = d.getDate() } m = m > 9 ? m : "0" + m d = d > 9 ? d : "0" + d return y + "-" + m + "-" + d } /* 将时间戳格式化为yyyy-MM-dd hh:mm:ss格式,其它格式可自行更改 */ function formatTimeMills(timeMills, sign) { var date = new Date(timeMills) var timeStr = date.getFullYear() + sign if (date.getMonth() < 9) { // 月份从0开始的 timeStr += "0" } timeStr += date.getMonth() + 1 + sign timeStr += date.getDate() < 10 ? "0" + date.getDate() : date.getDate() timeStr += " " timeStr += date.getHours() < 10 ? "0" + date.getHours() : date.getHours() timeStr += ":" timeStr += date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes() /* timeStr += ':'; timeStr += date.getSeconds() < 10 ? ('0' + date.getSeconds()) : date.getSeconds(); */ return timeStr } /* 获取今天的零点/最后一刻 */ function getTodayStartOrEnd(sign) { let resultTime = "" if (sign == "start") { resultTime = new Date(new Date(new Date().toLocaleDateString()).getTime()) } else if (sign == "end") { resultTime = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1) } return formatTimeMills(resultTime, "-") } let num = (a) => { if (a != null && a.toString() != "") { let r = /^-?(0|[1-9]+\d*|[1-9]+\d*\.\d+|0\.\d+)$/ if (r.test(a.toString())) { return true } } return false } /* 除法 */ function division(a, b) { if (!num(a) || !num(b)) { return null } let c, d, f, g try { c = a.toString().split(".")[1].length } catch (e) { c = 0 } try { d = b.toString().split(".")[1].length } catch (e) { d = 0 } f = Number(a.toString().replace(".", "")) g = Number(b.toString().replace(".", "")) return parseFloat(((f / g) * Math.pow(10, d - c)).toFixed(2)) } /* 金额格式化 */ function amountFormatting(amount, sign) { let changeAmount = 0 let resultAmount = 0 if (sign == 0) { changeAmount = division(amount, 100) resultAmount = changeAmount.toFixed(2).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, "$&,") } else if (sign == 1) { resultAmount = amount.toFixed(0).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, "$&,") } return resultAmount } // 获取n天后的日期 function getNdaysLater(n) { var nowDate = new Date() let nDaysLaterDate = getFullDate(nowDate.getTime() + 24 * 60 * 60 * 1000 * n) //获取明天日期 return nDaysLaterDate } export const debounce = (fn, t) => { let delay = t || 500 let timer return function () { let args = arguments if (timer) { clearTimeout(timer) } timer = setTimeout(() => { timer = null fn.apply(this, args) }, delay) } } // 将回显的地址码,转为文本供只读组件使用 ,第一个参数为地址码数组,第二,三个参数为省市区的JSON,只有收付通会用到第三个参数 const addressBack = (list, address, addressTow) => { if (!list || list.length == 0) return "" // 非空校验 let addressText = "" // 绝大多数清空都没有第二个 地址JSON表 if (!addressTow) { let first = address.find((val) => val.value == list[0]) addressText += first.text + "/" let second = first.children.find((val) => val.value == list[1]) addressText += second.text + "/" let third = second.children.find((val) => val.value == list[2]) addressText += third.text } else { // 收付通 选择 支行所在地址专属 (只能选择到市一级) let first = addressTow.find((val) => val.value == list[0]) addressText += first.text + "/" let second = first.children.find((val) => val.value == list[1]) addressText += second.text } return addressText } // 将回显的行业mcc码,转为文本供只读组件使用 ,第一个参数为地址码数组,第二参数为行业mcc的JSON, 第三个参数为取值方式,拼接的截取取值,数组的拿最后一个值 const mccBack = (mccCode, mccJson, type) => { if (!mccCode) return "" // 非空校验 let mccText = "" if (type == "splicing") { let code if (mccCode) code = mccCode.split("_")[1] for (var i = 0; i < mccJson.length; i++) { mccText = mccJson[i].children.find((val) => val.value == code) if (mccText) break } mccText = mccText.text } else if (type == "last") { for (var i = 0; i < mccJson.length; i++) { mccText = mccJson[i].children.find((val) => val.value == mccCode) if (mccText) break } mccText = mccText.text } return mccText } // 制作图片 const drawQRcode = (url) => { let params = url // 二维码参数 var imgData = qrcode.drawImg(params, { typeNumber: 4, // 密度 errorCorrectLevel: "Q", // 纠错等级 size: 175, // 白色边框 }) return imgData } function addressCode(val, address) { const data = [] address.forEach((v) => { if (val.indexOf(v.text) != -1) { data.push(v) v.children.forEach((ite) => { if (val.indexOf(ite.text) != -1) { data.push(ite) ite.children.forEach((value) => { if (val.indexOf(value.text) != -1) { data.push(value) } }) } }) } }) const obj = { text: [], code: [], } data.forEach((v, i) => { obj.text.push(v.text) obj.code.push(v.value) }) obj.text = obj.text.join("/") return obj } export default { getNowTime, amountFormatting, getNdaysLater, getTodayStartOrEnd, addressBack, mccBack, drawQRcode, addressCode, }