导入处理

This commit is contained in:
李志强 2023-12-29 15:18:36 +08:00
parent c403dadb93
commit 80fb10fa46
3 changed files with 63 additions and 34 deletions

View File

@ -72,12 +72,6 @@ export const updateProject = (data) => {
data, data,
}; };
}; };
export const updateDataSource = (data) => {
return {
type: UPDATE_DATASOURCE,
data,
};
};
const saveAllVersionSuccess = (data) => { const saveAllVersionSuccess = (data) => {
return { return {
type: SAVE_ALL_VERSION_SUCCESS, type: SAVE_ALL_VERSION_SUCCESS,
@ -415,6 +409,13 @@ export const openDemoProject = (h, t, title, type) => {
}; };
}; };
export const updateDataSource = (data) => {
return (dispatch, getState) => {
let isDemoProject = getState().core.isDemoProject;
dispatch(readProjectSuccess(data, [], '', isDemoProject));
}
}
export const close = () => { export const close = () => {
return { return {
type: CLOSE_PROJECT, type: CLOSE_PROJECT,

View File

@ -1,7 +1,8 @@
import { executeSql } from '@/services/api.ts'; import { executeSql } from '@/services/api.ts';
import { Divider, Upload } from 'antd'; import { Divider, Upload,message } from 'antd';
import numeral from 'numeral'; import numeral from 'numeral';
import React, { forwardRef, useImperativeHandle, useRef, useState } from 'react'; import React, { forwardRef, useImperativeHandle, useRef, useState } from 'react';
import { SketchPicker } from 'react-color'; import { SketchPicker } from 'react-color';
import _ from 'lodash/object' import _ from 'lodash/object'
import { import {
@ -32,6 +33,7 @@ export default React.memo(
resize, resize,
sliderChange, sliderChange,
dataSource, dataSource,
getDataSource,
restProps, restProps,
jumpPosition, jumpPosition,
jumpDetail, jumpDetail,
@ -186,32 +188,57 @@ export default React.memo(
const props = { const props = {
maxCount: 1, maxCount: 1,
accept: 'application/vnd.ms-excel',
beforeUpload: (file) => { beforeUpload: (file) => {
new Promise((resolve, reject) => { if(file.name.indexOf('xls') === -1){
const reader = new FileReader(); message.error('请上传xlsx格式文件')
reader.onload = (e) => { return
resolve(e.target.result); }
}; const { entities, domains } = JSON.parse(JSON.stringify(dataSource));
reader.onerror = reject; const reader = new FileReader();
reader.readAsBinaryString(file); reader.onload = function (e) {
}) const data = new Uint8Array(e.target.result); // 获取文件数据
.then((fileData) => { const workbook = XLSX.read(data, { type: 'array' }); // 解析为工作簿对象
// 解析XLSX const keyList = entities[0].headers
return XLSX.read(fileData, { type: 'binary' }); .filter((item) => item.refKey !== 'isStandard')
.map((item) => item.refKey);
workbook.SheetNames.map((defName, index) => {
const worksheet = workbook.Sheets[workbook.SheetNames[index]]; // 获取工作表
let fields = XLSX.utils.sheet_to_json(worksheet, { header: keyList }); // 将工作表转换为JSON数组
fields = fields.slice(1).map(item => {
// 判断为哪个数据域字典
const {
len,
id,
defName,
scale,
uiHint,
} = domains.find((domain) => domain.defName === item.domain);
const obj = { ...item, domain: id }
const booleanList = ['notNull', 'primaryKey', 'autoIncrement', 'hideInGraph']
booleanList.forEach((key) => {
obj[key] = item[key] === '是'
})
return obj
})
const entitie = {
...entities[0],
fields,
defName,
id: entities[0].id + Math.random()
}
entities.push(entitie)
}) })
.then((workBook) => { const currentDataSource = getDataSource();
const { entities, domains } = dataSource; console.log(entities, currentDataSource, restProps);
const keyList = entities[0].headers // 字典映射
.filter((item) => item.refKey !== 'isStandard')
.map((item) => item.refKey);
let data = workBook.Sheets.forEach((sheet) => { restProps.updateDataSource({
window.XLSX.utils.sheet_to_json(workBook.Sheets[sheet], { ...currentDataSource,
headers: keyList, entities
}); })
});
}); };
reader.readAsArrayBuffer(file); // 读取文件为ArrayBuffer
return false; return false;
}, },
fileList, fileList,
@ -271,7 +298,7 @@ export default React.memo(
//style={{cursor: 'move'}} //style={{cursor: 'move'}}
onClick={iconClick} onClick={iconClick}
groupKey="empty" groupKey="empty"
//onMouseDown={e => iconClick(e, 'empty')} //onMouseDown={e => iconClick(e, 'empty')}
/> />
<GroupIcon <GroupIcon
className={`${currentPrefix}-icongroup`} className={`${currentPrefix}-icongroup`}
@ -281,7 +308,7 @@ export default React.memo(
onClick={iconClick} onClick={iconClick}
groupKey="group" groupKey="group"
disable={activeTab?.type !== 'diagram'} disable={activeTab?.type !== 'diagram'}
//onMouseDown={e => iconClick(e, 'group')} //onMouseDown={e => iconClick(e, 'group')}
/> />
<GroupIcon <GroupIcon
className={`${currentPrefix}-icongroup`} className={`${currentPrefix}-icongroup`}
@ -303,13 +330,13 @@ export default React.memo(
//style={{cursor: 'move'}} //style={{cursor: 'move'}}
icon={ icon={
<div className={`${currentPrefix}-head-rect`}> <div className={`${currentPrefix}-head-rect`}>
<span>{}</span> <span>{ }</span>
</div> </div>
} }
groupKey="polygon" groupKey="polygon"
onClick={iconClick} onClick={iconClick}
disable={activeTab?.type !== 'diagram'} disable={activeTab?.type !== 'diagram'}
//onMouseDown={e => iconClick(e, 'polygon')} //onMouseDown={e => iconClick(e, 'polygon')}
/> />
<div className="fontColor1"> <div className="fontColor1">

View File

@ -1729,6 +1729,7 @@ const Index = React.memo(
<Loading visible={common.loading} title={common.title}> <Loading visible={common.loading} title={common.title}>
<HeaderTool <HeaderTool
dataSource={restProps.dataSource} dataSource={restProps.dataSource}
getDataSource={getDataSource}
ref={headerToolRef} ref={headerToolRef}
currentPrefix={currentPrefix} currentPrefix={currentPrefix}
close={restProps.close} close={restProps.close}