sunmao-sdk
Version:
榫卯-开箱即用赋能-sdk
93 lines (83 loc) • 2.61 kB
JavaScript
import moment from "moment";
function stringContains(str, text) {
return str.indexOf(text) > -1;
}
export const isObj = a =>
stringContains(Object.prototype.toString.call(a), "Object");
const isApiString = str => {
if (typeof str !== "string") return false;
if (str.indexOf("$api.") === 0 || str.indexOf("$func.") === 0) return true;
return false;
};
const getApiName = str => {
if (str.indexOf("$api.") === 0) return str.substring(5);
if (str.indexOf("$func.") === 0) return str.substring(6);
return "";
};
export const buildSchema = (schema, api) => {
if (typeof schema !== "object" || schema === null) {
if (isApiString(schema)) {
const apiName = getApiName(schema);
if (api && api[apiName] && typeof api[apiName] === "function") {
return api[apiName];
} else {
return () => {
apiName && console.error(`没有找到匹配的函数: ${apiName}`);
};
}
}
return schema;
}
if (Array.isArray(schema)) {
const result = [...schema];
return result.map(item => buildSchema(item, api));
}
// 剩下是 result 是对象的情况
const result = { ...schema };
const keys = Object.keys(result);
keys.forEach(key => {
result[key] = buildSchema(result[key], api);
});
return result;
};
export const getDateTime = time => moment(time).format("YY/MM/DD HH:mm");
export const getDate = time => moment(time).format("YY/MM/DD");
// 如果是函数,则解析,如果不是,直接返回值
export const parseFunctionValue = (value, params, cb) => {
let _value = value;
if (typeof _value === "function") {
_value = _value(params);
} else {
cb && typeof cb === "function" && cb();
}
return _value;
};
/**
* 过滤 空 属性
* 这个方法不会影响原来的对象,而是返回一个新对象
*/
export function filterParams(obj) {
let _newPar = {};
for (let key in obj) {
//如果对象属性的值不为空,就保存该属性(这里我做了限制,如果属性的值为0,保存该属性。如果属性的值全部是空格,属于为空。)
if (
(obj[key] === 0 || obj[key]) &&
obj[key].toString().replace(/(^\s*)|(\s*$)/g, "") !== ""
) {
//记录属性
_newPar[key] = obj[key];
}
}
//返回对象
return _newPar;
}
/**
* 判断是否拥有acl权限
*/
export function getAclPermission(permissionList, tag) {
if (!tag) return true;
let retPermission = false;
const item = (permissionList || []).find(per => per.permissionName == tag);
if (item) retPermission = item.accessible;
return retPermission;
}