UNPKG

wiz-frameworks

Version:

wizlong react framework

493 lines (405 loc) 18.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require("react"); var _react2 = _interopRequireDefault(_react); var _dva = require("dva"); var _wizLayoutTemplate = require("../wizLayoutTemplate"); var _wizLayoutTemplate2 = _interopRequireDefault(_wizLayoutTemplate); var _WizContainer2 = require("../WizContainer"); var _utils = require("../../utils"); var _propTypes = require("prop-types"); var _propTypes2 = _interopRequireDefault(_propTypes); var _store = require("store2"); var _store2 = _interopRequireDefault(_store); var _moment = require("moment"); var _moment2 = _interopRequireDefault(_moment); require("antd/dist/antd.css"); var _utils2 = require("./utils"); var _tool = require("../../tool"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /** * WizReduxTemplate 通用模版 * @author mll * @date 2019-10-15 */ var WizSpin = _tool.component_tool.WizSpin; var log = _tool.utils_tool.log, isType = _tool.utils_tool.isType; var WizReduxTemplate = function (_WizContainer) { _inherits(WizReduxTemplate, _WizContainer); function WizReduxTemplate(props) { _classCallCheck(this, WizReduxTemplate); var _this = _possibleConstructorReturn(this, (WizReduxTemplate.__proto__ || Object.getPrototypeOf(WizReduxTemplate)).call(this, props)); _initialiseProps.call(_this); _this.state = { params: {} }; _this.request = {}; //componentName:检查 var _componentName = _this.props.componentName; if (_componentName && _componentName !== "") { _this.componentName = _componentName; } else { log.error("WizReduxTemplate: 必须传入 容器名称(componentName)!"); } //基础数据检查 (0, _utils2._basic_check)(_this.props); //执行需要初始化的方法 var _this$props = _this.props, initApiKeys = _this$props.initApiKeys, keepInitApiParams = _this$props.keepInitApiParams, apis = _this$props.apis, apiDataFormat = _this$props.apiDataFormat; _this.buildOnchange = (0, _utils2._buildOnChange)(apiDataFormat); //构造参数变化的监控方法 _this.propsAndStateOnChange = _extends({}, (0, _utils2._buildOnChange)(apiDataFormat)); _this._initApis = {}; //Object.assign(this._initApis, apis) //备份初始化的apis的apiParams _this._backUpApis = {}; for (var key in apis) { var val = apis[key]; _this._backUpApis[key] = val["payload"]["apiParams"]; _this._initApis[key] = val["payload"]["apiParams"]; } return _this; } _createClass(WizReduxTemplate, [{ key: "_componentWillMount", value: function _componentWillMount() { var _props = this.props, apis = _props.apis, initApiKeys = _props.initApiKeys; (0, _utils2._doInitRequest)(apis, initApiKeys, this._doAction); } /** * 页面卸载时,根据备份的参数 初始化api */ }, { key: "componentWillUnmount", value: function componentWillUnmount() { var apis = this.props.apis; for (var key in this._backUpApis) { var val = this._backUpApis[key]; if (apis[key] && apis[key]["payload"]) { apis[key]["payload"]["apiParams"] = val; } } } /** * 过滤查询字段值为undefined&null&''的字段,不需要进行查询 * @param {Object} fieldsValue */ }, { key: "fitterFieldsValue", value: function fitterFieldsValue() { var fieldsValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var fitterValues = {}; Object.keys(fieldsValue).forEach(function (key) { var value = fieldsValue[key]; if (value !== null && value !== undefined && value !== "") { fitterValues[key] = value; } }); return fitterValues; } // 请求参数缓存方法 /** * 执行请求方法 * @param {*} key * @param {*} params */ /** * 获取loading状态 */ }, { key: "_render", value: function _render() { var _props2 = this.props, templateConfig = _props2.templateConfig, needLoading = _props2.needLoading; var loading = this.getLoading(); var isLoading = needLoading ? loading : false; if (isType(templateConfig, "Object")) { return _react2["default"].createElement( WizSpin, { spinning: isLoading }, _react2["default"].createElement(_wizLayoutTemplate2["default"], _extends({}, this.props, templateConfig, { params: this.state.params, doAction: this._doAction, loading: loading })) ); } else { log.error("WizReduxTemplate: 必须传入 templateConfig"); return null; } } }]); return WizReduxTemplate; }(_WizContainer2.WizContainer); var _initialiseProps = function _initialiseProps() { var _this2 = this; this.componentName = ""; this._formatValue = function (value) { var _item = void 0; for (var item in value) { if (Array.isArray(value[item]) && !value[item].length) { delete value[item]; } if (Object.prototype.toString.call(value[item]) === "[object Object]") { if (value[item]._SEARCHBAR) { _item = value[item]; value[item] = _item.value; } } } return value; }; this._filterValue = function () { var currentParams = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var cacheParams = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; for (var i in currentParams) { for (var j in cacheParams) { if (i === j) { if (cacheParams[j] && cacheParams[j]._SEARCHBAR && currentParams[i] && !currentParams[i]._SEARCHBAR) { delete currentParams[i]; } break; } } } return _extends({}, cacheParams, currentParams); }; this._formatParams = function () { var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var realValues = {}; // 真实值 作为接口入参 var cacheValues = {}; // 缓存值 作为缓存对象 var _item = void 0, _itemType = void 0; for (var item in params) { _item = params[item]; _itemType = Object.prototype.toString.call(_item); if (_itemType === "[object Object]") { if (_item._SEARCHBAR) { var _item2 = _item, value = _item2.value; realValues[item] = value; cacheValues[item] = _item; } else { // other... realValues[item] = cacheValues[item] = _item; } } else if (_itemType === "[object Array]") { if (_moment2["default"].isMoment(_item[0])) { realValues[item] = (0, _moment2["default"])(_item).format(); cacheValues[item] = _item; } else { realValues[item] = cacheValues[item] = _item; } } else { realValues[item] = cacheValues[item] = _item; } } return [realValues, cacheValues]; }; this._cache = function () { var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; var initApiKeys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; var _ACTIONKEY = params._ACTIONKEY; // if (params._ACTIONKEY !== "list") { // delete params._ACTIONKEY; // return [this._formatValue(this.fitterFieldsValue(params)), {}]; // } // delete params._ACTIONKEY; params._CACHE = true; // if (params._NOCACHE !== undefined || params._NOCACHE !== null) { // delete params[params._NOCACHE]; // delete params._NOCACHE; // } if (!params._CACHE) { delete params._CACHE; // return [this._formatValue(this.fitterFieldsValue(params)), {}]; } delete params._CACHE; var _realValue = void 0, _cacheValue = void 0; var _key = window.location.hash; var _searchMap = _store2["default"].session("searchMap") || []; var _index = _searchMap.findIndex(function (val) { return val.key === _key; }); if (_index !== -1) { // 判断是否有重置标识(CACHECLEAR) if (params._CACHECLEAR) { // 删除标识 delete params._CACHECLEAR; var keepInitApiParams = _this2.props.keepInitApiParams; if (keepInitApiParams) { _realValue = _cacheValue = params; } else { // 重置后 真实值与缓存值相等 _realValue = _cacheValue = _this2._formatParams(params)[0]; } } else { var _params = _this2._filterValue(params, _searchMap[_index].value[_ACTIONKEY]); var _formatParams = _this2._formatParams(_params), _formatParams2 = _slicedToArray(_formatParams, 2), realValue = _formatParams2[0], cacheValue = _formatParams2[1]; _realValue = realValue; _cacheValue = cacheValue; } _searchMap[_index].value[_ACTIONKEY] = _cacheValue; } else { var _formatParams3 = _this2._formatParams(params), _formatParams4 = _slicedToArray(_formatParams3, 2), _realValue2 = _formatParams4[0], _cacheValue2 = _formatParams4[1]; _realValue = _realValue2; _cacheValue = _cacheValue2; _searchMap.push({ key: _key, value: _defineProperty({}, _ACTIONKEY, _cacheValue) }); } if (initApiKeys.length && initApiKeys.filter(function (initApiKey) { return initApiKey === key; }).length) { _store2["default"].session("searchMap", _searchMap); } else if (key === 'list') { // list 为组件目前写死的search请求key _store2["default"].session("searchMap", _searchMap); } // 真实值过滤查询字段值为undefined&null&''&'null'的字段 return [_this2.fitterFieldsValue(_realValue), _cacheValue]; }; this._doAction = function (key) { var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var _props3 = _this2.props, apis = _props3.apis, keepInitApiParams = _props3.keepInitApiParams, initApiKeys = _props3.initApiKeys, cache = _props3.cache; var initParams = (0, _utils2._getInitApiParams)(key, _this2._initApis, keepInitApiParams); var _p = params; // for (const key in params) { // if (params[key] || params[key] == 0 || params[key] ==='') { // _p[key] = params[key] // } // } var _params = _extends({}, initParams, _p, { _ACTIONKEY: key }); var _cache = _this2._cache(_params, key, initApiKeys), _cache2 = _slicedToArray(_cache, 2), realValues = _cache2[0], cacheValues = _cache2[1]; var api = (0, _utils2._build_request)(key, cache ? realValues : _params, apis); if (api) { if (key === 'list' && cache) { _this2.setState({ params: _extends({}, cacheValues, { _SEARCHBARCACHE: true }) }); } _this2.doAction(api); } else { log.error("WizReduxTemplate: 未找到key为: " + key + " 的api,请检查apis!"); } }; this.getLoading = function () { var flag = false; var actionPath = _this2.props.actionPath; if (actionPath && isType(actionPath, "Array")) { actionPath.some(function (key) { var _key = key + "Loading"; if (_this2.props[_key]) { flag = true; return true; } }); } else { log.error("WizReduxTemplate: 获取loading状态失败!"); } return flag; }; }; var mapStateToProps = function mapStateToProps(state, ownProps) { var r = {}; var apis = ownProps.apis; var actionPath = []; if (apis) { for (var key in apis) { var api = apis[key]; var path = api["path"].split("/")[0]; if (actionPath.indexOf(path) === -1) { actionPath.push(path); } r[key] = _utils.modelUtils.getModelEntity(state[path], api["payload"]["key"]); } } var loading = _utils.modelUtils.getLoadingState(state, actionPath); return _extends({}, r, loading, { actionPath: actionPath, _datas: r }); }; exports["default"] = (0, _dva.connect)(mapStateToProps)(WizReduxTemplate); /** * 参数定义 * isRequired 是必传标示 * componentName:必传⭐️,容器名称 * apis:必传⭐️,数据请求参数 * initApiKeys:需要初始化调用的api的key * * 具体配置请看文档:sgm_template.md */ WizReduxTemplate.propTypes = { /** * 容器名称,必传⭐️ */ componentName: _propTypes2["default"].string.isRequired, /** * 数据请求参数,必传⭐️ * 格式:{ * list:{path,payload}, * search:{path,payload}, * } */ apis: _propTypes2["default"].object.isRequired, /** * 需要初始化调用的api的key */ initApiKeys: _propTypes2["default"].array, /** * 是否需要保留初始化参数,默认false 生效一次 */ keepInitApiParams: _propTypes2["default"].bool, /** * 对获取到的数据进行格式化 */ apiDataFormat: _propTypes2["default"].object, /** * 使用wizLayoutTemplate的配置 */ templateConfig: _propTypes2["default"].object, /** * 是否需要loading */ needLoading: _propTypes2["default"].bool, /** * 是否需要缓存,默认true */ cache: _propTypes2["default"].bool }; /** * 默认值 */ WizReduxTemplate.defaultProps = { needLoading: true, keepInitApiParams: false, cache: true }; module.exports = exports['default'];