UNPKG

vue-ant-patterns

Version:

vue-ant-patterns

954 lines (856 loc) 29.2 kB
import _regeneratorRuntime from 'babel-runtime/regenerator'; import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator'; import _toConsumableArray from 'babel-runtime/helpers/toConsumableArray'; import _extends from 'babel-runtime/helpers/extends'; import _defineProperty from 'babel-runtime/helpers/defineProperty'; import { Steps, Button, Icon, Upload, Switch, Progress, Select, Spin } from 'ant-design-vue'; import DContainer from '../../es/d-container'; import PropTypes from 'ant-design-vue/es/_util/vue-types'; import { request } from '../_biz-utils'; import { uuid } from '../../es/_utils'; import LocaleReceiver from 'ant-design-vue/es/locale-provider/LocaleReceiver'; import EN from './locale/en_US'; import CN from './locale/zh_CN'; import Vue from 'vue'; var Horizontal = DContainer.Horizontal, Vertical = DContainer.Vertical; var Step = Steps.Step; var Option = Select.Option; var _globalJSConfig = (global || window).globalJSConfig, API_PREFIX = _globalJSConfig.API_PREFIX, API_HOST = _globalJSConfig.API_HOST; // 用到的接口 import { _exportSampleFile, _getBlob, _addBlob, _deleteBlob, _importExcelFile, _getPercent, _getTask, _getSetIdList, _getFileBolb, serviceTypeUrl } from './default'; var langType = { 'zh-cn': CN, 'en-us': EN }; var exportSampleFileUrl = null, getBlobUrl = null, addBlobUrl = null, deleteBlobUrl = null, importExcelFileUrl = null, getPercentUrl = null, getTaskUrl = null, getSetIdListUrl = null, getFileBolbUrl = null; var BImportExport = { name: 'BImportExport', props: { tmpCode: String, prefixCls: PropTypes.string.def('depot-import-export'), showSetId: PropTypes.bool.def(false), setIdLabel: PropTypes.string, // 表集合labelName setIdTableName: PropTypes.string, // 表集合 showCancelAndFinish: PropTypes.bool.def(false) // 是否显示取消按钮,弹框里用 }, data: function data() { var _ref; return _ref = { localeCode: 'zh-cn', current: 0, // steps: [{ title: '下载模板/上传文件' }, { title: '数据校验' }, { title: '导入完成' }], fileId: '', fileList: [], importNowDataChecked: false }, _defineProperty(_ref, 'fileList', []), _defineProperty(_ref, 'validate', 0), _defineProperty(_ref, 'percent', 0), _defineProperty(_ref, 'optionList', []), _defineProperty(_ref, 'blobIds', []), _defineProperty(_ref, 'timer', null), _defineProperty(_ref, 'taskId', null), _defineProperty(_ref, 'errorTemplate', ''), _defineProperty(_ref, 'setId', null), _defineProperty(_ref, 'importSuccessFlag', false), _defineProperty(_ref, 'taskTimer', null), _defineProperty(_ref, 'iconLoading', false), _ref; }, computed: { steps: function steps() { var localeCode = this.localeCode; return [{ title: langType[localeCode].stepOneName }, { title: langType[localeCode].stepTwoName }, { title: langType[localeCode].stepThreeName }]; } }, created: function created() { this.getServiceType(); }, mounted: function mounted() {}, methods: { /** * @description: 获取服务类型 * @param {*} * @return {*} */ getServiceType: function getServiceType() { var _this = this; var tmpCode = this.$props.tmpCode; request({ url: serviceTypeUrl, data: { templateCode: tmpCode } }).then(function (_ref2) { var data = _ref2.data; if (data.code !== 200) { return Promise.reject(data.msg); } var type = data.data; _this.setUrl(type); var showSetId = _this.$props.showSetId; if (showSetId) { _this.getSetIdsByTableName(); } })['catch'](function (e) { console.log(e); _this.$message.warning(e); }); }, /** * @description: 设置导入导出相关接口的路径 * @param {*} type: 各个模块的服务名 * @return {*} */ setUrl: function setUrl(type) { exportSampleFileUrl = API_HOST + type + '/' + API_PREFIX + _exportSampleFile; getBlobUrl = API_HOST + type + '/' + API_PREFIX + _getBlob; addBlobUrl = API_HOST + type + '/' + API_PREFIX + _addBlob; deleteBlobUrl = API_HOST + type + '/' + API_PREFIX + _deleteBlob; importExcelFileUrl = API_HOST + type + '/' + API_PREFIX + _importExcelFile; getPercentUrl = API_HOST + type + '/' + API_PREFIX + _getPercent; getTaskUrl = API_HOST + type + '/' + API_PREFIX + _getTask; getSetIdListUrl = API_HOST + type + '/' + API_PREFIX + _getSetIdList; getFileBolbUrl = API_HOST + type + '/' + API_PREFIX + _getFileBolb; }, /** * 获取默认语言 */ getDefaultLocale: function getDefaultLocale() { var result = { lang: _extends({}, CN) }; return result; }, // 下一步 next: function next() { var localeCode = this.localeCode, blobIds = this.blobIds, setId = this.setId; var showSetId = this.$props.showSetId; var localMsg = langType[localeCode]; if (this.current === 0) { if (showSetId && !setId) { // 提示需要选择表集合 this.$message.warning(localMsg.setIdWarnMsg); return; } else if (blobIds.length < 1) { // 提示需要上传文件 this.$message.warning(localMsg.uploadMsg); return; } else { this.checkValidate(); } } else if (this.current === 1) { if (this.timer) { clearInterval(this.timer); this.timer = null; } if (this.validate === 1) { // 校验成功后可以导入 this.importFile(); this.validate = 0; } else { this.$message.warning(localMsg.importMsg); return; } } this.current++; }, // 上一步 prev: function prev() { if (this.current === 1) { if (this.timer) { clearInterval(this.timer); this.timer = null; } this.percent = 0; this.validate = 0; } this.current--; }, // 取消,有弹框的取消事件 cancelEvent: function cancelEvent() { this.$emit('modalCancel'); }, // 完成 doneEvent: function doneEvent() { this.$emit('modalOk'); }, /** * @description: 通过文件流方式下载文件 * @param {type} * @return {type} */ getFileStream: function getFileStream(fileId) { var _this2 = this; request({ url: getFileBolbUrl, data: { fileId: fileId }, responseType: 'blob' }).then(function (res) { // let fileName = res.headers['content-disposition']; var fileName = window.decodeURI(res.headers['content-disposition'].split('=')[1], 'UTF-8'); var data = res.request.response; if (window.navigator.msSaveBlob) { // IE以及IE内核的浏览器 try { window.navigator.msSaveBlob(data, fileName); } catch (e) { console.log('msSaveBlob', e); } } else { var url = window.URL.createObjectURL(data); var link = document.createElement('a'); link.style.display = 'none'; link.href = url; link.setAttribute('download', fileName); // 文件名 document.body.appendChild(link); link.click(); document.body.removeChild(link); // 下载完成移除元素 window.URL.revokeObjectURL(url); // 释放掉blob对象 } _this2.iconLoading = false; })['catch'](function (e) { _this2.iconLoading = false; }); }, // 下载模板文件 downloadFile: function downloadFile() { var _this3 = this; var importNowDataChecked = this.importNowDataChecked, $props = this.$props; var tmpCode = $props.tmpCode; this.iconLoading = true; request({ url: exportSampleFileUrl, data: { templateCode: tmpCode, exportData: importNowDataChecked ? '1' : '0' // 0 不带数据, 1-带数据 } }).then(function (_ref3) { var data = _ref3.data; if (data.code === 200) { if (data.data && data.data.split('|')) { var fileId = data.data.split('|')[0]; var name = data.data.split('|')[1]; _this3.fileId = fileId; // 不用a标签下载,在ie下不支持,换blob方式 _this3.getFileStream(fileId); // const url = `${getBlobUrl}?fileId=${fileId}`; // let link = document.createElement('a'); // link.style.display = 'none'; // link.href = url; // link.setAttribute('download', name); // 文件名 // document.body.appendChild(link); // link.click(); // document.body.removeChild(link); // 下载完成移除元素 } } else { _this3.iconLoading = false; _this3.$message.error(data.msg); } })['catch'](function (e) { _this3.iconLoading = false; }); }, // 选择表结合数据 handleSetIdsChange: function handleSetIdsChange(value) { this.setId = value; }, // 是否导入现有数据开关 onChangeSwitch: function onChangeSwitch(checked) { this.importNowDataChecked = checked; }, // 渲染第一步 renderFirstStep: function renderFirstStep() { var _this4 = this; var h = this.$createElement; var fileList = this.fileList, localeCode = this.localeCode, optionList = this.optionList, $props = this.$props, setId = this.setId; var setIdLabel = $props.setIdLabel, showSetId = $props.showSetId; var headersObj = {}; var token = Vue.ils.get('token'); var userId = Vue.ils.get('userId'); if (token) { headersObj.Authorization = 'Bearer ' + token; } if (userId) { headersObj.userId = userId; } var uploadProps = { props: { name: 'file', accept: '.xlsx, .xls, .csv', action: addBlobUrl, headers: headersObj, fileList: fileList, remove: function remove(file) { if (file && file.response.code === 200) { var blobId = file.response.data[0]; request({ url: deleteBlobUrl, data: { blobId: blobId } }).then(function (_ref4) { var data = _ref4.data; _this4.$message.success('删除成功'); _this4.blobIds = []; }); } } }, on: { change: function change(info) { _this4.taskId = null; _this4.blobIds = []; // 先清空 var file = info.file; info.fileList = info.fileList.slice(-1); if (file.status === 'done') { var _fileList = [].concat(_toConsumableArray(info.fileList)); _fileList = _fileList.map(function (file) { if (file.response && file.response.data) { var blobId = file.response.data[0]; _this4.blobIds.push(blobId); file.url = getBlobUrl + '?fileId=' + blobId; file.linkProps = { download: file.name }; } return file; }); } else if (file.status === 'error') { _this4.$message.error('上传失败'); } _this4.fileList = info.fileList; } } }; var localMsg = langType[localeCode]; return h('ul', [h( 'li', { 'class': 'steps-li' }, [h( 'p', { 'class': 'first-step-name' }, [localMsg.stepOne] ), h( Button, { on: { 'click': this.downloadFile }, attrs: { loading: this.iconLoading } }, [localMsg.downTemplate] ), h( 'span', { 'class': 'first-step-exported' }, [localMsg.exportData, ' '] ), h(Switch, { attrs: { checked: this.importNowDataChecked }, on: { 'change': this.onChangeSwitch } })] ), h( 'li', { 'class': 'steps-li' }, [h( 'p', { 'class': 'first-step-name' }, [localMsg.stepTwo] ), h( 'p', { 'class': 'first-step-desc' }, [localMsg.uploadTip] ), showSetId ? h( 'div', { 'class': 'step-set-box margin-top-xs' }, [h( 'span', { 'class': 'step-name' }, [setIdLabel, ': '] ), h( Select, { 'class': 'step-set-select', attrs: { value: setId }, on: { 'change': this.handleSetIdsChange } }, [optionList.map(function (item) { return h( Option, { key: item.id }, [item.name] ); })] )] ) : '', h( Upload, uploadProps, [h( Button, { attrs: { type: 'primary' } }, [h(Icon, { attrs: { type: 'upload' } }), ' ', localMsg.upload] )] )] )]); }, // 渲染第二步 renderSecondStep: function renderSecondStep() { var h = this.$createElement; var localeCode = this.localeCode, errorTemplate = this.errorTemplate; var localMsg = langType[localeCode]; return this.validate === 0 ? h( Horizontal, { 'class': 'second-step-container flex flex-center-center second-step-progress' }, [h( 'p', { 'class': 'second-step-progress-txt' }, [localMsg.verifyIng] ), h(Progress, { attrs: { percent: this.percent } })] ) : this.validate === 1 ? h( Horizontal, { 'class': 'second-step-container flex flex-center-center' }, [h(Icon, { attrs: { type: 'check-circle', theme: 'filled' }, style: { fontSize: '36px', color: '#66bb3d' } }), h( 'h2', { 'class': 'third-step-info' }, [localMsg.verifySuccess] )] ) : h( Vertical, { 'class': 'second-step-container flex flex-center-center' }, [h( Horizontal, { 'class': 'margin-bottom-lg' }, [h(Icon, { attrs: { type: 'close-circle', theme: 'filled' }, style: { fontSize: '36px', color: '#ED5A50' } }), h( 'h2', { 'class': 'third-step-info' }, [localMsg.verifyFailed] )] ), h( 'div', { 'class': 'second-step-error-tips' }, [errorTemplate] )] ); }, // 渲染第三步 renderThirdStep: function renderThirdStep() { var h = this.$createElement; var localeCode = this.localeCode; var localMsg = langType[localeCode]; return this.importSuccessFlag ? h( Horizontal, { 'class': 'second-step-container flex flex-center-center' }, [h(Icon, { attrs: { type: 'check-circle', theme: 'filled' }, style: { fontSize: '36px', color: '#66bb3d' } }), h( 'h2', { 'class': 'third-step-info' }, [localMsg.importSuccess] )] ) : h( Horizontal, { 'class': 'third-step-container flex-center-center' }, [h(Spin, { attrs: { tip: 'Loading...' } })] ); }, // 渲染步骤条内容 renderStepContent: function renderStepContent() { var current = this.current; if (current === 0) { return this.renderFirstStep(); } else if (current === 1) { return this.renderSecondStep(); } else if (current === 2) { return this.renderThirdStep(); } else {} }, // 获取表结合列表 getSetIdsByTableName: function getSetIdsByTableName() { var _this5 = this; var setIdTableName = this.$props.setIdTableName; request({ url: getSetIdListUrl, data: { tableName: setIdTableName } }).then(function (_ref5) { var data = _ref5.data; if (data.code === 200) { _this5.optionList = data.data; } }); }, // 校验 checkValidate: function () { var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var _this6 = this; var uuidV, paramData, setId, paramsData, resData, _resData$data, code, data; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: uuidV = uuid(); paramData = {}; setId = this.setId; // 如果有表集合 if (setId) { paramData = { setid: setId }; } paramsData = { url: importExcelFileUrl, data: { checkExcel: '1', // 检查 uuid: uuidV, templateCode: this.$props.tmpCode, params: paramData, fileIds: this.blobIds } }; _context.next = 7; return request(paramsData); case 7: resData = _context.sent; _resData$data = resData.data, code = _resData$data.code, data = _resData$data.data; this.taskId = data; if (code === 200) { this.timer = setInterval(function () { _this6.getPercent(uuidV); }, 500); } case 11: case 'end': return _context.stop(); } } }, _callee, this); })); function checkValidate() { return _ref6.apply(this, arguments); } return checkValidate; }(), // 获取百分比 getPercent: function () { var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(uuidV) { var _this7 = this; var paramsData, resData, _resData$data2, code, data, msg; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: paramsData = { url: getPercentUrl, data: { uuid: uuidV, templateCode: this.$props.tmpCode, param: {} } }; _context2.next = 3; return request(paramsData); case 3: resData = _context2.sent; _resData$data2 = resData.data, code = _resData$data2.code, data = _resData$data2.data, msg = _resData$data2.msg; if (code === 200) { if (data === 100) { this.clearTimer(this.timer); // 这里特殊处理,不真正置为100,因为此时getTask返回的status可能还没完成, // 待getTask返回状态完成时候,再置为100, this.percent = 98; this.taskTimer = setInterval(function () { _this7.getTask('check'); }, 1000); } else if (data === 0) { // 为了特殊处理第一次校验后台比较慢的问题,前端一点点变化 if (data <= this.percent && this.percent < 30) { this.percent++; } } } else { this.clearTimer(this.timer); } case 6: case 'end': return _context2.stop(); } } }, _callee2, this); })); function getPercent(_x) { return _ref7.apply(this, arguments); } return getPercent; }(), // 获取校验结果 /** * * @param {类型} type * 'import': 导入类型 * 'check': 校验类型 */ getTask: function () { var _ref8 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(type) { var _this8 = this; var h, paramsData, resData, _resData$data3, code, data, localeCode, localMsg, status, fileName, url, fileId; return _regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: h = this.$createElement; paramsData = { url: getTaskUrl, data: { taskId: this.taskId } }; _context3.prev = 2; _context3.next = 5; return request(paramsData); case 5: resData = _context3.sent; _resData$data3 = resData.data, code = _resData$data3.code, data = _resData$data3.data; if (code === 200) { localeCode = this.localeCode; localMsg = langType[localeCode]; status = data[0].status.code; // 校验 if (type !== 'import') { if (status === 'done') { this.percent = 100; this.validate = 1; // 校验成功 this.clearTimer(this.taskTimer); } else if (status === 'exception') { this.percent = 100; this.clearTimer(this.taskTimer); if ((data[0].type === 'import' || data[0].type === '导入') && data[0].fileId) { fileName = data[0].fileName.split('|')[1]; url = getBlobUrl + '?fileId=' + data[0].fileId; fileId = data[0].fileId; // let link = document.createElement('a'); // link.href = url; // link.setAttribute('download', fileName); // 文件名 this.errorTemplate = h('span', [localMsg.pleaseInfo, h( 'a', { attrs: { href: 'javascript:void(0)' }, on: { 'click': function click() { _this8.downloadErrorFile(fileId); } } }, [fileName] ), localMsg.seeMoreError]); this.validate = 2; } else { this.errorTemplate = h('span', [data[0].msg]); this.validate = 2; } } else {} } else { // 导入 if (status === 'done') { this.clearTimer(this.taskTimer); this.importSuccessFlag = true; } else if (status === 'exception') { this.clearTimer(this.taskTimer); this.importSuccessFlag = false; } else {} } } _context3.next = 13; break; case 10: _context3.prev = 10; _context3.t0 = _context3['catch'](2); this.clearTimer(this.taskTimer); case 13: case 'end': return _context3.stop(); } } }, _callee3, this, [[2, 10]]); })); function getTask(_x2) { return _ref8.apply(this, arguments); } return getTask; }(), downloadErrorFile: function downloadErrorFile(fileId) { this.getFileStream(fileId); }, // 清空timer计时器 clearTimer: function clearTimer(timer) { clearInterval(timer); timer = null; }, // 导入文件 importFile: function () { var _ref9 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() { var _this9 = this; var paramData, setId, paramsData, resData, _resData$data4, code, data, msg; return _regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: paramData = {}; setId = this.setId; // 如果有表集合 if (setId) { paramData = { setid: setId }; } paramsData = { url: importExcelFileUrl, data: { checkExcel: '2', // 导入 templateCode: this.$props.tmpCode, params: paramData, fileIds: this.blobIds } }; _context4.next = 6; return request(paramsData); case 6: resData = _context4.sent; _resData$data4 = resData.data, code = _resData$data4.code, data = _resData$data4.data, msg = _resData$data4.msg; // console.log(resData.data); if (code === 200) { this.taskId = data; this.taskTimer = setInterval(function () { _this9.getTask('import'); }, 1000); } case 9: case 'end': return _context4.stop(); } } }, _callee4, this); })); function importFile() { return _ref9.apply(this, arguments); } return importFile; }(), renderComponent: function renderComponent(contextLocale, locale) { var _this10 = this; var h = this.$createElement; this.localeCode = locale; var localMsg = langType[locale]; var current = this.current, steps = this.steps, $props = this.$props; var showCancelAndFinish = $props.showCancelAndFinish; return h('div', [h( Steps, { attrs: { current: current } }, [steps.map(function (item) { return h(Step, { key: item.title, attrs: { title: item.title } }); })] ), h( 'div', { 'class': 'steps-content' }, [this.renderStepContent()] ), h( Horizontal, { attrs: { grow: true }, 'class': 'flex-justify-end steps-action' }, [showCancelAndFinish && h( Button, { style: { marginLeft: 8 }, on: { 'click': function click() { return _this10.cancelEvent(); } } }, [localMsg.cancel] ), current > 0 && h( Button, { style: { marginLeft: 8 }, on: { 'click': function click() { return _this10.prev(); } } }, [localMsg.prevStep] ), current < steps.length - 1 && h( Button, { attrs: { type: 'primary' }, on: { 'click': function click() { return _this10.next(); } } }, [localMsg.nextStep] ), showCancelAndFinish && current === steps.length - 1 && h( Button, { attrs: { type: 'primary' }, on: { 'click': function click() { return _this10.doneEvent(); } } }, [localMsg.finish] )] )]); } }, beforeDestroy: function beforeDestroy() { this.clearTimer(this.timer); this.clearTimer(this.taskTimer); }, render: function render() { var h = arguments[0]; return h(LocaleReceiver, { attrs: { componentName: 'bImportExport', defaultLocale: this.getDefaultLocale }, scopedSlots: { 'default': this.renderComponent } }); } }; BImportExport.install = function (Vue) { Vue.component(BImportExport.name, BImportExport); }; export default BImportExport;