UNPKG

devfty

Version:

Devfty is a library for developer building low code factory

1,130 lines (1,079 loc) 39.4 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.devfty = {})); })(this, (function (exports) { 'use strict'; function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _typeof$1(o) { "@babel/helpers - typeof"; return _typeof$1 = "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$1(o); } function toPrimitive(t, r) { if ("object" != _typeof$1(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof$1(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function toPropertyKey(t) { var i = toPrimitive(t, "string"); return "symbol" == _typeof$1(i) ? i : i + ""; } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof$1(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _defineProperty(e, r, t) { return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } var CacheUtil = /*#__PURE__*/function () { function CacheUtil() { _classCallCheck(this, CacheUtil); _defineProperty(this, "cache", {}); } _createClass(CacheUtil, [{ key: "setCache", value: function setCache(key, value) { this.cache[key] = value; } }, { key: "getCache", value: function getCache(key) { var _this$cache; return (_this$cache = this.cache) === null || _this$cache === void 0 ? void 0 : _this$cache[key]; } }, { key: "clearCache", value: function clearCache(key) { delete this.cache[key]; } }]); return CacheUtil; }(); var EventUtil = /*#__PURE__*/function () { function EventUtil() { _classCallCheck(this, EventUtil); _defineProperty(this, "events", new Map()); } _createClass(EventUtil, [{ key: "on", value: function on(name, callback) { var _this$events, _this$events2; var fnList = (_this$events = this.events) === null || _this$events === void 0 ? void 0 : _this$events.get(name); if (!Array.isArray(fnList)) { fnList = []; } fnList.push(callback); (_this$events2 = this.events) === null || _this$events2 === void 0 ? void 0 : _this$events2.set(name, fnList); } }, { key: "execute", value: function execute(name) { var _this$events3; for (var _len = arguments.length, data = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { data[_key - 1] = arguments[_key]; } var fnList = (_this$events3 = this.events) === null || _this$events3 === void 0 ? void 0 : _this$events3.get(name); if (!Array.isArray(fnList)) { return; } fnList.forEach(function (fn) { if (typeof fn === 'function') { fn.apply(undefined, data); } }); } }, { key: "un", value: function un(name, callback) { var _this$events4, _this$events5; var fnList = (_this$events4 = this.events) === null || _this$events4 === void 0 ? void 0 : _this$events4.get(name); if (!Array.isArray(fnList)) { return; } var list = fnList.filter(function (fn) { return fn !== callback; }); (_this$events5 = this.events) === null || _this$events5 === void 0 ? void 0 : _this$events5.set(name, list); } }, { key: "clearEvents", value: function clearEvents() { var _this$events6; (_this$events6 = this.events) === null || _this$events6 === void 0 ? void 0 : _this$events6.clear(); } }]); return EventUtil; }(); function _createSuper$1(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct$1(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct$1() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } var BaseModel = /*#__PURE__*/function () { function BaseModel(args) { _classCallCheck(this, BaseModel); _defineProperty(this, "_data", {}); Object.assign(this._data, args); var cacheUtils = new CacheUtil(); var events = new EventUtil(); this._set_data('events', events); this._set_data('cache', cacheUtils); this._set_data('controls', []); } _createClass(BaseModel, [{ key: "_del_data", value: function _del_data(key) { if (!key) return; delete this._data[key]; } }, { key: "_cls_data", value: function _cls_data() { this._data = null; } }, { key: "_set_data", value: function _set_data(name, val) { this._data[name] = val; } }, { key: "_get_data", value: function _get_data(name) { return this._data[name]; } }, { key: "getState", value: function getState(key) { var controls = this._get_data('controls') || []; if ((controls === null || controls === void 0 ? void 0 : controls.length) > 0) { var _controls$, _controls$$getState; var val = (_controls$ = controls[0]) === null || _controls$ === void 0 ? void 0 : (_controls$$getState = _controls$.getState) === null || _controls$$getState === void 0 ? void 0 : _controls$$getState.call(_controls$, key); this._set_data('value', val); return val; } return this._get_data(key); } }, { key: "setState", value: function setState(key, val) { var controls = this._get_data('controls') || []; if ((controls === null || controls === void 0 ? void 0 : controls.length) > 0) { var _controls$2; (_controls$2 = controls[0]) === null || _controls$2 === void 0 ? void 0 : _controls$2.setState(key, val); } this._set_data(key, val); } }, { key: "get", value: function get(name) { return this._data[name]; } }, { key: "bindControl", value: function bindControl(ref) { this._set_data('controls', [ref]); } }, { key: "unbindControl", value: function unbindControl(ref) { this._set_data('controls', []); } }, { key: "getVisible", value: function getVisible() { return this.getState('visible'); } }, { key: "setVisible", value: function setVisible(v) { return this.setState('visible', !!v); } }, { key: "getValue", value: function getValue() { return this.getState('value'); } }, { key: "setValue", value: function setValue(v) { return this.setState('value', !!v); } }, { key: "on", value: function on(name, callback) { var _this$_get_data; (_this$_get_data = this._get_data('events')) === null || _this$_get_data === void 0 ? void 0 : _this$_get_data.on(name, callback); } }, { key: "execute", value: function execute(name) { var _this$_get_data2; for (var _len = arguments.length, data = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { data[_key - 1] = arguments[_key]; } (_this$_get_data2 = this._get_data('events')) === null || _this$_get_data2 === void 0 ? void 0 : _this$_get_data2.execute.apply(_this$_get_data2, [name].concat(data)); } }, { key: "un", value: function un(name, callback) { var _this$_get_data3; (_this$_get_data3 = this._get_data('events')) === null || _this$_get_data3 === void 0 ? void 0 : _this$_get_data3.un(name, callback); } }, { key: "setCache", value: function setCache(key, value) { var _this$_get_data4; (_this$_get_data4 = this._get_data('cache')) === null || _this$_get_data4 === void 0 ? void 0 : _this$_get_data4.setCache(key, value); } }, { key: "getCache", value: function getCache(key) { var _this$_get_data5; return (_this$_get_data5 = this._get_data('cache')) === null || _this$_get_data5 === void 0 ? void 0 : _this$_get_data5.getCache(key); } }, { key: "clearCache", value: function clearCache(key) { var _this$_get_data6; (_this$_get_data6 = this._get_data('cache')) === null || _this$_get_data6 === void 0 ? void 0 : _this$_get_data6.clearCache(key); } }]); return BaseModel; }(); var FilterModel = /*#__PURE__*/function (_BaseModel) { _inherits(FilterModel, _BaseModel); var _super = _createSuper$1(FilterModel); function FilterModel() { _classCallCheck(this, FilterModel); return _super.apply(this, arguments); } _createClass(FilterModel, [{ key: "mounted", value: function mounted() { var _this = this; var _conditions = this.get('conditions'); var params = {}; _conditions.forEach(function (r) { _this.get(r.name); params[r.name] = r.defaultValue; }); this._set_data('params', params); } }, { key: "unmounted", value: function unmounted() { this._set_data('params', {}); this._set_data('conditions', []); } }, { key: "getParams", value: function getParams() { var _this2 = this; var params = {}; var conditions = this.get('conditions'); conditions.forEach(function (r) { var model = _this2.get(r.name); params[r.name] = model === null || model === void 0 ? void 0 : model.getValue(); }); this._set_data('params', params); return params; } }, { key: "search", value: function search() { var params = this.getParams(); this.execute('search', params); } }]); return FilterModel; }(BaseModel); var SimpleModel = /*#__PURE__*/function (_BaseModel2) { _inherits(SimpleModel, _BaseModel2); var _super2 = _createSuper$1(SimpleModel); function SimpleModel(args) { _classCallCheck(this, SimpleModel); return _super2.call(this, args); } _createClass(SimpleModel, [{ key: "getDisabled", value: function getDisabled() { return this.getState('disabled'); } }, { key: "setDisabled", value: function setDisabled(v) { return this.setState('disabled', !!v); } }, { key: "getReadonly", value: function getReadonly() { return this.getState('readonly'); } }, { key: "setReadonly", value: function setReadonly(v) { this.setState('readonly', !!v); } }]); return SimpleModel; }(BaseModel); var ViewModel = /*#__PURE__*/function (_BaseModel3) { _inherits(ViewModel, _BaseModel3); var _super3 = _createSuper$1(ViewModel); function ViewModel() { _classCallCheck(this, ViewModel); return _super3.apply(this, arguments); } return _createClass(ViewModel); }(BaseModel); var models = { FilterModel: FilterModel, SimpleModel: SimpleModel, ViewModel: ViewModel }; var _excluded = ["component", "children", "render"]; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } var isInit = false; var components = {}; var renderFactory = null; function _initComponent(name, comp) { if (!name) return; if (typeof name === 'string') { if (!components[name]) { console.log("[warning] Component ".concat(name, " is exist!")); return; } components[name] = comp; return; } Object.assign(components, name); } function getComponent(name) { return components[name]; } function _initRenderFactory(fn) { renderFactory = fn; } function renderComponent(config) { if (!Array.isArray(config)) { return; } var _component = []; config.forEach(function (item, i) { if (!item.component) { return; } var _com = item.component, _children = item.children, _render = item.render, props = _objectWithoutProperties(item, _excluded); var component = getComponent(_com); var children = renderComponent(_children); var node = { component: component, children: children }; props.index = i; if (typeof _render === 'function') { _component.push(_render({ node: node, props: props })); } else if (typeof renderFactory === 'function') { _component.push(renderFactory({ node: node, props: props })); } else { Object.assign(node, props); _component.push(node); } }); return _component; } var Builder = /*#__PURE__*/function (_BaseModel) { _inherits(Builder, _BaseModel); var _super = _createSuper(Builder); function Builder() { _classCallCheck(this, Builder); return _super.apply(this, arguments); } _createClass(Builder, [{ key: "init", value: function init(fn) { if (isInit) return; if (typeof fn === 'function') { fn(); } isInit = true; } }, { key: "initComponent", value: function initComponent(name, comp) { _initComponent(name, comp); } }, { key: "initRenderFactory", value: function initRenderFactory(fn) { _initRenderFactory(fn); } }, { key: "initRequest", value: function initRequest() {} }, { key: "render", value: function render(config) { return renderComponent(config); } }]); return Builder; }(BaseModel); function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } var regeneratorRuntime$1 = {exports: {}}; var OverloadYield = {exports: {}}; (function (module) { function _OverloadYield(e, d) { this.v = e, this.k = d; } module.exports = _OverloadYield, module.exports.__esModule = true, module.exports["default"] = module.exports; }(OverloadYield)); var regenerator$1 = {exports: {}}; var regeneratorDefine = {exports: {}}; (function (module) { function _regeneratorDefine(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } module.exports = _regeneratorDefine = function regeneratorDefine(e, r, n, t) { if (r) i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n;else { var o = function o(r, n) { _regeneratorDefine(e, r, function (e) { return this._invoke(r, n, e); }); }; o("next", 0), o("throw", 1), o("return", 2); } }, module.exports.__esModule = true, module.exports["default"] = module.exports, _regeneratorDefine(e, r, n, t); } module.exports = _regeneratorDefine, module.exports.__esModule = true, module.exports["default"] = module.exports; }(regeneratorDefine)); (function (module) { var regeneratorDefine$1 = regeneratorDefine.exports; function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return regeneratorDefine$1(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (regeneratorDefine$1(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, regeneratorDefine$1(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, regeneratorDefine$1(u, "constructor", GeneratorFunctionPrototype), regeneratorDefine$1(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", regeneratorDefine$1(GeneratorFunctionPrototype, o, "GeneratorFunction"), regeneratorDefine$1(u), regeneratorDefine$1(u, o, "Generator"), regeneratorDefine$1(u, n, function () { return this; }), regeneratorDefine$1(u, "toString", function () { return "[object Generator]"; }), (module.exports = _regenerator = function _regenerator() { return { w: i, m: f }; }, module.exports.__esModule = true, module.exports["default"] = module.exports)(); } module.exports = _regenerator, module.exports.__esModule = true, module.exports["default"] = module.exports; }(regenerator$1)); var regeneratorAsync = {exports: {}}; var regeneratorAsyncGen = {exports: {}}; var regeneratorAsyncIterator = {exports: {}}; (function (module) { var OverloadYield$1 = OverloadYield.exports; var regeneratorDefine$1 = regeneratorDefine.exports; function AsyncIterator(t, e) { function n(r, o, i, f) { try { var c = t[r](o), u = c.value; return u instanceof OverloadYield$1 ? e.resolve(u.v).then(function (t) { n("next", t, i, f); }, function (t) { n("throw", t, i, f); }) : e.resolve(u).then(function (t) { c.value = t, i(c); }, function (t) { return n("throw", t, i, f); }); } catch (t) { f(t); } } var r; this.next || (regeneratorDefine$1(AsyncIterator.prototype), regeneratorDefine$1(AsyncIterator.prototype, "function" == typeof Symbol && Symbol.asyncIterator || "@asyncIterator", function () { return this; })), regeneratorDefine$1(this, "_invoke", function (t, o, i) { function f() { return new e(function (e, r) { n(t, i, e, r); }); } return r = r ? r.then(f, f) : f(); }, !0); } module.exports = AsyncIterator, module.exports.__esModule = true, module.exports["default"] = module.exports; }(regeneratorAsyncIterator)); (function (module) { var regenerator = regenerator$1.exports; var regeneratorAsyncIterator$1 = regeneratorAsyncIterator.exports; function _regeneratorAsyncGen(r, e, t, o, n) { return new regeneratorAsyncIterator$1(regenerator().w(r, e, t, o), n || Promise); } module.exports = _regeneratorAsyncGen, module.exports.__esModule = true, module.exports["default"] = module.exports; }(regeneratorAsyncGen)); (function (module) { var regeneratorAsyncGen$1 = regeneratorAsyncGen.exports; function _regeneratorAsync(n, e, r, t, o) { var a = regeneratorAsyncGen$1(n, e, r, t, o); return a.next().then(function (n) { return n.done ? n.value : a.next(); }); } module.exports = _regeneratorAsync, module.exports.__esModule = true, module.exports["default"] = module.exports; }(regeneratorAsync)); var regeneratorKeys = {exports: {}}; (function (module) { function _regeneratorKeys(e) { var n = Object(e), r = []; for (var t in n) r.unshift(t); return function e() { for (; r.length;) if ((t = r.pop()) in n) return e.value = t, e.done = !1, e; return e.done = !0, e; }; } module.exports = _regeneratorKeys, module.exports.__esModule = true, module.exports["default"] = module.exports; }(regeneratorKeys)); var regeneratorValues = {exports: {}}; var _typeof = {exports: {}}; (function (module) { function _typeof(o) { "@babel/helpers - typeof"; return module.exports = _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; }, module.exports.__esModule = true, module.exports["default"] = module.exports, _typeof(o); } module.exports = _typeof, module.exports.__esModule = true, module.exports["default"] = module.exports; }(_typeof)); (function (module) { var _typeof$1 = _typeof.exports["default"]; function _regeneratorValues(e) { if (null != e) { var t = e["function" == typeof Symbol && Symbol.iterator || "@@iterator"], r = 0; if (t) return t.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) return { next: function next() { return e && r >= e.length && (e = void 0), { value: e && e[r++], done: !e }; } }; } throw new TypeError(_typeof$1(e) + " is not iterable"); } module.exports = _regeneratorValues, module.exports.__esModule = true, module.exports["default"] = module.exports; }(regeneratorValues)); (function (module) { var OverloadYield$1 = OverloadYield.exports; var regenerator = regenerator$1.exports; var regeneratorAsync$1 = regeneratorAsync.exports; var regeneratorAsyncGen$1 = regeneratorAsyncGen.exports; var regeneratorAsyncIterator$1 = regeneratorAsyncIterator.exports; var regeneratorKeys$1 = regeneratorKeys.exports; var regeneratorValues$1 = regeneratorValues.exports; function _regeneratorRuntime() { var r = regenerator(), e = r.m(_regeneratorRuntime), t = (Object.getPrototypeOf ? Object.getPrototypeOf(e) : e.__proto__).constructor; function n(r) { var e = "function" == typeof r && r.constructor; return !!e && (e === t || "GeneratorFunction" === (e.displayName || e.name)); } var o = { "throw": 1, "return": 2, "break": 3, "continue": 3 }; function a(r) { var e, t; return function (n) { e || (e = { stop: function stop() { return t(n.a, 2); }, "catch": function _catch() { return n.v; }, abrupt: function abrupt(r, e) { return t(n.a, o[r], e); }, delegateYield: function delegateYield(r, o, a) { return e.resultName = o, t(n.d, regeneratorValues$1(r), a); }, finish: function finish(r) { return t(n.f, r); } }, t = function t(r, _t, o) { n.p = e.prev, n.n = e.next; try { return r(_t, o); } finally { e.next = n.n; } }), e.resultName && (e[e.resultName] = n.v, e.resultName = void 0), e.sent = n.v, e.next = n.n; try { return r.call(this, e); } finally { n.p = e.prev, n.n = e.next; } }; } return (module.exports = _regeneratorRuntime = function _regeneratorRuntime() { return { wrap: function wrap(e, t, n, o) { return r.w(a(e), t, n, o && o.reverse()); }, isGeneratorFunction: n, mark: r.m, awrap: function awrap(r, e) { return new OverloadYield$1(r, e); }, AsyncIterator: regeneratorAsyncIterator$1, async: function async(r, e, t, o, u) { return (n(e) ? regeneratorAsyncGen$1 : regeneratorAsync$1)(a(r), e, t, o, u); }, keys: regeneratorKeys$1, values: regeneratorValues$1 }; }, module.exports.__esModule = true, module.exports["default"] = module.exports)(); } module.exports = _regeneratorRuntime, module.exports.__esModule = true, module.exports["default"] = module.exports; }(regeneratorRuntime$1)); // TODO(Babel 8): Remove this file. var runtime = regeneratorRuntime$1.exports(); var regenerator = runtime; // Copied from https://github.com/facebook/regenerator/blob/main/packages/runtime/runtime.js#L736= try { regeneratorRuntime = runtime; } catch (accidentalStrictMode) { if (typeof globalThis === "object") { globalThis.regeneratorRuntime = runtime; } else { Function("r", "regeneratorRuntime = r")(runtime); } } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } /** * 公共请求类 * @param {*} options * @returns */ function XHR(options) { var xhr = new XMLHttpRequest(); if (options.times === undefined) { options.times = 3; } options.id = options.id || +new Date(); var url = options.url; var postData = _typeof$1(options.param) === 'object' ? JSON.stringify(options.param) : options.param || ''; if (!options.noNeedDpm) { url += "?d=".concat(options.id); } if (options.urlParams) { for (var index = 0; index < options.urlParams.length; index++) { var item = options.urlParams[index]; url += "&".concat(item.key, "=").concat(item.value); } } console.info("http[".concat(options.id, "] :: url = ").concat(url, ", postData=").concat(postData)); var param = options.param; var method = (options.method || 'GET').toUpperCase(); // GET POST var timeout = options.timeout || 5000; var type = options.type || 'text'; // text json var callback = options.callback; var done = false; xhr.onreadystatechange = function () { console.info("http[".concat(options.id, "] :: readyState = ").concat(xhr.readyState)); if (!done && xhr.readyState === 4) { console.info("http[".concat(options.id, "] :: header = ").concat(xhr.getAllResponseHeaders())); console.info("http[".concat(options.id, "] :: status = ").concat(xhr.status)); xhr.status === 404 || console.info("http[".concat(options.id, "] :: responseText = ").concat(xhr.responseText)); done = true; var error = null; if (xhr.status !== 200) { error = { code: xhr.status, text: xhr.statusText, toString: function toString() { return "".concat(this.code, ": ").concat(this.text); } }; } var ret = xhr.responseText; if (type.toLowerCase() === 'json') { try { ret = JSON.parse(ret); } catch (e) { error = { code: 555, text: 'json parse error', toString: function toString() { return "".concat(this.code, ": ").concat(this.text); } }; ret = null; } } if (error && options.times > 0) { options.times = options.times - 1; XHR(options); } else { var response; try { response = JSON.parse(ret); } catch (e) { console.info("callback parse error! err:".concat(JSON.stringify(e))); response = ret; } callback(response, error, xhr, options); } } }; xhr.open(method, url, true); if (method === 'POST') { xhr.setRequestHeader('Access-Control-Allow-Origin', '*'); xhr.setRequestHeader('Content-type', 'application/json'); // xhr.setRequestHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Connection, User-Agent, Cookie,Token') if (typeof param !== 'string') { param = JSON.stringify(param); } } else { param = null; } xhr.send(param); setTimeout(function () { if (!done) { done = true; xhr.abort(); if (options.times > 0) { options.times = options.times - 1; XHR(options); } else { var error = { code: -1001, text: 'Timeout', toString: function toString() { return "".concat(this.code, ": ").concat(this.text); } }; // const ret = xhr.responseText var ret = null; console.info("http[".concat(options.id, "] :: error").concat(JSON.stringify(error))); callback(ret, error, xhr, options); } } }, timeout); var cancel = function cancel() { done = true; try { xhr.abort(); } catch (e) { console.info("http[".concat(options.id, "] :: cancel exception")); } }; return cancel; } function Request(options) { return new Promise(function (resolve, reject) { XHR(_objectSpread(_objectSpread({}, options), {}, { callback: function callback(resp, err) { resolve(resp, err); } })); }); } var request = { get: function get(url, params, config) { var param = params || {}; var options = _objectSpread({ url: url, method: 'get', params: param }, config); return new Promise( /*#__PURE__*/function () { var _ref = _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee(resolve, reject) { var res; return regenerator.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: _context.next = 2; return Request(options); case 2: res = _context.sent; resolve((res === null || res === void 0 ? void 0 : res.data) || res); case 4: case "end": return _context.stop(); } }, _callee); })); return function (_x, _x2) { return _ref.apply(this, arguments); }; }()); }, getText: function getText(url, params, config) { var param = params || {}; var options = _objectSpread({ url: url, method: 'get', params: param }, config); return new Promise( /*#__PURE__*/function () { var _ref2 = _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee2(resolve, reject) { var res; return regenerator.wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return Request(options); case 2: res = _context2.sent; resolve(res); case 4: case "end": return _context2.stop(); } }, _callee2); })); return function (_x3, _x4) { return _ref2.apply(this, arguments); }; }()); }, post: function post(url, params, config) { var param = params || {}; var formData = parseToFormData(param); var options = _objectSpread({ url: url, method: 'post', data: formData }, config); return new Promise( /*#__PURE__*/function () { var _ref3 = _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee3(resolve, reject) { var res; return regenerator.wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return request(options); case 2: res = _context3.sent; resolve(res === null || res === void 0 ? void 0 : res.data); case 4: case "end": return _context3.stop(); } }, _callee3); })); return function (_x5, _x6) { return _ref3.apply(this, arguments); }; }()); } }; var index = { builder: new Builder(), utils: { cache: new CacheUtil(), event: new EventUtil(), models: models, request: request } }; exports["default"] = index; Object.defineProperty(exports, '__esModule', { value: true }); }));