@wufengteam/wform
Version:
@wufengteam/wform
412 lines (411 loc) • 21.3 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _antdMobile = require("antd-mobile-5");
var _icons = require("@ant-design/icons");
var _form = require("@lingxiteam/factory/lib/utils/form");
var _utils = require("../../utils");
var _tool = require("../../utils/tool");
var _util = require("../util");
require("./index.css");
var _constant = require("../constant");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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; }
/**
* api查询组件pc与app端差别不大,仅有两点
* 1.form的实例获取不一样(但是获取值和设置值的方式是一样的)
* 2.按钮以及下拉框的宽度不一致
*/
var ApiH5 = function ApiH5(props) {
var _a;
// eslint-disable-next-line max-len
var _props$apiObject = props.apiObject,
apiObject = _props$apiObject === void 0 ? {} : _props$apiObject,
_props$apiParams = props.apiParams,
apiParams = _props$apiParams === void 0 ? {} : _props$apiParams,
_props$apiRespond = props.apiRespond,
apiRespond = _props$apiRespond === void 0 ? {} : _props$apiRespond,
_props$apiOptions = props.apiOptions,
apiOptions = _props$apiOptions === void 0 ? {} : _props$apiOptions,
pageId = props.pageId,
buttonName = props.buttonName,
fieldAttr = props.fieldAttr,
renderType = props.renderType,
outsideDisabled = props.disabled;
var _useState = (0, _react.useState)([]),
_useState2 = _slicedToArray(_useState, 2),
selectData = _useState2[0],
setSelectData = _useState2[1];
var _useState3 = (0, _react.useState)(false),
_useState4 = _slicedToArray(_useState3, 2),
pickerVisible = _useState4[0],
setPickerVisible = _useState4[1];
var _useState5 = (0, _react.useState)(''),
_useState6 = _slicedToArray(_useState5, 2),
showValue = _useState6[0],
setShowValue = _useState6[1];
var _apiRespond$root = apiRespond.root,
root = _apiRespond$root === void 0 ? {
value: ''
} : _apiRespond$root,
_apiRespond$respond = apiRespond.respond,
respond = _apiRespond$respond === void 0 ? {} : _apiRespond$respond,
_apiRespond$rootIsArr = apiRespond.rootIsArray,
rootIsArray = _apiRespond$rootIsArr === void 0 ? false : _apiRespond$rootIsArr;
// H5获取当前表单实例
var _ref = (0, _react.useContext)(_form.DFormContext) || {},
form = _ref.form;
var disabled = renderType ? renderType === 'editor' : fieldAttr.includes('read') || outsideDisabled;
var _ref2 = ((_a = props === null || props === void 0 ? void 0 : props.getEngineApis) === null || _a === void 0 ? void 0 : _a.call(props)) || {},
_ref2$customEngineApi = _ref2.customEngineApi,
customEngineApi = _ref2$customEngineApi === void 0 ? {} : _ref2$customEngineApi;
/**
* 接口返参样例 {"capabilityCode":"shange_weatherInfo","apiCode":"shange_weatherInfo","serviceRequest":
* {"query":{"city":"11001","key":"4234b1472fd2b4982daaaf1c5958f1f2"}},"appId":"864338437506973696"}
*/
var callRhinEngine = function callRhinEngine(_ref3) {
var capabilityCode = _ref3.capabilityCode,
apiCode = _ref3.apiCode,
serviceRequest = _ref3.serviceRequest,
appId = _ref3.appId;
var _a;
(_a = customEngineApi === null || customEngineApi === void 0 ? void 0 : customEngineApi.callRhinEngine) === null || _a === void 0 ? void 0 : _a.call(customEngineApi, {
capabilityCode: capabilityCode,
apiCode: apiCode,
serviceRequest: serviceRequest,
appId: appId
}).then(function (res) {
var _a, _b, _c, _d, _e;
if ((0, _tool.isPlainObject)(res)) {
var data = (0, _util.getRespondDataHasArray)((res === null || res === void 0 ? void 0 : res.resultObject) || res, (res === null || res === void 0 ? void 0 : res.resultObject) && ((_a = root === null || root === void 0 ? void 0 : root.value) === null || _a === void 0 ? void 0 : _a.includes('resultObject')) ? root === null || root === void 0 ? void 0 : root.value.replace('resultObject.', '') : root === null || root === void 0 ? void 0 : root.value);
try {
if (Object.keys(data).length === 0) {
if ((_b = res === null || res === void 0 ? void 0 : res.resultObject) === null || _b === void 0 ? void 0 : _b.msg) {
_antdMobile.Toast.show({
icon: 'fail',
content: (_c = res === null || res === void 0 ? void 0 : res.resultObject) === null || _c === void 0 ? void 0 : _c.msg
});
}
}
} catch (error) {
console.log(error);
}
if (rootIsArray) {
// console.log('data', data);
if (!Array.isArray(data)) {
_antdMobile.Toast.show({
icon: 'fail',
content: '配置错误,请检查 api 查询配置。'
});
} else {
setSelectData(data.map(function (i, index) {
var _a;
return Object.assign(Object.assign({}, i), {
__id: index,
// 做一个值的转换,label去取设定的字段值
label: i[(_a = apiOptions === null || apiOptions === void 0 ? void 0 : apiOptions.options) === null || _a === void 0 ? void 0 : _a.title],
value: index
});
}));
// 默认展示第一个
var dataObj = {};
if (respond && JSON.stringify(respond) !== '{}') {
Object.keys(respond).forEach(function (key) {
var arr = key.split('root.');
var respondObj = (0, _util.getRespondData)(data[0], arr[1]);
dataObj[respond[key]] = respondObj;
});
}
if (form) {
form.setFieldsValue(dataObj);
}
setShowValue((_d = data[0]) === null || _d === void 0 ? void 0 : _d[(_e = apiOptions === null || apiOptions === void 0 ? void 0 : apiOptions.options) === null || _e === void 0 ? void 0 : _e.title]);
}
} else {
var _dataObj = {};
if (respond && JSON.stringify(respond) !== '{}') {
Object.keys(respond).forEach(function (key) {
var arr = key.split('root.');
var respondObj = (0, _util.getRespondData)(data, arr[1]);
_dataObj[respond[key]] = respondObj;
});
}
if (form) {
form.setFieldsValue(_dataObj);
}
}
}
});
};
var dealResult = function dealResult(res) {
var _a, _b, _c, _d, _e;
if ("".concat(res === null || res === void 0 ? void 0 : res.resultCode) === '0') {
if ((0, _tool.isPlainObject)(res)) {
console.log('res', res);
var data = (0, _util.getRespondDataHasArray)((res === null || res === void 0 ? void 0 : res.resultObject) || res, (res === null || res === void 0 ? void 0 : res.resultObject) && ((_a = root === null || root === void 0 ? void 0 : root.value) === null || _a === void 0 ? void 0 : _a.includes('resultObject')) ? root === null || root === void 0 ? void 0 : root.value.replace('resultObject.', '') : root === null || root === void 0 ? void 0 : root.value);
try {
if (Object.keys(data).length === 0) {
if ((_b = res === null || res === void 0 ? void 0 : res.resultObject) === null || _b === void 0 ? void 0 : _b.msg) {
_antdMobile.Toast.show({
icon: 'fail',
content: (_c = res === null || res === void 0 ? void 0 : res.resultObject) === null || _c === void 0 ? void 0 : _c.msg
});
}
}
} catch (error) {
console.log(error);
}
if (rootIsArray) {
if (!Array.isArray(data)) {
_antdMobile.Toast.show({
icon: 'fail',
content: '配置错误,请检查 api 查询配置。'
});
} else {
setSelectData(data.map(function (i, index) {
var _a;
return Object.assign(Object.assign({}, i), {
__id: index,
// 做一个值的转换,label去取设定的字段值
label: i[(_a = apiOptions === null || apiOptions === void 0 ? void 0 : apiOptions.options) === null || _a === void 0 ? void 0 : _a.title],
value: index
});
}));
// 默认展示第一个
var dataObj = {};
if (respond && JSON.stringify(respond) !== '{}') {
Object.keys(respond).forEach(function (key) {
var arr = key.split('root.');
var respondObj = (0, _util.getRespondData)(data[0], arr[1]);
dataObj[respond[key]] = respondObj;
});
}
if (form) {
form.setFieldsValue(dataObj);
}
setShowValue((_d = data[0]) === null || _d === void 0 ? void 0 : _d[(_e = apiOptions === null || apiOptions === void 0 ? void 0 : apiOptions.options) === null || _e === void 0 ? void 0 : _e.title]);
}
} else {
var _dataObj2 = {};
if (respond && JSON.stringify(respond) !== '{}') {
Object.keys(respond).forEach(function (key) {
var arr = key.split('root.');
var respondObj = (0, _util.getRespondData)(Array.isArray(data) && data[0] ? data[0] : data, arr[1]);
_dataObj2[respond[key]] = respondObj;
});
}
if (form) {
form.setFieldsValue(_dataObj2);
}
}
}
} else {
_antdMobile.Toast.show({
icon: 'fail',
content: res === null || res === void 0 ? void 0 : res.resultMsg
});
}
};
var callSqlService = function callSqlService(_ref4) {
var appId = _ref4.appId,
serviceCode = _ref4.serviceCode,
params = _ref4.params;
var _a;
(_a = customEngineApi === null || customEngineApi === void 0 ? void 0 : customEngineApi.callSqlService) === null || _a === void 0 ? void 0 : _a.call(customEngineApi, {
appId: appId,
pageId: pageId,
serviceCode: serviceCode,
params: params
}).then(function (res) {
dealResult(res);
});
};
var callInnerService = function callInnerService(params) {
var _a;
(_a = customEngineApi.callInnerService) === null || _a === void 0 ? void 0 : _a.call(customEngineApi, params).then(function (res) {
dealResult(res);
});
};
var callStdService = function callStdService(params) {
var _a;
(_a = customEngineApi === null || customEngineApi === void 0 ? void 0 : customEngineApi.callStdService) === null || _a === void 0 ? void 0 : _a.call(customEngineApi, params).then(function (res) {
dealResult(res);
});
};
var onConfirm = function onConfirm(value) {
try {
var da = selectData[value];
if (da) {
setShowValue(da === null || da === void 0 ? void 0 : da.label);
var dataObj = {};
Object.keys(respond).forEach(function (key) {
var arr = key.split('root.');
var respondObj = (0, _util.getRespondData)(da, arr[1]);
dataObj[respond[key]] = respondObj;
});
if (form) {
form.setFieldsValue(dataObj);
}
}
} catch (error) {
_antdMobile.Toast.show({
icon: 'fail',
content: '数据返回异常,请检查配置'
});
}
};
var onSubmit = function onSubmit() {
var _a, _b, _c;
if (disabled) {
return;
}
// 清空已有数据
var dataObj = {};
if (respond && JSON.stringify(respond) !== '{}') {
Object.keys(respond).forEach(function (key) {
dataObj[respond[key]] = undefined;
});
}
if (form) {
form.setFieldsValue(dataObj);
}
setShowValue('');
// 查询 API
// const { requestJson = {} } = apiObject as any;
// 这里要判断 requestJson 的值是在哪个对象中。
// 再匹配到 apiParams
var serviceRequest = {};
if (form) {
// const apiPramsObj = listToObj(apiParams?.params ?? []);
// console.log('apiPramsObj', apiPramsObj);
var formValues = form.getFieldsValue();
if ((apiObject === null || apiObject === void 0 ? void 0 : apiObject.tabKey) === _constant.SERVICE_KEY.RHIN) {
// 外部服务
if (apiParams && (apiParams === null || apiParams === void 0 ? void 0 : apiParams.params) && JSON.stringify(apiParams === null || apiParams === void 0 ? void 0 : apiParams.params) !== '{}') {
Object.keys(apiParams.params).forEach(function (key) {
if (apiParams.params[key] && Array.isArray(apiParams.params[key]) && apiParams.params[key].length > 0) {
var params = {};
apiParams.params[key].forEach(function (item) {
// 对象 、数组数据配置
if (item.children && Array.isArray(item.children) && item.children.length > 0) {
if (item.dataKeyType === 'object') {
// 对象
var childParams = {};
item.children.forEach(function (child) {
childParams[child.dataKey] = child.dataType === '1' ? child.dataValue : formValues[child === null || child === void 0 ? void 0 : child.dataValue];
});
params[item.dataKey] = childParams;
} else {
// 数组
var list = [];
if ((item === null || item === void 0 ? void 0 : item.children) && Array.isArray(item === null || item === void 0 ? void 0 : item.children) && (item === null || item === void 0 ? void 0 : item.children.length) > 0) {
// 数据源
var formData = formValues === null || formValues === void 0 ? void 0 : formValues[item.dataValue];
if (formData && Array.isArray(formData) && formData.length > 0) {
formData.forEach(function (data) {
var childParams = {};
item.children.forEach(function (child) {
childParams[child.dataKey] = child.dataType === '1' ? child.dataValue : data[child === null || child === void 0 ? void 0 : child.dataValue];
});
if (JSON.stringify(childParams) !== '{}') {
list.push(childParams);
}
});
}
}
params[item.dataKey] = list;
}
} else {
// 字段
params[item.dataKey] = item.dataType === '1' ? item.dataValue : formValues[item === null || item === void 0 ? void 0 : item.dataValue];
}
});
serviceRequest[key.replace('request', '').toLowerCase()] = params;
}
});
}
callRhinEngine({
capabilityCode: apiObject === null || apiObject === void 0 ? void 0 : apiObject.busiServiceNbr,
apiCode: apiObject === null || apiObject === void 0 ? void 0 : apiObject.busiServiceNbr,
serviceRequest: serviceRequest,
appId: window.appId
});
} else {
// 非外部服务
var serviceParams = {};
if (apiParams && (apiParams === null || apiParams === void 0 ? void 0 : apiParams.params) && ((_a = apiParams === null || apiParams === void 0 ? void 0 : apiParams.params) === null || _a === void 0 ? void 0 : _a.root) && Array.isArray((_b = apiParams === null || apiParams === void 0 ? void 0 : apiParams.params) === null || _b === void 0 ? void 0 : _b.root) && ((_c = apiParams.params) === null || _c === void 0 ? void 0 : _c.root.length) > 0) {
apiParams.params.root.forEach(function (param) {
serviceParams[param.dataKey] = param.dataType === '1' ? param.dataValue : formValues[param === null || param === void 0 ? void 0 : param.dataValue];
});
}
if ((apiObject === null || apiObject === void 0 ? void 0 : apiObject.tabKey) === _constant.SERVICE_KEY.INNER) {
// 请求层
var params = {
serviceMethod: apiObject === null || apiObject === void 0 ? void 0 : apiObject.serviceMethod,
servicePath: apiObject === null || apiObject === void 0 ? void 0 : apiObject.servicePath,
appId: window.appId,
serviceCode: apiObject === null || apiObject === void 0 ? void 0 : apiObject.busiServiceNbr,
params: Object.assign({}, serviceParams)
};
callInnerService(params);
} else if ((apiObject === null || apiObject === void 0 ? void 0 : apiObject.tabKey) === _constant.SERVICE_KEY.STD) {
// 编排
callStdService({
appId: window.appId,
serviceCode: apiObject === null || apiObject === void 0 ? void 0 : apiObject.busiServiceNbr,
pageId: pageId,
version: '1.0',
serviceVersionId: apiObject === null || apiObject === void 0 ? void 0 : apiObject.serviceVersionId,
parameters: Object.assign({}, serviceParams)
});
} else {
// sql
callSqlService({
appId: window.appId,
serviceCode: apiObject === null || apiObject === void 0 ? void 0 : apiObject.busiServiceNbr,
params: Object.assign({}, serviceParams)
});
}
}
}
};
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
className: "".concat(_utils.prefix, "-api-h5-wrap")
}, /*#__PURE__*/_react.default.createElement("div", {
onClick: function onClick() {
if (disabled) {
return;
}
if (rootIsArray) {
if (selectData.length > 0) {
setPickerVisible(true);
}
} else {
onSubmit();
}
},
className: showValue ? "".concat(_utils.prefix, "-select-value") : "".concat(_utils.prefix, "-placeholder")
}, rootIsArray ? showValue || buttonName : buttonName), /*#__PURE__*/_react.default.createElement(_icons.SearchOutlined, {
className: "".concat(_utils.prefix, "-search-wrap"),
onClick: onSubmit
})), /*#__PURE__*/_react.default.createElement(_antdMobile.Picker, {
title: "\u8BF7\u9009\u62E9",
columns: [selectData],
onConfirm: onConfirm,
visible: pickerVisible,
onClose: function onClose() {
return setPickerVisible(false);
}
}));
};
var _default = exports.default = ApiH5;