@bos-alpha/progress
Version:
进度管理
160 lines (159 loc) • 10.8 kB
JavaScript
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 } from "react/jsx-runtime";
import { useState } from 'react';
import { Radio, Input, Upload, message, Checkbox, Button, Progress } from 'antd';
import * as api from '@bos-alpha/common/lib/services';
import { prefixCls, cookieToken, Modal } from '@bos-alpha/common';
import styled from '@emotion/styled';
export var ImportModal = function (_a) {
var visible = _a.visible, onCancel = _a.onCancel, onOk = _a.onOk;
var _b = useState([]), fileList = _b[0], setFileList = _b[1];
// 模板类型
var _c = useState('project'), tplType = _c[0], setTplType = _c[1];
// 是否覆盖当前进度
var _d = useState(false), isOverRide = _d[0], setIsOverRide = _d[1];
// 上传进度
var _e = useState(0), uploadProgress = _e[0], setUploadProgress = _e[1];
// 选中文件
var beforeUpload = function (file) {
setFileList([file]);
return false;
};
var onImportWbs = function () {
var file = fileList[0];
var fileSuffixArr = file.name.split('.');
var fileType = fileSuffixArr[fileSuffixArr.length - 1];
// 如果选中的是project并且上传的文件是excel
var matchRes = (tplType === 'excel' && fileType === 'mpp') ||
(tplType === 'project' && fileType !== 'mpp') ||
(fileType !== 'mpp' && fileType !== 'xls' && fileType !== 'xlsx');
if (matchRes) {
return message.warning(tplType === 'excel' ? '请选择excel文件' : '请选择mmp文件');
}
api.uploadWbsFile({
data: {
file: file
},
params: {
clean: isOverRide
}
}, {
onUploadProgress: function (e) {
if (e.lengthComputable) {
setUploadProgress(Math.floor((e.loaded / e.total) * 100));
}
}
})
.then(function (res) {
if (res.code !== 'SUCCESS')
return message.error(res.message);
setUploadProgress(0);
setFileList([]);
onOk();
})
.finally(function () {
setUploadProgress(0);
});
};
var downloadTemplateFile = function () { return __awaiter(void 0, void 0, void 0, function () {
var name, res, blobURL, tempLink, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
name = tplType === 'excel'
? '导入进度文件模板.xlsx'
: '导入进度文件模板.mpp';
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, api.getTemplate({
params: {
// name: encodeURIComponent(name),
name: name,
access_token: cookieToken()
}
})];
case 2:
res = _a.sent();
console.log(res);
if (res.data) {
blobURL = window.URL
? window.URL.createObjectURL(res.data)
: window.webkitURL.createObjectURL(res.data);
tempLink = document.createElement('a');
tempLink.style.display = 'none';
tempLink.href = blobURL;
tempLink.setAttribute('download', name);
document.body.appendChild(tempLink);
tempLink.click();
document.body.removeChild(tempLink);
window.URL.revokeObjectURL(blobURL);
}
return [3 /*break*/, 4];
case 3:
err_1 = _a.sent();
console.log(err_1);
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); };
return (_jsx(Container, { children: _jsx(Modal, __assign({ className: "upload-progress-file", centered: true, title: "\u5BFC\u5165\u8FDB\u5EA6\u6587\u4EF6", visible: visible, okText: "\u5F00\u59CB\u5BFC\u5165", cancelText: "\u53D6\u6D88", width: 540, closable: false, keyboard: false, maskClosable: false, onOk: onImportWbs, cancelButtonProps: { className: 'cancel-btn' }, onCancel: onCancel, okButtonProps: { disabled: fileList.length !== 1 }, getContainer: false }, { children: _jsxs("div", { children: [_jsxs("div", __assign({ className: "row" }, { children: [_jsxs(Radio.Group, __assign({ onChange: function (e) { return setTplType(e.target.value); }, value: tplType }, { children: [_jsx(Radio, __assign({ value: 'project' }, { children: "project\u6587\u4EF6" }), void 0), _jsx(Radio, __assign({ value: 'excel' }, { children: "excel\u6587\u4EF6" }), void 0)] }), void 0), _jsx("div", __assign({ className: "tpl-download" }, { children: _jsxs("span", __assign({ className: "download-btn", onClick: downloadTemplateFile }, { children: ["\u70B9\u51FB\u4E0B\u8F7D\u8FDB\u5EA6\u6587\u4EF6\u6A21\u677F\uFF08", tplType, "\u6587\u4EF6\uFF09"] }), void 0) }), void 0)] }), void 0), _jsx("div", __assign({ className: "row" }, { children: _jsx(Upload, __assign({ accept: tplType === 'project' ? '.mpp' : '.xls,.xlsx', showUploadList: false, beforeUpload: beforeUpload, onChange: function (_a) {
var file = _a.file, fileList = _a.fileList;
console.log(file, fileList);
} }, { children: _jsxs("div", __assign({ className: "row" }, { children: [_jsx(Input, { readOnly: true, disabled: true, className: "ipt", value: fileList.length === 1
? fileList[0].name
: '', placeholder: "\u8BF7\u9009\u62E9\u8FDB\u5EA6\u6587\u4EF6" }, void 0), _jsx(Button, __assign({ type: "primary" }, { children: "\u9009\u62E9" }), void 0)] }), void 0) }), void 0) }), void 0), _jsx("div", __assign({ className: "row" }, { children: _jsx(Checkbox, __assign({ onChange: function (e) { return setIsOverRide(e.target.checked); }, checked: isOverRide }, { children: "\u6E05\u9664\u5F53\u524D\u8FDB\u5EA6\u540E\u5BFC\u5165" }), void 0) }), void 0), _jsx("div", __assign({ className: "row" }, { children: uploadProgress > 0 && (_jsx(Progress, { percent: uploadProgress, status: "active" }, void 0)) }), void 0)] }, void 0) }), void 0) }, void 0));
};
var Container = styled.div(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n .row {\n display: flex;\n padding-bottom: 20px;\n .ipt {\n width: 320px;\n }\n }\n .", "-modal-footer {\n text-align: center;\n .", "-btn {\n width: 100px;\n border-radius: 20px;\n }\n }\n .tpl-download {\n padding-left: 20px;\n & > .download-btn {\n color: #1890ff;\n cursor: pointer;\n }\n }\n"], ["\n .row {\n display: flex;\n padding-bottom: 20px;\n .ipt {\n width: 320px;\n }\n }\n .", "-modal-footer {\n text-align: center;\n .", "-btn {\n width: 100px;\n border-radius: 20px;\n }\n }\n .tpl-download {\n padding-left: 20px;\n & > .download-btn {\n color: #1890ff;\n cursor: pointer;\n }\n }\n"])), prefixCls, prefixCls);
var templateObject_1;