UNPKG

@bos-model-alpha/data

Version:

数据管理

149 lines (148 loc) 9.87 kB
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) { if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } return cooked; }; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; import { useState } from 'react'; import { Modal, Input, Upload, Button, message } from 'antd'; import cookies from 'react-cookies'; import styled from '@emotion/styled'; import { read as xlsxRead } from 'xlsx'; import * as services from '@bos-alpha/common/lib/services'; import { downFile } from '@bos-alpha/common/lib/utils'; import importIcon from '../img-base64/data-import'; export var DataImport = function () { var _a = useState(false), visible = _a[0], setVisible = _a[1]; var _b = useState(), file = _b[0], setFile = _b[1]; var getTemp = function () { return __awaiter(void 0, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, services.getTemplate({ params: { name: '数据导入配置文件模板.xlsx', access_token: cookies.load('access_token') } })]; case 1: res = _a.sent(); downFile(res.data, '数据导入配置文件模板.xlsx'); return [2 /*return*/]; } }); }); }; // 确定 var onConfirm = function () { return __awaiter(void 0, void 0, void 0, function () { var reader; return __generator(this, function (_a) { if (!file || (file && !file.name)) return [2 /*return*/, message.error('请选择导入文件')]; if (file.name.indexOf('xls') === -1) return [2 /*return*/, message.error('选择文件格式不正确')]; reader = new FileReader(); reader.onload = function (e) { var data = e.target.result; var wb = xlsxRead(data, { type: 'binary' }) || {}; var className = Object.keys(wb.Sheets) .filter(function (key) { return key !== '导入数据'; }) .map(function (key) { return (wb.Sheets[key].B1 || {}).h; }) .join(','); Modal.confirm({ title: '导入数据', content: "\u6B64\u64CD\u4F5C\u5C06\u4F1A\u66F4\u65B0".concat(className, "\u6570\u636E\uFF0C\u662F\u5426\u786E\u8BA4\uFF1F"), okText: '确定', cancelText: '取消', okButtonProps: { shape: 'round', style: { width: '106px' } }, cancelButtonProps: { shape: 'round', style: { width: '106px' } }, onOk: function () { return new Promise(function (resolve, reject) { services .importFile({ data: { file: file } }) .then(function (res) { message.success('导入成功'); resolve(res); setVisible(false); }) .catch(function (err) { resolve(err); }); }); } }); }; reader.readAsBinaryString(file); return [2 /*return*/]; }); }); }; var onImportChange = function (file) { setFile(file); return false; }; return (_jsxs(_Fragment, { children: [_jsxs(Btn, __assign({ onClick: function () { return setVisible(true); } }, { children: [_jsx("img", { className: "icon", src: importIcon, alt: "" }), "\u5BFC\u5165\u6570\u636E"] })), _jsxs(Modal, __assign({ visible: visible, title: "\u5BFC\u5165\u6570\u636E", onCancel: function () { return setVisible(false); }, footer: null }, { children: [_jsxs(TempSelect, { children: [_jsx("div", { children: "\u9009\u62E9\u6570\u636E\u5BFC\u5165\u914D\u7F6E\u6587\u4EF6\uFF1A" }), _jsx(Input, { className: "file-ipt", type: "text", disabled: true, value: file === null || file === void 0 ? void 0 : file.name, placeholder: "\u8BF7\u9009\u62E9\u6570\u636E\u5BFC\u5165\u914D\u7F6E\u6587\u4EF6" }), _jsx(Upload, __assign({ accept: ".xls,.xlsx", showUploadList: false, beforeUpload: onImportChange }, { children: _jsx(Button, __assign({ type: "primary", shape: "round" }, { children: "\u9009\u62E9" })) }))] }), _jsx(DownloadButton, __assign({ type: "link", onClick: getTemp }, { children: "\u70B9\u51FB\u4E0B\u8F7D\u6570\u636E\u5BFC\u5165\u6A21\u677F" })), _jsxs(FooterBtns, { children: [_jsx(Button, __assign({ type: "default", shape: "round", className: "btn", onClick: function () { return setVisible(false); } }, { children: "\u53D6\u6D88" })), _jsx(Button, __assign({ type: "primary", shape: "round", className: "btn", onClick: onConfirm }, { children: "\u786E\u5B9A" }))] })] }))] })); }; var Btn = styled.span(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n margin: 0 16px;\n display: flex;\n color: #444;\n cursor: pointer;\n & > .icon {\n width: 20px;\n height: 20px;\n }\n"], ["\n margin: 0 16px;\n display: flex;\n color: #444;\n cursor: pointer;\n & > .icon {\n width: 20px;\n height: 20px;\n }\n"]))); var DownloadButton = styled(Button)(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n padding-left: 0;\n margin-top: 20px;\n"], ["\n padding-left: 0;\n margin-top: 20px;\n"]))); var TempSelect = styled.div(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n display: flex;\n align-items: center;\n & > .file-ipt {\n width: 240px;\n margin-right: 10px;\n }\n"], ["\n display: flex;\n align-items: center;\n & > .file-ipt {\n width: 240px;\n margin-right: 10px;\n }\n"]))); var FooterBtns = styled.div(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n display: flex;\n justify-content: center;\n padding-top: 40px;\n & > .btn {\n width: 106px;\n margin: 0 5px;\n }\n"], ["\n display: flex;\n justify-content: center;\n padding-top: 40px;\n & > .btn {\n width: 106px;\n margin: 0 5px;\n }\n"]))); var templateObject_1, templateObject_2, templateObject_3, templateObject_4;