vue-rise-design
Version:
一款服务于 Rise 前端低代码平台的表单设计器
318 lines (276 loc) • 7.15 kB
JavaScript
import XLSX from 'xlsx';
// 查询数据类型
export const dataTypeOf = (obj) => {
var toString = Object.prototype.toString;
var map = {
'[object Boolean]': 'boolean',
'[object Number]': 'number',
'[object String]': 'string',
'[object Function]': 'function',
'[object Array]': 'array',
'[object Date]': 'date',
'[object RegExp]': 'regExp',
'[object Undefined]': 'undefined',
'[object Null]': 'null',
'[object Object]': 'object',
};
return map[toString.call(obj)];
};
//是否为数字
export function isNumberStr(str) {
return /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g.test(str);
}
/**
* 函数去抖
* @param {Function} callback
* @param {number} delay
* @return {*}
*/
export function debounce(delay, callback) {
let timeoutID;
function wrapper() {
const self = this;
const args = arguments;
function exec() {
callback.apply(self, args);
}
clearTimeout(timeoutID);
timeoutID = setTimeout(exec, delay);
}
return wrapper;
}
// 判断空对象
export const isEmptyObject = (obj) => {
if (dataTypeOf(obj) === 'boolean') {
return !obj;
}
if (dataTypeOf(obj) === 'number') {
return false;
}
let name;
for (name in obj) {
return false;
}
return true;
};
// 深拷贝
export const DEEP_CLONE = (data) => {
var t = dataTypeOf(data),
o,
i,
ni;
if (t === 'array') {
o = [];
} else if (t === 'object') {
o = {};
} else {
return data;
}
if (t === 'array') {
for (i = 0, ni = data.length; i < ni; i++) {
o.push(DEEP_CLONE(data[i]));
}
return o;
} else if (t === 'object') {
for (i in data) {
o[i] = DEEP_CLONE(data[i]);
}
return o;
}
};
//寻找目标对象
export const FIND_ITEM = (prop, array, key = 'prop') => {
for (let index = 0; index < array.length; index++) {
const element = array[index];
if (element[key] == prop) {
return element;
} else if (element.column && element.column.length !== 0) {
let tag = FIND_ITEM(prop, element.column, key);
if (tag) return tag;
}
}
};
//寻找实时目标对象
export const FIND_REAL_ITEM = (
prop,
array,
key = 'prop',
parentDisabled = false,
parentIsHide = false,
) => {
for (let index = 0; index < array.length; index++) {
const element = array[index];
if (parentDisabled) {
element.disabled = true;
}
if (parentIsHide) {
element.isHide = true;
}
if (element[key] == prop) {
return element;
} else if (element.column && element.column.length !== 0) {
let tag = FIND_REAL_ITEM(
prop,
element.column,
key,
element.disabled,
element.isHide,
);
if (tag) return tag;
}
}
};
//寻找目标组件父级
export const FIND_PARENT = (prop, array, key = 'id') => {
for (let index = 0; index < array.length; index++) {
const element = array[index];
if (element[key] == prop) {
return { parents: array, index };
} else if (element.column && element.column.length !== 0) {
let tag = FIND_PARENT(prop, element.column, key);
if (tag) return tag;
}
}
};
//字符串对象转换
export const SMARTS_OBJECT = (
type = 'find',
propertyName,
target,
value = null,
) => {
if (
typeof propertyName !== 'string' ||
propertyName === undefined ||
(type !== 'validator' &&
(target === undefined || typeof target !== 'object'))
) {
console.error('[Error][SMARTS_OBJECT] parameter error !');
return;
}
let list = []; //解构 propertyName 后的数组
//以 . 区分切割成数组 , 解构 [] 对象写法
const regex = /(\[)(.+?)(?=\])/g;
propertyName.split('.').forEach((item) => {
const objProp = item.replace(/\[.+?\]/g, '');
const arrayProp = item.match(regex) || [];
let filterProp = arrayProp.map((currentValue) => {
return currentValue.replace(/\[/g, '');
});
if (objProp) {
// @ts-ignore
filterProp.unshift(objProp);
}
list.push(...filterProp);
});
//创建不存在对象函数
const build_obj = (arr = [], val) => {
if (arr.length === 0) {
return val;
}
let created_obj;
//初始值确定
if (isNumberStr(arr[0])) {
created_obj = [];
} else {
created_obj = {};
}
//设置循环体
let loopBody = created_obj;
for (let index = 0; index < arr.length; index++) {
const current = arr[index],
next = arr[index + 1];
if (next === undefined) {
loopBody[current] = val;
return created_obj;
}
if (isNumberStr(next)) {
loopBody[current] = [];
} else {
loopBody[current] = {};
}
loopBody = loopBody[current];
}
};
if (type === 'created') {
for (let index = 0; index < list.length; index++) {
const current = list[index];
if (target[current] === undefined) {
if (typeof target !== 'object') {
console.error(
`[Error][SMARTS_OBJECT] Intermediate variable '${
list[index - 1]
}' is not Object !`,
);
} else {
target[current] = build_obj(list.slice(index + 1), value);
}
return;
}
target = target[current];
}
return target;
} else if (type === 'vue') {
for (let index = 0; index < list.length; index++) {
const current = list[index];
if (target[current] === undefined || index == list.length - 1) {
if (typeof target !== 'object') {
console.error(
`[Error][SMARTS_OBJECT] Intermediate variable '${
list[index - 1]
}' is not Object !`,
);
} else {
return {
target: target,
prop: current,
value: build_obj(list.slice(index + 1), value),
};
}
return;
}
target = target[current];
}
} else if (type === 'validator') {
let validatorProp = list[0];
for (let index = 1; index < list.length; index++) {
const current = list[index];
validatorProp = validatorProp + '[' + current + ']';
}
return validatorProp;
} else {
for (let index = 0; index < list.length; index++) {
if (target === undefined) {
return;
}
const item = list[index];
target = target[item];
}
return target;
}
};
// 表格导出 Excel 工具
export const Export_Excel = (tableOption, tableData, title) => {
let thead = [],
theadId = [];
const fileName = title || '导出表格';
tableOption.forEach((item) => {
thead.push(item.label);
theadId.push(item.prop);
});
let excelData = [
thead, // 表头
];
tableData.forEach((item) => {
let tag = [];
theadId.forEach((child) => {
tag.push(item[child]);
});
excelData.push(tag);
});
const ws_name = 'Sheet1';
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.aoa_to_sheet(excelData);
XLSX.utils.book_append_sheet(wb, ws, ws_name); // 将数据添加到工作薄
XLSX.writeFile(wb, `${fileName}.xlsx`);
};