sunmao-sdk
Version:
榫卯-开箱即用赋能-sdk
223 lines (174 loc) • 6.05 kB
JavaScript
;
function _typeof2(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof2 = function _typeof2(obj) { return typeof obj; }; } else { _typeof2 = function _typeof2(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof2(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.filterParams = filterParams;
exports.getAclPermission = getAclPermission;
exports.parseFunctionValue = exports.getDate = exports.getDateTime = exports.buildSchema = exports.isObj = void 0;
var _moment = _interopRequireDefault(require("moment"));
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (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 = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(source, true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(source).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
return target;
}
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 _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance");
}
function _iterableToArray(iter) {
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) {
arr2[i] = arr[i];
}
return arr2;
}
}
function _typeof(obj) {
if (typeof Symbol === "function" && _typeof2(Symbol.iterator) === "symbol") {
_typeof = function _typeof(obj) {
return _typeof2(obj);
};
} else {
_typeof = function _typeof(obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : _typeof2(obj);
};
}
return _typeof(obj);
}
function stringContains(str, text) {
return str.indexOf(text) > -1;
}
var isObj = function isObj(a) {
return stringContains(Object.prototype.toString.call(a), "Object");
};
exports.isObj = isObj;
var isApiString = function isApiString(str) {
if (typeof str !== "string") return false;
if (str.indexOf("$api.") === 0 || str.indexOf("$func.") === 0) return true;
return false;
};
var getApiName = function getApiName(str) {
if (str.indexOf("$api.") === 0) return str.substring(5);
if (str.indexOf("$func.") === 0) return str.substring(6);
return "";
};
var buildSchema = function buildSchema(schema, api) {
if (_typeof(schema) !== "object" || schema === null) {
if (isApiString(schema)) {
var apiName = getApiName(schema);
if (api && api[apiName] && typeof api[apiName] === "function") {
return api[apiName];
} else {
return function () {
apiName && console.error("\u6CA1\u6709\u627E\u5230\u5339\u914D\u7684\u51FD\u6570: ".concat(apiName));
};
}
}
return schema;
}
if (Array.isArray(schema)) {
var _result = _toConsumableArray(schema);
return _result.map(function (item) {
return buildSchema(item, api);
});
} // 剩下是 result 是对象的情况
var result = _objectSpread({}, schema);
var keys = Object.keys(result);
keys.forEach(function (key) {
result[key] = buildSchema(result[key], api);
});
return result;
};
exports.buildSchema = buildSchema;
var getDateTime = function getDateTime(time) {
return (0, _moment["default"])(time).format("YY/MM/DD HH:mm");
};
exports.getDateTime = getDateTime;
var getDate = function getDate(time) {
return (0, _moment["default"])(time).format("YY/MM/DD");
}; // 如果是函数,则解析,如果不是,直接返回值
exports.getDate = getDate;
var parseFunctionValue = function parseFunctionValue(value, params, cb) {
var _value = value;
if (typeof _value === "function") {
_value = _value(params);
} else {
cb && typeof cb === "function" && cb();
}
return _value;
};
/**
* 过滤 空 属性
* 这个方法不会影响原来的对象,而是返回一个新对象
*/
exports.parseFunctionValue = parseFunctionValue;
function filterParams(obj) {
var _newPar = {};
for (var key in obj) {
//如果对象属性的值不为空,就保存该属性(这里我做了限制,如果属性的值为0,保存该属性。如果属性的值全部是空格,属于为空。)
if ((obj[key] === 0 || obj[key]) && obj[key].toString().replace(/(^\s*)|(\s*$)/g, "") !== "") {
//记录属性
_newPar[key] = obj[key];
}
} //返回对象
return _newPar;
}
/**
* 判断是否拥有acl权限
*/
function getAclPermission(permissionList, tag) {
if (!tag) return true;
var retPermission = false;
var item = (permissionList || []).find(function (per) {
return per.permissionName == tag;
});
if (item) retPermission = item.accessible;
return retPermission;
}