UNPKG

@wangofnextdoor/zlblog

Version:

浙里办日志和老年模式工具包

140 lines (139 loc) 4.38 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getZwlogInstance = exports.isZwlogLoaded = exports.isLoad = exports.zwlogRecord = exports.initZwlog = exports.getNavigationBarTitleText = exports.getCurRoute = void 0; const config_1 = __importDefault(require("./config")); let zwlog; let pageLogMap = {}; // 获取当前页面路由 const getCurRoute = () => { if (typeof window !== 'undefined') { // H5环境 return window.location.pathname; } else { // 小程序环境 const routes = getCurrentPages(); if (routes.length === 0) return ''; return routes[routes.length - 1].route || ''; } }; exports.getCurRoute = getCurRoute; // 获取页面的导航title const getNavigationBarTitleText = () => { var _a; if (typeof window !== 'undefined') { // H5环境 return document.title; } else { // 小程序环境 const pages = getCurrentPages(); const page = pages[pages.length - 1]; return ((_a = page === null || page === void 0 ? void 0 : page.options) === null || _a === void 0 ? void 0 : _a.title) || ''; } }; exports.getNavigationBarTitleText = getNavigationBarTitleText; /** * 初始化浙里办日志 * @param userId 用户ID * @param userNickName 用户昵称 */ const initZwlog = (userId, userNickName) => { if (typeof ZwLog === 'undefined') { console.warn('ZwLog is not available'); return; } zwlog = new ZwLog({ _user_id: userId, _user_nick: userNickName, }); }; exports.initZwlog = initZwlog; /** * 记录点击事件埋点 * @param userId 用户ID * @param userNick 用户昵称 */ const zwlogRecord = (userId, userNick) => { console.log('zheliban === 点击事件埋点'); if (!zwlog) { console.warn('ZwLog not initialized'); return; } const t2 = (Math.random() / 10).toFixed(2); const t0 = (Math.random() / 10).toFixed(2); zwlog.onReady(() => { if (!zwlog) return; const path = (0, exports.getCurRoute)(); console.log('zheliban === path', path); // 发送PV埋点 const pvParams = { miniAppId: config_1.default.miniAppId, miniAppName: config_1.default.miniAppName, log_status: '02', Page_duration: (Number(t2) * 10 + Number(t0) * 10).toFixed(2) + '秒', t2: t2 + '秒', t0: t0 + '秒', _user_nick: userNick, userType: 'person', }; zwlog.sendPV(pvParams); // 记录点击事件 const clickParams = { pageId: path, pageName: (0, exports.getNavigationBarTitleText)(), miniAppId: config_1.default.miniAppId, userNick: userNick, userType: 'person', }; zwlog.record('open', 'click', clickParams); }); }; exports.zwlogRecord = zwlogRecord; /** * 登录跳转 * @param bIsAlipayMini 是否为支付宝小程序 */ const isLoad = (bIsAlipayMini) => { if (typeof window === 'undefined') { console.warn('isLoad只能在浏览器环境中使用'); return; } if (bIsAlipayMini) { window.location.replace(`https://puser.zjzwfw.gov.cn/sso/alipay.do?action=ssoLogin&servicecode=${config_1.default.serviceCode}`); } else { window.location.replace(`https://puser.zjzwfw.gov.cn/sso/mobile.do?action=oauth&scope=1&servicecode=${config_1.default.serviceCode}`); } }; exports.isLoad = isLoad; /** * 检查是否已加载 * @returns 是否已加载 */ const isZwlogLoaded = () => { return typeof zwlog !== 'undefined' && zwlog !== null; }; exports.isZwlogLoaded = isZwlogLoaded; /** * 获取当前ZwLog实例 * @returns ZwLog实例 */ const getZwlogInstance = () => { return zwlog; }; exports.getZwlogInstance = getZwlogInstance; exports.default = { initZwlog: exports.initZwlog, zwlogRecord: exports.zwlogRecord, isLoad: exports.isLoad, getCurRoute: exports.getCurRoute, getNavigationBarTitleText: exports.getNavigationBarTitleText, isZwlogLoaded: exports.isZwlogLoaded, getZwlogInstance: exports.getZwlogInstance, };