219 lines
6.5 KiB
TypeScript
219 lines
6.5 KiB
TypeScript
import numeral from 'numeral'
|
||
import { read, utils, writeFileXLSX } from 'xlsx';
|
||
|
||
export type TreeSelectModel = {
|
||
value: number | string,
|
||
id: number | string,
|
||
title: string,
|
||
pId: number | string,
|
||
disabled?: boolean
|
||
}
|
||
export type TreeNodeDto = {
|
||
node: any;
|
||
children: TreeNodeDto[];
|
||
}
|
||
/**
|
||
* @description: [{node:{},chidlren:[]}] => [{...node,children}]
|
||
* @param {any} node 需要拼接的父节点
|
||
* @param {any} chidlren 需要遍历的平级节点 合并节点中的
|
||
* @return {*} 返回[node:{...,children:[]}]
|
||
*/
|
||
export function wrapTreeNode(data: TreeNodeDto[]) {
|
||
|
||
const wrapData: any = data.map((item: TreeNodeDto) => {
|
||
const node = { ...item.node };
|
||
|
||
if (item.children && item.children.length > 0) {
|
||
node.children = wrapTreeNode(item.children);
|
||
}
|
||
return node
|
||
});
|
||
return wrapData;
|
||
}
|
||
|
||
export function formatTreeForProTable(data: TreeNodeDto[]) {
|
||
|
||
const wrapData = wrapTreeNode(data);
|
||
return {
|
||
data: wrapData,
|
||
current: 1,
|
||
pageSize: 100,
|
||
total: 100,
|
||
success: true,
|
||
};
|
||
}
|
||
|
||
export function tableList(list: any) {
|
||
return {
|
||
data: list.List || [],
|
||
current: list.PageIndex || 1,
|
||
pageSize: list.pageSize || 10,
|
||
total: list.TotalCount || 0,
|
||
otherData: list?.OtherData || '',
|
||
success: true,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* @description:生产的数据仅提供 treeSelect组件treeDataSimpleMode模式使用的 数据,
|
||
* @param fields 需要转化的数据,
|
||
* @param treeKeys fields 与 TreeSelectModel 数据键值 如 fields as EnumItem ,treeKeys: { title: 'fieldEnumName', value: 'id', id:'id', pId:'parentId' }
|
||
* @returns TreeSelectModel[]
|
||
*/
|
||
export const getTreeSelectOption = async (treeKeys: TreeSelectModel, fields: any[]) => {
|
||
const option: TreeSelectModel[] = []
|
||
const { value, title, pId, id } = treeKeys
|
||
fields.forEach(async (item: any) => {
|
||
|
||
option.push({ value: item[value], title: item[title], pId: item[pId], id: item[id] })
|
||
if (item.children) {
|
||
const children: TreeSelectModel[] = await getTreeSelectOption(treeKeys, item.children)
|
||
option.push(...children)
|
||
}
|
||
|
||
})
|
||
return option
|
||
}
|
||
|
||
// 转换数据为option格式数据
|
||
export function formateOptions(list: [], rules: { name: string; value: string; other?: string }) {
|
||
// let options: { label: string; value: number | string; other?: string | number }[] = [];
|
||
const { name, value, other } = rules;
|
||
|
||
if (list && other) {
|
||
return list.map((n) => {
|
||
return {
|
||
label: n[name],
|
||
value: n[value],
|
||
other: n[other],
|
||
};
|
||
});
|
||
} if (list) {
|
||
return list.map((n) => {
|
||
return {
|
||
label: n[name],
|
||
value: n[value],
|
||
};
|
||
});
|
||
}
|
||
return [];
|
||
}
|
||
|
||
// 转换options数据value类型为 number
|
||
export function formateField(list: { label: string; value: string | number }[]) {
|
||
const valueNumber: { label: string; value: number }[] = [];
|
||
|
||
list.map((n: any) => {
|
||
if (!isNaN(Number(n.value))) {
|
||
valueNumber.push({
|
||
label: n.label,
|
||
value: numeral(n.value).value(),
|
||
});
|
||
}
|
||
});
|
||
return valueNumber.length > 0 ? valueNumber : list;
|
||
}
|
||
|
||
// 转换树节点的value类型为string
|
||
export function formateTreeField(list: TreeNodeDto[]) {
|
||
const valueNumber: any[] = list.map((item: TreeNodeDto) => {
|
||
const node = { label: item.node.label, value: item.node.value.toString(), type: item.node.type, ico: item.node.ico };
|
||
if (item.children && item.children.length > 0) {
|
||
node.children = formateTreeField(item.children);
|
||
}
|
||
return node
|
||
});
|
||
return valueNumber.length > 0 ? valueNumber : list;
|
||
}
|
||
|
||
// 把图片格式转化为 ui框架需要的数据格式
|
||
export const transferImg = (orgIamges: any[]) => {
|
||
const newImages = orgIamges.map((n: any) => {
|
||
if (typeof n === 'object') {
|
||
|
||
return {
|
||
uid: n.ImageId, // 注意,这个uid一定不能少,否则上传失败
|
||
name: n.ImageName,
|
||
status: 'done',
|
||
url: n.ImageUrl, // url 是展示在页面上的绝对链接
|
||
imgUrl: n.ImagePath,
|
||
}
|
||
}
|
||
const [name] = [...n.split("/")].reverse()
|
||
return {
|
||
uid: new Date().getTime(), // 注意,这个uid一定不能少,否则上传失败
|
||
name,
|
||
status: 'done',
|
||
url: n, // url 是展示在页面上的绝对链接
|
||
imgUrl: n,
|
||
}
|
||
|
||
})
|
||
return newImages
|
||
}
|
||
|
||
|
||
/**
|
||
* @description: 导出excel
|
||
* @param {string} tableData csv表格数据
|
||
* @param {string} exportExcelName 导出表名
|
||
* @param {string} mergeCell 第一个合并单元格名称
|
||
* @param {any} mergeArray 合并配置数组
|
||
* @return {*}
|
||
*/
|
||
const handleChange = (str: any)=>{
|
||
const list = str.split(':')
|
||
const keyType = list[1].slice(0, 1)
|
||
const keys = Number(list[1].slice(1)) + 2
|
||
return `${list[0]}:${keyType}${keys}`
|
||
}
|
||
export const exportFile = async (tableData: string, exportExcelName: string, mergeCell?: string, mergeArray?: any,searchTime?: string) => {
|
||
let printTable = tableData
|
||
if (mergeCell) {
|
||
printTable = mergeCell + printTable
|
||
}
|
||
const dataBlob = new Blob(["\uFEFF".concat(printTable)], {
|
||
type: 'text/csv;charset=utf-8',
|
||
});
|
||
const f = await (await fetch(URL.createObjectURL(dataBlob))).arrayBuffer()
|
||
const reader = read(f)
|
||
const file = reader.Sheets[reader.SheetNames[0]]; // get the first worksheet
|
||
// file['!ref'] = handleChange(file['!ref'])
|
||
const data = utils.sheet_to_json(file);
|
||
// 打印的数据内容
|
||
let ws = utils.json_to_sheet(data);
|
||
// ws.A1.v='经营项目信息'
|
||
const wb = utils.book_new();
|
||
// 将要打印的数据复制一份
|
||
// 因为要打印标题 所以数据要全部往下移一行
|
||
// 下面代码的逻辑简单来说就是 本身的全部数据往下移动一行 将第一行的内容变为表格的标题
|
||
const realWS = utils.json_to_sheet(data)
|
||
// {
|
||
// A1: '场地租赁(分账收银模式)收入确认明细表'
|
||
// }
|
||
// 把复制的数据 全部内容变为空
|
||
for (const key in realWS) {
|
||
if (key!=='!ref'){
|
||
realWS[key].v = ' '
|
||
}
|
||
}
|
||
// 它的属性名称都是A1 A2这样的 所以截取前面 然后数字+1
|
||
// 该方法暂时只适配于该表 若出现AA1这样的属性即excel列名 就要修改了
|
||
// 全部数据往下赋值一行 留出第一行
|
||
for (const key in ws) {
|
||
const keyType = key.slice(0, 1)
|
||
const keys = Number(key.slice(1)) + 1
|
||
realWS[`${keyType}${keys}`] = ws[key]
|
||
}
|
||
// 赋值给要打印的内容
|
||
ws = realWS
|
||
// 设置表格标题
|
||
ws.A1.v = '场地租赁(分账收银模式)收入确认明细表'
|
||
// 要合并单元格的位置
|
||
if (mergeArray) ws['!merges'] = mergeArray
|
||
// 这个!ref 是判断他要变为excel的开头结束位置 我们要变大两行才能装下我们得全部内容 所以用方法修改它
|
||
ws['!ref'] = handleChange(ws['!ref'])
|
||
utils.book_append_sheet(wb, ws, "Data");
|
||
writeFileXLSX(wb, `场地租赁(分析收银模式)收入确认明细表_${searchTime}.xls`);
|
||
}
|