足力健前端,vue版本

index.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. import { version } from '../package.json';
  2. const STAT_VERSION = version;
  3. const STAT_URL = 'https://tongji.dcloud.io/uni/stat';
  4. const STAT_H5_URL = 'https://tongji.dcloud.io/uni/stat.gif';
  5. const PAGE_PVER_TIME = 1800;
  6. const APP_PVER_TIME = 300;
  7. const OPERATING_TIME = 10;
  8. const UUID_KEY = '__DC_STAT_UUID';
  9. const UUID_VALUE = '__DC_UUID_VALUE';
  10. function getUuid() {
  11. let uuid = '';
  12. if (getPlatformName() === 'n') {
  13. try {
  14. uuid = plus.runtime.getDCloudId();
  15. } catch (e) {
  16. uuid = '';
  17. }
  18. return uuid
  19. }
  20. try {
  21. uuid = uni.getStorageSync(UUID_KEY);
  22. } catch (e) {
  23. uuid = UUID_VALUE;
  24. }
  25. if (!uuid) {
  26. uuid = Date.now() + '' + Math.floor(Math.random() * 1e7);
  27. try {
  28. uni.setStorageSync(UUID_KEY, uuid);
  29. } catch (e) {
  30. uni.setStorageSync(UUID_KEY, UUID_VALUE);
  31. }
  32. }
  33. return uuid;
  34. }
  35. const getSgin = (statData) => {
  36. let arr = Object.keys(statData);
  37. let sortArr = arr.sort();
  38. let sgin = {};
  39. let sginStr = '';
  40. for (var i in sortArr) {
  41. sgin[sortArr[i]] = statData[sortArr[i]];
  42. sginStr += sortArr[i] + '=' + statData[sortArr[i]] + '&';
  43. }
  44. // const options = sginStr.substr(0, sginStr.length - 1)
  45. // sginStr = sginStr.substr(0, sginStr.length - 1) + '&key=' + STAT_KEY;
  46. // const si = crypto.createHash('md5').update(sginStr).digest('hex');
  47. return {
  48. sign: '',
  49. options: sginStr.substr(0, sginStr.length - 1)
  50. };
  51. };
  52. const getSplicing = (data) => {
  53. let str = '';
  54. for (var i in data) {
  55. str += i + '=' + data[i] + '&';
  56. }
  57. return str.substr(0, str.length - 1)
  58. };
  59. const getTime = () => {
  60. return parseInt(new Date().getTime() / 1000);
  61. };
  62. const getPlatformName = () => {
  63. const platformList = {
  64. 'app-plus': 'n',
  65. 'h5': 'h5',
  66. 'mp-weixin': 'wx',
  67. 'mp-alipay': 'ali',
  68. 'mp-baidu': 'bd',
  69. 'mp-toutiao': 'tt',
  70. 'mp-qq': 'qq'
  71. };
  72. return platformList[process.env.VUE_APP_PLATFORM];
  73. };
  74. const getPackName = () => {
  75. let packName = '';
  76. if (getPlatformName() === 'wx' || getPlatformName() === 'qq') {
  77. // 兼容微信小程序低版本基础库
  78. if(uni.canIUse('getAccountInfoSync')){
  79. packName = uni.getAccountInfoSync().miniProgram.appId || '';
  80. }
  81. }
  82. return packName
  83. };
  84. const getVersion = () => {
  85. return getPlatformName() === 'n' ? plus.runtime.version : '';
  86. };
  87. const getChannel = () => {
  88. const platformName = getPlatformName();
  89. let channel = '';
  90. if (platformName === 'n') {
  91. channel = plus.runtime.channel;
  92. }
  93. return channel;
  94. };
  95. const getScene = (options) => {
  96. const platformName = getPlatformName();
  97. let scene = '';
  98. if (options) {
  99. return options;
  100. }
  101. if (platformName === 'wx') {
  102. scene = uni.getLaunchOptionsSync().scene;
  103. }
  104. return scene;
  105. };
  106. const First__Visit__Time__KEY = 'First__Visit__Time';
  107. const Last__Visit__Time__KEY = 'Last__Visit__Time';
  108. const getFirstVisitTime = () => {
  109. const timeStorge = uni.getStorageSync(First__Visit__Time__KEY);
  110. let time = 0;
  111. if (timeStorge) {
  112. time = timeStorge;
  113. } else {
  114. time = getTime();
  115. uni.setStorageSync(First__Visit__Time__KEY, time);
  116. uni.removeStorageSync(Last__Visit__Time__KEY);
  117. }
  118. return time;
  119. };
  120. const getLastVisitTime = () => {
  121. const timeStorge = uni.getStorageSync(Last__Visit__Time__KEY);
  122. let time = 0;
  123. if (timeStorge) {
  124. time = timeStorge;
  125. } else {
  126. time = '';
  127. }
  128. uni.setStorageSync(Last__Visit__Time__KEY, getTime());
  129. return time;
  130. };
  131. const PAGE_RESIDENCE_TIME = '__page__residence__time';
  132. let First_Page_residence_time = 0;
  133. let Last_Page_residence_time = 0;
  134. const setPageResidenceTime = () => {
  135. First_Page_residence_time = getTime();
  136. if (getPlatformName() === 'n') {
  137. uni.setStorageSync(PAGE_RESIDENCE_TIME, getTime());
  138. }
  139. return First_Page_residence_time
  140. };
  141. const getPageResidenceTime = () => {
  142. Last_Page_residence_time = getTime();
  143. if (getPlatformName() === 'n') {
  144. First_Page_residence_time = uni.getStorageSync(PAGE_RESIDENCE_TIME);
  145. }
  146. return Last_Page_residence_time - First_Page_residence_time
  147. };
  148. const TOTAL__VISIT__COUNT = 'Total__Visit__Count';
  149. const getTotalVisitCount = () => {
  150. const timeStorge = uni.getStorageSync(TOTAL__VISIT__COUNT);
  151. let count = 1;
  152. if (timeStorge) {
  153. count = timeStorge;
  154. count++;
  155. }
  156. uni.setStorageSync(TOTAL__VISIT__COUNT, count);
  157. return count;
  158. };
  159. const GetEncodeURIComponentOptions = (statData) => {
  160. let data = {};
  161. for (let prop in statData) {
  162. data[prop] = encodeURIComponent(statData[prop]);
  163. }
  164. return data;
  165. };
  166. let Set__First__Time = 0;
  167. let Set__Last__Time = 0;
  168. const getFirstTime = () => {
  169. let time = new Date().getTime();
  170. Set__First__Time = time;
  171. Set__Last__Time = 0;
  172. return time;
  173. };
  174. const getLastTime = () => {
  175. let time = new Date().getTime();
  176. Set__Last__Time = time;
  177. return time;
  178. };
  179. const getResidenceTime = (type) => {
  180. let residenceTime = 0;
  181. if (Set__First__Time !== 0) {
  182. residenceTime = Set__Last__Time - Set__First__Time;
  183. }
  184. residenceTime = parseInt(residenceTime / 1000);
  185. residenceTime = residenceTime < 1 ? 1 : residenceTime;
  186. if (type === 'app') {
  187. let overtime = residenceTime > APP_PVER_TIME ? true : false;
  188. return {
  189. residenceTime,
  190. overtime
  191. };
  192. }
  193. if (type === 'page') {
  194. let overtime = residenceTime > PAGE_PVER_TIME ? true : false;
  195. return {
  196. residenceTime,
  197. overtime
  198. };
  199. }
  200. return {
  201. residenceTime
  202. };
  203. };
  204. const getRoute = () => {
  205. var pages = getCurrentPages();
  206. var page = pages[pages.length - 1];
  207. let _self = page.$vm;
  208. if (getPlatformName() === 'bd') {
  209. return _self.$mp && _self.$mp.page.is;
  210. } else {
  211. return (_self.$scope && _self.$scope.route) || (_self.$mp && _self.$mp.page.route);
  212. }
  213. };
  214. const getPageRoute = (self) => {
  215. var pages = getCurrentPages();
  216. var page = pages[pages.length - 1];
  217. let _self = page.$vm;
  218. let query = self._query;
  219. let str = query && JSON.stringify(query) !== '{}' ? '?' + JSON.stringify(query) : '';
  220. // clear
  221. self._query = '';
  222. if (getPlatformName() === 'bd') {
  223. return _self.$mp && _self.$mp.page.is + str;
  224. } else {
  225. return (_self.$scope && _self.$scope.route + str )|| (_self.$mp && _self.$mp.page.route + str);
  226. }
  227. };
  228. const getPageTypes = (self) => {
  229. if (self.mpType === 'page' || (self.$mp && self.$mp.mpType === 'page') || self.$options.mpType === 'page') {
  230. return true;
  231. }
  232. return false;
  233. };
  234. const calibration = (eventName, options) => {
  235. // login 、 share 、pay_success 、pay_fail 、register 、title
  236. if(!eventName){
  237. console.error(`uni.report 缺少 [eventName] 参数`);
  238. return true
  239. }
  240. if (typeof eventName !== 'string') {
  241. console.error(`uni.report [eventName] 参数类型错误,只能为 String 类型`);
  242. return true
  243. }
  244. if (eventName.length > 255) {
  245. console.error(`uni.report [eventName] 参数长度不能大于 255`);
  246. return true
  247. }
  248. if (typeof options !== 'string' && typeof options !== 'object') {
  249. console.error(`uni.report [options] 参数类型错误,只能为 String 或 Object 类型`);
  250. return true
  251. }
  252. if (typeof options === 'string' && options.length > 255) {
  253. console.error(`uni.report [options] 参数长度不能大于 255`);
  254. return true
  255. }
  256. if (eventName === 'title' && typeof options !== 'string') {
  257. console.error('uni.report [eventName] 参数为 title 时,[options] 参数只能为 String 类型');
  258. return true
  259. }
  260. };
  261. const PagesJson = require('uni-pages?{"type":"style"}').default;
  262. const statConfig = require('uni-stat-config').default || require('uni-stat-config');
  263. const resultOptions = uni.getSystemInfoSync();
  264. class Util {
  265. constructor() {
  266. this.self = '';
  267. this._retry = 0;
  268. this._platform = '';
  269. this._query = {};
  270. this._navigationBarTitle = {
  271. config: '',
  272. page: '',
  273. report: '',
  274. lt: ''
  275. };
  276. this._operatingTime = 0;
  277. this._reportingRequestData = {
  278. '1': [],
  279. '11': []
  280. };
  281. this.__prevent_triggering = false;
  282. this.__licationHide = false;
  283. this.__licationShow = false;
  284. this._lastPageRoute = '';
  285. this.statData = {
  286. uuid: getUuid(),
  287. ut: getPlatformName(),
  288. mpn: getPackName(),
  289. ak: statConfig.appid,
  290. usv: STAT_VERSION,
  291. v: getVersion(),
  292. ch: getChannel(),
  293. cn: '',
  294. pn: '',
  295. ct: '',
  296. t: getTime(),
  297. tt: '',
  298. p: resultOptions.platform === 'android' ? 'a' : 'i',
  299. brand: resultOptions.brand || '',
  300. md: resultOptions.model,
  301. sv: resultOptions.system.replace(/(Android|iOS)\s/, ''),
  302. mpsdk: resultOptions.SDKVersion || '',
  303. mpv: resultOptions.version || '',
  304. lang: resultOptions.language,
  305. pr: resultOptions.pixelRatio,
  306. ww: resultOptions.windowWidth,
  307. wh: resultOptions.windowHeight,
  308. sw: resultOptions.screenWidth,
  309. sh: resultOptions.screenHeight
  310. };
  311. }
  312. _applicationShow() {
  313. if (this.__licationHide) {
  314. getLastTime();
  315. const time = getResidenceTime('app');
  316. if (time.overtime) {
  317. let options = {
  318. path: this._lastPageRoute,
  319. scene: this.statData.sc
  320. };
  321. this._sendReportRequest(options);
  322. }
  323. this.__licationHide = false;
  324. }
  325. }
  326. _applicationHide(self, type) {
  327. this.__licationHide = true;
  328. getLastTime();
  329. const time = getResidenceTime();
  330. getFirstTime();
  331. const route = getPageRoute(this);
  332. this._sendHideRequest({
  333. urlref: route,
  334. urlref_ts: time.residenceTime
  335. }, type);
  336. }
  337. _pageShow() {
  338. const route = getPageRoute(this);
  339. const routepath = getRoute();
  340. this._navigationBarTitle.config = PagesJson &&
  341. PagesJson.pages[routepath] &&
  342. PagesJson.pages[routepath].titleNView &&
  343. PagesJson.pages[routepath].titleNView.titleText ||
  344. PagesJson &&
  345. PagesJson.pages[routepath] &&
  346. PagesJson.pages[routepath].navigationBarTitleText || '';
  347. if (this.__licationShow) {
  348. getFirstTime();
  349. this.__licationShow = false;
  350. // console.log('这是 onLauch 之后执行的第一次 pageShow ,为下次记录时间做准备');
  351. this._lastPageRoute = route;
  352. return;
  353. }
  354. getLastTime();
  355. this._lastPageRoute = route;
  356. const time = getResidenceTime('page');
  357. if (time.overtime) {
  358. let options = {
  359. path: this._lastPageRoute,
  360. scene: this.statData.sc
  361. };
  362. this._sendReportRequest(options);
  363. }
  364. getFirstTime();
  365. }
  366. _pageHide() {
  367. if (!this.__licationHide) {
  368. getLastTime();
  369. const time = getResidenceTime('page');
  370. this._sendPageRequest({
  371. url: this._lastPageRoute,
  372. urlref: this._lastPageRoute,
  373. urlref_ts: time.residenceTime
  374. });
  375. this._navigationBarTitle = {
  376. config: '',
  377. page: '',
  378. report: '',
  379. lt: ''
  380. };
  381. return;
  382. }
  383. }
  384. _login() {
  385. this._sendEventRequest({
  386. key: 'login'
  387. }, 0);
  388. }
  389. _share() {
  390. this._sendEventRequest({
  391. key: 'share'
  392. }, 0);
  393. }
  394. _payment(key) {
  395. this._sendEventRequest({
  396. key
  397. }, 0);
  398. }
  399. _sendReportRequest(options) {
  400. this._navigationBarTitle.lt = '1';
  401. let query = options.query && JSON.stringify(options.query) !== '{}' ? '?' + JSON.stringify(options.query) : '';
  402. this.statData.lt = '1';
  403. this.statData.url = (options.path + query) || '';
  404. this.statData.t = getTime();
  405. this.statData.sc = getScene(options.scene);
  406. this.statData.fvts = getFirstVisitTime();
  407. this.statData.lvts = getLastVisitTime();
  408. this.statData.tvc = getTotalVisitCount();
  409. if (getPlatformName() === 'n') {
  410. this.getProperty();
  411. } else {
  412. this.getNetworkInfo();
  413. }
  414. }
  415. _sendPageRequest(opt) {
  416. let {
  417. url,
  418. urlref,
  419. urlref_ts
  420. } = opt;
  421. this._navigationBarTitle.lt = '11';
  422. let options = {
  423. ak: this.statData.ak,
  424. uuid: this.statData.uuid,
  425. lt: '11',
  426. ut: this.statData.ut,
  427. url,
  428. tt: this.statData.tt,
  429. urlref,
  430. urlref_ts,
  431. ch: this.statData.ch,
  432. usv: this.statData.usv,
  433. t: getTime(),
  434. p: this.statData.p
  435. };
  436. this.request(options);
  437. }
  438. _sendHideRequest(opt, type) {
  439. let {
  440. urlref,
  441. urlref_ts
  442. } = opt;
  443. let options = {
  444. ak: this.statData.ak,
  445. uuid: this.statData.uuid,
  446. lt: '3',
  447. ut: this.statData.ut,
  448. urlref,
  449. urlref_ts,
  450. ch: this.statData.ch,
  451. usv: this.statData.usv,
  452. t: getTime(),
  453. p: this.statData.p
  454. };
  455. this.request(options, type);
  456. }
  457. _sendEventRequest({
  458. key = '',
  459. value = ""
  460. } = {}) {
  461. const route = this._lastPageRoute;
  462. let options = {
  463. ak: this.statData.ak,
  464. uuid: this.statData.uuid,
  465. lt: '21',
  466. ut: this.statData.ut,
  467. url: route,
  468. ch: this.statData.ch,
  469. e_n: key,
  470. e_v: typeof(value) === 'object' ? JSON.stringify(value) : value.toString(),
  471. usv: this.statData.usv,
  472. t: getTime(),
  473. p: this.statData.p
  474. };
  475. this.request(options);
  476. }
  477. getNetworkInfo() {
  478. uni.getNetworkType({
  479. success: (result) => {
  480. this.statData.net = result.networkType;
  481. this.getLocation();
  482. }
  483. });
  484. }
  485. getProperty() {
  486. plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
  487. this.statData.v = wgtinfo.version || '';
  488. this.getNetworkInfo();
  489. });
  490. }
  491. getLocation() {
  492. if (statConfig.getLocation) {
  493. uni.getLocation({
  494. type: 'wgs84',
  495. geocode: true,
  496. success: (result) => {
  497. if (result.address) {
  498. this.statData.cn = result.address.country;
  499. this.statData.pn = result.address.province;
  500. this.statData.ct = result.address.city;
  501. }
  502. this.statData.lat = result.latitude;
  503. this.statData.lng = result.longitude;
  504. this.request(this.statData);
  505. }
  506. });
  507. } else {
  508. this.statData.lat = 0;
  509. this.statData.lng = 0;
  510. this.request(this.statData);
  511. }
  512. }
  513. request(data, type) {
  514. let time = getTime();
  515. const title = this._navigationBarTitle;
  516. data.ttn = title.page;
  517. data.ttpj = title.config;
  518. data.ttc = title.report;
  519. let requestData = this._reportingRequestData;
  520. if (getPlatformName() === 'n') {
  521. requestData = uni.getStorageSync('__UNI__STAT__DATA') || {};
  522. }
  523. if (!requestData[data.lt]) {
  524. requestData[data.lt] = [];
  525. }
  526. requestData[data.lt].push(data);
  527. if (getPlatformName() === 'n') {
  528. uni.setStorageSync('__UNI__STAT__DATA', requestData);
  529. }
  530. if (getPageResidenceTime() < OPERATING_TIME && !type) {
  531. return
  532. }
  533. let uniStatData = this._reportingRequestData;
  534. if (getPlatformName() === 'n') {
  535. uniStatData = uni.getStorageSync('__UNI__STAT__DATA');
  536. }
  537. // 时间超过,重新获取时间戳
  538. setPageResidenceTime();
  539. let firstArr = [];
  540. let contentArr = [];
  541. let lastArr = [];
  542. for (let i in uniStatData) {
  543. const rd = uniStatData[i];
  544. rd.forEach((elm) => {
  545. const newData = getSplicing(elm);
  546. if (i === 0) {
  547. firstArr.push(newData);
  548. } else if (i === 3) {
  549. lastArr.push(newData);
  550. } else {
  551. contentArr.push(newData);
  552. }
  553. });
  554. }
  555. firstArr.push(...contentArr, ...lastArr);
  556. let optionsData = {
  557. usv: STAT_VERSION, //统计 SDK 版本号
  558. t: time, //发送请求时的时间戮
  559. requests: JSON.stringify(firstArr),
  560. };
  561. this._reportingRequestData = {};
  562. if (getPlatformName() === 'n') {
  563. uni.removeStorageSync('__UNI__STAT__DATA');
  564. }
  565. if (data.ut === 'h5') {
  566. this.imageRequest(optionsData);
  567. return
  568. }
  569. if (getPlatformName() === 'n' && this.statData.p === 'a') {
  570. setTimeout(() => {
  571. this._sendRequest(optionsData);
  572. }, 200);
  573. return
  574. }
  575. this._sendRequest(optionsData);
  576. }
  577. _sendRequest(optionsData) {
  578. uni.request({
  579. url: STAT_URL,
  580. method: 'POST',
  581. // header: {
  582. // 'content-type': 'application/json' // 默认值
  583. // },
  584. data: optionsData,
  585. success: () => {
  586. // if (process.env.NODE_ENV === 'development') {
  587. // console.log('stat request success');
  588. // }
  589. },
  590. fail: (e) => {
  591. if (++this._retry < 3) {
  592. setTimeout(() => {
  593. this._sendRequest(optionsData);
  594. }, 1000);
  595. }
  596. }
  597. });
  598. }
  599. /**
  600. * h5 请求
  601. */
  602. imageRequest(data) {
  603. let image = new Image();
  604. let options = getSgin(GetEncodeURIComponentOptions(data)).options;
  605. image.src = STAT_H5_URL + '?' + options;
  606. }
  607. sendEvent(key, value) {
  608. // 校验 type 参数
  609. if (calibration(key, value)) return
  610. if (key === 'title') {
  611. this._navigationBarTitle.report = value;
  612. return
  613. }
  614. this._sendEventRequest({
  615. key,
  616. value: typeof(value) === 'object' ? JSON.stringify(value) : value
  617. }, 1);
  618. }
  619. }
  620. class Stat extends Util {
  621. static getInstance() {
  622. if (!this.instance) {
  623. this.instance = new Stat();
  624. }
  625. return this.instance;
  626. }
  627. constructor() {
  628. super();
  629. this.instance = null;
  630. // 注册拦截器
  631. if (typeof uni.addInterceptor === 'function' && process.env.NODE_ENV !== 'development') {
  632. this.addInterceptorInit();
  633. this.interceptLogin();
  634. this.interceptShare(true);
  635. this.interceptRequestPayment();
  636. }
  637. }
  638. addInterceptorInit() {
  639. let self = this;
  640. uni.addInterceptor('setNavigationBarTitle', {
  641. invoke(args) {
  642. self._navigationBarTitle.page = args.title;
  643. }
  644. });
  645. }
  646. interceptLogin() {
  647. let self = this;
  648. uni.addInterceptor('login', {
  649. complete() {
  650. self._login();
  651. }
  652. });
  653. }
  654. interceptShare(type) {
  655. let self = this;
  656. if (!type) {
  657. self._share();
  658. return
  659. }
  660. uni.addInterceptor('share', {
  661. success() {
  662. self._share();
  663. },
  664. fail() {
  665. self._share();
  666. }
  667. });
  668. }
  669. interceptRequestPayment() {
  670. let self = this;
  671. uni.addInterceptor('requestPayment', {
  672. success() {
  673. self._payment('pay_success');
  674. },
  675. fail() {
  676. self._payment('pay_fail');
  677. }
  678. });
  679. }
  680. report(options, self) {
  681. this.self = self;
  682. // if (process.env.NODE_ENV === 'development') {
  683. // console.log('report init');
  684. // }
  685. setPageResidenceTime();
  686. this.__licationShow = true;
  687. this._sendReportRequest(options, true);
  688. }
  689. load(options, self) {
  690. if (!self.$scope && !self.$mp) {
  691. const page = getCurrentPages();
  692. self.$scope = page[page.length - 1];
  693. }
  694. this.self = self;
  695. this._query = options;
  696. }
  697. show(self) {
  698. this.self = self;
  699. if (getPageTypes(self)) {
  700. this._pageShow(self);
  701. } else {
  702. this._applicationShow(self);
  703. }
  704. }
  705. ready(self) {
  706. // this.self = self;
  707. // if (getPageTypes(self)) {
  708. // this._pageShow(self);
  709. // }
  710. }
  711. hide(self) {
  712. this.self = self;
  713. if (getPageTypes(self)) {
  714. this._pageHide(self);
  715. } else {
  716. this._applicationHide(self, true);
  717. }
  718. }
  719. error(em) {
  720. if (this._platform === 'devtools') {
  721. if (process.env.NODE_ENV === 'development') {
  722. console.info('当前运行环境为开发者工具,不上报数据。');
  723. }
  724. // return;
  725. }
  726. let emVal = '';
  727. if (!em.message) {
  728. emVal = JSON.stringify(em);
  729. } else {
  730. emVal = em.stack;
  731. }
  732. let options = {
  733. ak: this.statData.ak,
  734. uuid: this.statData.uuid,
  735. lt: '31',
  736. ut: this.statData.ut,
  737. ch: this.statData.ch,
  738. mpsdk: this.statData.mpsdk,
  739. mpv: this.statData.mpv,
  740. v: this.statData.v,
  741. em: emVal,
  742. usv: this.statData.usv,
  743. t: getTime(),
  744. p: this.statData.p
  745. };
  746. this.request(options);
  747. }
  748. }
  749. const stat = Stat.getInstance();
  750. let isHide = false;
  751. const lifecycle = {
  752. onLaunch(options) {
  753. stat.report(options, this);
  754. },
  755. onReady() {
  756. stat.ready(this);
  757. },
  758. onLoad(options) {
  759. stat.load(options, this);
  760. // 重写分享,获取分享上报事件
  761. if (this.$scope && this.$scope.onShareAppMessage) {
  762. let oldShareAppMessage = this.$scope.onShareAppMessage;
  763. this.$scope.onShareAppMessage = function(options) {
  764. stat.interceptShare(false);
  765. return oldShareAppMessage.call(this, options)
  766. };
  767. }
  768. },
  769. onShow() {
  770. isHide = false;
  771. stat.show(this);
  772. },
  773. onHide() {
  774. isHide = true;
  775. stat.hide(this);
  776. },
  777. onUnload() {
  778. if (isHide) {
  779. isHide = false;
  780. return
  781. }
  782. stat.hide(this);
  783. },
  784. onError(e) {
  785. stat.error(e);
  786. }
  787. };
  788. function main() {
  789. if (process.env.NODE_ENV === 'development') {
  790. uni.report = function(type, options) {};
  791. }else{
  792. const Vue = require('vue');
  793. (Vue.default || Vue).mixin(lifecycle);
  794. uni.report = function(type, options) {
  795. stat.sendEvent(type, options);
  796. };
  797. }
  798. }
  799. main();