yinghe-lowcode-zln
Version:
基于vue、ant-design-vue,datagrid的低代码平台
715 lines (697 loc) • 21.2 kB
JavaScript
const reg = /{([a-zA-Z0-9\u4e00-\u9fa5]+).(\S+)}/
const mergeReg = /#MERGE.\S+#/
export function viewReport (xs, str, dataList, isPrint) {
var viewData
if (dataList === undefined || dataList === null) {
viewData = parseNoData(str)
} else {
viewData = parseData(str, dataList, isPrint)
}
viewData.toolPrintSizeObj = { printType: 'A4', widthPx: 794, heightPx: 1047 }
xs.loadData(viewData)
return viewData
// dealPicTop()
}
// 空表
function parseNoData (str) {
for (var rowKey in str.rows) {
var row = str.rows[rowKey]
for (var cellKey in row.cells) {
var cell = row.cells[cellKey]
var cellText = cell.text
if (reg.test(cellText)) {
cellText = ''
} else if (mergeReg.test(cellText)) {
cellText = cellText.replace('#MERGE.','').replace('#','')
}
cell.text = cellText
}
}
return str
}
// 将表达式的json数据转化成真实数据
function parseData (strObj, dataList, isPrint = false) {
if (isPrint) {
dataList = dealDataList(dataList)
dataList = getExcelData(dataList.dataList)
}
Object.keys(dataList).forEach(key => {
delete dataList[`${key}_count`]
if (key.endsWith('_total')) {
delete dataList[key]
}
})
// 将数据里的key转成小写
Object.values(dataList).forEach(item => {
item instanceof Array && item.forEach(row => {
Object.keys(row).forEach(key => {
const lowerKey = key.toLowerCase()
if (lowerKey !== key) {
row[lowerKey] = row[key]
delete row[key]
}
})
})
})
if (!dataList) { return }
const dataListLen = Object.keys(dataList)
.filter(item => !item.endsWith('_isPage'))
.length
if (dataListLen === 1) {
// 有一个数据集,并且数据集中只有一条记录
const resultArray = Object.values(dataList)
if (!resultArray) { return }
const dataArryLen = resultArray[0].length
if (dataArryLen === 1) {
strObj = dealOneData(strObj, dataList)
} else if (dataArryLen > 1) {
strObj = dealManySource(strObj, dataList)
} else {
strObj = dealNoData(strObj)
}
} else if (dataListLen > 1) {
strObj = dealManySource(strObj, dataList)
}
// 字典数据处理
dictDataHandler(strObj)
return strObj
}
/**
* 获得分页信息
* @param {} resultList
*/
function dealDataList (resultList) {
// 处理之前老数据,只有一个数据源,
if (Object.keys(resultList).length === 1) {
const dataKey = Object.keys(resultList)[0]
const dataObj = resultList[dataKey]
if (
!dataObj.isPage &&
dataObj.total > 1) {
dataObj['isPage'] = '1'
}
}
const dataList = JSON.parse(JSON.stringify(resultList))
let count = 1
const pageObj = {}
Object.keys(dataList).forEach(key => {
const dataObj = dataList[key]
Object.keys(dataObj).forEach(resultKey => {
if (resultKey.endsWith('total')) {
// 是否是分页数据集
if (dataObj[`isPage`] === '1' && dataObj['list'].length > 1) {
count = dataObj[`count`]
pageObj[key] = dataObj['total']
}
delete dataObj['total']
delete dataObj['count']
}
})
})
return {
dataList,
count,
pageObj
}
}
function getExcelData (dataList) {
const excelDataList = {}
for (const key in dataList) {
const dataObj = dataList[key]
excelDataList[key] = JSON.parse(JSON.stringify(dataObj.list))
excelDataList[`${key}_isPage`] = dataObj['isPage']
}
return excelDataList
}
/**
* 单条数据
* @param strObj
* @param dataList
* @returns {*}
*/
function dealOneData (strObj, dataList) {
// 有一个数据集,并且数据集中只有一条记录,详情表单
const resultArray = Object.values(dataList)
const resultList = resultArray[0][0]
const excelRows = strObj.rows
for (const rowKey in excelRows) {
const cells = excelRows[rowKey].cells
if (!cells) { continue }
for (const cellKey in cells) {
const cell = cells[cellKey]
const text = cell.text
if (reg.test(text) && Object.keys(dataList)[0] === text.match(reg)[1]) {
const cellText = resultList[text.match(reg)[2].toLowerCase()] || ''
cell.text = '' + cellText
} else if (mergeReg.test(text)) {
cell.text = text.replace('#MERGE.','').replace('#','')
}
}
}
return strObj
}
/**
* 多条数据
* @param strObj:表格配置
* @param dataList:数据集合
* @returns {*}
*/
function dealManySource (strObj = {}, dataList = {}) {
let rowObj = getRowObj(strObj, dataList)
if (Object.values(rowObj) && Object.values(rowObj).length > 0) {
const dataSetNumberArr = Object.values(rowObj)
.filter(item => item['isPage'] === '1')
.map(item => item.dataSetNumber)
if (dataSetNumberArr && dataSetNumberArr.length > 0) {
// 保留数据集位置,打印全部时,第二页到最后只打印列表数据
strObj.dataSetNumber = Math.min(...dataSetNumberArr)
}
}
const rowNumObj = {}
const initRowLen = Object.keys(strObj.rows).length
for (const key in rowObj) {
// 遍历数据
const tempObj = rowObj[key]
if (!tempObj) { continue }
const dataCells = tempObj.dataCells
const currentCell = {}
// var mergeCols = [] // 存储需要merge的行字段
for (const cellIndex in dataCells) {
const cellObj = dataCells[cellIndex]
const cellText = String(cellObj.text)
if (!cellText) { continue }
if (cellText.startsWith('#{')) {
if (cellText.includes(key)) {
currentCell[cellIndex] = dataCells[cellIndex]
}
// } else if (mergeReg.test(cellText) && mergeCols.indexOf(cellText) < 0) {
// mergeCols.push(cellText)
// // cellText = cellText.replace('#MERGE.','').replace('#','')
// // dataCells[cellIndex].text = cellText
// dataCells[cellIndex].aggregate = 'group'
// dataCells[cellIndex].merge = [dataList[key].length - 1,0]
// currentCell[cellIndex] = dataCells[cellIndex]
} else {
currentCell[cellIndex] = dataCells[cellIndex]
}
}
const rowNumSize = new Set(tempObj.rowNums).size
const cellNumSize = new Set(tempObj.cellNums).size
if (rowNumSize > 1 && cellNumSize !== 1) {
strObj.rows = {
...strObj.rows,
...dealOneData(strObj, {
[key]: dataList[key]
}).rows
}
} else {
let currentDataRow = {}
if (rowNumSize > 1) {
// 横向分组处理数据
const currentCellNum = Math.min(...tempObj.cellNums)
const strRows = JSON.parse(JSON.stringify(strObj.rows))
const currentRow = {}
for (const rowIndex in strRows) {
if (Number(rowIndex) < 0) { continue }
const strrCells = strRows[rowIndex].cells
for (const cellsIndex in strrCells) {
if (Number(cellsIndex) === currentCellNum) {
currentRow[rowIndex] = strrCells[cellsIndex]
}
}
}
// currentCellNum 列数
currentDataRow = getRowData(tempObj.height, currentCellNum, currentRow, dataList[key])
} else {
// 获得当前行位置 currentRowNum 行数
const currentRowNum = Math.min(...tempObj.rowNums)
currentDataRow = getRowData(tempObj.height, currentRowNum, currentCell, dataList[key])
}
const newDataLen = Object.keys({
...strObj.rows,
}).length + Object.keys(currentDataRow).length - 1
if (newDataLen <= initRowLen && cellNumSize !== 1) {
// 没有新数据生成
strObj.rows = {
...strObj.rows,
...currentDataRow
}
rowObj = getRowObj(strObj, dataList)
} else {
if (cellNumSize === 1) {
// 横向分组提取数据
for (const rowIndex in currentDataRow) {
for (const cellIndex in currentDataRow[rowIndex].cells) {
strObj.rows[rowIndex].cells[cellIndex] = currentDataRow[rowIndex].cells[cellIndex]
}
}
rowObj = getRowObj(strObj, dataList)
} else {
// 数据有新增
// let maxRowNum = (
// Object.keys({
// ...strObj.rows,
// ...currentDataRow
// })
// .filter(item => item != 'len')).length
// 取出新数据的位置
const dataKeys = Object.keys(currentDataRow)
const dataStartIndex = Math.min(...dataKeys)
const dataEndIndex = Math.max(...dataKeys)
// 取出数据剩余的部分
const rowDataOthers = {}
var rowDataOthersIndex = []
for (var rowIndex in strObj.rows) {
if (rowIndex === 'len' || Number(rowIndex) <= dataStartIndex) { continue }
const hasText = Object.values(strObj.rows[rowIndex].cells).some(item => item.text)
if (hasText) {
rowDataOthersIndex.push(dataKeys.length - 1 + parseInt(rowIndex))
rowDataOthers[dataKeys.length - 1 + parseInt(rowIndex)] = {...strObj.rows[rowIndex]}
strObj.rows[rowIndex] = { cells: { 0: { text: "" } } }
}
}
for (var i = dataStartIndex; i <= dataEndIndex; i++) {
strObj.rows[i] = currentDataRow[i]
}
let maxRowIndex = rowDataOthersIndex[0]
const newOthers = {}
for (const key in rowDataOthers) {
newOthers[maxRowIndex++] = {
...rowDataOthers[key]
}
}
strObj.rows = {
...strObj.rows,
...newOthers
}
rowObj = getRowObj(strObj, dataList)
}
}
}
}
const minRowNum = Math.min(...Object.values(rowNumObj))
strObj.styles.push({
'border': {
'bottom': ['thin', '#000'],
'top': ['thin', '#000'],
'left': ['thin', '#000'],
'right': ['thin', '#000']
}
})
const borderStyleLen = strObj.styles.length - 1
for (const key in strObj.rows) {
if (key < minRowNum) { continue }
const cells = strObj.rows[key].cells
if (!cells) { continue }
Object.values(cells).forEach(cell => {
if (!cell.style) {
cell.style = borderStyleLen
}
})
}
return strObj
}
/**
* 没有数据情况
* @param strObj
*/
function dealNoData (strObj = {}) {
const rows = strObj.rows
for (const rowIndex in rows) {
const cells = rows[rowIndex].cells
for (const cellIndex in cells) {
const cell = cells[cellIndex]
if (reg.test(cell.text)) {
cell.text = ''
} else if (mergeReg.test(cell.text)) {
cell.text = cell.text.replace('#MERGE.','').replace('#','')
}
}
}
return strObj
}
/**
* 字典数据处理
* @param json
*/
function dictDataHandler (json) {
if (json) {
const rows = json.rows
// const dictInfo = view.dictInfo
const dictInfo = []
if (rows) {
// 遍历行
Object.keys(rows).map(rowIndex => {
const row = rows[rowIndex].cells
if (row) {
// 遍历列
Object.keys(row).map(colIndex => {
// 获取单元格
const cell = row[colIndex]
if (cell.isDict === 1) {
const tempText = cell.text
const tempDictList = dictInfo[cell.dictCode]
cell.text = getDictTextByValue(tempDictList, tempText)
}
})
}
})
}
}
}
function getRowObj (strObj = {}, dataList = {}) {
// 多个数据源遍历
const rowObj = {}
// 保留数据集行
const strRows = JSON.parse(JSON.stringify(strObj.rows))
let strRowObj = {}
for (const rowIndex in strRows) {
if (Number(rowIndex) < 0) { continue }
strRowObj = strRows[rowIndex]
const cellObj = strRowObj.cells
if (!cellObj) {
continue
}
let dbName = ''
for (const cellIndex in cellObj) {
const cellText = cellObj[cellIndex].text || ''
if (reg.test(cellText)) {
// 匹配数据集数据
const cellTextArr = cellText.match(reg)
dbName = cellTextArr[1]
const field = cellTextArr[2]
if (!rowObj[dbName]) {
rowObj[dbName] = {}
rowObj[dbName].rowNums = []
rowObj[dbName].cellNums = []
rowObj[dbName].tableFields = []
rowObj[dbName].height = strRowObj.height
// 记录数据集样式
rowObj[dbName].dataCells = strRows[rowIndex].cells
// 数据集上一行记录
const total = (dataList[dbName] && dataList[dbName].length) || 0
if (total > 1) {
rowObj[dbName].prevRow = strRows[(rowIndex - 1 < 0) ? 0 : (rowIndex - 1)]
} else {
rowObj[dbName].prevRow = strRows[rowIndex]
}
rowObj[dbName].dataSetNumber = Number(rowIndex)
rowObj[dbName].isPage = dataList[`${dbName}_isPage`]
}
rowObj[dbName].rowNums.push(parseInt(rowIndex))
rowObj[dbName].cellNums.push(parseInt(cellIndex))
rowObj[dbName].tableFields.push(field)
} else {
if (rowObj[dbName]) {
rowObj[dbName].rowNums.push(parseInt(rowIndex))
rowObj[dbName].cellNums.push(parseInt(cellIndex))
rowObj[dbName].tableFields.push('')
}
}
}
};
// 数据集高度
if (strObj.dataSetNumber) {
strObj.dataSetHeight = strObj.rows[strObj.dataSetNumber].height
}
return rowObj
}
function getRowData (height, startRowNum, dataCells, resultDataList) {
if (!resultDataList) { return {} }
let rowData = {}
// 判断有无分组
let dataCellsFlag = 0
let dataRightFlag = 0
for (const cellIndex in dataCells) {
if (dataCells[cellIndex].aggregate === 'group' || (dataCells[cellIndex].text !== undefined && dataCells[cellIndex].text.indexOf('group(') !== -1)) {
dataCellsFlag++
}
if (dataCells[cellIndex].direction === 'right' || (dataCells[cellIndex].text !== undefined && dataCells[cellIndex].text.indexOf('groupRight(') !== -1)) {
dataRightFlag++
}
}
// 处理数据
let newresultList = []
const groupObj = []
let maxRowNum = startRowNum
const groupStartNum = maxRowNum
if (dataCellsFlag === 0 && dataRightFlag === 0) {
// 列表数据
rowData = backSelectData(resultDataList, dataCells, maxRowNum, rowData, height)
} else if (dataCellsFlag > 0 && dataRightFlag === 0) {
// 分组数据 maxRowNum 行数
newresultList = backGroupData(dataCellsFlag, dataCells, resultDataList, newresultList, groupObj)
backSelectData(newresultList, dataCells, maxRowNum, rowData, height)
// 设置合并单元格
const maxendRowNum = startRowNum + newresultList.length
for (const key in groupObj) {
if (maxRowNum >= maxendRowNum) {
maxRowNum = groupStartNum
}
const cellIndex = groupObj[key].cellIndex
const count = groupObj[key].count
const cell = rowData[maxRowNum].cells[cellIndex]
if (count > 1) {
cell.merge = [count - 1, 0]
}
maxRowNum = maxRowNum + 1
for (let m = 1; m < count; m++) {
rowData[maxRowNum] && delete rowData[maxRowNum].cells[cellIndex]
maxRowNum++
}
}
} else if (dataRightFlag > 0) {
// 分组数据 maxRowNum 列数
newresultList = backGroupData(dataRightFlag, dataCells, resultDataList, newresultList, groupObj)
backSelectRightData(newresultList, dataCells, maxRowNum, rowData, height)
// 设置合并单元格
const maxendRowNum = startRowNum + newresultList.length
for (const key in groupObj) {
if (maxRowNum >= maxendRowNum) {
maxRowNum = groupStartNum
}
const cellIndex = groupObj[key].cellIndex
const count = groupObj[key].count
const cell = rowData[cellIndex].cells[maxRowNum]
if (count > 1) {
cell.merge = [0, count - 1]
}
maxRowNum = maxRowNum + 1
for (let m = 1; m < count; m++) {
rowData[cellIndex] && delete rowData[cellIndex].cells[maxRowNum]
maxRowNum++
}
}
}
return rowData
}
function getMaxIndexInRows (strObj) {
if (!strObj) { return 0 }
return Math.max(...Object.keys(strObj)
.filter(item => item !== 'len')
.map(item => Number(item)))
}
/**
* 获取字典 文本
* @param ls
* @param Value
* @returns {string|*}
*/
function getDictTextByValue (ls, value) {
if (!ls || ls.length === 0) {
return value
}
let text = ''
for (const item of ls) {
if (item.value === value) {
text = item.text
break
}
}
return text
}
// 将数据提取为rows中的格式
function backSelectData (resultDataList, dataCells, maxRowNum, rowData, height) {
if (resultDataList.length === 0) {
// 空数据
const newCellObj = {}
for (const cellIndex in dataCells) {
var item = {...dataCells[cellIndex]}
if (reg.test(item.text)) {
item.text = ''
}
newCellObj[cellIndex] = item
}
rowData[maxRowNum] = {}
rowData[maxRowNum].height = height
rowData[maxRowNum].cells = newCellObj
} else {
for (let i = 0; i < resultDataList.length; i++) {
const resultObj = resultDataList[i]
const newCellObj = {}
for (const cellIndex in dataCells) {
newCellObj[cellIndex] = {
...dataCells[cellIndex]
}
let text = newCellObj[cellIndex].text
if (text !== undefined && reg.test(text)) {
if (text.match(reg)[2].indexOf('group(') !== -1) {
const field2 = subStringStr(text.match(reg)[2], 'group(', ')')
text = resultObj[field2.toLowerCase()] || ''
newCellObj[cellIndex].text = text
} else {
text = resultObj[text.match(reg)[2].toLowerCase()] || ''
newCellObj[cellIndex].text = text
}
}
else if (text !== undefined && mergeReg.test(text)) {
if (i === 0) {
newCellObj[cellIndex].text = text.replace('#MERGE.','').replace('#','')
if (resultDataList.length > 1) {
newCellObj[cellIndex].aggregate = 'group'
newCellObj[cellIndex].merge = [resultDataList.length - 1,0]
}
} else {
delete newCellObj[cellIndex]
}
}
}
rowData[maxRowNum] = {}
rowData[maxRowNum].height = height
rowData[maxRowNum].cells = newCellObj
maxRowNum++
}
}
return rowData
}
// 处理分组数据
function backGroupData (dataCellsFlag, dataCells, resultDataList, newresultList, groupObj) {
var listgroup = []
let isProcessed = true
let listgroupSize = 0
for (const cellIndex in dataCells) {
if (!dataCells[cellIndex].text) { continue }
if (dataCells[cellIndex].text !== undefined && (dataCells[cellIndex].text.indexOf('group(') !== -1 || dataCells[cellIndex].text.indexOf('groupRight(') !== -1)) {
const cellMe = subStringStr(dataCells[cellIndex].text, '#{', '}').split('.')[1]
let field = ''
if (dataCells[cellIndex].text.indexOf('group(') !== -1) {
field = subStringStr(cellMe, 'group(', ')')
} else if (dataCells[cellIndex].text.indexOf('groupRight(') !== -1) {
field = subStringStr(cellMe, 'groupRight(', ')')
}
// 一个字段分组
if (dataCellsFlag === 1) {
listgroup = arrayGroupBy(resultDataList, field.toLowerCase())
listGroupFe(listgroup, groupObj, field, cellIndex)
for (let i = 0; i < listgroup.length; i++) {
newresultList.push.apply(newresultList, listgroup[i])
}
} else if (dataCellsFlag > 1) {
// 多个字段分组
if (isProcessed) {
// 第一次分组
listgroup = arrayGroupBy(resultDataList, field)
listGroupFe(listgroup, groupObj, field, cellIndex)
listgroupSize = listgroup.length
} else {
// 其余字段分组
var listgroupThree = []
newresultList = []
for (let i = 0; i < listgroupSize; i++) {
var listgroupTwo = []
listgroupTwo = arrayGroupBy(listgroup[i], field)
listGroupFe(listgroupTwo, groupObj, field, cellIndex)
for (let j = 0; j < listgroupTwo.length; j++) {
listgroupThree.push(listgroupTwo[j])
}
}
// 将分组数据合并为集合
listgroup = listgroupThree
listgroupSize = listgroup.length
for (let i = 0; i < listgroupThree.length; i++) {
newresultList.push.apply(newresultList, listgroupThree[i])
}
}
}
isProcessed = false
}
}
return newresultList
}
function backSelectRightData (resultDataList, dataCells, maxRowNum, rowData, height) {
for (const cellIndex in dataCells) {
var dataRight = []
rowData[cellIndex] = {}
rowData[cellIndex].height = height
let startMaxRowNum = maxRowNum
for (let i = 0; i < resultDataList.length; i++) {
const newCellObj = {}
newCellObj[startMaxRowNum] = {
...dataCells[cellIndex]
}
const text = newCellObj[startMaxRowNum].text
const resultObj = resultDataList[i]
if (reg.test(text)) {
if (text.match(reg)[2].indexOf('groupRight(') !== -1) {
const field2 = subStringStr(text.match(reg)[2], 'groupRight(', ')')
const textfield = resultObj[field2] || ''
newCellObj[startMaxRowNum].text = textfield
} else {
const textfield = resultObj[text.match(reg)[2].toLowerCase()] || ''
newCellObj[startMaxRowNum].text = textfield
}
}
rowData[cellIndex].cells = newCellObj
dataRight.push(rowData[cellIndex].cells)
startMaxRowNum++
}
for (const dataRightindex in dataRight) {
rowData[cellIndex].cells = {
...rowData[cellIndex].cells,
...dataRight[dataRightindex]
}
}
}
return rowData
}
// 截取字符串方法
function subStringStr (str, strStart, strEnd) {
const strStartIndex = str.indexOf(strStart)
const strEndIndex = str.indexOf(strEnd)
if (strStartIndex < 0) {
return ''
}
if (strEndIndex < 0) {
return ''
}
const result = str.substring(strStartIndex, strEndIndex).substring(strStart.length)
return result
}
// 分组方法
const arrayGroupBy = (list, field) => {
const sorted = groupBy(list, function (item) {
return [item[field]]
})
return sorted
}
const groupBy = (array, f) => {
const groups = {}
array.forEach(function (o) {
var group = JSON.stringify(f(o))
groups[group] = groups[group] || []
groups[group].push(o)
})
return Object.keys(groups).map(function (group) {
return groups[group]
})
}
// 将需要合并的个数和列位置记录
function listGroupFe (listgroup, groupObj, field, cellIndex) {
listgroup.forEach(listItem => {
const groupFieldObj = {}
groupFieldObj.count = listItem.length
groupFieldObj.cellIndex = Number(cellIndex)
groupObj.push(groupFieldObj)
})
return groupObj
}