/** * @Description: 扩展基类原型方法 * @ComponentName: System.tools * @Author: wangzhigang11 * @Date: 2022-06-24 19:35 */ /** * 扩展基类原型方法,已载入全局基类,可直接调用 * @module common/systemToolExtend * @Author: wangzhigang11 * @Date: 2022-07-19 14:07 */ import checkTypes from "@/utils/checkTypes"; const ArrayPrototype = Array.prototype const NumberPrototype = Number.prototype const StringPrototype = String.prototype const FilePrototype = File.prototype /** * 根据内容结果获取列表中的内容项 * @function * @param { string } code 编码 * @param { string } itemKey 列表项中的筛选项 默认为value * @example * const arr1 = [ { label: 'xxx', value: 1 }, { label: 'yyy', value: 2 } ] * arr1.getItemByCode(1) // -> { label: 'xxx', value: 1 } * * const arr2 = [ { a: '1', b: 2 }, { a: '3', b: 4 } ] * arr2.getItemByCode(1, 'a') // -> { a: '1', b: 2 } * @returns {object} */ ArrayPrototype.getItemByCode = function (code, itemKey = 'value') { if (!checkTypes.isArray(this)) return {} for (const item of this) { if (`${item[itemKey]}` === `${code}`) return item } return {} } /** * 根据逗号分割的内容获取结果 * @param { string[] } code 需要查找的内容 * @param { string } code 需要查找的内容 * @param { object } option 查找的配置 * @param { string } option.label 查找结果字符串的映射字段 * @param { string } option.key 查找结果的键的映射字段 * @param { string } option.separator 分割符,默认为',' * @param { string } option.emptyText 查找结果无效时显示的内容 * @example * const arr = [{id: 1, name: '小红'}, [id: 3, name: '小明']] * arr.getLabelByCodes('1,3', { label: 'name', key: 'id' }) // => '小红,小明' * arr.getLabelByCodes('1,3', { label: 'name', key: 'id', separator: '/' }) // => '小红/小明' * @returns {string} */ ArrayPrototype.getLabelByCodes = function (code, option = {}) { if (!checkTypes.isArray(this)) return '' const curCode = checkTypes.isNumber(code) && code === 0 ? '0' : code || '' let filterCode = [] if (checkTypes.isArray(code)) { filterCode = code.filter(item => !!item) } else { filterCode = `${curCode}`.split(',').filter(item => !!item) } return filterCode.map(itemCode => { return this.getItemByCode(itemCode, option.key || 'value')[option.label || 'label'] || '' }).filter(item => item !== '').join(option.separator || ',') || option.emptyText || '' } ArrayPrototype.getIndexByCode = function (code, itemKey = 'value') { return this.findIndex(cur => cur[itemKey] === code) } ArrayPrototype.hasCode = function (code, key = 'value') { const filter = this.findIndex(item => item[key] === code) return filter >= 0 } /** * 数据处理工具,保留小数点后几位,并添加单位 * @function superFixed * @private * @param { number} len 保留位数 * @param { string } unit 单位 * @example * const test = 1 * test.superFixed(2) // => 1.00 * test.superFixed(2, '个') // => '1.00个' * @returns {string|number} */ NumberPrototype.superFixed = function (len = 0, unit = '') { return (this || 0).toFixed(len) + unit } /** * 数据处理工具,保留小数点后几位,并添加单位 * @function superFixed * @param { number} len 保留位数 * @param { string } unit 单位 * @example * const test1 = '1' * test1.superFixed(2) // => 1.00 * test1.superFixed(2) // => 1.00 * const test2 = 1 * test2.superFixed(2) // => 1.00 * test2.superFixed(2, '个') // => '1.00个' * @returns {string|number} */ StringPrototype.superFixed = function (len = 0, unit = '') { // 如果是数字型字符串 返回保留后的 if (/^[-\+]?[\d]*\.?[\d]*$/.test(this)) return Number(this).superFixed(len, unit) // 如果是其他字符串,返回本身 return this } function base64ToBlob(base64) { const parts = base64.split(';base64,'); const contentType = parts[0].split(':')[1]; const raw = window.atob(parts[1]); const rawLength = raw.length; const uInt8Array = new Uint8Array(rawLength); for (let i = 0; i < rawLength; ++i) { uInt8Array[i] = raw.charCodeAt(i); } return new Blob([uInt8Array], {type: contentType}); } StringPrototype.toFile = function () { // 使用Base64字符串创建一个File对象 return new File([base64ToBlob(this)], `file_${new Date().valueOf()}`, { type: 'image/png', }) } /** * 文件转base64 * @param {boolean} splitHeader 是否需要切掉base64头 * @example * file.toBase64() // => 'image/jpeg;base64,VFZSSmVrMVVTWG8lM0Q=' * file.toBase64(true) // => 'VFZSSmVrMVVTWG8lM0Q=' * @returns {string} */ FilePrototype.toBase64 = function (splitHeader = false) { return new Promise((resolve, reject) => { const fileReader = new FileReader() fileReader.readAsDataURL(this) fileReader.onload = (e) => { let base64Str = e.target.result if (splitHeader) { base64Str = base64Str.substr(base64Str.indexOf(',') + 1) } resolve(base64Str) } }) }