标签:red his val china key data 文件的 ref 数据
export function throttle (fn, interval = 500) { let canRun = true return function () { if (!canRun) return canRun = false setTimeout(() => { fn.apply(this, arguments) canRun = true }, interval) } }
export function debounce (fn, wait = 500, immediate) { let timer return function () { if (immediate) { fn.apply(this, arguments) } if (timer) clearTimeout(timer) timer = setTimeout(() => { fn.apply(this, arguments) }, wait) } }
获取文件名
export function downloadUrlFile (url, fileName) { url = url.replace(/\\/g, ‘/‘) const xhr = new XMLHttpRequest() xhr.open(‘GET‘, url, true) xhr.responseType = ‘blob‘ // xhr.setRequestHeader(‘Authorization‘, ‘Basic a2VybWl0Omtlcm1pdA==‘); xhr.onload = () => { if (xhr.status === 200) { // 获取文件blob数据并保存 saveAs(xhr.response, fileName) } } xhr.send() }
时间戳简析
export function dateFormat (fmt, date) { let ret const opt = { ‘Y+‘: date.getFullYear().toString(), // 年 ‘m+‘: (date.getMonth() + 1).toString(), // 月 ‘d+‘: date.getDate().toString(), // 日 ‘H+‘: date.getHours().toString(), // 时 ‘M+‘: date.getMinutes().toString(), // 分 ‘S+‘: date.getSeconds().toString() // 秒 // 有其他格式化字符需求可以继续添加,必须转化成字符串 } for (const k in opt) { ret = new RegExp(‘(‘ + k + ‘)‘).exec(fmt) if (ret) { fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, ‘0‘))) } } return fmt }
export function numberParseChina (money) { // 汉字的数字 let cnNums = [‘零‘, ‘壹‘, ‘贰‘, ‘叁‘, ‘肆‘, ‘伍‘, ‘陆‘, ‘柒‘, ‘捌‘, ‘玖‘] // 基本单位 let cnIntRadice = [‘‘, ‘拾‘, ‘佰‘, ‘仟‘] // 对应整数部分扩展单位 let cnIntUnits = [‘‘, ‘万‘, ‘亿‘, ‘兆‘] // 对应小数部分单位 let cnDecUnits = [‘角‘, ‘分‘, ‘毫‘, ‘厘‘] // 整数金额时后面跟的字符 let cnInteger = ‘整‘ // 整型完以后的单位 let cnIntLast = ‘圆‘ // 最大处理的数字 let maxNum = 999999999999999.9999 // 金额整数部分 let integerNum // 金额小数部分 let decimalNum // 输出的中文金额字符串 let chineseStr = ‘‘ // 分离金额后用的数组,预定义 let parts if (money === ‘‘) { return ‘‘ } money = parseFloat(money) if (money >= maxNum) { // 超出最大处理数字 return ‘‘ } if (money === 0) { chineseStr = cnNums[0] + cnIntLast + cnInteger return chineseStr } // 转换为字符串 money = money.toString() if (money.indexOf(‘.‘) === -1) { integerNum = money decimalNum = ‘‘ } else { parts = money.split(‘.‘) integerNum = parts[0] decimalNum = parts[1].substr(0, 4) } // 获取整型部分转换 if (parseInt(integerNum, 10) > 0) { let zeroCount = 0 let IntLen = integerNum.length for (let i = 0; i < IntLen; i++) { let n = integerNum.substr(i, 1) let p = IntLen - i - 1 let q = p / 4 let m = p % 4 if (n === ‘0‘) { zeroCount++ } else { if (zeroCount > 0) { chineseStr += cnNums[0] } // 归零 zeroCount = 0 chineseStr += cnNums[parseInt(n)] + cnIntRadice[m] } if (m === 0 && zeroCount < 4) { chineseStr += cnIntUnits[q] } } chineseStr += cnIntLast } // 小数部分 if (decimalNum !== ‘‘) { let decLen = decimalNum.length for (let i = 0; i < decLen; i++) { let n = decimalNum.substr(i, 1) if (n !== ‘0‘) { chineseStr += cnNums[Number(n)] + cnDecUnits[i] } } } if (chineseStr === ‘‘) { chineseStr += cnNums[0] + cnIntLast + cnInteger } else if (decimalNum === ‘‘) { chineseStr += cnInteger } return chineseStr }
数组去重
export function filterArrayByKey (arr, key) { if (!(Array.isArray(arr))) return [] const hash = {} return arr.filter((item) => { if (hash[item[key]]) return false hash[item[key]] = true return true }) }
export function formatMoney (s, n) { if (s) { n = n > 0 && n <= 20 ? n : 2 s = parseFloat((s + ‘‘).replace(/[^\d.-]/g, ‘‘)).toFixed(n) + ‘‘ let l = s.split(‘.‘)[0].split(‘‘).reverse() let r = s.split(‘.‘)[1] let t = ‘‘ for (let i = 0; i < l.length; i++) { t += l[i] + ((i + 1) % 3 === 0 && (i + 1) !== l.length ? ‘,‘ : ‘‘) } return t.split(‘‘).reverse().join(‘‘) + ‘.‘ + r } }
export const cleanEmptyInArray = function (array) { let [...newArray] = array const count = newArray.length for (let i = count - 1; i >= 0; i--) { if (newArray[i] === ‘‘ || newArray[i] === null || newArray[i] === undefined) { newArray.splice(i, 1) } } return newArray }
基于ekement表格合计方法
Vue.prototype.$calcSummaries = function (columns, data) { const sums = [] // console.log(‘data‘, data) // console.log(‘columns‘, columns) columns.forEach((column, index) => { const values = data && data.map(item => Number(item[column.property])) // console.log(values) if (column.columnKey) { sums[index] = values.reduce((prev, curr) => { const value = Number(curr) if (!isNaN(value)) { // console.log(‘111‘) return column.columnKey === ‘amount‘ ? (Number(prev) + Number(curr)).toFixed(2) : column.columnKey === ‘number‘ ? (Number(prev) + Number(curr)).toFixed(3) : column.columnKey === ‘amount0‘ ? (Number(prev) + Number(curr)).toFixed(0) : (Number(prev) + Number(curr)).toFixed(6) } else { console.log(‘222‘) return column.columnKey === ‘amount‘ ? Number(prev).toFixed(2) : column.columnKey === ‘number‘ ? Number(prev).toFixed(3) : Number(prev).toFixed(6) } }, 0) } else if (index === 0) { sums[index] = ‘合计‘ } else { sums[index] = ‘‘ } }) return sums } 页面内部合计方法 getSummaries (param) { const { columns, data } = param return this.$calcSummaries(columns, data) } 需要合计行加配置项 column-key="amount"
标签:red his val china key data 文件的 ref 数据
原文地址:https://www.cnblogs.com/followme789/p/12956968.html