UNPKG

sunmao-sdk

Version:

榫卯-开箱即用赋能-sdk

184 lines (182 loc) 6.91 kB
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 async function initLog(whiteList, oldFetch, whiteMatch) { window.fetch = (...e) => { // 记录实时请求时间,避免请求时间过长导致误差过大 const requestTime = new Date(); return oldFetch(...e).then(res => { const url = e[0]; // 可设置为白名单接口 if ( !( (res.sunmaoLog || isLogFetch(url, e[1]?.body)) // 已是榫卯log标记,如是,则不在拦截记录 ) ) { res.sunmaoLog = true; // 请求回调时间,用于计算接口请求总耗时 const responseTime = new Date(); const resClone = res.clone(); setTimeout(async () => { try { let json; // 不符合Restful API接口规范,不做记录 try { json = await resClone.json(); } catch {} // web页面资源 css umi.js html 等等 if (!isObject(json)) { commonUtils.log("页面资源请求,不做日志记录----url:", "", url); return; } // 组装日志信息 const CPInfo = CP.getCPInfo(); const env = CP.getSunmaoParams().env; const curHost = CPInfo.host; let serverName = curHost; let apiAndQuery, api, apiType, query, post, params; // 通用获取post入参 try { post = JSON.parse(e?.[1]?.body); } catch {} // 解析获取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?.url && isSkipLog(post.url) ) { //新增第5类 // 5、走网关代理 if (post?.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 = { ...query, ...e?.[1]?.params }; params = { ...CPInfo.extParams, ...query, ...post }; api = params.hsfMethod || api; apiType = params.hsfMethod ? "HSF" : "HTTP"; // 白名单配置 if ( !api || (isArray(whiteList) && whiteList.includes(api)) || (whiteMatch && whiteMatch(api)) // 支持自定义拦截函数 ) { commonUtils.log("请求白名单:", api, params, url); return; } let apiConfig = CP.logConfigMap[api]; if (!apiConfig) { // 无缓存接口配置信息,实时请求获取 try { const { data } = await redirectFetchPost(sunmaoLog, { api, env, sunmaoLogMethod: "queryAdminInterfaceConfig" }); if (isObject(data)) { CP.logConfigMap[api] = data; localStorageUtils.setObject( "sunmao_logConfigMap", CP.logConfigMap ); apiConfig = data; } else { commonUtils.log("未配置接口信息,请开发人员注意!", "", api); } } catch (e) { commonUtils.log("未配置接口信息,请开发人员注意!", e, api); } } const searchTag = params[apiConfig?.searchTag || "orderId"] || params.orderId || params.corpId; // 组合日志信息 const logParams = { ...apiConfig, operator: `${CPInfo.extParams?.bucWorkId}(${CPInfo.extParams?.bucName})`, success: resClone.ok ? json.ok || json.success ? 1 : 0 : resClone.status, envName: env, env: env, hostName: curHost, serverName, directory: window.location.href, 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); } catch (e) { commonUtils.log("请求日志记录失败!请注意!", e); } }, 1); } return res; }); }; }