From c9851406185199d62a0d465137d70a99dd6770ed Mon Sep 17 00:00:00 2001 From: zhangzhen <525765282@qq.com> Date: Thu, 19 Jun 2025 14:45:01 +0800 Subject: [PATCH] 公式页面新增导入模板功能; --- package.json | 3 ++- src/components/Common/CommonElementEvent.js | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 66eb30c..e398a5b 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,8 @@ "react-sortable-hoc": "^2.0.0", "react-to-print": "^3.0.5", "umi": "^4.4.11", - "weixin-js-sdk": "1.6.0" + "weixin-js-sdk": "1.6.0", + "xlsx": "^0.18.5" }, "devDependencies": { "@types/react": "^18.0.33", diff --git a/src/components/Common/CommonElementEvent.js b/src/components/Common/CommonElementEvent.js index 28600a9..a2cc6ca 100644 --- a/src/components/Common/CommonElementEvent.js +++ b/src/components/Common/CommonElementEvent.js @@ -2,6 +2,7 @@ /* eslint-disable array-callback-return,no-undef,prefer-destructuring */ import React, { Component } from 'react'; import { Modal, message } from 'antd-v4'; +import * as XLSX from 'xlsx'; import commonConfig from '../../utils/config'; import * as commonFunc from './commonFunc'; import * as commonBusiness from './commonBusiness'; /* 单据业务功能 */ @@ -4068,6 +4069,9 @@ export default (ChildComponent) => { } this.props.onSaveState({ masterData, masterConfig, customerInfoData, ...addState, contactData, enabled: sModelsType === 'system/sisformulaInfo' ? this.props.enabled : true, bUserModel: masterData.bUserModel, + }, () => { + this.masterChangeCb && this.masterChangeCb(); + this.masterChangeCb = null; }); }; handleChangeProductParteName = (sAllPartsName, sisproductclassifyProcessClassifyConfigNew) => { @@ -4640,6 +4644,78 @@ export default (ChildComponent) => { return newWin; } + // 导入模版功能 + handleImportExcel = () => { + const fileInput = document.createElement('input'); + fileInput.type = 'file'; + fileInput.accept = '.xlsx, .xls'; + fileInput.style.position = 'fixed'; + fileInput.style.left = '-9999px'; + document.body.appendChild(fileInput); + + fileInput.addEventListener('change', (e) => { + const file = e.target.files[0]; + if (file) { + if (!file) return; + + const reader = new FileReader(); + reader.readAsArrayBuffer(file); + reader.onload = (evt) => { + try { + const data = evt.target.result; + const workbook = XLSX.read(data, { type: 'binary' }); + const sColTitleName = []; + workbook.SheetNames.forEach((sheetName, i) => { + const sheet = workbook.Sheets[sheetName]; + const parsedData = XLSX.utils.sheet_to_json(sheet, { header: 1 }); + const [firstRow, ...restRows] = parsedData; + sColTitleName.push(firstRow.reduce((pre, item, index) => { + if (index === 0) { + pre.sName = item.replace('sName', ''); + } else if (item.endsWith('sName')) { + pre[`sName${index}`] = item.replace('sName', ''); + } else { + pre[`sValue${index}`] = item; + } + return pre; + }, { panelName: sheetName })); + }); + this.masterChangeCb = () => { + const addState = {}; + workbook.SheetNames.forEach((sheetName, i) => { + const sheet = workbook.Sheets[sheetName]; + const parsedData = XLSX.utils.sheet_to_json(sheet, { header: 1 }); + const [firstRow, ...restRows] = parsedData; + const tableName = `customizeParam${i || ''}`; + let { [`${tableName}Data`]: tableData = [], [`${tableName}DelData`]: tableDelData = [] } = this.props; + tableDelData = [...tableDelData, ...tableData.map(item => ({ ...item, handleType: 'del' }))]; + tableData = restRows.map(item => { + const columnKeys = Object.keys(sColTitleName[i]); + columnKeys.shift(); + return { ...this.handleTableAdd(tableName, true), ...item.reduce((pre, cur, index) => { + pre[columnKeys[index]] = cur; + return pre; + }, { sType: tableName }) }; + }); + addState[`${tableName}Data`] = tableData; + addState[`${tableName}DelData`] = tableDelData; + }); + this.props.onSaveState(addState); + }; + this.handleMasterChange('masterData', 'sColTitleName', { sColTitleName: JSON.stringify(sColTitleName) }, undefined, []); + } catch (error) { + console.log('=====err', error); + message.error('文件格式错误'); + } + }; + } + + document.body.removeChild(fileInput); + }); + + fileInput.click(); + } + /* 按钮点击功能 */ handleBtnClick = (e, btnName) => { if (btnName === 'BtnAccounts') { @@ -4648,8 +4724,10 @@ export default (ChildComponent) => { this.handleDesignFunction(); } else if (e === 'BtnResetpwd') { /* 管理员重置密码 */ this.handleResetPwd(); - } if (e === 'BtnOut') { + } else if (e === 'BtnOut') { this.handleOut(); + } else if (e === 'BtnImportExcel') { // 导入Excel模版 + this.handleImportExcel(); } // else if (e === 'BtnConfigCustomizeParam') { /* 生成变量设置 */ // const { masterData, customizeParamConfig } = this.props; -- libgit2 0.22.2