uni-stat.cjs.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  1. 'use strict';
  2. /**
  3. * 获取系统信息
  4. */
  5. const sys = uni.getSystemInfoSync();
  6. // 访问开始即启动小程序,访问结束结分为:进入后台超过5min、在前台无任何操作超过30min、在新的来源打开小程序;
  7. const STAT_VERSION = process.env.UNI_COMPILER_VERSION;
  8. const STAT_URL = 'https://tongji.dcloud.io/uni/stat';
  9. const STAT_H5_URL = 'https://tongji.dcloud.io/uni/stat.gif';
  10. const PAGE_PVER_TIME = 1800; // 页面在前台无操作结束访问时间 单位s
  11. const APP_PVER_TIME = 300; // 应用在后台结束访问时间 单位s
  12. const OPERATING_TIME = 10; // 数据上报时间 单位s
  13. const DIFF_TIME = 60 * 1000 * 60 * 24;
  14. const appid = process.env.UNI_APP_ID; // 做应用隔离
  15. const dbSet = (name, value) => {
  16. let data = uni.getStorageSync('$$STAT__DBDATA:'+appid) || {};
  17. if (!data) {
  18. data = {};
  19. }
  20. data[name] = value;
  21. uni.setStorageSync('$$STAT__DBDATA:'+appid, data);
  22. };
  23. const dbGet = (name) => {
  24. let data = uni.getStorageSync('$$STAT__DBDATA:'+appid) || {};
  25. if (!data[name]) {
  26. let dbdata = uni.getStorageSync('$$STAT__DBDATA:'+appid);
  27. if (!dbdata) {
  28. dbdata = {};
  29. }
  30. if (!dbdata[name]) {
  31. return undefined
  32. }
  33. data[name] = dbdata[name];
  34. }
  35. return data[name]
  36. };
  37. const dbRemove = (name) => {
  38. let data = uni.getStorageSync('$$STAT__DBDATA:'+appid) || {};
  39. if (data[name]) {
  40. delete data[name];
  41. uni.setStorageSync('$$STAT__DBDATA:'+appid, data);
  42. } else {
  43. data = uni.getStorageSync('$$STAT__DBDATA:'+appid);
  44. if (data[name]) {
  45. delete data[name];
  46. uni.setStorageSync('$$STAT__DBDATA:'+appid, data);
  47. }
  48. }
  49. };
  50. // 获取 manifest.json 中统计配置
  51. const uniStatisticsConfig = process.env.UNI_STATISTICS_CONFIG;
  52. let statConfig = {
  53. appid: process.env.UNI_APP_ID,
  54. };
  55. let titleJsons = {};
  56. let debug = !!process.env.UNI_STAT_DEBUG || false;
  57. // #ifdef VUE3
  58. titleJsons = process.env.UNI_STAT_TITLE_JSON;
  59. // #endif
  60. // #ifndef VUE3
  61. // eslint-disable-next-line no-restricted-globals
  62. const pagesTitle = require('uni-pages?{"type":"style"}').default;
  63. let pagesData = pagesTitle.pages;
  64. for (let i in pagesData) {
  65. const style = pagesData[i];
  66. const titleText =
  67. // MP
  68. style.navigationBarTitleText ||
  69. // ali
  70. style.defaultTitle ||
  71. // H5 || App
  72. style.navigationBar?.titleText ||
  73. '';
  74. if (titleText) {
  75. titleJsons[i] = titleText;
  76. }
  77. }
  78. // #endif
  79. // TODO 在云函数中获取,暂时注释
  80. const UUID_KEY = '__DC_STAT_UUID';
  81. const UUID_VALUE = '__DC_UUID_VALUE';
  82. function getUuid() {
  83. let uuid = '';
  84. if (get_platform_name() === 'n') {
  85. try {
  86. uuid = plus.runtime.getDCloudId();
  87. } catch (e) {
  88. uuid = '';
  89. }
  90. return uuid
  91. }
  92. try {
  93. uuid = uni.getStorageSync(UUID_KEY);
  94. } catch (e) {
  95. uuid = UUID_VALUE;
  96. }
  97. if (!uuid) {
  98. uuid = Date.now() + '' + Math.floor(Math.random() * 1e7);
  99. try {
  100. uni.setStorageSync(UUID_KEY, uuid);
  101. } catch (e) {
  102. uni.setStorageSync(UUID_KEY, UUID_VALUE);
  103. }
  104. }
  105. return uuid
  106. }
  107. const get_uuid = (statData) => {
  108. // 有可能不存在 deviceId(一般不存在就是出bug了),就自己生成一个
  109. return sys.deviceId || getUuid()
  110. };
  111. /**
  112. * 获取老版的 deviceid ,兼容以前的错误 deviceid
  113. * @param {*} statData
  114. * @returns
  115. */
  116. const get_odid = (statData) => {
  117. let odid = '';
  118. if (get_platform_name() === 'n') {
  119. try {
  120. odid = plus.device.uuid;
  121. } catch (e) {
  122. odid = '';
  123. }
  124. return odid
  125. }
  126. return sys.deviceId || getUuid()
  127. };
  128. /**
  129. * 获取配置信息 如 appid
  130. */
  131. const stat_config = statConfig;
  132. const get_sgin = (statData) => {
  133. let arr = Object.keys(statData);
  134. let sortArr = arr.sort();
  135. let sgin = {};
  136. let sginStr = '';
  137. for (var i in sortArr) {
  138. sgin[sortArr[i]] = statData[sortArr[i]];
  139. sginStr += sortArr[i] + '=' + statData[sortArr[i]] + '&';
  140. }
  141. return {
  142. sign: '',
  143. options: sginStr.substr(0, sginStr.length - 1),
  144. }
  145. };
  146. const get_encodeURIComponent_options = (statData) => {
  147. let data = {};
  148. for (let prop in statData) {
  149. data[prop] = encodeURIComponent(statData[prop]);
  150. }
  151. return data
  152. };
  153. /**
  154. * 获取当前平台
  155. * 移动端 : 'n',
  156. * h5 : 'h5',
  157. * 微信 : 'wx',
  158. * 阿里 : 'ali',
  159. * 百度 : 'bd',
  160. * 头条 : 'tt',
  161. * qq : 'qq',
  162. * 快应用 : 'qn',
  163. * 快手 : 'ks',
  164. * 飞书 : 'lark',
  165. * 快应用 : 'qw',
  166. * 钉钉 : 'dt'
  167. */
  168. const get_platform_name = () => {
  169. // 苹果审核代码中禁止出现 alipay 字样 ,需要特殊处理一下
  170. const aliArr = ['y', 'a', 'p', 'mp-ali'];
  171. const platformList = {
  172. app: 'n',
  173. 'app-plus': 'n',
  174. h5: 'h5',
  175. 'mp-weixin': 'wx',
  176. [aliArr.reverse().join('')]: 'ali',
  177. 'mp-baidu': 'bd',
  178. 'mp-toutiao': 'tt',
  179. 'mp-qq': 'qq',
  180. 'quickapp-native': 'qn',
  181. 'mp-kuaishou': 'ks',
  182. 'mp-lark': 'lark',
  183. 'quickapp-webview': 'qw',
  184. };
  185. if (platformList[process.env.VUE_APP_PLATFORM] === 'ali') {
  186. if (my && my.env) {
  187. const clientName = my.env.clientName;
  188. if (clientName === 'ap') return 'ali'
  189. if (clientName === 'dingtalk') return 'dt'
  190. // TODO 缺少 ali 下的其他平台
  191. }
  192. }
  193. return platformList[process.env.VUE_APP_PLATFORM]
  194. };
  195. /**
  196. * 获取小程序 appid
  197. */
  198. const get_pack_name = () => {
  199. let packName = '';
  200. if (get_platform_name() === 'wx' || get_platform_name() === 'qq') {
  201. // 兼容微信小程序低版本基础库
  202. if (uni.canIUse('getAccountInfoSync')) {
  203. packName = uni.getAccountInfoSync().miniProgram.appId || '';
  204. }
  205. }
  206. if (get_platform_name() === 'n') ;
  207. return packName
  208. };
  209. /**
  210. * 应用版本
  211. */
  212. const get_version = () => {
  213. return get_platform_name() === 'n' ? plus.runtime.version : ''
  214. };
  215. /**
  216. * 获取渠道
  217. */
  218. const get_channel = () => {
  219. const platformName = get_platform_name();
  220. let channel = '';
  221. if (platformName === 'n') {
  222. channel = plus.runtime.channel;
  223. }
  224. return channel
  225. };
  226. /**
  227. * 获取小程序场景值
  228. * @param {Object} options 页面信息
  229. */
  230. const get_scene = (options) => {
  231. const platformName = get_platform_name();
  232. let scene = '';
  233. if (options) {
  234. return options
  235. }
  236. if (platformName === 'wx') {
  237. scene = uni.getLaunchOptionsSync().scene;
  238. }
  239. return scene
  240. };
  241. /**
  242. * 获取拼接参数
  243. */
  244. const get_splicing = (data) => {
  245. let str = '';
  246. for (var i in data) {
  247. str += i + '=' + data[i] + '&';
  248. }
  249. return str.substr(0, str.length - 1)
  250. };
  251. /**
  252. * 获取页面url,不包含参数
  253. */
  254. const get_route$1 = (pageVm) => {
  255. let _self = pageVm || get_page_vm();
  256. if (get_platform_name() === 'bd') {
  257. let mp_route = _self.$mp && _self.$mp.page && _self.$mp.page.is;
  258. let scope_route = _self.$scope && _self.$scope.is;
  259. return mp_route || scope_route || ''
  260. } else {
  261. return (
  262. _self.route ||
  263. (_self.$scope && _self.$scope.route) ||
  264. (_self.$mp && _self.$mp.page.route)
  265. )
  266. }
  267. };
  268. /**
  269. * 获取页面url, 包含参数
  270. */
  271. const get_page_route = (pageVm) => {
  272. // 从 app 进入应用 ,没有 $page ,获取不到路由 ,需要获取页面 尝试从 getCurrentPages 获取也页面实例
  273. // FIXME 尽量不使用 getCurrentPages ,大部分获取路由是从 onHide 获取 ,这时可以获取到,如果是 onload ,则可能获取不到,比如 百度
  274. let page = pageVm && (pageVm.$page || (pageVm.$scope && pageVm.$scope.$page));
  275. let lastPageRoute = uni.getStorageSync('_STAT_LAST_PAGE_ROUTE');
  276. if (!page) return lastPageRoute || ''
  277. // 如果找不到 fullPath 就取 route 的值
  278. return page.fullPath === '/' ? page.route : page.fullPath || page.route
  279. };
  280. /**
  281. * 获取页面实例
  282. */
  283. const get_page_vm = () => {
  284. let pages = getCurrentPages();
  285. let $page = pages[pages.length - 1];
  286. if (!$page) return null
  287. return $page.$vm
  288. };
  289. /**
  290. * 获取页面类型
  291. */
  292. const get_page_types = (self) => {
  293. // XXX 百度有问题 ,获取的都是 componet ,等待修复
  294. if (
  295. self.mpType === 'page' ||
  296. self.$mpType === 'page' ||
  297. (self.$mp && self.$mp.mpType === 'page') ||
  298. self.$options.mpType === 'page'
  299. ) {
  300. return 'page'
  301. }
  302. if (
  303. self.mpType === 'app' ||
  304. self.$mpType === 'app' ||
  305. (self.$mp && self.$mp.mpType === 'app') ||
  306. self.$options.mpType === 'app'
  307. ) {
  308. return 'app'
  309. }
  310. return null
  311. };
  312. /**
  313. * 处理上报参数
  314. * @param {Object} 需要处理的数据
  315. */
  316. const handle_data = (statData) => {
  317. let firstArr = [];
  318. let contentArr = [];
  319. let lastArr = [];
  320. for (let i in statData) {
  321. const rd = statData[i];
  322. rd.forEach((elm) => {
  323. let newData = '';
  324. {
  325. newData = get_splicing(elm);
  326. }
  327. if (i === 0) {
  328. firstArr.push(newData);
  329. } else if (i === 3) {
  330. lastArr.push(newData);
  331. } else {
  332. contentArr.push(newData);
  333. }
  334. });
  335. }
  336. firstArr.push(...contentArr, ...lastArr);
  337. // 参数需要处理成字符串,方便上传
  338. return JSON.stringify(firstArr)
  339. };
  340. /**
  341. * 自定义事件参数校验
  342. */
  343. const calibration = (eventName, options) => {
  344. // login 、 share 、pay_success 、pay_fail 、register 、title
  345. if (!eventName) {
  346. console.error(`uni.report Missing [eventName] parameter`);
  347. return true
  348. }
  349. if (typeof eventName !== 'string') {
  350. console.error(
  351. `uni.report [eventName] Parameter type error, it can only be of type String`
  352. );
  353. return true
  354. }
  355. if (eventName.length > 255) {
  356. console.error(
  357. `uni.report [eventName] Parameter length cannot be greater than 255`
  358. );
  359. return true
  360. }
  361. if (typeof options !== 'string' && typeof options !== 'object') {
  362. console.error(
  363. 'uni.report [options] Parameter type error, Only supports String or Object type'
  364. );
  365. return true
  366. }
  367. if (typeof options === 'string' && options.length > 255) {
  368. console.error(
  369. `uni.report [options] Parameter length cannot be greater than 255`
  370. );
  371. return true
  372. }
  373. if (eventName === 'title' && typeof options !== 'string') {
  374. console.error(
  375. `uni.report [eventName] When the parameter is title, the [options] parameter can only be of type String`
  376. );
  377. return true
  378. }
  379. };
  380. const get_page_name = (routepath) => {
  381. return (titleJsons && titleJsons[routepath]) || ''
  382. };
  383. const Report_Data_Time = 'Report_Data_Time';
  384. const Report_Status = 'Report_Status';
  385. const is_report_data = () => {
  386. return new Promise((resolve, reject) => {
  387. let start_time = '';
  388. let end_time = new Date().getTime();
  389. let diff_time = DIFF_TIME;
  390. let report_status = 1;
  391. try {
  392. start_time = uni.getStorageSync(Report_Data_Time);
  393. report_status = uni.getStorageSync(Report_Status);
  394. } catch (e) {
  395. start_time = '';
  396. report_status = 1;
  397. }
  398. if (report_status === '') {
  399. requestData(({ enable }) => {
  400. uni.setStorageSync(Report_Data_Time, end_time);
  401. uni.setStorageSync(Report_Status, enable);
  402. if (enable === 1) {
  403. resolve();
  404. }
  405. });
  406. return
  407. }
  408. if (report_status === 1) {
  409. resolve();
  410. }
  411. if (!start_time) {
  412. uni.setStorageSync(Report_Data_Time, end_time);
  413. start_time = end_time;
  414. }
  415. if (end_time - start_time > diff_time) {
  416. requestData(({ enable }) => {
  417. uni.setStorageSync(Report_Data_Time, end_time);
  418. uni.setStorageSync(Report_Status, enable);
  419. });
  420. }
  421. })
  422. };
  423. const requestData = (done) => {
  424. const appid = process.env.UNI_APP_ID;
  425. let formData = {
  426. usv: STAT_VERSION,
  427. conf: JSON.stringify({
  428. ak: appid,
  429. }),
  430. };
  431. uni.request({
  432. url: STAT_URL,
  433. method: 'GET',
  434. data: formData,
  435. success: (res) => {
  436. const { data } = res;
  437. if (data.ret === 0) {
  438. typeof done === 'function' &&
  439. done({
  440. enable: data.enable,
  441. });
  442. }
  443. },
  444. fail: (e) => {
  445. let report_status_code = 1;
  446. try {
  447. report_status_code = uni.getStorageSync(Report_Status);
  448. } catch (e) {
  449. report_status_code = 1;
  450. }
  451. if (report_status_code === '') {
  452. report_status_code = 1;
  453. }
  454. typeof done === 'function' &&
  455. done({
  456. enable: report_status_code,
  457. });
  458. },
  459. });
  460. };
  461. /**
  462. * 是否开启 debug 模式
  463. */
  464. const is_debug = debug;
  465. /**
  466. * 日志输出
  467. * @param {*} data
  468. */
  469. const log = (data, type) => {
  470. let msg_type = '';
  471. switch (data.lt) {
  472. case '1':
  473. msg_type = '应用启动';
  474. break
  475. case '3':
  476. msg_type = '应用进入后台';
  477. break
  478. case '11':
  479. msg_type = '页面切换';
  480. break
  481. case '21':
  482. msg_type = '事件触发';
  483. break
  484. case '31':
  485. msg_type = '应用错误';
  486. break
  487. case '101':
  488. msg_type = 'PUSH';
  489. break
  490. }
  491. // #ifdef APP
  492. // 在 app 中,日志转为 字符串
  493. if (typeof data === 'object') {
  494. data = JSON.stringify(data);
  495. }
  496. // #endif
  497. if (type) {
  498. console.log(`=== 统计队列数据上报 ===`);
  499. console.log(data);
  500. console.log(`=== 上报结束 ===`);
  501. return
  502. }
  503. if (msg_type) {
  504. console.log(`=== 统计数据采集:${msg_type} ===`);
  505. console.log(data);
  506. console.log(`=== 采集结束 ===`);
  507. }
  508. };
  509. /**
  510. * 获取上报时间间隔
  511. * @param {*} defaultTime 默认上报间隔时间 单位s
  512. */
  513. const get_report_Interval = (defaultTime) => {
  514. let time = uniStatisticsConfig.reportInterval;
  515. // 如果上报时间配置为0 相当于立即上报
  516. if (Number(time) === 0) return 0
  517. time = time || defaultTime;
  518. let reg = /(^[1-9]\d*$)/;
  519. // 如果不是整数,则默认为上报间隔时间
  520. if (!reg.test(time)) return defaultTime
  521. return Number(time)
  522. };
  523. /**
  524. * 获取隐私协议配置
  525. */
  526. const is_push_clientid = () => {
  527. if (uniStatisticsConfig.collectItems) {
  528. const ClientID = uniStatisticsConfig.collectItems.uniPushClientID;
  529. return typeof ClientID === 'boolean' ? ClientID : false
  530. }
  531. return false
  532. };
  533. /**
  534. * 是否上报页面数据
  535. * @returns
  536. */
  537. const is_page_report = ()=>{
  538. if(uniStatisticsConfig.collectItems){
  539. const statPageLog = uniStatisticsConfig.collectItems.uniStatPageLog;
  540. // 如果字段不存在返回 true , 如果是boolean 值按原值返回,如果是其他类型 返回false
  541. if(statPageLog === undefined) return true
  542. return typeof statPageLog === 'boolean' ? statPageLog : true
  543. }
  544. return true
  545. };
  546. // 首次访问时间
  547. const FIRST_VISIT_TIME_KEY = '__first__visit__time';
  548. // 最后访问时间
  549. const LAST_VISIT_TIME_KEY = '__last__visit__time';
  550. /**
  551. * 获取当前时间
  552. */
  553. const get_time = () => {
  554. return parseInt(new Date().getTime() / 1000)
  555. };
  556. /**
  557. * 获取首次访问时间
  558. */
  559. const get_first_visit_time = () => {
  560. const timeStorge = dbGet(FIRST_VISIT_TIME_KEY);
  561. let time = 0;
  562. if (timeStorge) {
  563. time = timeStorge;
  564. } else {
  565. time = get_time();
  566. dbSet(FIRST_VISIT_TIME_KEY, time);
  567. // 首次访问需要 将最后访问时间置 0
  568. dbRemove(LAST_VISIT_TIME_KEY);
  569. }
  570. return time
  571. };
  572. /**
  573. * 最后访问时间
  574. */
  575. const get_last_visit_time = () => {
  576. const timeStorge = dbGet(LAST_VISIT_TIME_KEY);
  577. let time = 0;
  578. if (timeStorge) {
  579. time = timeStorge;
  580. }
  581. dbSet(LAST_VISIT_TIME_KEY, get_time());
  582. return time
  583. };
  584. // 页面停留时间记录key
  585. const PAGE_RESIDENCE_TIME = '__page__residence__time';
  586. let First_Page_Residence_Time = 0;
  587. let Last_Page_Residence_Time = 0;
  588. /**
  589. * 设置页面停留时间
  590. */
  591. const set_page_residence_time = () => {
  592. First_Page_Residence_Time = get_time();
  593. dbSet(PAGE_RESIDENCE_TIME, First_Page_Residence_Time);
  594. return First_Page_Residence_Time
  595. };
  596. /**
  597. * 获取页面停留时间
  598. */
  599. const get_page_residence_time = () => {
  600. Last_Page_Residence_Time = get_time();
  601. First_Page_Residence_Time = dbGet(PAGE_RESIDENCE_TIME);
  602. return Last_Page_Residence_Time - First_Page_Residence_Time
  603. };
  604. /**
  605. * 获取总访问次数
  606. */
  607. const TOTAL_VISIT_COUNT = '__total__visit__count';
  608. const get_total_visit_count = () => {
  609. const timeStorge = dbGet(TOTAL_VISIT_COUNT);
  610. let count = 1;
  611. if (timeStorge) {
  612. count = timeStorge;
  613. count++;
  614. }
  615. dbSet(TOTAL_VISIT_COUNT, count);
  616. return count
  617. };
  618. const FIRST_TIME = '__first_time';
  619. /**
  620. * 设置页面首次访问时间,用户获取页面/应用停留时常
  621. */
  622. const set_first_time = () => {
  623. // 获取当前时间 ,以下代码获取到是毫秒级时间戳 ,实际上用到是秒级时间戳,所以需要除以1000
  624. // const time = new Date().getTime()
  625. let time = get_time();
  626. const timeStorge = dbSet(FIRST_TIME,time);
  627. return timeStorge
  628. };
  629. /**
  630. * 获取最后一次时间 ,暂时用不到,直接获取当前时间即可
  631. */
  632. // export const get_last_time = () => {
  633. // let time = new Date().getTime()
  634. // Set__Last__Time = time
  635. // return time
  636. // }
  637. /**
  638. * 获取页面 \ 应用停留时间
  639. */
  640. const get_residence_time = (type) => {
  641. let residenceTime = 0;
  642. const first_time = dbGet(FIRST_TIME);
  643. const last_time = get_time();
  644. if (first_time !== 0) {
  645. residenceTime = last_time - first_time;
  646. }
  647. // 将毫秒级时间戳转换为秒级时间戳,因为直接获取的是秒级时间戳,所以不需要转换
  648. // residenceTime = parseInt(residenceTime / 1000)
  649. residenceTime = residenceTime < 1 ? 1 : residenceTime;
  650. if (type === 'app') {
  651. let overtime = residenceTime > APP_PVER_TIME ? true : false;
  652. return {
  653. residenceTime,
  654. overtime,
  655. }
  656. }
  657. if (type === 'page') {
  658. let overtime = residenceTime > PAGE_PVER_TIME ? true : false;
  659. return {
  660. residenceTime,
  661. overtime,
  662. }
  663. }
  664. return {
  665. residenceTime,
  666. }
  667. };
  668. const eport_Interval = get_report_Interval(OPERATING_TIME);
  669. // 统计数据默认值
  670. let statData = {
  671. uuid: get_uuid(), // 设备标识
  672. ak: stat_config.appid, // uni-app 应用 Appid
  673. p: sys.platform === 'android' ? 'a' : 'i', // 手机系统
  674. ut: get_platform_name(), // 平台类型
  675. mpn: get_pack_name(), // 原生平台包名、小程序 appid
  676. usv: STAT_VERSION, // 统计 sdk 版本
  677. v: get_version(), // 应用版本,仅app
  678. ch: get_channel(), // 渠道信息
  679. cn: '', // 国家
  680. pn: '', // 省份
  681. ct: '', // 城市
  682. t: get_time(), // 上报数据时的时间戳
  683. tt: '',
  684. brand: sys.brand || '', // 手机品牌
  685. md: sys.model, // 手机型号
  686. sv: sys.system.replace(/(Android|iOS)\s/, ''), // 手机系统版本
  687. mpsdk: sys.SDKVersion || '', // x程序 sdk version
  688. mpv: sys.version || '', // 小程序平台版本 ,如微信、支付宝
  689. lang: sys.language, // 语言
  690. pr: sys.pixelRatio, // pixelRatio 设备像素比
  691. ww: sys.windowWidth, // windowWidth 可使用窗口宽度
  692. wh: sys.windowHeight, // windowHeight 可使用窗口高度
  693. sw: sys.screenWidth, // screenWidth 屏幕宽度
  694. sh: sys.screenHeight, // screenHeight 屏幕高度
  695. };
  696. class Report {
  697. constructor() {
  698. // 页面实例
  699. this.self = '';
  700. // 进入应用标识
  701. this.__licationShow = false;
  702. // 离开应用标识
  703. this.__licationHide = false;
  704. // 统计默认值
  705. this.statData = statData;
  706. // 标题默认值
  707. this._navigationBarTitle = {
  708. config: '',
  709. page: '',
  710. report: '',
  711. lt: '',
  712. };
  713. // 页面参数
  714. this._query = {};
  715. // 页面最后停留页面的 url
  716. // this._lastPageRoute = ''
  717. // 注册拦截器
  718. let registerInterceptor = typeof uni.addInterceptor === 'function';
  719. if (registerInterceptor) {
  720. this.addInterceptorInit();
  721. this.interceptLogin();
  722. this.interceptShare(true);
  723. this.interceptRequestPayment();
  724. }
  725. }
  726. addInterceptorInit() {
  727. let self = this;
  728. uni.addInterceptor('setNavigationBarTitle', {
  729. invoke(args) {
  730. self._navigationBarTitle.page = args.title;
  731. },
  732. });
  733. }
  734. interceptLogin() {
  735. let self = this;
  736. uni.addInterceptor('login', {
  737. complete() {
  738. self._login();
  739. },
  740. });
  741. }
  742. interceptShare(type) {
  743. let self = this;
  744. if (!type) {
  745. self._share();
  746. return
  747. }
  748. uni.addInterceptor('share', {
  749. success() {
  750. self._share();
  751. },
  752. fail() {
  753. self._share();
  754. },
  755. });
  756. }
  757. interceptRequestPayment() {
  758. let self = this;
  759. uni.addInterceptor('requestPayment', {
  760. success() {
  761. self._payment('pay_success');
  762. },
  763. fail() {
  764. self._payment('pay_fail');
  765. },
  766. });
  767. }
  768. _login() {
  769. this.sendEventRequest(
  770. {
  771. key: 'login',
  772. },
  773. 0
  774. );
  775. }
  776. _share() {
  777. this.sendEventRequest(
  778. {
  779. key: 'share',
  780. },
  781. 0
  782. );
  783. }
  784. _payment(key) {
  785. this.sendEventRequest(
  786. {
  787. key,
  788. },
  789. 0
  790. );
  791. }
  792. /**
  793. * 进入应用触发
  794. */
  795. applicationShow() {
  796. // 通过 __licationHide 判断保证是进入后台后在次进入应用,避免重复上报数据
  797. if (this.__licationHide) {
  798. const time = get_residence_time('app');
  799. // 需要判断进入后台是否超过时限 ,默认是 30min ,是的话需要执行进入应用的上报
  800. if (time.overtime) {
  801. let lastPageRoute = uni.getStorageSync('_STAT_LAST_PAGE_ROUTE');
  802. let options = {
  803. path: lastPageRoute,
  804. scene: this.statData.sc,
  805. cst: 2,
  806. };
  807. this.sendReportRequest(options);
  808. }
  809. // 状态重置
  810. this.__licationHide = false;
  811. }
  812. }
  813. /**
  814. * 离开应用触发
  815. * @param {Object} self
  816. * @param {Object} type
  817. */
  818. applicationHide(self, type) {
  819. if (!self) {
  820. // 表示应用切换到后台 ,此时需要从页面栈获取页面实例
  821. self = get_page_vm();
  822. }
  823. // 进入应用后台保存状态,方便进入前台后判断是否上报应用数据
  824. this.__licationHide = true;
  825. const time = get_residence_time();
  826. const route = get_page_route(self);
  827. uni.setStorageSync('_STAT_LAST_PAGE_ROUTE', route);
  828. this.sendHideRequest(
  829. {
  830. urlref: route,
  831. urlref_ts: time.residenceTime,
  832. },
  833. type
  834. );
  835. // 更新页面首次访问时间
  836. set_first_time();
  837. }
  838. /**
  839. * 进入页面触发
  840. */
  841. pageShow(self) {
  842. // 清空值 ,初始化 ,避免污染后面的上报数据
  843. this._navigationBarTitle = {
  844. config: '',
  845. page: '',
  846. report: '',
  847. lt: '',
  848. };
  849. const route = get_page_route(self);
  850. const routepath = get_route$1(self);
  851. this._navigationBarTitle.config = get_page_name(routepath);
  852. // 表示应用触发 ,页面切换不触发之后的逻辑
  853. if (this.__licationShow) {
  854. // 更新页面首次访问时间
  855. set_first_time();
  856. // this._lastPageRoute = route
  857. uni.setStorageSync('_STAT_LAST_PAGE_ROUTE', route);
  858. this.__licationShow = false;
  859. return
  860. }
  861. const time = get_residence_time('page');
  862. // 停留时间
  863. if (time.overtime) {
  864. let options = {
  865. path: route,
  866. scene: this.statData.sc,
  867. cst: 3,
  868. };
  869. this.sendReportRequest(options);
  870. }
  871. // 更新页面首次访问时间
  872. set_first_time();
  873. }
  874. /**
  875. * 离开页面触发
  876. */
  877. pageHide(self) {
  878. if (!this.__licationHide) {
  879. const time = get_residence_time('page');
  880. let route = get_page_route(self);
  881. let lastPageRoute = uni.getStorageSync('_STAT_LAST_PAGE_ROUTE');
  882. if (!lastPageRoute) {
  883. lastPageRoute = route;
  884. }
  885. uni.setStorageSync('_STAT_LAST_PAGE_ROUTE', route);
  886. this.sendPageRequest({
  887. url: route,
  888. urlref: lastPageRoute,
  889. urlref_ts: time.residenceTime,
  890. });
  891. // this._lastPageRoute = route
  892. return
  893. }
  894. }
  895. /**
  896. * 发送请求,应用维度上报
  897. * @param {Object} options 页面信息
  898. * @param {Boolean} type 是否立即上报
  899. */
  900. sendReportRequest(options, type) {
  901. this._navigationBarTitle.lt = '1';
  902. this._navigationBarTitle.config = get_page_name(options.path);
  903. let is_opt = options.query && JSON.stringify(options.query) !== '{}';
  904. let query = is_opt ? '?' + JSON.stringify(options.query) : '';
  905. const last_time = get_last_visit_time();
  906. // 非老用户
  907. if(last_time !== 0 || !last_time){
  908. const odid = get_odid();
  909. // 1.0 处理规则
  910. {
  911. this.statData.odid = odid;
  912. }
  913. }
  914. Object.assign(this.statData, {
  915. lt: '1',
  916. url: options.path + query || '',
  917. t: get_time(),
  918. sc: get_scene(options.scene),
  919. fvts: get_first_visit_time(),
  920. lvts: last_time,
  921. tvc: get_total_visit_count(),
  922. // create session type 上报类型 ,1 应用进入 2.后台30min进入 3.页面30min进入
  923. cst: options.cst || 1,
  924. });
  925. if (get_platform_name() === 'n') {
  926. this.getProperty(type);
  927. } else {
  928. this.getNetworkInfo(type);
  929. }
  930. }
  931. /**
  932. * 发送请求,页面维度上报
  933. * @param {Object} opt
  934. */
  935. sendPageRequest(opt) {
  936. let { url, urlref, urlref_ts } = opt;
  937. this._navigationBarTitle.lt = '11';
  938. let options = {
  939. ak: this.statData.ak,
  940. uuid: this.statData.uuid,
  941. p: this.statData.p,
  942. lt: '11',
  943. ut: this.statData.ut,
  944. url,
  945. tt: this.statData.tt,
  946. urlref,
  947. urlref_ts,
  948. ch: this.statData.ch,
  949. usv: this.statData.usv,
  950. t: get_time(),
  951. };
  952. this.request(options);
  953. }
  954. /**
  955. * 进入后台上报数据
  956. * @param {Object} opt
  957. * @param {Object} type
  958. */
  959. sendHideRequest(opt, type) {
  960. let { urlref, urlref_ts } = opt;
  961. let options = {
  962. ak: this.statData.ak,
  963. uuid: this.statData.uuid,
  964. p: this.statData.p,
  965. lt: '3',
  966. ut: this.statData.ut,
  967. urlref,
  968. urlref_ts,
  969. ch: this.statData.ch,
  970. usv: this.statData.usv,
  971. t: get_time(),
  972. };
  973. this.request(options, type);
  974. }
  975. /**
  976. * 自定义事件上报
  977. */
  978. sendEventRequest({ key = '', value = '' } = {}) {
  979. let routepath = '';
  980. try {
  981. routepath = get_route$1();
  982. } catch (error) {
  983. const launch_options = dbGet('__launch_options');
  984. routepath = launch_options.path;
  985. }
  986. this._navigationBarTitle.config = get_page_name(routepath);
  987. this._navigationBarTitle.lt = '21';
  988. let options = {
  989. ak: this.statData.ak,
  990. uuid: this.statData.uuid,
  991. p: this.statData.p,
  992. lt: '21',
  993. ut: this.statData.ut,
  994. url: routepath,
  995. ch: this.statData.ch,
  996. e_n: key,
  997. e_v: typeof value === 'object' ? JSON.stringify(value) : value.toString(),
  998. usv: this.statData.usv,
  999. t: get_time(),
  1000. };
  1001. this.request(options);
  1002. }
  1003. sendPushRequest(options, cid) {
  1004. let time = get_time();
  1005. const statData = {
  1006. lt: '101',
  1007. cid: cid,
  1008. t: time,
  1009. ut: this.statData.ut,
  1010. };
  1011. // debug 打印打点信息
  1012. if (is_debug) {
  1013. log(statData);
  1014. }
  1015. const stat_data = handle_data({
  1016. 101: [statData],
  1017. });
  1018. let optionsData = {
  1019. usv: STAT_VERSION, //统计 SDK 版本号
  1020. t: time, //发送请求时的时间戮
  1021. requests: stat_data,
  1022. };
  1023. {
  1024. if (statData.ut === 'h5') {
  1025. this.imageRequest(optionsData);
  1026. return
  1027. }
  1028. }
  1029. // XXX 安卓需要延迟上报 ,否则会有未知错误,需要验证处理
  1030. if (get_platform_name() === 'n' && this.statData.p === 'a') {
  1031. setTimeout(() => {
  1032. this.sendRequest(optionsData);
  1033. }, 200);
  1034. return
  1035. }
  1036. this.sendRequest(optionsData);
  1037. }
  1038. /**
  1039. * 获取wgt资源版本
  1040. */
  1041. getProperty(type) {
  1042. plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
  1043. this.statData.v = wgtinfo.version || '';
  1044. this.getNetworkInfo(type);
  1045. });
  1046. }
  1047. /**
  1048. * 获取网络信息
  1049. */
  1050. getNetworkInfo(type) {
  1051. uni.getNetworkType({
  1052. success: (result) => {
  1053. this.statData.net = result.networkType;
  1054. this.getLocation(type);
  1055. },
  1056. });
  1057. }
  1058. /**
  1059. * 获取位置信息
  1060. */
  1061. getLocation(type) {
  1062. if (stat_config.getLocation) {
  1063. uni.getLocation({
  1064. type: 'wgs84',
  1065. geocode: true,
  1066. success: (result) => {
  1067. if (result.address) {
  1068. this.statData.cn = result.address.country;
  1069. this.statData.pn = result.address.province;
  1070. this.statData.ct = result.address.city;
  1071. }
  1072. this.statData.lat = result.latitude;
  1073. this.statData.lng = result.longitude;
  1074. this.request(this.statData, type);
  1075. },
  1076. });
  1077. } else {
  1078. this.statData.lat = 0;
  1079. this.statData.lng = 0;
  1080. this.request(this.statData, type);
  1081. }
  1082. }
  1083. /**
  1084. * 发送请求
  1085. * @param {Object} data 上报数据
  1086. * @param {Object} type 类型
  1087. */
  1088. request(data, type) {
  1089. let time = get_time();
  1090. const title = this._navigationBarTitle;
  1091. Object.assign(data, {
  1092. ttn: title.page,
  1093. ttpj: title.config,
  1094. ttc: title.report,
  1095. });
  1096. let uniStatData = dbGet('__UNI__STAT__DATA') || {};
  1097. if (!uniStatData[data.lt]) {
  1098. uniStatData[data.lt] = [];
  1099. }
  1100. // 加入队列
  1101. uniStatData[data.lt].push(data);
  1102. dbSet('__UNI__STAT__DATA', uniStatData);
  1103. let page_residence_time = get_page_residence_time();
  1104. // debug 打印打点信息
  1105. if (is_debug) {
  1106. log(data);
  1107. }
  1108. // 判断时候到达上报时间 ,默认 10 秒上报
  1109. if (page_residence_time < eport_Interval && !type) return
  1110. // 时间超过,重新获取时间戳
  1111. set_page_residence_time();
  1112. const stat_data = handle_data(uniStatData);
  1113. let optionsData = {
  1114. usv: STAT_VERSION, //统计 SDK 版本号
  1115. t: time, //发送请求时的时间戮
  1116. requests: stat_data,
  1117. };
  1118. // 重置队列
  1119. dbRemove('__UNI__STAT__DATA');
  1120. {
  1121. if (data.ut === 'h5') {
  1122. this.imageRequest(optionsData);
  1123. return
  1124. }
  1125. }
  1126. // XXX 安卓需要延迟上报 ,否则会有未知错误,需要验证处理
  1127. if (get_platform_name() === 'n' && this.statData.p === 'a') {
  1128. setTimeout(() => {
  1129. this.sendRequest(optionsData);
  1130. }, 200);
  1131. return
  1132. }
  1133. this.sendRequest(optionsData);
  1134. }
  1135. getIsReportData() {
  1136. return is_report_data()
  1137. }
  1138. /**
  1139. * 数据上报
  1140. * @param {Object} optionsData 需要上报的数据
  1141. */
  1142. sendRequest(optionsData) {
  1143. {
  1144. this.getIsReportData().then(() => {
  1145. uni.request({
  1146. url: STAT_URL,
  1147. method: 'POST',
  1148. data: optionsData,
  1149. success: () => {
  1150. if (is_debug) {
  1151. log(optionsData, true);
  1152. }
  1153. },
  1154. fail: (e) => {
  1155. if (++this._retry < 3) {
  1156. if (is_debug) {
  1157. console.warn('=== 统计上报错误,尝试重新上报!');
  1158. console.error(e);
  1159. }
  1160. setTimeout(() => {
  1161. this.sendRequest(optionsData);
  1162. }, 1000);
  1163. }
  1164. },
  1165. });
  1166. });
  1167. }
  1168. }
  1169. /**
  1170. * h5 请求
  1171. */
  1172. imageRequest(data) {
  1173. this.getIsReportData().then(() => {
  1174. let image = new Image();
  1175. let options = get_sgin(get_encodeURIComponent_options(data)).options;
  1176. image.src = STAT_H5_URL + '?' + options;
  1177. if (is_debug) {
  1178. log(data, true);
  1179. }
  1180. });
  1181. }
  1182. sendEvent(key, value) {
  1183. // 校验 type 参数
  1184. if (calibration(key, value)) return
  1185. if (key === 'title') {
  1186. this._navigationBarTitle.report = value;
  1187. return
  1188. }
  1189. this.sendEventRequest(
  1190. {
  1191. key,
  1192. value: typeof value === 'object' ? JSON.stringify(value) : value,
  1193. },
  1194. 1
  1195. );
  1196. }
  1197. }
  1198. class Stat extends Report {
  1199. static getInstance() {
  1200. if (!uni.__stat_instance) {
  1201. uni.__stat_instance = new Stat();
  1202. }
  1203. return uni.__stat_instance
  1204. }
  1205. constructor() {
  1206. super();
  1207. }
  1208. /**
  1209. * 获取推送id
  1210. */
  1211. pushEvent(options) {
  1212. const ClientID = is_push_clientid();
  1213. if (uni.getPushClientId && ClientID) {
  1214. uni.getPushClientId({
  1215. success: (res) => {
  1216. const cid = res.cid || false;
  1217. // 只有获取到才会上传
  1218. if (cid) {
  1219. this.sendPushRequest(options, cid);
  1220. }
  1221. },
  1222. });
  1223. }
  1224. }
  1225. /**
  1226. * 进入应用
  1227. * @param {Object} options 页面参数
  1228. * @param {Object} self 当前页面实例
  1229. */
  1230. launch(options, self) {
  1231. // 初始化页面停留时间 start
  1232. set_page_residence_time();
  1233. this.__licationShow = true;
  1234. dbSet('__launch_options', options);
  1235. // 应用初始上报参数为1
  1236. options.cst = 1;
  1237. this.sendReportRequest(options, true);
  1238. }
  1239. load(options, self) {
  1240. this.self = self;
  1241. this._query = options;
  1242. }
  1243. appHide(self) {
  1244. this.applicationHide(self, true);
  1245. }
  1246. appShow(self) {
  1247. this.applicationShow(self);
  1248. }
  1249. show(self) {
  1250. this.self = self;
  1251. if (get_page_types(self) === 'page') {
  1252. const isPageReport = is_page_report();
  1253. if (isPageReport) {
  1254. this.pageShow(self);
  1255. }
  1256. }
  1257. // #ifdef VUE3
  1258. if (get_platform_name() === 'h5' || get_platform_name() === 'n') {
  1259. if (get_page_types(self) === 'app') {
  1260. this.appShow();
  1261. }
  1262. }
  1263. // #endif
  1264. // #ifndef VUE3
  1265. if (get_page_types(self) === 'app') {
  1266. this.appShow();
  1267. }
  1268. // #endif
  1269. }
  1270. hide(self) {
  1271. this.self = self;
  1272. if (get_page_types(self) === 'page') {
  1273. const isPageReport = is_page_report();
  1274. if (isPageReport) {
  1275. this.pageHide(self);
  1276. }
  1277. }
  1278. // #ifdef VUE3
  1279. if (get_platform_name() === 'h5' || get_platform_name() === 'n') {
  1280. if (get_page_types(self) === 'app') {
  1281. this.appHide();
  1282. }
  1283. }
  1284. // #endif
  1285. // #ifndef VUE3
  1286. if (get_page_types(self) === 'app') {
  1287. this.appHide();
  1288. }
  1289. // #endif
  1290. }
  1291. error(em) {
  1292. // 开发工具内不上报错误
  1293. // if (this._platform === 'devtools') {
  1294. // if (process.env.NODE_ENV === 'development') {
  1295. // console.info('当前运行环境为开发者工具,不上报数据。')
  1296. // return
  1297. // }
  1298. // }
  1299. let emVal = '';
  1300. if (!em.message) {
  1301. emVal = JSON.stringify(em);
  1302. } else {
  1303. emVal = em.stack;
  1304. }
  1305. let route = '';
  1306. try {
  1307. route = get_route();
  1308. } catch (e) {
  1309. // 未获取到页面路径
  1310. route = '';
  1311. }
  1312. let options = {
  1313. ak: this.statData.ak,
  1314. uuid: this.statData.uuid,
  1315. p: this.statData.p,
  1316. lt: '31',
  1317. url: route,
  1318. ut: this.statData.ut,
  1319. ch: this.statData.ch,
  1320. mpsdk: this.statData.mpsdk,
  1321. mpv: this.statData.mpv,
  1322. v: this.statData.v,
  1323. em: emVal,
  1324. usv: this.statData.usv,
  1325. t: parseInt(new Date().getTime() / 1000),
  1326. };
  1327. this.request(options);
  1328. }
  1329. }
  1330. var Stat$1 = Stat;
  1331. const stat = Stat$1.getInstance();
  1332. // 用于判断是隐藏页面还是卸载页面
  1333. let isHide = false;
  1334. const lifecycle = {
  1335. onLaunch(options) {
  1336. // 进入应用上报数据
  1337. stat.launch(options, this);
  1338. // 上报push推送id
  1339. stat.pushEvent(options);
  1340. },
  1341. onLoad(options) {
  1342. stat.load(options, this);
  1343. // 重写分享,获取分享上报事件
  1344. if (this.$scope && this.$scope.onShareAppMessage) {
  1345. let oldShareAppMessage = this.$scope.onShareAppMessage;
  1346. this.$scope.onShareAppMessage = function (options) {
  1347. stat.interceptShare(false);
  1348. return oldShareAppMessage.call(this, options)
  1349. };
  1350. }
  1351. },
  1352. onShow() {
  1353. isHide = false;
  1354. stat.show(this);
  1355. },
  1356. onHide() {
  1357. isHide = true;
  1358. stat.hide(this);
  1359. },
  1360. onUnload() {
  1361. if (isHide) {
  1362. isHide = false;
  1363. return
  1364. }
  1365. stat.hide(this);
  1366. },
  1367. onError(e) {
  1368. stat.error(e);
  1369. },
  1370. };
  1371. // 加载统计代码
  1372. function load_stat() {
  1373. // #ifdef VUE3
  1374. uni.onCreateVueApp((app) => {
  1375. app.mixin(lifecycle);
  1376. uni.report = function (type, options) {
  1377. stat.sendEvent(type, options);
  1378. };
  1379. });
  1380. if (get_platform_name() !== 'h5' && get_platform_name() !== 'n') {
  1381. uni.onAppHide(() => {
  1382. stat.appHide(get_page_vm());
  1383. });
  1384. uni.onAppShow(() => {
  1385. stat.appShow(get_page_vm());
  1386. });
  1387. }
  1388. // #endif
  1389. // #ifndef VUE3
  1390. // eslint-disable-next-line no-restricted-globals
  1391. const Vue = require('vue')
  1392. ;(Vue.default || Vue).mixin(lifecycle);
  1393. uni.report = function (type, options) {
  1394. stat.sendEvent(type, options);
  1395. };
  1396. // #endif
  1397. }
  1398. function main() {
  1399. if (is_debug) {
  1400. {
  1401. // #ifndef APP-NVUE
  1402. console.log('=== uni统计开启,version:1.0 ===');
  1403. // #endif
  1404. }
  1405. load_stat();
  1406. } else {
  1407. if (process.env.NODE_ENV === 'development') {
  1408. uni.report = function (type, options) {};
  1409. } else {
  1410. load_stat();
  1411. }
  1412. }
  1413. }
  1414. main();