sunmao-sdk
Version:
榫卯-开箱即用赋能-sdk
234 lines (197 loc) • 13.2 kB
JavaScript
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; }
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); }); }; }
import { host, redirectUrl, getHsfHost, sunmaoLog, isSkipLog, isLogFetch, getDictionaries, getPageDetails } from "./net/api";
import { postNew } from "./net/request";
import qs from "qs";
import moment from "moment";
import { isArray, isObject, isString } from "lodash";
import * as localStorageUtils from "./utils/localStorageUtils";
import * as commonUtils from "./utils/commonUtils";
import { CP } from ".";
import { redirectFetchPost } from "./net/service"; // 获取查询页详细Schema数据
export function initLog(_x, _x2, _x3) {
return _initLog.apply(this, arguments);
}
function _initLog() {
_initLog = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(whiteList, oldFetch, whiteMatch) {
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
window.fetch = function () {
for (var _len = arguments.length, e = new Array(_len), _key = 0; _key < _len; _key++) {
e[_key] = arguments[_key];
}
// 记录实时请求时间,避免请求时间过长导致误差过大
var requestTime = new Date();
return oldFetch.apply(void 0, e).then(function (res) {
var _e$;
var url = e[0]; // 可设置为白名单接口
if (!(res.sunmaoLog || isLogFetch(url, (_e$ = e[1]) === null || _e$ === void 0 ? void 0 : _e$.body))) {
res.sunmaoLog = true; // 请求回调时间,用于计算接口请求总耗时
var responseTime = new Date();
var resClone = res.clone();
setTimeout( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
var _post, _e$3, _apiConfig, _CPInfo$extParams, _CPInfo$extParams2, json, CPInfo, env, curHost, serverName, apiAndQuery, api, apiType, query, post, params, _e$2, _post2, apiConfig, _yield$redirectFetchP, data, searchTag, logParams;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.prev = 1;
_context.next = 4;
return resClone.json();
case 4:
json = _context.sent;
_context.next = 9;
break;
case 7:
_context.prev = 7;
_context.t0 = _context["catch"](1);
case 9:
if (isObject(json)) {
_context.next = 12;
break;
}
commonUtils.log("页面资源请求,不做日志记录----url:", "", url);
return _context.abrupt("return");
case 12:
// 组装日志信息
CPInfo = CP.getCPInfo();
env = CP.getSunmaoParams().env;
curHost = CPInfo.host;
serverName = curHost;
// 通用获取post入参
try {
post = JSON.parse(e === null || e === void 0 ? void 0 : (_e$2 = e[1]) === null || _e$2 === void 0 ? void 0 : _e$2.body);
} catch (_unused2) {} // 解析获取api、query入参
// url 服务暂时分为4类
if (!url.includes("http")) {
// 1、host自主服务 无需域名前缀
apiAndQuery = url.split("?");
} else if (url.includes(curHost)) {
// 2、host自主服务 含域名前缀
apiAndQuery = url.split(serverName)[1].split("?");
} else if (url.includes(getHsfHost())) {
// 3、分不同环境下榫卯地址
serverName = getHsfHost();
apiAndQuery = url.split(serverName)[1].split("?");
} else if (url.includes(host)) {
// 4、线上环境下榫卯地址
serverName = host;
apiAndQuery = url.split(serverName)[1].split("?");
} else if (url.includes(redirectUrl) && ((_post = post) === null || _post === void 0 ? void 0 : _post.url) && isSkipLog(post.url)) {
//新增第5类
// 5、走网关代理
if (((_post2 = post) === null || _post2 === void 0 ? void 0 : _post2.url) && isSkipLog(post.url)) {
apiAndQuery = post.url.split("?");
if (post.url.includes(getDictionaries)) {
// 5.1 获取新字典来源 埋点
serverName = getDictionaries;
apiAndQuery[0] = "/enum/dictionary/queryEnum.action";
} else if (post.url.includes(getPageDetails)) {
// 5.2 获取榫卯页面 埋点
serverName = host;
apiAndQuery[0] = "/ext/page/details";
}
} else {
// 非特殊场景网关代理不记录
apiAndQuery = [];
}
} else {
// 其他跨域场景
apiAndQuery = url.split("?");
serverName = curHost || apiAndQuery[0];
}
api = apiAndQuery[0];
apiAndQuery[1] && (query = qs.parse(apiAndQuery[1]));
query = _objectSpread(_objectSpread({}, query), e === null || e === void 0 ? void 0 : (_e$3 = e[1]) === null || _e$3 === void 0 ? void 0 : _e$3.params);
params = _objectSpread(_objectSpread(_objectSpread({}, CPInfo.extParams), query), post);
api = params.hsfMethod || api;
apiType = params.hsfMethod ? "HSF" : "HTTP"; // 白名单配置
if (!(!api || isArray(whiteList) && whiteList.includes(api) || whiteMatch && whiteMatch(api) // 支持自定义拦截函数
)) {
_context.next = 27;
break;
}
commonUtils.log("请求白名单:", api, params, url);
return _context.abrupt("return");
case 27:
apiConfig = CP.logConfigMap[api];
if (apiConfig) {
_context.next = 40;
break;
}
_context.prev = 29;
_context.next = 32;
return redirectFetchPost(sunmaoLog, {
api: api,
env: env,
sunmaoLogMethod: "queryAdminInterfaceConfig"
});
case 32:
_yield$redirectFetchP = _context.sent;
data = _yield$redirectFetchP.data;
if (isObject(data)) {
CP.logConfigMap[api] = data;
localStorageUtils.setObject("sunmao_logConfigMap", CP.logConfigMap);
apiConfig = data;
} else {
commonUtils.log("未配置接口信息,请开发人员注意!", "", api);
}
_context.next = 40;
break;
case 37:
_context.prev = 37;
_context.t1 = _context["catch"](29);
commonUtils.log("未配置接口信息,请开发人员注意!", _context.t1, api);
case 40:
searchTag = params[((_apiConfig = apiConfig) === null || _apiConfig === void 0 ? void 0 : _apiConfig.searchTag) || "orderId"] || params.orderId || params.corpId; // 组合日志信息
logParams = _objectSpread(_objectSpread({}, apiConfig), {}, {
operator: "".concat((_CPInfo$extParams = CPInfo.extParams) === null || _CPInfo$extParams === void 0 ? void 0 : _CPInfo$extParams.bucWorkId, "(").concat((_CPInfo$extParams2 = CPInfo.extParams) === null || _CPInfo$extParams2 === void 0 ? void 0 : _CPInfo$extParams2.bucName, ")"),
success: resClone.ok ? json.ok || json.success ? 1 : 0 : resClone.status,
envName: env,
env: env,
hostName: curHost,
serverName: serverName,
directory: window.location.href,
api: api,
requestParam: JSON.stringify(params),
responseParam: JSON.stringify(json),
searchTag: isString(searchTag) ? searchTag : JSON.stringify(searchTag),
interfaceTime: responseTime.getTime() - requestTime.getTime(),
requestTime: moment(requestTime).format("YYYY-MM-DD HH:mm:ss"),
sunmaoLogMethod: "insertAdminOperationLog"
});
delete logParams.gmtCreate;
delete logParams.gmtModified;
redirectFetchPost(sunmaoLog, logParams);
_context.next = 50;
break;
case 47:
_context.prev = 47;
_context.t2 = _context["catch"](0);
commonUtils.log("请求日志记录失败!请注意!", _context.t2);
case 50:
case "end":
return _context.stop();
}
}
}, _callee, null, [[0, 47], [1, 7], [29, 37]]);
})), 1);
}
return res;
});
};
case 1:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
return _initLog.apply(this, arguments);
}