UNPKG

sunmao-sdk

Version:

榫卯-开箱即用赋能-sdk

292 lines (273 loc) 7.92 kB
import { isArray, isInteger, isString } from "lodash"; import CP from "../config"; import moment from "moment"; import qs from "qs"; import { getHsfHost, sunmaoLog } from "../net/api"; import { redirectFetchPost } from "../net/service"; export const getUrlQuery = () => { try { const locData = location || window.location; const query = locData.query || qs.parse(locData.search?.split("?")[1]); return query; } catch {} return {}; }; /** * 是否 空对象 */ export function isEmptyObj(obj) { if (isObj(obj)) { let arr = Object.getOwnPropertyNames(filterParams(obj)); return arr.length === 0; } return true; } /** * 是否 对象 */ export function isObj(obj) { return Object.prototype.toString.call(obj) === "[object Object]"; } /** * 数组 转 Map */ export function arrayKeyToMap(array, key) { if (!array || !Array.isArray(array)) return new Map(); return new Map(array.map(i => [i[key], i])); } /** * 过滤 空 属性 * 这个方法不会影响原来的对象,而是返回一个新对象 */ 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; } /** * 跟据 , 分隔符 设置传参 */ export function splitParams(obj) { let _newPar = {}; for (let key in obj) { const value = obj[key]; if (key.includes(",") && isArray(value)) { const keys = key.split(","); // keys 与 value 取小值作为最大长度,避免数组溢出 const maxLength = Math.min(keys.length, value.length); for (let i = 0; i < maxLength; i++) { _newPar[keys[i]] = value[i]; } } else { _newPar[key] = value; } } //返回对象 return _newPar; } /** * 获取第一个数组 * @param {对象} obj * @returns 对象中第一个数组,包含对象本身 或 false */ export function getFirstList(obj) { if (Array.isArray(obj)) return strToObjOfList(obj); //兼容万一,对象的话,找到第一个[],赋值,可再优化,根据key去匹配, else if (typeof obj === "object" && null != obj) { for (let key in obj) { if (Array.isArray(obj[key])) { return strToObjOfList(obj[key]); } } } return false; } /** * 如果是 string 数组 * 则将 string 数组 转为 obj数组 * @param {对象} obj * @returns 对象中第一个数组,包含对象本身 或 false */ export function strToObjOfList(list) { try { // 如果首参为 string if (isString(list[0])) { return list.map(value => ({ key: value, preName: value })); } } catch {} return list; } /** * 获取对应函数对象 * @param {函数名称} apiName * @param {函数集合} apis * @returns 有则返回函数对象 无则 false */ export function getFunc(apiName, apis) { if (apis && apis[apiName] && typeof apis[apiName] === "function") return apis[apiName]; else apiName && log(`没有找到匹配的函数: ${apiName}`); return false; } export function getUrl(url = "") { const isHsf = url.substr(0, 16) == "/api/hsf/common?"; if (isHsf) { url = getHsfHost() + url; if (isObj(CP.getCPInfo().extParams)) { url = url + (url.indexOf("?") > -1 ? "&" : "?") + Object.keys(CP.getCPInfo().extParams) .map(key => { return `${key}=${encodeURIComponent( CP.getCPInfo().extParams[key]?.toString() )}`; }) .join("&"); } } return url.includes("https://") || url.includes("http://") || url.includes("//") ? url : (CP.getCPInfo().host || "") + url; } export function toStringArray(value) { if (isArray(value) && isInteger(value?.[0])) { return value.map(i => `${i}`); } return value; } export function log(...info) { try { // 开发主动配置关闭devLog // 线上环境 prod 不开启 // 内部url 不开启 if ( CP.getCPInfo().devLog && CP.getSunmaoParams().env !== "prod" && !info[2]?.includes("/ext/") ) console.log(info); } catch {} } /** * 判断是否拥有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; } /** * 获取按键配置信息 * @param {第一优先级} disabled 按键能否操作 * @param {按键触发类型} funcType * @param {权限码} perTag * @param {权限集合} permissionList * @returns 按键配置信息 */ export function getBtnConfig(disabled, funcType, perTag, permissionList) { const btnConfig = {}; // 按键是否弹框提示再触发 btnConfig.again = "35" === funcType; // 获取按键权限 const permission = getAclPermission(permissionList, perTag); // 配置按键权限 且 无权限 if (perTag && !permission) { btnConfig.again = true; // 直接跳转页面链接 btnConfig.btnProps = { disabled, title: "暂无权限,是否前往申请?", onConfirm: () => (window.location.href = `https://acl.alibaba-inc.com/apply/cart/detail.htm?pnames=${perTag}`) }; } return btnConfig; } 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 && CP.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 parseFunctionValue = (value, params, cb) => { let _value = value; if (typeof _value === "function") { _value = _value(params); } else { cb && typeof cb === "function" && cb(); } return _value; }; // 用户行为日志 export const behaviorLog = behaviorName => { try { // 组合日志信息 const logParams = { methodName: "榫卯行为日志", searchTag: behaviorName, api: "榫卯行为日志", remarks: "用户操作习惯记录-使用榫卯行为日志-" + behaviorName, apiType: "OTHER", applicationName: "榫卯平台", requestParam: behaviorName, responseParam: behaviorName, directory: window.location.href, success: 1, interfaceTime: 1, requestTime: moment().format("YYYY-MM-DD HH:mm:ss"), operator: `${CP.getCPInfo().extParams?.bucWorkId}(${ CP.getCPInfo().extParams?.bucName })`, envName: CP.getSunmaoParams().env, hostName: CP.getCPInfo().host, serverName: CP.getCPInfo().host, sunmaoLogMethod: "insertAdminOperationLog" }; redirectFetchPost(sunmaoLog, logParams); } catch {} };