alipayLogger.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. /* eslint-disable */
  2. Date.now = Date.now || function () {
  3. return new Date().getTime();
  4. };
  5. let SEQUENCE = Date.now();
  6. const noop = function () {};
  7. const getCwarn = function () {
  8. const t = typeof console === 'object' ? console.warn : noop;
  9. try {
  10. const e = {
  11. warn: t
  12. };
  13. e.warn.call(e);
  14. } catch (n) {
  15. return noop;
  16. }
  17. return t;
  18. };
  19. var util = {
  20. noop,
  21. warn: getCwarn(),
  22. createObject(t) {
  23. if (Object.create) {
  24. return Object.create(t);
  25. }
  26. const e = function () {};
  27. return e.prototype = t, new e();
  28. },
  29. each(t, e) {
  30. let n = 0;
  31. const r = t.length;
  32. if (this.T(t, 'Array')) {
  33. for (; n < r && !1 !== e.call(t[n], t[n], n); n++) {}
  34. } else {
  35. for (n in t) {
  36. if (!1 === e.call(t[n], t[n], n)) {
  37. break;
  38. }
  39. }
  40. }
  41. return t;
  42. },
  43. safetyCall(t, e, n) {
  44. if (typeof t !== 'function') {
  45. return n;
  46. }
  47. try {
  48. return t.apply(this, e);
  49. } catch (r) {
  50. return n;
  51. }
  52. },
  53. ignoreByRule(t, e) {
  54. if (!t || !e) return !1;
  55. if ((this.isString(e) || e.source || this.T(e) === 'Function') && (e = [e]), !this.isArray(e)) {
  56. return util.warn('[cloudMonitor] invalid rules of ignore config, (list of) String/RegExp/Funcitons are available'), !1;
  57. }
  58. for (var n, r = [], o = 0, i = e.length; o < i; o++) if (n = e[o], this.isString(n)) r.push(n.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1'));else if (n && n.source) r.push(n.source);else if (n && this.T(n) === 'Function' && !0 === this.safetyCall(n, [t], !1)) return !0;
  59. const a = new RegExp(r.join('|'), 'i');
  60. return !!(r.length && a.test && a.test(t));
  61. },
  62. T(t, e) {
  63. const n = Object.prototype.toString.call(t).substring(8).replace(']', '');
  64. return e ? n === e : n;
  65. },
  66. J(t) {
  67. if (!t || typeof t !== 'string') {
  68. return t;
  69. }
  70. let e = null;
  71. try {
  72. e = JSON.parse(t);
  73. } catch (n) {}
  74. return e;
  75. },
  76. pick(t) {
  77. return t === 1 || Math.ceil(Math.random() * t) === 1;
  78. },
  79. verifyConfig(t) {
  80. if ('sample' in t) {
  81. const e = t.sample;
  82. let n = e;
  83. e && /^\d+(\.\d+)?%$/.test(e) && (n = parseInt(100 / parseFloat(e))), n > 0 && n < 1 && (n = parseInt(1 / n)), n >= 1 && n <= 100 ? t.sample = n : delete t.sample;
  84. }
  85. return t;
  86. },
  87. delay(t, e) {
  88. return e === -1 ? (t(), null) : setTimeout(t, e || 0);
  89. },
  90. ext(t) {
  91. for (let e = 1, n = arguments.length; e < n; e++) {
  92. const r = arguments[e];
  93. for (const o in r) {
  94. Object.prototype.hasOwnProperty.call(r, o) && (t[o] = r[o]);
  95. }
  96. }
  97. return t;
  98. },
  99. sub(t, e) {
  100. const n = {};
  101. return this.each(t, (t, r) => {
  102. e.indexOf(r) !== -1 && (n[r] = t);
  103. }), n;
  104. },
  105. uu() {
  106. for (var t, e, n = 20, r = new Array(n), o = Date.now().toString(36).split(''); n-- > 0;) {
  107. e = (t = 36 * Math.random() | 0).toString(36), r[n] = t % 3 ? e : e.toUpperCase();
  108. }
  109. for (let i = 0; i < 8; i++) {
  110. r.splice(3 * i + 2, 0, o[i]);
  111. }
  112. return r.join('');
  113. },
  114. seq() {
  115. return (SEQUENCE++).toString(36);
  116. },
  117. decode(t) {
  118. try {
  119. t = decodeURIComponent(t);
  120. } catch (e) {}
  121. return t;
  122. },
  123. encode(t, e) {
  124. try {
  125. t = e ? encodeURIComponent(t).replace(/\(/g, '%28').replace(/\)/g, '%29') : encodeURIComponent(t);
  126. } catch (n) {}
  127. return t;
  128. },
  129. serialize(t) {
  130. t = t || {};
  131. const e = [];
  132. for (const n in t) {
  133. Object.prototype.hasOwnProperty.call(t, n) && t[n] !== undefined && e.push(`${n}=${this.encode(t[n], n === 'msg')}`);
  134. }
  135. return e.join('&');
  136. },
  137. checkAPI(t, e) {
  138. if (!t || typeof t !== 'string') {
  139. return !1;
  140. }
  141. let n = /openmonitor(\.(dev|sit|test))?\.alipay[\w-]*/.test(t);
  142. return !n && e && (n = /(\.png)|(\.gif)|(alicdn\.com)/.test(t)), !n;
  143. },
  144. checkAutoError(t) {
  145. return !(!t || !t.message) && !/failed[\w\s]+fetch/i.test(t.message);
  146. },
  147. cutUrlSearch(t) {
  148. return t && typeof t === 'string' ? t.replace(/^(.*?https?:)?\/\//, '').replace(/\?.*$/, '').replace(/&.*$/, '').replace(/\/\d{1,}$/, '/*').replace(/\/\d{1,}\//, '/*/') : '';
  149. },
  150. patchPath(t) {
  151. if (!t || typeof t !== 'string') return '';
  152. t = t.replace(/^\//, '');
  153. return t.includes('pages/') ? t.replace(/\.\.\//g, '') : t.replace(/^\.\./, 'pages');
  154. },
  155. getRandIP() {
  156. for (var t = [], e = 0; e < 4; e++) {
  157. const n = Math.floor(256 * Math.random());
  158. t[e] = (n > 15 ? '' : '0') + n.toString(16);
  159. }
  160. return t.join('');
  161. },
  162. getSortNum(t) {
  163. return t ? (t += 1) >= 1000 && t <= 9999 ? t : t < 1000 ? t + 1000 : t % 10000 + 1000 : 1000;
  164. },
  165. getRandNum(t) {
  166. return t && typeof t === 'string' ? t.length < 5 ? this.getNum(5) : t.substring(t.length - 5) : this.getNum(5);
  167. },
  168. getNum(t) {
  169. for (var e = [], n = 0; n < t; n++) {
  170. const r = Math.floor(16 * Math.random());
  171. e[n] = r.toString(16);
  172. }
  173. return e.join('');
  174. },
  175. isFunction(t) {
  176. return typeof t === 'function';
  177. },
  178. isPlainObject(t) {
  179. return Object.prototype.toString.call(t) === '[object Object]';
  180. },
  181. isString(t) {
  182. return Object.prototype.toString.call(t) === '[object String]';
  183. },
  184. isArray(t) {
  185. return Object.prototype.toString.call(t) === '[object Array]';
  186. },
  187. joinRegExp(t) {
  188. for (var e, n = [], r = 0, o = t.length; r < o; r++) {
  189. e = t[r], this.isString(e) ? n.push(e.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1')) : e && e.source && n.push(e.source);
  190. }
  191. return new RegExp(n.join('|'), 'i');
  192. },
  193. getPropertyValue(j, arr) {
  194. if (!arr) return '';
  195. for (const k in j) {
  196. if (arr.indexOf(k) > -1 && typeof j[k] !== 'object') {
  197. return `${j[k]}`;
  198. }
  199. }
  200. return '';
  201. },
  202. hashCode(s) {
  203. let hash = 0;
  204. let i;
  205. let chr;
  206. for (i = 0; i < s.length; i++) {
  207. chr = s.charCodeAt(i);
  208. hash = (hash << 5) - hash + chr;
  209. hash |= 0; // Convert to 32bit integer
  210. }
  211. return hash;
  212. },
  213. toHex(s) {
  214. return !s ? null : `000${this.hashCode(s).toString(16)}`.slice(-4);
  215. },
  216. replaceAll(str, find, replace) {
  217. return str.replace(new RegExp(find, 'g'), replace);
  218. }
  219. };
  220. const util_1 = util;
  221. const pushToQueue = function (e, t) {
  222. let i;
  223. {
  224. if (t.t !== 'error' || !(i = e.requestQueue[0]) || i.t !== 'error' || t.msg !== i.msg) {
  225. if (t.t === 'behavior') {
  226. const n = e.requestQueue && e.requestQueue.length;
  227. if (n > 0 && e.requestQueue[n - 1].t === 'behavior') {
  228. const r = t.behavior || [];
  229. e.requestQueue[n - 1].behavior.concat(r);
  230. } else e.requestQueue.push(t);
  231. } else e.requestQueue.unshift(t);
  232. return e.onReady(() => {
  233. e.requestTimmer = util.delay(() => {
  234. e.clear();
  235. }, e.requestQueue[0] && e.requestQueue[0].t === 'error' ? 3e3 : -1);
  236. }), !0;
  237. }
  238. i.times++;
  239. }
  240. };
  241. var Base = function (t) {
  242. return this.ver = '1.0.9', this.syn = 0, this.err = 0, this._conf = util.ext({}, Base.dftCon), this.sampleCache = {}, this.requestQueue = [], this.hash = util.seq(), this.resetSession(), this.setConfig(t), this.rip = util.getRandIP(), this._common = {}, this;
  243. };
  244. Base.dftCon = {
  245. sample: 1,
  246. tag: '',
  247. imgUrls: ['https://openmonitor.alipay-eco.com/cloudmonitor/put.htm?'],
  248. release: undefined,
  249. environment: 'production',
  250. charset: 'utf-8'
  251. }, Base.prototype = {
  252. constructor: Base,
  253. getPage() {
  254. const e = this._conf.page;
  255. return util.safetyCall(e, [], `${e}`);
  256. },
  257. setPage() {},
  258. setConfig(e) {
  259. e && typeof e === 'object' && (util.verifyConfig(e), e = this.setImgUrl(e), this._conf = util.ext({}, this._conf, e));
  260. },
  261. setImgUrl(e) {
  262. return e.imgUrl && (this._conf.imgUrls = [e.imgUrl]), e;
  263. },
  264. sendRequest() {},
  265. postData() {},
  266. commonInfo() {
  267. return {};
  268. },
  269. setCommonInfo(e) {
  270. e && typeof e === 'object' && (this._common = util.ext({}, this._common, e));
  271. },
  272. resetSession() {
  273. this.session = util.uu(), this.sBegin = Date.now();
  274. },
  275. getConfig(e) {
  276. return e ? this._conf[e] : util.ext({}, this._conf);
  277. },
  278. sampling(e) {
  279. return e === 1 || (typeof this.sampleCache[e] === 'boolean' ? this.sampleCache[e] : (this.sampleCache[e] = util.pick(e), this.sampleCache[e]));
  280. },
  281. clear() {
  282. let e;
  283. const t = this;
  284. if (t.syn > 2) {
  285. return clearTimeout(t.requestTimmer), t.requestTimmer = setTimeout(() => {
  286. t.clear();
  287. }, 50), t;
  288. }
  289. for (clearTimeout(t.requestTimmer), t.requestTimmer = null; t.syn < 2 && (e = t.requestQueue.pop()); t.syn++) {
  290. e.t === 'res' ? t.postData(e, 'res') : e.t === 'error' ? t.postData(e, 'err') : e.t === 'behavior' ? t.postData(e, 'behavior') : t.sendRequest(e);
  291. }
  292. !!t.requestQueue.length && (t.requestTimmer = setTimeout(() => {
  293. t.clear();
  294. }, 50));
  295. return this;
  296. },
  297. getToken(e) {
  298. const t = this._conf.token;
  299. return !t ? '-' : t[e] || '-';
  300. },
  301. _lg(e, t, i) {
  302. const n = this._conf;
  303. const r = t.page || util.patchPath(this.getPage());
  304. const s = n.ignore || {};
  305. const o = s.ignoreErrors;
  306. const u = s.ignoreUrls;
  307. const a = s.ignoreApis;
  308. return util.ignoreByRule(r, u) || util.ignoreByRule(util.decode(r), u) ? this : e === 'error' && (util.ignoreByRule(t.msg, o) || util.ignoreByRule(util.decode(t.msg), o)) ? this : e === 'api' && (util.ignoreByRule(t.api, a) || util.ignoreByRule(util.decode(t.api), a)) ? this : t && !n.disabled && (n.pid || n.token) ? i && !this.sampling(i) ? this : (t = util.ext({
  309. t: e,
  310. page: r,
  311. environment: n.environment,
  312. _input_charset: n.charset,
  313. timestamp: Date.now()
  314. }, t, this.commonInfo(), this._common, {
  315. token: this.getToken(e),
  316. pid: n.pid || '-',
  317. _v: this.ver,
  318. sampling: i || 1
  319. }), pushToQueue(this, t)) : this;
  320. },
  321. custom(e, t) {
  322. if (!e || typeof e !== 'object') {
  323. return this;
  324. }
  325. let i = !1;
  326. const r = {
  327. timestamp: Date.now()
  328. };
  329. return util.each(e, (e, t) => !(i = t && t.length <= 20) && util.warn(`[CloudMonitor] invalid key: ${t}`), r[`x-${t}`] = e, i), i ? this._lg('custom', r, t || 1) : this;
  330. },
  331. logInfo() {
  332. if (arguments.length === 0) return;
  333. let msg = '';
  334. for (let e = 0, n = arguments.length; e < n; e++) {
  335. msg += ` ${JSON.stringify(arguments[e])}`;
  336. }
  337. const r = {
  338. timestamp: Date.now(),
  339. level: 'info',
  340. msg: msg.substring(1)
  341. };
  342. return this._lg('log', r, 1), this;
  343. },
  344. report(k, e) {
  345. if (!k || !e || typeof e !== 'object') {
  346. return this;
  347. }
  348. let i = !1;
  349. const r = {
  350. timestamp: Date.now()
  351. };
  352. return util.each(e, (e, t) => !(i = t && t.length <= 20) && util.warn(`[CloudMonitor] invalid key: ${t}`), r[`x-${t}`] = e, i), i ? this._lg(k, r, 1) : this;
  353. }
  354. };
  355. const base = Base;
  356. const validApiKeys = ['api', 'success', 'time', 'code', 'msg', 'begin', 'response', 'c1', 'c2', 'c3'];
  357. const parseStatData = function (t, e) {
  358. const r = t.split('::');
  359. return r.length > 1 ? util_1.ext({
  360. group: r[0],
  361. key: r[1]
  362. }, e) : util_1.ext({
  363. group: 'default_group',
  364. key: r[0]
  365. }, e);
  366. };
  367. const Reporter = function (t) {
  368. base.call(this, t);
  369. let e;
  370. try {
  371. e = typeof performance === 'object' ? performance.timing.fetchStart : Date.now();
  372. } catch (r) {
  373. e = Date.now();
  374. }
  375. return this._startTime = e, this;
  376. };
  377. Reporter.prototype = util_1.createObject(base.prototype), util_1.ext(base.dftCon, {
  378. startTime: null
  379. }), util_1.ext(Reporter.prototype, {
  380. constructor: Reporter,
  381. _super: base,
  382. sum(t, e, r) {
  383. try {
  384. return this._lg('sum', parseStatData(t, {
  385. val: e || 1,
  386. begin: Date.now()
  387. }), r);
  388. } catch (n) {
  389. util_1.warn(`[retcode] can not get parseStatData: ${n}`);
  390. }
  391. },
  392. avg(t, e, r) {
  393. try {
  394. return this._lg('avg', parseStatData(t, {
  395. val: e || 0,
  396. begin: Date.now()
  397. }), r);
  398. } catch (n) {
  399. util_1.warn(`[retcode] can not get parseStatData: ${n}`);
  400. }
  401. },
  402. percent(t, e, r, n) {
  403. try {
  404. return this._lg('percent', parseStatData(t, {
  405. subkey: e,
  406. val: r || 0,
  407. begin: Date.now()
  408. }), n);
  409. } catch (i) {
  410. util_1.warn(`[retcode] can not get parseStatData: ${i}`);
  411. }
  412. },
  413. msg(t, e) {
  414. if (t && !(t.length > 180)) {
  415. return this.custom({
  416. msg: t
  417. }, e);
  418. }
  419. },
  420. error(t, e) {
  421. if (!t) {
  422. return util_1.warn(`[cloudMonitor] invalid param e: ${t}`), this;
  423. }
  424. arguments.length === 1 ? (typeof t === 'string' && (t = {
  425. message: t
  426. }, e = {}), typeof t === 'object' && (e = t = t.error || t)) : (typeof t === 'string' && (t = {
  427. message: t
  428. }), typeof e !== 'object' && (e = {}));
  429. const r = t.name || 'CustomError';
  430. var n = util_1.encode(t.message);
  431. const i = util_1.encode(util_1.replaceAll(t.stack || '', 'http.*\\d*:\\d*', 'unknow'));
  432. e = e || {};
  433. const s = {
  434. begin: Date.now(),
  435. cate: r,
  436. msg: n.substring(0, 256),
  437. stack: i && i.substring(0, 256),
  438. file: e.filename || '',
  439. line: e.lineno || '',
  440. col: e.colno || '',
  441. err: {
  442. msg_raw: n,
  443. stack_raw: i
  444. }
  445. };
  446. var n = (this.getConfig('ignore') || {}).ignoreErrors;
  447. return util.ignoreByRule(s.msg, n) || util.ignoreByRule(util.decode(s.msg), n) ? this : this.beforeSend && this.beforeSend('error', s), this._lg('error', s, 1);
  448. },
  449. behavior(t) {
  450. if (t) {
  451. const e = typeof t === 'object' && t.behavior ? t : {
  452. behavior: t
  453. };
  454. return this.beforeSend && this.beforeSend('behavior', t), this._lg('behavior', e, 1);
  455. }
  456. },
  457. api(e, t, r, i, a, s) {
  458. if (!e) {
  459. return util.warn('[cloudMonitor] api is null'), this;
  460. }
  461. e = typeof e === 'string' ? {
  462. api: e,
  463. success: t,
  464. time: r,
  465. code: i,
  466. msg: a,
  467. begin: s
  468. } : util.sub(e, validApiKeys);
  469. if (e.code = e.code || '', e.msg = e.msg || '', e.success = e.success ? 1 : 0, e.time = +e.time, e.begin = e.begin || '', !e.api || isNaN(e.time)) {
  470. return util.warn('[cloudMonitor] invalid time or api'), this;
  471. }
  472. const c = (this.getConfig('ignore') || {}).ignoreApis;
  473. if (util.ignoreByRule(e.api, c) || util.ignoreByRule(util.decode(e.api), c)) return this;
  474. this.beforeSend && this.beforeSend('api', e);
  475. return this._lg('api', e, e.success && !e.response && this.getConfig('sample'));
  476. },
  477. resource(t, e) {
  478. if (!t || !util_1.isPlainObject(t)) {
  479. return util_1.warn(`[cloudMonitor] invalid param data: ${t}`), this;
  480. }
  481. const r = Object.keys(t);
  482. const n = ['begin', 'dom', 'load', 'res', 'dl'];
  483. let i = !1;
  484. for (const o in n) {
  485. if (r.indexOf(n[o]) < 0) {
  486. i = !0;
  487. break;
  488. }
  489. }
  490. if (i) {
  491. return util_1.warn(`[cloudMonitor] lack param data: ${t}`), this;
  492. }
  493. const a = {
  494. begin: t.begin || Date.now(),
  495. dom: t.dom || '',
  496. load: t.load || '',
  497. res: util_1.isArray(t.res) ? JSON.stringify(t.res) : JSON.stringify([]),
  498. dl: t.dl || ''
  499. };
  500. return this._lg('res', a, e);
  501. }
  502. }), Reporter._super = base, Reporter._root = base, base.Reporter = Reporter;
  503. const reporter = Reporter;
  504. const MiniProgramLogger = function (t) {
  505. const o = this;
  506. return Reporter.call(o, t), o._health = {
  507. errcount: 0,
  508. apisucc: 0,
  509. apifail: 0
  510. }, o.DEFAUT_PAGE_PATH = '[app]', o.isSendPerf = !1, o.beforeSend = function (e, t) {
  511. e === 'error' ? o._health.errcount++ : e === 'api' && o._health[t.success ? 'apisucc' : 'apifail']++;
  512. }, typeof o.autoSetCommonInfo === 'function' && o.autoSetCommonInfo(), this;
  513. };
  514. MiniProgramLogger.prototype = util.createObject(Reporter.prototype), util.ext(Reporter._root.dftCon, {
  515. sendRequest() {},
  516. getCurrentPage() {},
  517. getPrePage() {}
  518. }), util.ext(MiniProgramLogger.prototype, {
  519. constructor: MiniProgramLogger,
  520. _super: Reporter,
  521. onReady(e) {
  522. const t = this;
  523. t._common.uid ? e() : setTimeout(() => {
  524. t.onReady(e);
  525. }, 100);
  526. },
  527. sendRequest(e, o) {
  528. const a = this;
  529. if (a.getConfig('debug') || a.err > 0) {
  530. typeof console !== 'undefined' && console && typeof console.log === 'function' && console.log('[cloudMonitor] [DEBUG MODE] log data', e);
  531. } else {
  532. const r = a._conf.sendRequest;
  533. util.each(a.getConfig('imgUrls'), (t, i) => {
  534. typeof e === 'object' && (e = util.serialize(e));
  535. let n = t + e;
  536. o && (n += '&post_res=');
  537. if (typeof r === 'function') {
  538. try {
  539. !i ? r(n, o, a) : r(n, o);
  540. } catch (i) {
  541. util.warn('[cloudMonitor] error in sendRequest', i);
  542. }
  543. }
  544. });
  545. }
  546. },
  547. postData(e, o) {
  548. const t = {};
  549. t[o] = e[o], delete e[o], this.sendRequest(e, t);
  550. },
  551. getPage(delta) {
  552. const e = this._conf.getCurrentPage;
  553. if (typeof e === 'function') {
  554. try {
  555. const o = e();
  556. if (o && typeof o === 'string') {
  557. return o;
  558. }
  559. } catch (t) {
  560. util.warn('[cloudMonitor] error in getPage', t);
  561. }
  562. }
  563. return typeof e === 'string' && e ? e : this.DEFAUT_PAGE_PATH;
  564. },
  565. addHook() {
  566. return this;
  567. },
  568. removeHook() {
  569. return this;
  570. },
  571. hookApp(e) {
  572. const o = this;
  573. const t = {
  574. onError(t) {
  575. const n = arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments);
  576. const r = e.onError;
  577. try {
  578. o.error(t);
  579. } catch (t) {
  580. util.warn('[cloudMonitor] error in hookApp:onError', t);
  581. }
  582. if (typeof r === 'function') {
  583. return r.apply(this, n);
  584. }
  585. }
  586. };
  587. return util.ext({}, e, t);
  588. },
  589. autoSetCommonInfo() {
  590. this.setCommonInfo({
  591. app: 'mini_common',
  592. uid: this._conf.uid
  593. });
  594. },
  595. _lgPV(t) {
  596. if (!t) return;
  597. if (typeof t === 'string') {
  598. t = {
  599. page: t
  600. };
  601. }
  602. t.page = util.cutUrlSearch(t.page);
  603. t.url && (t.url = util.cutUrlSearch(t.url));
  604. !t.spmPre && (t.spmPre = util.toHex(this.getPage()) || ''), !t.spmId && (t.spmId = util.toHex(t.page) || ''), !t.scene && (t.scene = ''), this._lg('pv', t);
  605. },
  606. sendHealthOnPageShowOrHide() {
  607. try {
  608. this.sessionPage = this.getPage(), this.sendHealth(), this.speedCache && (this._lg('speed', this.speedCache), this.speedCache = null, clearTimeout(this.speedTimmer)), this.clear();
  609. } catch (o) {
  610. util.warn('[cloudMonitor] error in sendHealthOnPageShowOrHide', o);
  611. }
  612. },
  613. sendHealth() {
  614. if (this.sessionPage) {
  615. const e = util.ext({}, this._health);
  616. e.healthy = e.errcount > 0 ? 0 : 1, e.begin = Date.now();
  617. const o = e.begin - this.sBegin;
  618. e.page = this.sessionPage, e.stay = o, this._lg('health', e, 1), this._health = {
  619. errcount: 0,
  620. apisucc: 0,
  621. apifail: 0
  622. }, this.sessionPage = null;
  623. }
  624. },
  625. parseResponse(e) {
  626. if (!e || typeof e !== 'object') return {};
  627. const r = '';
  628. let c = util.getPropertyValue(e, this._conf.code) || e.stat || e.status || e.code || e.success;
  629. let u = util.getPropertyValue(e, this._conf.msg) || e.msg || e.message || e.subMsg || e.errorMsg || e.ret || e.errorResponse || '';
  630. typeof u === 'object' && (c = c || u.code, u = u.msg || u.message || u.info || u.ret || JSON.stringify(u));
  631. return {
  632. code: c,
  633. msg: u,
  634. response: util.pick(10000) ? JSON.stringify(e).substr(0, 256) : ''
  635. };
  636. }
  637. });
  638. MiniProgramLogger._super = reporter, MiniProgramLogger._root = reporter._root, reporter.MiniProgramLogger = MiniProgramLogger;
  639. const miniProgramLogger = MiniProgramLogger;
  640. const STORAGE_MINIPROGRAM_ALIPAY_UID_KEY = 'STORAGE_MINIPROGRAM_ALIPAY_UID';
  641. const AlipayLogger = function (t) {
  642. return MiniProgramLogger.call(this, t), this;
  643. };
  644. AlipayLogger.prototype = util.createObject(MiniProgramLogger.prototype), util.ext(MiniProgramLogger._root.dftCon, {
  645. sendRequest(t, e, a) {
  646. if (typeof my !== 'undefined' && my && typeof my.request === 'function') {
  647. try {
  648. let o;
  649. let r = 'GET';
  650. e && (r = 'POST', o = JSON.stringify(e)), my.request({
  651. url: t,
  652. method: r,
  653. data: o,
  654. dataType: 'json',
  655. fail(t) {
  656. util.warn('[cloudMonitor] sendRequest fail', t);
  657. a && a.err++;
  658. },
  659. complete(_t) {
  660. a && a.syn--;
  661. }
  662. });
  663. } catch (n) {
  664. util.warn('[cloudMonitor] error in conf sendRequest', n);
  665. }
  666. }
  667. },
  668. getCurrentPage() {
  669. if (typeof getCurrentPages === 'function') {
  670. try {
  671. const t = getCurrentPages() || [];
  672. const e = t[t.length - 1];
  673. return e && e.route || null;
  674. } catch (o) {
  675. util.warn('[cloudMonitor] error in conf getCurrentPage', o);
  676. }
  677. }
  678. },
  679. getPrePage(delta) {
  680. if (typeof getCurrentPages === 'function') {
  681. try {
  682. const t = getCurrentPages() || [];
  683. const e = delta === undefined ? t[t.length - 2] : delta >= t.length ? t[0] : t[t.length - 1 - delta];
  684. return e && e.route || null;
  685. } catch (o) {
  686. util.warn('[cloudMonitor] error in conf getCurrentPage', o);
  687. }
  688. }
  689. }
  690. }), util.ext(AlipayLogger.prototype, {
  691. constructor: AlipayLogger,
  692. _super: MiniProgramLogger,
  693. autoSetCommonInfo() {
  694. this.setCommonInfo({
  695. app: 'mini_alipay'
  696. }), this.autoSetNetworkType(), this.autoSetUid(), this.autoSystemInfo(), this.autoSetAppId(), this.autoSetSDKVersion(), this.autoSetRunScene();
  697. },
  698. autoSetUid() {
  699. const t = this;
  700. if (t._conf && t._conf.uid) {
  701. t.setCommonInfo({
  702. uid: t._conf.uid
  703. });
  704. } else if (typeof my !== 'undefined' && my && typeof my.getStorage === 'function') {
  705. try {
  706. my.getStorage({
  707. key: STORAGE_MINIPROGRAM_ALIPAY_UID_KEY,
  708. success(e) {
  709. if (e && e.data && typeof e.data === 'string') {
  710. t.setCommonInfo({
  711. uid: e.data
  712. });
  713. } else if (typeof my.setStorage === 'function') {
  714. const o = util.uu();
  715. my.setStorage({
  716. key: STORAGE_MINIPROGRAM_ALIPAY_UID_KEY,
  717. data: o,
  718. success() {
  719. t.setCommonInfo({
  720. uid: o
  721. });
  722. },
  723. fail(t) {
  724. util.warn('[cloudMonitor] error in setStorage', t);
  725. }
  726. });
  727. }
  728. },
  729. fail(t) {
  730. util.warn('[cloudMonitor] error in getStorage', t);
  731. }
  732. });
  733. } catch (e) {
  734. util.warn('[cloudMonitor] error in autoSetUid', e);
  735. }
  736. }
  737. },
  738. autoSystemInfo() {
  739. const t = this;
  740. if (typeof my !== 'undefined' && my && typeof my.getSystemInfo === 'function') {
  741. try {
  742. my.getSystemInfo({
  743. success(e) {
  744. e && typeof e.model === 'string' && typeof e.version === 'string' && typeof e.platform === 'string' && typeof e.language === 'string' && t.setCommonInfo({
  745. mobile: e.model,
  746. version: e.version,
  747. platform: e.platform,
  748. system: e.system,
  749. resolution: `${e.windowWidth}*${e.windowHeight}`,
  750. language: e.language
  751. });
  752. },
  753. fail(t) {
  754. util.warn('[cloudMonitor] autoSystemInfo getSystemInfo fail', t);
  755. }
  756. });
  757. } catch (e) {
  758. util.warn('[cloudMonitor] error in getSystemInfo', e);
  759. }
  760. }
  761. },
  762. autoSetNetworkType() {
  763. const t = this;
  764. if (typeof my !== 'undefined' && my && typeof my.getNetworkType === 'function') {
  765. try {
  766. my.getNetworkType({
  767. success(e) {
  768. e && typeof e.networkType === 'string' && t.setCommonInfo({
  769. net: e.networkType
  770. });
  771. },
  772. fail(t) {
  773. util.warn('[cloudMonitor] autoSetNetworkType getNetworkType fail', t);
  774. }
  775. });
  776. } catch (e) {
  777. util.warn('[cloudMonitor] error in autoSetNetworkType', e);
  778. }
  779. }
  780. },
  781. autoSetAppId() {
  782. const t = this;
  783. if (typeof my !== 'undefined' && my && typeof my.getAppIdSync === 'function') {
  784. try {
  785. const appIdRes = my.getAppIdSync();
  786. t.setCommonInfo({
  787. appId: appIdRes.appId
  788. });
  789. } catch (e) {
  790. util.warn('[cloudMonitor] error in autoSetNetworkType', e);
  791. }
  792. }
  793. },
  794. autoSetSDKVersion() {
  795. const t = this;
  796. if (typeof my !== 'undefined' && my && typeof my.SDKVersion === 'string') {
  797. t.setCommonInfo({
  798. sdk_version: my.SDKVersion
  799. });
  800. }
  801. },
  802. autoSetRunScene() {
  803. const t = this;
  804. if (typeof my !== 'undefined' && my && typeof my.getRunScene === 'function') {
  805. try {
  806. my.getRunScene({
  807. success(e) {
  808. t._conf.environment = e.envVersion;
  809. }
  810. });
  811. } catch (e) {
  812. util.warn('[cloudMonitor] error in autoSetRunScene', e);
  813. }
  814. }
  815. },
  816. addHook() {
  817. const e = this;
  818. const t = util;
  819. const ca = e._conf.autoReportApi;
  820. const cp = e._conf.autoReportPage;
  821. !e.isHookInstantiated && (function () {
  822. if (ca) {
  823. if (typeof my !== 'undefined' && my && typeof my.request === 'function') {
  824. const s = my.request;
  825. var a = Object.getOwnPropertyDescriptor(my, 'request');
  826. a && a.writable && (my.request = function (i) {
  827. const a = new Date().getTime();
  828. let n = i;
  829. if (i && i.url) {
  830. const o = t.cutUrlSearch(i.url);
  831. if (!t.checkAPI(o, !0)) {
  832. return s.call(my, n);
  833. }
  834. let u = n && n.headers;
  835. const f = e._conf.extendApi;
  836. u && typeof u === 'object' || (u = {});
  837. const p = {
  838. success(t) {
  839. typeof i.success === 'function' && i.success(t);
  840. const s = new Date().getTime();
  841. let u = {};
  842. if (t.data && (!i.dataType || i.dataType === 'json')) {
  843. u = e.parseResponse(t.data);
  844. f && (u = util.ext(u, util.safetyCall(f, [i.data, t.data], {})));
  845. }
  846. e.api(util.ext({
  847. api: o,
  848. success: !0,
  849. time: s - a,
  850. code: '',
  851. msg: '',
  852. response: ''
  853. }, u));
  854. },
  855. fail(t) {
  856. typeof i.fail === 'function' && i.fail(t);
  857. const s = new Date().getTime();
  858. let n = '';
  859. t && t.status && (n = t.status);
  860. n == '' && t.error && (n = t.error);
  861. let u = '';
  862. const r = f ? util.safetyCall(f, [i.data], {}) : {};
  863. t && t.body && (u = (u = JSON.stringify(t.body)).substring(0, 256)), e.api(util.ext({
  864. api: o,
  865. success: !1,
  866. time: s - a,
  867. code: n,
  868. msg: u
  869. }, r));
  870. }
  871. };
  872. n = t.ext({}, n, p);
  873. }
  874. return s.call(my, n);
  875. });
  876. }
  877. }
  878. if (cp && typeof my !== 'undefined' && my) {
  879. if (typeof my.call === 'function') {
  880. const c = my.call;
  881. var a = Object.getOwnPropertyDescriptor(my, 'call');
  882. a && a.writable && (my.call = function (i, j, k) {
  883. if (i && i == 'startApp' && j && j.param && j.param.url) {
  884. let o = j.param.url;
  885. o = t.cutUrlSearch(o);
  886. e._lgPV({
  887. page: o,
  888. c3: 'c'
  889. });
  890. }
  891. return e.sendHealthOnPageShowOrHide(), c.call(my, i, j, k);
  892. });
  893. }
  894. if (typeof my.navigateTo === 'function') {
  895. const n = my.navigateTo;
  896. var a = Object.getOwnPropertyDescriptor(my, 'navigateTo');
  897. a && a.writable && (my.navigateTo = function (i) {
  898. if (i && i.url) {
  899. const o = t.cutUrlSearch(t.patchPath(i.url));
  900. e._lgPV({
  901. page: o,
  902. c3: 'nt'
  903. });
  904. }
  905. return e.sendHealthOnPageShowOrHide(), n.call(my, i);
  906. });
  907. }
  908. if (typeof my.navigateBack === 'function') {
  909. const w = my.navigateBack;
  910. var a = Object.getOwnPropertyDescriptor(my, 'navigateBack');
  911. a && a.writable && (my.navigateBack = function (i) {
  912. e._lgPV({
  913. page: e._conf.getPrePage(i || 1),
  914. c3: 'nb'
  915. });
  916. return e.sendHealthOnPageShowOrHide(), w.call(my, i);
  917. });
  918. }
  919. if (typeof my.redirectTo === 'function') {
  920. const x = my.redirectTo;
  921. var a = Object.getOwnPropertyDescriptor(my, 'redirectTo');
  922. a && a.writable && (my.redirectTo = function (i) {
  923. if (i && i.url) {
  924. const o = t.cutUrlSearch(t.patchPath(i.url));
  925. e._lgPV({
  926. page: o,
  927. c3: 'rt'
  928. });
  929. }
  930. return e.sendHealthOnPageShowOrHide(), x.call(my, i);
  931. });
  932. }
  933. if (typeof my.switchTab === 'function') {
  934. const y = my.switchTab;
  935. var a = Object.getOwnPropertyDescriptor(my, 'switchTab');
  936. a && a.writable && (my.switchTab = function (i) {
  937. if (i && i.url) {
  938. const o = t.cutUrlSearch(t.patchPath(i.url));
  939. e._lgPV({
  940. page: o,
  941. spmPre: 'T',
  942. c3: 'st'
  943. });
  944. }
  945. return e.sendHealthOnPageShowOrHide(), y.call(my, i);
  946. });
  947. }
  948. if (typeof my.reLaunch === 'function') {
  949. const z = my.reLaunch;
  950. var a = Object.getOwnPropertyDescriptor(my, 'reLaunch');
  951. a && a.writable && (my.reLaunch = function (i) {
  952. if (i && i.url) {
  953. const o = t.cutUrlSearch(t.patchPath(i.url));
  954. e._lgPV({
  955. page: o,
  956. spmPre: 'T',
  957. c3: 'rl'
  958. });
  959. }
  960. return z.call(my, i);
  961. });
  962. }
  963. if (typeof my.navigateToMiniProgram === 'function') {
  964. const m = my.navigateToMiniProgram;
  965. var a = Object.getOwnPropertyDescriptor(my, 'navigateToMiniProgram');
  966. a && a.writable && (my.navigateToMiniProgram = function (i) {
  967. if (i) {
  968. let o = i.path || i.appId || '-';
  969. o = t.cutUrlSearch(o);
  970. e._lgPV({
  971. page: o,
  972. c3: 'ntmp'
  973. });
  974. }
  975. return m.call(my, i);
  976. });
  977. }
  978. }
  979. }.call(e), e.isHookInstantiated = !0);
  980. return e;
  981. },
  982. init(t) {
  983. if (!t || !t.pid) {
  984. util.warn('[cloudMonitor] not set pid');
  985. }
  986. let p;
  987. const e = this;
  988. if (t.options) {
  989. p = t.options.path, t.scene = t.options.scene, delete t.options;
  990. }
  991. if (!t.ignore) {
  992. t.ignore = {
  993. ignoreUrls: [/\.(png|jpg|gif|jpeg)$/, /[\"{}]+/, /\.tfs.alipayobjects.com\.*/]
  994. };
  995. }
  996. this.setConfig(t), this.addHook();
  997. if (p) {
  998. e.entrance = util.toHex(p);
  999. e.onReady(() => {
  1000. e._lg('pv', {
  1001. spmPre: 'E',
  1002. spmId: e.entrance || '',
  1003. scene: e._conf.scene || '',
  1004. c3: 'init'
  1005. });
  1006. });
  1007. }
  1008. return this;
  1009. }
  1010. });
  1011. AlipayLogger._super = miniProgramLogger, AlipayLogger._root = miniProgramLogger._root, miniProgramLogger.AlipayLogger = AlipayLogger;
  1012. export default new AlipayLogger();