UNPKG

@bos-alpha/data

Version:

数据管理

329 lines (328 loc) 20.4 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 }; } }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { useEffect, useRef, useState } from 'react'; import { Form, Input, Select, Radio, Button, message } from 'antd'; import styled from '@emotion/styled'; import store from '../../store'; import { ModalTitle, FormBox, FooterButtons, exitConfirm } from '../import-steps'; import { Icon, Modal } from '@bos-alpha/common'; import * as api from '@bos-alpha/common/lib/services'; import { AuthType } from '../auth-type'; import { BodyType } from '../body-type'; import { KeyValGenerator } from '../key-val-generator'; import { getAuthorization, getKvObject } from '../../utils'; import { getSiteAuth } from './queryConfig'; import { ShowMore } from '../show-more'; var Item = Form.Item; /** * 导入任务预认证配置 * @returns */ export var StepTwo = function () { var form = Form.useForm()[0]; var _a = store(), isEdit = _a.isEdit, siteId = _a.siteId, taskId = _a.taskId, authId = _a.authId, authRequred = _a.authRequred, nextStep = _a.nextStep, preStep = _a.preStep, setVisible = _a.setVisible, setState = _a.setState; // Digest Auth认证高级设置 弹窗 var digestAuthInfo = useRef({}); // body形式 用于编辑时界面更新 var _b = useState('raw'), initBodyType = _b[0], setInitBodyType = _b[1]; // 认证方式 用于编辑时界面更新 var _c = useState({ type: 'none' }), initAuthType = _c[0], setInitAuthType = _c[1]; // 是否验证通过 var _d = useState(false), testOk = _d[0], setTestOk = _d[1]; // 验证通过的配置 var authInfo = useRef({}); useEffect(function () { // 编辑任务认证 fetchTaskAuth(); }, []); // eslint-disable-line // 获取当前任务已绑定的认证信息 var fetchTaskAuth = function () { return __awaiter(void 0, void 0, void 0, function () { var res, resInfo, method, url, params, authorization, body, _a, headers_1, path, _b, _c, type, authRest, _d, headerPrefix, token, username, password, digestRest, bodyType, reqHeaders_1; return __generator(this, function (_e) { switch (_e.label) { case 0: if (!siteId) return [2 /*return*/]; return [4 /*yield*/, api.linkedQuery({ data: getSiteAuth(siteId) })]; case 1: res = _e.sent(); // 处理认证信息回显 if (res.count > 0) { resInfo = res.data[0].importAuths.params; authInfo.current = resInfo; setState({ authId: res.data[0].importAuths._key }); method = resInfo.method, url = resInfo.url, params = resInfo.params, authorization = resInfo.authorization, body = resInfo.body, _a = resInfo.headers, headers_1 = _a === void 0 ? {} : _a, path = resInfo.path; _b = authorization || {}, _c = _b.type, type = _c === void 0 ? 'none' : _c, authRest = __rest(_b, ["type"]); // OAuth2.0特殊处理 if (type === 'token') { _d = (authRest.token || '').split(' '), headerPrefix = _d[0], token = _d[1]; if (token) { authRest.headerPrefix = headerPrefix; authRest.token = token; } } else if (type === 'digest') { username = authRest.username, password = authRest.password, digestRest = __rest(authRest, ["username", "password"]); setState({ digestInfo: digestRest }); } setInitAuthType({ type: type }); bodyType = headers_1['Content-Type'] === 'multipart/form-data' ? 'form-data' : 'raw'; reqHeaders_1 = {}; Object.keys(headers_1).forEach(function (key) { if (key !== 'Content-Type') { reqHeaders_1[key] = headers_1[key]; } }); setInitBodyType(bodyType); form.setFieldsValue(__assign({ method: method, url: url, params: params, path: path, headers: reqHeaders_1, bodyType: bodyType, body: body, // 认证方式回填 authType: type }, authRest)); setTestOk(true); } else if (res.count === 0 && isEdit) { setState({ authRequred: false }); form.setFieldsValue({ authRequred: false }); } return [2 /*return*/]; } }); }); }; var onExit = function (type) { exitConfirm(__assign(__assign({}, (type === 'preStep' ? { title: '返回上一步', content: '未保存的配置将会丢失,确定返回上一步?' } : {})), { onOk: function () { if (type === 'preStep') { preStep(); } else { setVisible(false); } } })); }; // 测试连接 var onAuthTest = function () { return __awaiter(void 0, void 0, void 0, function () { var vals, method, url, params, bodyType, headers, body, path, authType, data, res, err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); return [4 /*yield*/, form.validateFields()]; case 1: vals = _a.sent(); console.log(vals); method = vals.method, url = vals.url, params = vals.params, bodyType = vals.bodyType, headers = vals.headers, body = vals.body, path = vals.path, authType = vals.authType; data = { method: method, url: url, authorization: getAuthorization(authType, vals, digestAuthInfo.current), params: getKvObject(params), headers: __assign(__assign({}, getKvObject(headers)), { 'Content-Type': bodyType === 'form-data' ? 'multipart/form-data' : 'application/json' }), bodyType: bodyType, // body是JSON字符串 body: body || '{}', path: path || '$.data.access_token' // 返回token所在路径,xml使用XPath,json使用JSONPath格式 }; setTestOk(false); return [4 /*yield*/, api.postSitesAuthTest(siteId, { data: data, params: { taskId: taskId, save: false } })]; case 2: res = _a.sent(); console.log(res); if (res.code !== 200) { return [2 /*return*/, Modal.error({ title: '连接失败,请检查URL路径或参数', content: _jsx(ShowMore, { children: res.message }, void 0) })]; } if (!res.token) { return [2 /*return*/, message.error('Token路径错误或认证失败')]; } // 暂存验证通过的配置 用于保存 authInfo.current = data; setTestOk(true); return [3 /*break*/, 4]; case 3: err_1 = _a.sent(); console.log(err_1); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }; // 保存 保存并下一步 保存的信息为校验通过的信息 var onSave = function (next) { return __awaiter(void 0, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!authRequred) return [3 /*break*/, 2]; return [4 /*yield*/, api.postSitesAuthTest(siteId, { data: __assign(__assign({}, authInfo.current), (authId ? { authId: authId } : {})), params: { taskId: taskId, save: true } })]; case 1: res = _a.sent(); console.log(res); setState({ authId: res.authId }); _a.label = 2; case 2: if (!(!authRequred && Object.keys(authInfo.current).length)) return [3 /*break*/, 4]; // 删除上一次认证数据 return [4 /*yield*/, api.deleteEntity('importAuths', authId, { params: { delRelation: true } })]; case 3: // 删除上一次认证数据 _a.sent(); // 重置authId 不然下一步API认证会设置authId setState({ authId: '' }); _a.label = 4; case 4: if (!next) message.success('保存成功'); next && nextStep(); return [2 /*return*/]; } }); }); }; return (_jsxs("div", { children: [_jsx(ModalTitle, __assign({ current: 1, onClose: function () { return onExit('close'); } }, { children: "\u7B2C\u4E09\u65B9\u5E73\u53F0API\u767B\u5F55\u8BA4\u8BC1\u4FE1\u606F" }), void 0), _jsx(FormBox, { children: _jsxs(Form, __assign({ form: form, labelCol: { span: 4 }, wrapperCol: { span: 20 }, labelAlign: "left", colon: false, onValuesChange: function (vals) { console.log('onValuesChange:', vals); setTestOk(false); } }, { children: [_jsx(Item, __assign({ name: "authRequred", label: "\u662F\u5426\u9700\u9884\u8BA4\u8BC1", initialValue: authRequred }, { children: _jsxs(Radio.Group, __assign({ onChange: function (e) { var values = form.getFieldsValue(); var fields = Object.keys(values).filter(function (item) { return item !== 'authRequred'; }); form.resetFields(fields); setState({ authRequred: e.target.value }); } }, { children: [_jsx(Radio, __assign({ value: true }, { children: "\u662F" }), void 0), _jsx(Radio, __assign({ value: false }, { children: "\u5426" }), void 0)] }), void 0) }), void 0), _jsxs(Tips, { children: [_jsx(Icon, { className: "tips-icon", type: "iconicon_explain" }, void 0), _jsx("span", { children: "\u5982\u9700\u9884\u8BA4\u8BC1\u65F6\uFF0C\u5148\u901A\u8FC7\u8BA4\u8BC1\u63A5\u53E3\u83B7\u53D6\u7528\u6237token\uFF0C\u5426\u5219\uFF0C\u5728\u8C03\u7528\u6570\u636E\u6E90\u63A5\u53E3\u65F6\u63D0\u4F9B\u8BA4\u8BC1\u4FE1\u606F\uFF0C\u5982\u7528\u6237\u540D\u3001\u5BC6\u7801\u7B49" }, void 0)] }, void 0), _jsx(Item, __assign({ name: "method", label: "\u8BA4\u8BC1\u63A5\u53E3\u7C7B\u578B", initialValue: "GET", rules: [ { required: true, message: '请选择认证接口类型' } ] }, { children: _jsx(Select, { disabled: !authRequred, options: [ { label: 'GET', value: 'GET' }, { label: 'POST', value: 'POST' } ] }, void 0) }), void 0), _jsx(Item, __assign({ name: "url", label: "\u8BA4\u8BC1\u63A5\u53E3URL", required: true, rules: [ { validator: function (rule, val) { if (!val || val.trim() === '') { return Promise.reject('请输入认证接口URL'); } else if (!/^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*)?/.test(val)) { return Promise.reject('请输入正确的URL链接'); } return Promise.resolve(); } } ] }, { children: _jsx(Input.TextArea, { disabled: !authRequred, placeholder: "\u8BF7\u8F93\u5165\u8BA4\u8BC1\u63A5\u53E3URL", allowClear: true }, void 0) }), void 0), _jsx(Item, __assign({ name: "params", label: "URL\u8DEF\u5F84\u53C2\u6570" }, { children: _jsx(KeyValGenerator, { disabled: !authRequred }, void 0) }), void 0), _jsx(AuthType, { initAuthType: initAuthType, disabled: !authRequred, onDigestSave: function (vals) { return (digestAuthInfo.current = vals); } }, void 0), _jsx(Item, __assign({ name: "headers", label: "headers" }, { children: _jsx(KeyValGenerator, {}, void 0) }), void 0), _jsx(BodyType, { initBodyType: initBodyType, disabled: !authRequred }, void 0), _jsx(Item, __assign({ name: "path", label: "Token\u8DEF\u5F84", initialValue: "$.data.access_token", rules: [ { required: true, message: '请输入Token路径' } ] }, { children: _jsx(Input, { disabled: !authRequred, placeholder: "xml\u4F7F\u7528XPath\uFF0Cjson\u4F7F\u7528JSONPath\u683C\u5F0F" }, void 0) }), void 0), _jsxs(Tips, { children: [_jsx(Icon, { className: "tips-icon", type: "iconicon_explain" }, void 0), _jsx("span", { children: "\u5728\u540E\u7EED\u6B65\u9AA4\u4E2D\uFF0C\u53EF\u4EE5\u4F7F\u7528\u201C{token}\u201D\u5F15\u7528\u6B64Token" }, void 0)] }, void 0)] }), void 0) }, void 0), _jsxs(FooterButtons, { children: [_jsx(Button, __assign({ className: "pre-step", type: "default", shape: "round", onClick: function () { return onExit('preStep'); } }, { children: "\u4E0A\u4E00\u6B65" }), void 0), _jsx(Button, __assign({ type: "primary", ghost: true, shape: "round", disabled: !authRequred, onClick: function () { return onAuthTest(); } }, { children: "\u6D4B\u8BD5\u8FDE\u63A5" }), void 0), _jsx(Button, __assign({ type: "primary", shape: "round", disabled: !testOk, onClick: function () { return onSave(); } }, { children: "\u4FDD\u5B58" }), void 0), _jsx(Button, __assign({ type: "primary", shape: "round", disabled: authRequred && !testOk, onClick: function () { return onSave(true); } }, { children: "\u4FDD\u5B58\u5E76\u4E0B\u4E00\u6B65" }), void 0)] }, void 0)] }, void 0)); }; var Tips = styled.div(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n position: relative;\n padding-left: 28px;\n padding-bottom: 24px;\n .tips-icon {\n color: #ff9b28;\n position: absolute;\n left: 0;\n font-size: 20px;\n }\n"], ["\n position: relative;\n padding-left: 28px;\n padding-bottom: 24px;\n .tips-icon {\n color: #ff9b28;\n position: absolute;\n left: 0;\n font-size: 20px;\n }\n"]))); var templateObject_1;