@wufengteam/wform
Version:
@wufengteam/wform
155 lines • 8.18 kB
JavaScript
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useState } from 'react';
import { Button, Upload, message } from 'antd';
import { wufengController } from '@wufengteam/core';
import { prefix } from '../../utils';
import { TEMPLATE_NAME } from '../utils/menu';
import { getDownloadUrl } from '../utils';
// @ts-ignore
import recognitionPng from '../img/recognition.png';
import "../../index.css";
var TextRecognitionPc = function TextRecognitionPc(props) {
var _a, _b;
var _props$idenTemplate = props.idenTemplate,
idenTemplate = _props$idenTemplate === void 0 ? '1' : _props$idenTemplate,
onChange = props.onChange,
value = props.value,
_props$fieldMapping = props.fieldMapping,
fieldMapping = _props$fieldMapping === void 0 ? {} : _props$fieldMapping,
_props$basicStatus = props.basicStatus,
basicStatus = _props$basicStatus === void 0 ? 1 : _props$basicStatus,
_props$disabled = props.disabled,
disabled = _props$disabled === void 0 ? false : _props$disabled;
var _ref = ((_a = props === null || props === void 0 ? void 0 : props.getEngineApis) === null || _a === void 0 ? void 0 : _a.call(props)) || {},
_ref$customEngineApi = _ref.customEngineApi,
customEngineApi = _ref$customEngineApi === void 0 ? {} : _ref$customEngineApi;
var _ref2 = props === null || props === void 0 ? void 0 : props.$$componentItem,
renderType = _ref2.renderType;
// 获取当前表单实例
var form = props === null || props === void 0 ? void 0 : props.form;
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
isHovering = _useState2[0],
setIsHovering = _useState2[1];
var uploadProps = {
action: '',
accept: '.jpg,.jpeg,.png,.pdf,.tiff,.swf,.gif',
beforeUpload: function beforeUpload(file) {
return new Promise(function (resolve) {
var _a;
message.loading('加载中...');
if (renderType === 'editor') {
wufengController.getAction('uploadSingleFile', {
file: file,
fileName: file.name
}).then(function (upRes) {
getDownloadUrl(upRes === null || upRes === void 0 ? void 0 : upRes.appId, upRes.fileId).then(function (url) {
var formData = new FormData();
formData.append('file', file);
wufengController.getAction('identifyWordsApi', {
formData: formData,
ocrType: idenTemplate
}).then(function (res) {
message.destroy();
if (res && typeof res === 'string') {
var obj = JSON.parse(res);
var formValues = {};
Object.entries(obj).forEach(function (_ref3) {
var _ref4 = _slicedToArray(_ref3, 2),
key = _ref4[0],
val = _ref4[1];
if (fieldMapping[key]) formValues[fieldMapping[key]] = val;
});
onChange === null || onChange === void 0 ? void 0 : onChange(url);
form.setFieldsValue(formValues);
}
});
});
});
} else {
(_a = customEngineApi === null || customEngineApi === void 0 ? void 0 : customEngineApi.uploadSingleFile) === null || _a === void 0 ? void 0 : _a.call(customEngineApi, {
file: file,
fileName: file.name
}).then(function (upRes) {
getDownloadUrl(upRes === null || upRes === void 0 ? void 0 : upRes.appId, upRes.fileId).then(function (url) {
var _a;
var formData = new FormData();
formData.append('file', file);
(_a = customEngineApi === null || customEngineApi === void 0 ? void 0 : customEngineApi.identifyWordsApi) === null || _a === void 0 ? void 0 : _a.call(customEngineApi, {
formData: formData,
ocrType: idenTemplate
}).then(function (res) {
message.destroy();
if (res && typeof res === 'string') {
var obj = JSON.parse(res);
var formValues = {};
Object.entries(obj).forEach(function (_ref5) {
var _ref6 = _slicedToArray(_ref5, 2),
key = _ref6[0],
val = _ref6[1];
if (fieldMapping[key]) formValues[fieldMapping[key]] = val;
});
onChange === null || onChange === void 0 ? void 0 : onChange(url);
form.setFieldsValue(formValues);
}
});
});
});
}
return resolve(Upload.LIST_IGNORE);
});
},
disabled: disabled || basicStatus === 3
};
/**
* 删除按钮事件
*/
var deleteClick = function deleteClick() {
if (onChange) onChange(undefined);
var formValues = {};
Object.entries(fieldMapping).forEach(function (_ref7) {
var _ref8 = _slicedToArray(_ref7, 2),
_key = _ref8[0],
val = _ref8[1];
formValues[val] = undefined;
});
form === null || form === void 0 ? void 0 : form.setFieldsValue(formValues);
};
return /*#__PURE__*/React.createElement("div", {
className: "".concat(prefix, "-recogn")
}, !value && ( /*#__PURE__*/React.createElement(Upload, Object.assign({}, uploadProps), /*#__PURE__*/React.createElement(Button, {
disabled: disabled || ((_b = props === null || props === void 0 ? void 0 : props.fieldAttr) === null || _b === void 0 ? void 0 : _b.includes('read')) || basicStatus === 3,
className: "".concat(prefix, "-recogn-btn")
}, /*#__PURE__*/React.createElement("img", {
src: recognitionPng,
alt: "",
className: "".concat(prefix, "-recogn-btn")
}), TEMPLATE_NAME[idenTemplate]))), value && ( /*#__PURE__*/React.createElement("div", {
className: "".concat(prefix, "-recogn-content")
}, /*#__PURE__*/React.createElement("img", {
src: value,
alt: "",
className: "".concat(prefix, "-recogn-img"),
onMouseMove: function onMouseMove() {
return setIsHovering(true);
}
}), isHovering && ( /*#__PURE__*/React.createElement("div", {
className: "".concat(prefix, "-recogn-mantle"),
onMouseLeave: function onMouseLeave() {
return setIsHovering(false);
}
}, /*#__PURE__*/React.createElement(Button, {
onClick: deleteClick
}, "\u5220\u9664"), /*#__PURE__*/React.createElement(Upload, Object.assign({}, uploadProps), /*#__PURE__*/React.createElement(Button, {
type: "primary",
style: {
marginTop: '12px'
}
}, "\u66F4\u6362")))))));
};
export default TextRecognitionPc;