sunmao-sdk
Version:
榫卯-开箱即用赋能-sdk
163 lines (140 loc) • 5.98 kB
JavaScript
import "antd/es/message/style";
import _message from "antd/es/message";
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
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(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(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; }
import { sunmaoLog } from "./net/api";
import { isObject } from "lodash";
import * as localStorageUtils from "./utils/localStorageUtils";
import * as commonUtils from "./utils/commonUtils";
import { initLog } from "./fetchLogUtils";
var DEFAULTCPINFO = JSON.stringify({
host: "",
// 服务器请求域名
projectId: 0,
// 项目id
appId: 0,
// 应用id
user: "",
// 当前用户
errorToast: false,
// 报错提示形式, 默认打印log
requestFunc: null,
// 自定义请求函数
extParams: {},
// 额外请求传参,例如hsf所需参数
sunmao_Params: {
projectId: 0,
// 项目id
appId: 0,
// 应用id
user: "",
// 当前用户
env: "" // 网络环境
},
postOk: "" // 报错提示形式.请求返回自定义数据结构,未设则根据sdk,ok:true,status:200为请求成功
});
var CP = {
logConfigMap: {},
// 日志缓存
customConfigMap: {},
// 用户自定义配置缓存
schemaConfigMap: {},
// 页面配置缓存
info: JSON.parse(DEFAULTCPINFO),
getSunmaoParams: function getSunmaoParams() {
return CP.getCPInfo().sunmao_Params || {};
},
getCPInfo: function getCPInfo() {
return CP.info || JSON.parse(DEFAULTCPINFO);
},
init: function init(config) {
// 获取 ext 缓存
CP.info.extParams = localStorageUtils.getObject("bucInfo-CP.info.extParams");
if (isObject(config)) {
// 初始化 应用 信息
CP.info = _objectSpread(_objectSpread({}, CP.getCPInfo()), config); // 榫卯平台所需参数
CP.info.sunmao_Params = {
// 初始化常量, 不常修改
projectId: CP.info.projectId,
// 项目id
appId: CP.info.appId,
// 应用id
user: CP.info.user,
// 当前用户
env: CP.info.env // 网络环境
};
}
},
setExtParams: function setExtParams(extParams) {
if (isObject(extParams)) {
CP.info.extParams = extParams; // 设置 ext 缓存
localStorageUtils.setObject("bucInfo-CP.info.extParams", extParams, false);
}
},
error: function error(msg) {
if (msg.includes(sunmaoLog)) return;
if (CP.getCPInfo().errorToast) _message.error(msg);else console.error(msg);
},
openLog: function () {
var _openLog = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(whiteList, whiteMatch) {
var oldFetch;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
oldFetch = fetch; //whiteMatch 支持自定义拦截函数
initLog(whiteList, oldFetch, whiteMatch);
case 2:
case "end":
return _context.stop();
}
}
}, _callee);
}));
function openLog(_x, _x2) {
return _openLog.apply(this, arguments);
}
return openLog;
}(),
behaviorLog: function behaviorLog(behaviorName) {
// 行为日志
setTimeout(function () {
commonUtils.behaviorLog(behaviorName);
}, 1);
}
};
initAsync();
function initAsync() {
return _initAsync.apply(this, arguments);
}
function _initAsync() {
_initAsync = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return localStorageUtils.getObjectAsync("sunmao_logConfigMap");
case 2:
CP.logConfigMap = _context2.sent;
_context2.next = 5;
return localStorageUtils.getObjectAsync("sunmao_customConfigMap");
case 5:
CP.customConfigMap = _context2.sent;
_context2.next = 8;
return localStorageUtils.getObjectAsync("sunmao_schemaConfigMap");
case 8:
CP.schemaConfigMap = _context2.sent;
case 9:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
return _initAsync.apply(this, arguments);
}
export default CP;