diff --git a/README.md b/README.md index 3ddcb5dfd8d598aa5d3441256905f9b4cd98d83c..5da1b37ba94955a2bf33d4f43452747aea6d8fa0 100644 --- a/README.md +++ b/README.md @@ -61,8 +61,8 @@ ## 常见疑问 -- 问:不同端有不同的需求、不同的特色,登陆支付也不一样,如何统一? -- 答:差异部分使用条件编译。uni-app提供了灵活强大的[条件编译](https://uniapp.dcloud.io/platform)。可以完美处理复用部分和差异部分。真正一套工程源码。当业务升级时,不再需要多端维护。如果多端维护,经常会因为某些端的流量不大,就一直拖延无法让那些用户享受到最新服务。另外登陆支付在客户端部分,已经被uni-app统一成一样的api了。 +- 问:不同端有不同的需求、不同的特色,登录支付也不一样,如何统一? +- 答:差异部分使用条件编译。uni-app提供了灵活强大的[条件编译](https://uniapp.dcloud.io/platform)。可以完美处理复用部分和差异部分。真正一套工程源码。当业务升级时,不再需要多端维护。如果多端维护,经常会因为某些端的流量不大,就一直拖延无法让那些用户享受到最新服务。另外登录支付在客户端部分,已经被uni-app统一成一样的api了。 - 问:多端是不是一种妥协,是否会造成性能下降? diff --git a/jsconfig.json b/jsconfig.json index 5fb0b37701d289d66d02dd34c835c945f48adbec..5b9eea21335f2dfb2af18b35c5fa0a1f462b1060 100644 --- a/jsconfig.json +++ b/jsconfig.json @@ -2,6 +2,15 @@ "compilerOptions": { "baseUrl": ".", "paths": { + "uni-shared": [ + "./src/shared/index.js" + ], + "uni-helpers/*": [ + "./src/core/helpers/*" + ], + "uni-mixins/*": [ + "./src/core/view/mixins/*" + ], "uni-core/*": [ "./src/core/*" ], diff --git a/lib/apis.js b/lib/apis.js index 37418a33aca81e08ee4852647a4df35f7f91a1dd..ff3cb2cfab1c119736c60b5e12a7b47cd076cddb 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -248,7 +248,9 @@ const third = [ 'unPreloadPage', 'loadSubPackage', 'sendHostEvent', - 'navigateToMiniProgram' + 'navigateToMiniProgram', + 'getLaunchOptionsSync', + 'getEnterOptionsSync' ] const ad = [ diff --git a/packages/uni-cli-shared/components/ad.mixin.js b/packages/uni-cli-shared/components/ad.mixin.js index 62121c38a85852094669975748ea2acd984a239b..8a2aeb38168c08872dd11881cff5030044be8886 100644 --- a/packages/uni-cli-shared/components/ad.mixin.js +++ b/packages/uni-cli-shared/components/ad.mixin.js @@ -213,6 +213,7 @@ class AdHelper { const ad = this.get(options) if (ad.isLoad) { + this._lastWaterfallIndex = -1 ad.show((e) => { onclose && onclose(e) }, () => { @@ -220,6 +221,7 @@ class AdHelper { }) } else { ad.load(() => { + this._lastWaterfallIndex = -1 onload && onload() ad.show((e) => { onclose && onclose(e) @@ -242,16 +244,14 @@ class AdHelper { return } - if (index === 0) { - this._lastWaterfallIndex = -1 - } - const options2 = { adpid: adpid[index], urlCallback, retry: false } + console.log('ad.loadWaterfall::index=' + index) + this.load(options2, (res) => { this._lastWaterfallIndex = index onload(options2) @@ -260,7 +260,6 @@ class AdHelper { if (index >= adpid.length) { onfail(err) } else { - console.log('loadWaterfall::index=' + index) this.loadWaterfall(options, onload, onfail, index) } }) @@ -276,17 +275,18 @@ class AdHelper { return } - let idx = index if (this._lastWaterfallIndex > -1) { - idx = this._lastWaterfallIndex + index = this._lastWaterfallIndex } const options2 = { - adpid: adpid[idx], + adpid: adpid[index], urlCallback, retry: false } + console.log('ad.showWaterfall::index=' + index) + this.show(options2, () => { onload() }, (err) => { @@ -444,6 +444,9 @@ export default { }, methods: { load () { + if (this.isLoading) { + return + } this._startLoading() const invoke = this._isWaterfall() ? 'loadWaterfall' : 'load' this._adHelper[invoke](this._getAdOptions(), () => { @@ -454,6 +457,9 @@ export default { }, show () { + if (this.isLoading) { + return + } this._startLoading() const invoke = this._isWaterfall() ? 'showWaterfall' : 'show' this._adHelper[invoke](this._getAdOptions(), () => { diff --git a/packages/uni-stat/dist/uni-cloud-stat.cjs.js b/packages/uni-stat/dist/uni-cloud-stat.cjs.js index dcd459dea474ac8f34abd278919ac388fd9156db..783063ad888365d1f4573b9a7790f51526d72b28 100644 --- a/packages/uni-stat/dist/uni-cloud-stat.cjs.js +++ b/packages/uni-stat/dist/uni-cloud-stat.cjs.js @@ -6,7 +6,7 @@ const sys = uni.getSystemInfoSync(); // 访问开始即启动小程序,访问结束结分为:进入后台超过5min、在前台无任何操作超过30min、在新的来源打开小程序; -const STAT_VERSION = '0.0.1'; +const STAT_VERSION = process.env.UNI_COMPILER_VERSION; const STAT_URL = 'https://tongji.dcloud.io/uni/stat'; const STAT_H5_URL = 'https://tongji.dcloud.io/uni/stat.gif'; const PAGE_PVER_TIME = 1800; // 页面在前台无操作结束访问时间 单位s @@ -202,21 +202,10 @@ const get_scene = (options) => { return scene }; -/** - * 获取拼接参数 - */ -const get_splicing = (data) => { - let str = ''; - for (var i in data) { - str += i + '=' + data[i] + '&'; - } - return str.substr(0, str.length - 1) -}; - /** * 获取页面url,不包含参数 */ -const get_route = (pageVm) => { +const get_route$1 = (pageVm) => { let _self = pageVm || get_page_vm(); if (get_platform_name() === 'bd') { let mp_route = _self.$mp && _self.$mp.page && _self.$mp.page.is; @@ -290,7 +279,10 @@ const handle_data = (statData) => { for (let i in statData) { const rd = statData[i]; rd.forEach((elm) => { - const newData = get_splicing(elm); + let newData = ''; + { + newData = elm; + } if (i === 0) { firstArr.push(newData); } else if (i === 3) { @@ -506,18 +498,21 @@ const log = (data) => { } }; -let data = uni.getStorageSync('$$STAT__DBDATA') || {}; +const appid = process.env.UNI_APP_ID; // 做应用隔离 const dbSet = (name, value) => { + let data = uni.getStorageSync('$$STAT__DBDATA:'+appid) || {}; + if (!data) { data = {}; } data[name] = value; - uni.setStorageSync('$$STAT__DBDATA', data); + uni.setStorageSync('$$STAT__DBDATA:'+appid, data); }; const dbGet = (name) => { + let data = uni.getStorageSync('$$STAT__DBDATA:'+appid) || {}; if (!data[name]) { - let dbdata = uni.getStorageSync('$$STAT__DBDATA'); + let dbdata = uni.getStorageSync('$$STAT__DBDATA:'+appid); if (!dbdata) { dbdata = {}; } @@ -530,14 +525,15 @@ const dbGet = (name) => { }; const dbRemove = (name) => { + let data = uni.getStorageSync('$$STAT__DBDATA:'+appid) || {}; if (data[name]) { delete data[name]; - uni.setStorageSync('$$STAT__DBDATA', data); + uni.setStorageSync('$$STAT__DBDATA:'+appid, data); } else { - data = uni.getStorageSync('$$STAT__DBDATA'); + data = uni.getStorageSync('$$STAT__DBDATA:'+appid); if (data[name]) { delete data[name]; - uni.setStorageSync('$$STAT__DBDATA', data); + uni.setStorageSync('$$STAT__DBDATA:'+appid, data); } } }; @@ -621,35 +617,35 @@ const get_total_visit_count = () => { return count }; -let Set__First__Time = 0; -let Set__Last__Time = 0; +const FIRST_TIME = '__first_time'; /** - * 获取第一次时间 + * 设置页面首次访问时间,用户获取页面/应用停留时常 */ -const get_first_time = () => { - let time = new Date().getTime(); - Set__First__Time = time; - Set__Last__Time = 0; - return time +const set_first_time = () => { + const time = new Date().getTime(); + const timeStorge = dbSet(FIRST_TIME,time); + return timeStorge }; /** - * 获取最后一次时间 + * 获取最后一次时间 ,暂时用不到,直接获取当前时间即可 */ -const get_last_time = () => { - let time = new Date().getTime(); - Set__Last__Time = time; - return time -}; +// export const get_last_time = () => { +// let time = new Date().getTime() +// Set__Last__Time = time +// return time +// } /** * 获取页面 \ 应用停留时间 */ const get_residence_time = (type) => { let residenceTime = 0; - if (Set__First__Time !== 0) { - residenceTime = Set__Last__Time - Set__First__Time; + const first_time = dbGet(FIRST_TIME); + const last_time = get_time(); + if (first_time !== 0) { + residenceTime = last_time - first_time; } residenceTime = parseInt(residenceTime / 1000); @@ -811,7 +807,6 @@ class Report { applicationShow() { // 通过 __licationHide 判断保证是进入后台后在次进入应用,避免重复上报数据 if (this.__licationHide) { - get_last_time(); const time = get_residence_time('app'); // 需要判断进入后台是否超过时限 ,默认是 30min ,是的话需要执行进入应用的上报 if (time.overtime) { @@ -839,7 +834,6 @@ class Report { } // 进入应用后台保存状态,方便进入前台后判断是否上报应用数据 this.__licationHide = true; - get_last_time(); const time = get_residence_time(); const route = get_page_route(self); uni.setStorageSync('_STAT_LAST_PAGE_ROUTE', route); @@ -850,8 +844,8 @@ class Report { }, type ); - // 重置时间 - get_first_time(); + // 更新页面首次访问时间 + set_first_time(); } /** @@ -867,20 +861,19 @@ class Report { }; const route = get_page_route(self); - const routepath = get_route(self); + const routepath = get_route$1(self); this._navigationBarTitle.config = get_page_name(routepath); // 表示应用触发 ,页面切换不触发之后的逻辑 if (this.__licationShow) { - get_first_time(); + // 更新页面首次访问时间 + set_first_time(); // this._lastPageRoute = route uni.setStorageSync('_STAT_LAST_PAGE_ROUTE', route); this.__licationShow = false; return } - get_last_time(); - const time = get_residence_time('page'); // 停留时间 if (time.overtime) { @@ -890,8 +883,8 @@ class Report { }; this.sendReportRequest(options); } - // 重置时间 - get_first_time(); + // 更新页面首次访问时间 + set_first_time(); } /** @@ -899,7 +892,6 @@ class Report { */ pageHide(self) { if (!this.__licationHide) { - get_last_time(); const time = get_residence_time('page'); let route = get_page_route(self); let lastPageRoute = uni.getStorageSync('_STAT_LAST_PAGE_ROUTE'); @@ -992,8 +984,16 @@ class Report { * 自定义事件上报 */ sendEventRequest({ key = '', value = '' } = {}) { - // const route = this._lastPageRoute - const routepath = get_route(); + + let routepath = ''; + + try { + routepath = get_route$1(); + } catch (error) { + const launch_options = dbGet('__launch_options'); + routepath = launch_options.path; + } + this._navigationBarTitle.config = get_page_name(routepath); this._navigationBarTitle.lt = '21'; let options = { @@ -1227,6 +1227,7 @@ class Stat extends Report { // 初始化页面停留时间 start set_page_residence_time(); this.__licationShow = true; + dbSet('__launch_options', options); this.sendReportRequest(options, true); } load(options, self) { @@ -1249,7 +1250,7 @@ class Stat extends Report { } // #ifdef VUE3 - if (get_platform_name() !== 'h5' && get_platform_name() !== 'n') { + if (get_platform_name() === 'h5' || get_platform_name() === 'n') { if (get_page_types(self) === 'app') { this.appShow(); } @@ -1270,7 +1271,7 @@ class Stat extends Report { } // #ifdef VUE3 - if (get_platform_name() !== 'h5' && get_platform_name() !== 'n') { + if (get_platform_name() === 'h5' || get_platform_name() === 'n') { if (get_page_types(self) === 'app') { this.appHide(); } @@ -1286,23 +1287,33 @@ class Stat extends Report { error(em) { // 开发工具内不上报错误 - if (this._platform === 'devtools') { - if (process.env.NODE_ENV === 'development') { - console.info('当前运行环境为开发者工具,不上报数据。'); - return - } - } + // if (this._platform === 'devtools') { + // if (process.env.NODE_ENV === 'development') { + // console.info('当前运行环境为开发者工具,不上报数据。') + // return + // } + // } let emVal = ''; if (!em.message) { emVal = JSON.stringify(em); } else { emVal = em.stack; } + + let route = ''; + try { + route = get_route(); + }catch(e){ + // 未获取到页面路径 + route = ''; + } + let options = { ak: this.statData.ak, uuid: this.statData.uuid, p: this.statData.p, lt: '31', + url:route, ut: this.statData.ut, ch: this.statData.ch, mpsdk: this.statData.mpsdk, diff --git a/packages/uni-stat/dist/uni-cloud-stat.es.js b/packages/uni-stat/dist/uni-cloud-stat.es.js index 77c74d0221ffd465c7a379007f7ac6a3fb7e0114..7d9fbdcdcdd194cc2763734e22935b0ec48598a4 100644 --- a/packages/uni-stat/dist/uni-cloud-stat.es.js +++ b/packages/uni-stat/dist/uni-cloud-stat.es.js @@ -4,7 +4,7 @@ const sys = uni.getSystemInfoSync(); // 访问开始即启动小程序,访问结束结分为:进入后台超过5min、在前台无任何操作超过30min、在新的来源打开小程序; -const STAT_VERSION = '0.0.1'; +const STAT_VERSION = process.env.UNI_COMPILER_VERSION; const STAT_URL = 'https://tongji.dcloud.io/uni/stat'; const STAT_H5_URL = 'https://tongji.dcloud.io/uni/stat.gif'; const PAGE_PVER_TIME = 1800; // 页面在前台无操作结束访问时间 单位s @@ -200,21 +200,10 @@ const get_scene = (options) => { return scene }; -/** - * 获取拼接参数 - */ -const get_splicing = (data) => { - let str = ''; - for (var i in data) { - str += i + '=' + data[i] + '&'; - } - return str.substr(0, str.length - 1) -}; - /** * 获取页面url,不包含参数 */ -const get_route = (pageVm) => { +const get_route$1 = (pageVm) => { let _self = pageVm || get_page_vm(); if (get_platform_name() === 'bd') { let mp_route = _self.$mp && _self.$mp.page && _self.$mp.page.is; @@ -288,7 +277,10 @@ const handle_data = (statData) => { for (let i in statData) { const rd = statData[i]; rd.forEach((elm) => { - const newData = get_splicing(elm); + let newData = ''; + { + newData = elm; + } if (i === 0) { firstArr.push(newData); } else if (i === 3) { @@ -504,18 +496,21 @@ const log = (data) => { } }; -let data = uni.getStorageSync('$$STAT__DBDATA') || {}; +const appid = process.env.UNI_APP_ID; // 做应用隔离 const dbSet = (name, value) => { + let data = uni.getStorageSync('$$STAT__DBDATA:'+appid) || {}; + if (!data) { data = {}; } data[name] = value; - uni.setStorageSync('$$STAT__DBDATA', data); + uni.setStorageSync('$$STAT__DBDATA:'+appid, data); }; const dbGet = (name) => { + let data = uni.getStorageSync('$$STAT__DBDATA:'+appid) || {}; if (!data[name]) { - let dbdata = uni.getStorageSync('$$STAT__DBDATA'); + let dbdata = uni.getStorageSync('$$STAT__DBDATA:'+appid); if (!dbdata) { dbdata = {}; } @@ -528,14 +523,15 @@ const dbGet = (name) => { }; const dbRemove = (name) => { + let data = uni.getStorageSync('$$STAT__DBDATA:'+appid) || {}; if (data[name]) { delete data[name]; - uni.setStorageSync('$$STAT__DBDATA', data); + uni.setStorageSync('$$STAT__DBDATA:'+appid, data); } else { - data = uni.getStorageSync('$$STAT__DBDATA'); + data = uni.getStorageSync('$$STAT__DBDATA:'+appid); if (data[name]) { delete data[name]; - uni.setStorageSync('$$STAT__DBDATA', data); + uni.setStorageSync('$$STAT__DBDATA:'+appid, data); } } }; @@ -619,35 +615,35 @@ const get_total_visit_count = () => { return count }; -let Set__First__Time = 0; -let Set__Last__Time = 0; +const FIRST_TIME = '__first_time'; /** - * 获取第一次时间 + * 设置页面首次访问时间,用户获取页面/应用停留时常 */ -const get_first_time = () => { - let time = new Date().getTime(); - Set__First__Time = time; - Set__Last__Time = 0; - return time +const set_first_time = () => { + const time = new Date().getTime(); + const timeStorge = dbSet(FIRST_TIME,time); + return timeStorge }; /** - * 获取最后一次时间 + * 获取最后一次时间 ,暂时用不到,直接获取当前时间即可 */ -const get_last_time = () => { - let time = new Date().getTime(); - Set__Last__Time = time; - return time -}; +// export const get_last_time = () => { +// let time = new Date().getTime() +// Set__Last__Time = time +// return time +// } /** * 获取页面 \ 应用停留时间 */ const get_residence_time = (type) => { let residenceTime = 0; - if (Set__First__Time !== 0) { - residenceTime = Set__Last__Time - Set__First__Time; + const first_time = dbGet(FIRST_TIME); + const last_time = get_time(); + if (first_time !== 0) { + residenceTime = last_time - first_time; } residenceTime = parseInt(residenceTime / 1000); @@ -809,7 +805,6 @@ class Report { applicationShow() { // 通过 __licationHide 判断保证是进入后台后在次进入应用,避免重复上报数据 if (this.__licationHide) { - get_last_time(); const time = get_residence_time('app'); // 需要判断进入后台是否超过时限 ,默认是 30min ,是的话需要执行进入应用的上报 if (time.overtime) { @@ -837,7 +832,6 @@ class Report { } // 进入应用后台保存状态,方便进入前台后判断是否上报应用数据 this.__licationHide = true; - get_last_time(); const time = get_residence_time(); const route = get_page_route(self); uni.setStorageSync('_STAT_LAST_PAGE_ROUTE', route); @@ -848,8 +842,8 @@ class Report { }, type ); - // 重置时间 - get_first_time(); + // 更新页面首次访问时间 + set_first_time(); } /** @@ -865,20 +859,19 @@ class Report { }; const route = get_page_route(self); - const routepath = get_route(self); + const routepath = get_route$1(self); this._navigationBarTitle.config = get_page_name(routepath); // 表示应用触发 ,页面切换不触发之后的逻辑 if (this.__licationShow) { - get_first_time(); + // 更新页面首次访问时间 + set_first_time(); // this._lastPageRoute = route uni.setStorageSync('_STAT_LAST_PAGE_ROUTE', route); this.__licationShow = false; return } - get_last_time(); - const time = get_residence_time('page'); // 停留时间 if (time.overtime) { @@ -888,8 +881,8 @@ class Report { }; this.sendReportRequest(options); } - // 重置时间 - get_first_time(); + // 更新页面首次访问时间 + set_first_time(); } /** @@ -897,7 +890,6 @@ class Report { */ pageHide(self) { if (!this.__licationHide) { - get_last_time(); const time = get_residence_time('page'); let route = get_page_route(self); let lastPageRoute = uni.getStorageSync('_STAT_LAST_PAGE_ROUTE'); @@ -990,8 +982,16 @@ class Report { * 自定义事件上报 */ sendEventRequest({ key = '', value = '' } = {}) { - // const route = this._lastPageRoute - const routepath = get_route(); + + let routepath = ''; + + try { + routepath = get_route$1(); + } catch (error) { + const launch_options = dbGet('__launch_options'); + routepath = launch_options.path; + } + this._navigationBarTitle.config = get_page_name(routepath); this._navigationBarTitle.lt = '21'; let options = { @@ -1225,6 +1225,7 @@ class Stat extends Report { // 初始化页面停留时间 start set_page_residence_time(); this.__licationShow = true; + dbSet('__launch_options', options); this.sendReportRequest(options, true); } load(options, self) { @@ -1247,7 +1248,7 @@ class Stat extends Report { } // #ifdef VUE3 - if (get_platform_name() !== 'h5' && get_platform_name() !== 'n') { + if (get_platform_name() === 'h5' || get_platform_name() === 'n') { if (get_page_types(self) === 'app') { this.appShow(); } @@ -1268,7 +1269,7 @@ class Stat extends Report { } // #ifdef VUE3 - if (get_platform_name() !== 'h5' && get_platform_name() !== 'n') { + if (get_platform_name() === 'h5' || get_platform_name() === 'n') { if (get_page_types(self) === 'app') { this.appHide(); } @@ -1284,23 +1285,33 @@ class Stat extends Report { error(em) { // 开发工具内不上报错误 - if (this._platform === 'devtools') { - if (process.env.NODE_ENV === 'development') { - console.info('当前运行环境为开发者工具,不上报数据。'); - return - } - } + // if (this._platform === 'devtools') { + // if (process.env.NODE_ENV === 'development') { + // console.info('当前运行环境为开发者工具,不上报数据。') + // return + // } + // } let emVal = ''; if (!em.message) { emVal = JSON.stringify(em); } else { emVal = em.stack; } + + let route = ''; + try { + route = get_route(); + }catch(e){ + // 未获取到页面路径 + route = ''; + } + let options = { ak: this.statData.ak, uuid: this.statData.uuid, p: this.statData.p, lt: '31', + url:route, ut: this.statData.ut, ch: this.statData.ch, mpsdk: this.statData.mpsdk, diff --git a/packages/uni-stat/dist/uni-stat.cjs.js b/packages/uni-stat/dist/uni-stat.cjs.js index c4264527cd235a6ef17b292182aaa6b40fcee13d..164375e93654fe413c3a917e01eca75d21ab01bd 100644 --- a/packages/uni-stat/dist/uni-stat.cjs.js +++ b/packages/uni-stat/dist/uni-stat.cjs.js @@ -6,7 +6,7 @@ const sys = uni.getSystemInfoSync(); // 访问开始即启动小程序,访问结束结分为:进入后台超过5min、在前台无任何操作超过30min、在新的来源打开小程序; -const STAT_VERSION = '0.0.1'; +const STAT_VERSION = process.env.UNI_COMPILER_VERSION; const STAT_URL = 'https://tongji.dcloud.io/uni/stat'; const STAT_H5_URL = 'https://tongji.dcloud.io/uni/stat.gif'; const PAGE_PVER_TIME = 1800; // 页面在前台无操作结束访问时间 单位s @@ -216,7 +216,7 @@ const get_splicing = (data) => { /** * 获取页面url,不包含参数 */ -const get_route = (pageVm) => { +const get_route$1 = (pageVm) => { let _self = pageVm || get_page_vm(); if (get_platform_name() === 'bd') { let mp_route = _self.$mp && _self.$mp.page && _self.$mp.page.is; @@ -290,7 +290,10 @@ const handle_data = (statData) => { for (let i in statData) { const rd = statData[i]; rd.forEach((elm) => { - const newData = get_splicing(elm); + let newData = ''; + { + newData = get_splicing(elm); + } if (i === 0) { firstArr.push(newData); } else if (i === 3) { @@ -474,18 +477,21 @@ const log = (data) => { } }; -let data = uni.getStorageSync('$$STAT__DBDATA') || {}; +const appid = process.env.UNI_APP_ID; // 做应用隔离 const dbSet = (name, value) => { + let data = uni.getStorageSync('$$STAT__DBDATA:'+appid) || {}; + if (!data) { data = {}; } data[name] = value; - uni.setStorageSync('$$STAT__DBDATA', data); + uni.setStorageSync('$$STAT__DBDATA:'+appid, data); }; const dbGet = (name) => { + let data = uni.getStorageSync('$$STAT__DBDATA:'+appid) || {}; if (!data[name]) { - let dbdata = uni.getStorageSync('$$STAT__DBDATA'); + let dbdata = uni.getStorageSync('$$STAT__DBDATA:'+appid); if (!dbdata) { dbdata = {}; } @@ -498,14 +504,15 @@ const dbGet = (name) => { }; const dbRemove = (name) => { + let data = uni.getStorageSync('$$STAT__DBDATA:'+appid) || {}; if (data[name]) { delete data[name]; - uni.setStorageSync('$$STAT__DBDATA', data); + uni.setStorageSync('$$STAT__DBDATA:'+appid, data); } else { - data = uni.getStorageSync('$$STAT__DBDATA'); + data = uni.getStorageSync('$$STAT__DBDATA:'+appid); if (data[name]) { delete data[name]; - uni.setStorageSync('$$STAT__DBDATA', data); + uni.setStorageSync('$$STAT__DBDATA:'+appid, data); } } }; @@ -589,35 +596,35 @@ const get_total_visit_count = () => { return count }; -let Set__First__Time = 0; -let Set__Last__Time = 0; +const FIRST_TIME = '__first_time'; /** - * 获取第一次时间 + * 设置页面首次访问时间,用户获取页面/应用停留时常 */ -const get_first_time = () => { - let time = new Date().getTime(); - Set__First__Time = time; - Set__Last__Time = 0; - return time +const set_first_time = () => { + const time = new Date().getTime(); + const timeStorge = dbSet(FIRST_TIME,time); + return timeStorge }; /** - * 获取最后一次时间 + * 获取最后一次时间 ,暂时用不到,直接获取当前时间即可 */ -const get_last_time = () => { - let time = new Date().getTime(); - Set__Last__Time = time; - return time -}; +// export const get_last_time = () => { +// let time = new Date().getTime() +// Set__Last__Time = time +// return time +// } /** * 获取页面 \ 应用停留时间 */ const get_residence_time = (type) => { let residenceTime = 0; - if (Set__First__Time !== 0) { - residenceTime = Set__Last__Time - Set__First__Time; + const first_time = dbGet(FIRST_TIME); + const last_time = get_time(); + if (first_time !== 0) { + residenceTime = last_time - first_time; } residenceTime = parseInt(residenceTime / 1000); @@ -779,7 +786,6 @@ class Report { applicationShow() { // 通过 __licationHide 判断保证是进入后台后在次进入应用,避免重复上报数据 if (this.__licationHide) { - get_last_time(); const time = get_residence_time('app'); // 需要判断进入后台是否超过时限 ,默认是 30min ,是的话需要执行进入应用的上报 if (time.overtime) { @@ -807,7 +813,6 @@ class Report { } // 进入应用后台保存状态,方便进入前台后判断是否上报应用数据 this.__licationHide = true; - get_last_time(); const time = get_residence_time(); const route = get_page_route(self); uni.setStorageSync('_STAT_LAST_PAGE_ROUTE', route); @@ -818,8 +823,8 @@ class Report { }, type ); - // 重置时间 - get_first_time(); + // 更新页面首次访问时间 + set_first_time(); } /** @@ -835,20 +840,19 @@ class Report { }; const route = get_page_route(self); - const routepath = get_route(self); + const routepath = get_route$1(self); this._navigationBarTitle.config = get_page_name(routepath); // 表示应用触发 ,页面切换不触发之后的逻辑 if (this.__licationShow) { - get_first_time(); + // 更新页面首次访问时间 + set_first_time(); // this._lastPageRoute = route uni.setStorageSync('_STAT_LAST_PAGE_ROUTE', route); this.__licationShow = false; return } - get_last_time(); - const time = get_residence_time('page'); // 停留时间 if (time.overtime) { @@ -858,8 +862,8 @@ class Report { }; this.sendReportRequest(options); } - // 重置时间 - get_first_time(); + // 更新页面首次访问时间 + set_first_time(); } /** @@ -867,7 +871,6 @@ class Report { */ pageHide(self) { if (!this.__licationHide) { - get_last_time(); const time = get_residence_time('page'); let route = get_page_route(self); let lastPageRoute = uni.getStorageSync('_STAT_LAST_PAGE_ROUTE'); @@ -960,8 +963,16 @@ class Report { * 自定义事件上报 */ sendEventRequest({ key = '', value = '' } = {}) { - // const route = this._lastPageRoute - const routepath = get_route(); + + let routepath = ''; + + try { + routepath = get_route$1(); + } catch (error) { + const launch_options = dbGet('__launch_options'); + routepath = launch_options.path; + } + this._navigationBarTitle.config = get_page_name(routepath); this._navigationBarTitle.lt = '21'; let options = { @@ -1183,6 +1194,7 @@ class Stat extends Report { // 初始化页面停留时间 start set_page_residence_time(); this.__licationShow = true; + dbSet('__launch_options', options); this.sendReportRequest(options, true); } load(options, self) { @@ -1205,7 +1217,7 @@ class Stat extends Report { } // #ifdef VUE3 - if (get_platform_name() !== 'h5' && get_platform_name() !== 'n') { + if (get_platform_name() === 'h5' || get_platform_name() === 'n') { if (get_page_types(self) === 'app') { this.appShow(); } @@ -1226,7 +1238,7 @@ class Stat extends Report { } // #ifdef VUE3 - if (get_platform_name() !== 'h5' && get_platform_name() !== 'n') { + if (get_platform_name() === 'h5' || get_platform_name() === 'n') { if (get_page_types(self) === 'app') { this.appHide(); } @@ -1242,23 +1254,33 @@ class Stat extends Report { error(em) { // 开发工具内不上报错误 - if (this._platform === 'devtools') { - if (process.env.NODE_ENV === 'development') { - console.info('当前运行环境为开发者工具,不上报数据。'); - return - } - } + // if (this._platform === 'devtools') { + // if (process.env.NODE_ENV === 'development') { + // console.info('当前运行环境为开发者工具,不上报数据。') + // return + // } + // } let emVal = ''; if (!em.message) { emVal = JSON.stringify(em); } else { emVal = em.stack; } + + let route = ''; + try { + route = get_route(); + }catch(e){ + // 未获取到页面路径 + route = ''; + } + let options = { ak: this.statData.ak, uuid: this.statData.uuid, p: this.statData.p, lt: '31', + url:route, ut: this.statData.ut, ch: this.statData.ch, mpsdk: this.statData.mpsdk, diff --git a/packages/uni-stat/dist/uni-stat.es.js b/packages/uni-stat/dist/uni-stat.es.js index b6d89c0d886333ab6ce97da915dcbfc857b7542f..c71b286a369d68930ee39684d5168dcab9639581 100644 --- a/packages/uni-stat/dist/uni-stat.es.js +++ b/packages/uni-stat/dist/uni-stat.es.js @@ -4,7 +4,7 @@ const sys = uni.getSystemInfoSync(); // 访问开始即启动小程序,访问结束结分为:进入后台超过5min、在前台无任何操作超过30min、在新的来源打开小程序; -const STAT_VERSION = '0.0.1'; +const STAT_VERSION = process.env.UNI_COMPILER_VERSION; const STAT_URL = 'https://tongji.dcloud.io/uni/stat'; const STAT_H5_URL = 'https://tongji.dcloud.io/uni/stat.gif'; const PAGE_PVER_TIME = 1800; // 页面在前台无操作结束访问时间 单位s @@ -214,7 +214,7 @@ const get_splicing = (data) => { /** * 获取页面url,不包含参数 */ -const get_route = (pageVm) => { +const get_route$1 = (pageVm) => { let _self = pageVm || get_page_vm(); if (get_platform_name() === 'bd') { let mp_route = _self.$mp && _self.$mp.page && _self.$mp.page.is; @@ -288,7 +288,10 @@ const handle_data = (statData) => { for (let i in statData) { const rd = statData[i]; rd.forEach((elm) => { - const newData = get_splicing(elm); + let newData = ''; + { + newData = get_splicing(elm); + } if (i === 0) { firstArr.push(newData); } else if (i === 3) { @@ -472,18 +475,21 @@ const log = (data) => { } }; -let data = uni.getStorageSync('$$STAT__DBDATA') || {}; +const appid = process.env.UNI_APP_ID; // 做应用隔离 const dbSet = (name, value) => { + let data = uni.getStorageSync('$$STAT__DBDATA:'+appid) || {}; + if (!data) { data = {}; } data[name] = value; - uni.setStorageSync('$$STAT__DBDATA', data); + uni.setStorageSync('$$STAT__DBDATA:'+appid, data); }; const dbGet = (name) => { + let data = uni.getStorageSync('$$STAT__DBDATA:'+appid) || {}; if (!data[name]) { - let dbdata = uni.getStorageSync('$$STAT__DBDATA'); + let dbdata = uni.getStorageSync('$$STAT__DBDATA:'+appid); if (!dbdata) { dbdata = {}; } @@ -496,14 +502,15 @@ const dbGet = (name) => { }; const dbRemove = (name) => { + let data = uni.getStorageSync('$$STAT__DBDATA:'+appid) || {}; if (data[name]) { delete data[name]; - uni.setStorageSync('$$STAT__DBDATA', data); + uni.setStorageSync('$$STAT__DBDATA:'+appid, data); } else { - data = uni.getStorageSync('$$STAT__DBDATA'); + data = uni.getStorageSync('$$STAT__DBDATA:'+appid); if (data[name]) { delete data[name]; - uni.setStorageSync('$$STAT__DBDATA', data); + uni.setStorageSync('$$STAT__DBDATA:'+appid, data); } } }; @@ -587,35 +594,35 @@ const get_total_visit_count = () => { return count }; -let Set__First__Time = 0; -let Set__Last__Time = 0; +const FIRST_TIME = '__first_time'; /** - * 获取第一次时间 + * 设置页面首次访问时间,用户获取页面/应用停留时常 */ -const get_first_time = () => { - let time = new Date().getTime(); - Set__First__Time = time; - Set__Last__Time = 0; - return time +const set_first_time = () => { + const time = new Date().getTime(); + const timeStorge = dbSet(FIRST_TIME,time); + return timeStorge }; /** - * 获取最后一次时间 + * 获取最后一次时间 ,暂时用不到,直接获取当前时间即可 */ -const get_last_time = () => { - let time = new Date().getTime(); - Set__Last__Time = time; - return time -}; +// export const get_last_time = () => { +// let time = new Date().getTime() +// Set__Last__Time = time +// return time +// } /** * 获取页面 \ 应用停留时间 */ const get_residence_time = (type) => { let residenceTime = 0; - if (Set__First__Time !== 0) { - residenceTime = Set__Last__Time - Set__First__Time; + const first_time = dbGet(FIRST_TIME); + const last_time = get_time(); + if (first_time !== 0) { + residenceTime = last_time - first_time; } residenceTime = parseInt(residenceTime / 1000); @@ -777,7 +784,6 @@ class Report { applicationShow() { // 通过 __licationHide 判断保证是进入后台后在次进入应用,避免重复上报数据 if (this.__licationHide) { - get_last_time(); const time = get_residence_time('app'); // 需要判断进入后台是否超过时限 ,默认是 30min ,是的话需要执行进入应用的上报 if (time.overtime) { @@ -805,7 +811,6 @@ class Report { } // 进入应用后台保存状态,方便进入前台后判断是否上报应用数据 this.__licationHide = true; - get_last_time(); const time = get_residence_time(); const route = get_page_route(self); uni.setStorageSync('_STAT_LAST_PAGE_ROUTE', route); @@ -816,8 +821,8 @@ class Report { }, type ); - // 重置时间 - get_first_time(); + // 更新页面首次访问时间 + set_first_time(); } /** @@ -833,20 +838,19 @@ class Report { }; const route = get_page_route(self); - const routepath = get_route(self); + const routepath = get_route$1(self); this._navigationBarTitle.config = get_page_name(routepath); // 表示应用触发 ,页面切换不触发之后的逻辑 if (this.__licationShow) { - get_first_time(); + // 更新页面首次访问时间 + set_first_time(); // this._lastPageRoute = route uni.setStorageSync('_STAT_LAST_PAGE_ROUTE', route); this.__licationShow = false; return } - get_last_time(); - const time = get_residence_time('page'); // 停留时间 if (time.overtime) { @@ -856,8 +860,8 @@ class Report { }; this.sendReportRequest(options); } - // 重置时间 - get_first_time(); + // 更新页面首次访问时间 + set_first_time(); } /** @@ -865,7 +869,6 @@ class Report { */ pageHide(self) { if (!this.__licationHide) { - get_last_time(); const time = get_residence_time('page'); let route = get_page_route(self); let lastPageRoute = uni.getStorageSync('_STAT_LAST_PAGE_ROUTE'); @@ -958,8 +961,16 @@ class Report { * 自定义事件上报 */ sendEventRequest({ key = '', value = '' } = {}) { - // const route = this._lastPageRoute - const routepath = get_route(); + + let routepath = ''; + + try { + routepath = get_route$1(); + } catch (error) { + const launch_options = dbGet('__launch_options'); + routepath = launch_options.path; + } + this._navigationBarTitle.config = get_page_name(routepath); this._navigationBarTitle.lt = '21'; let options = { @@ -1181,6 +1192,7 @@ class Stat extends Report { // 初始化页面停留时间 start set_page_residence_time(); this.__licationShow = true; + dbSet('__launch_options', options); this.sendReportRequest(options, true); } load(options, self) { @@ -1203,7 +1215,7 @@ class Stat extends Report { } // #ifdef VUE3 - if (get_platform_name() !== 'h5' && get_platform_name() !== 'n') { + if (get_platform_name() === 'h5' || get_platform_name() === 'n') { if (get_page_types(self) === 'app') { this.appShow(); } @@ -1224,7 +1236,7 @@ class Stat extends Report { } // #ifdef VUE3 - if (get_platform_name() !== 'h5' && get_platform_name() !== 'n') { + if (get_platform_name() === 'h5' || get_platform_name() === 'n') { if (get_page_types(self) === 'app') { this.appHide(); } @@ -1240,23 +1252,33 @@ class Stat extends Report { error(em) { // 开发工具内不上报错误 - if (this._platform === 'devtools') { - if (process.env.NODE_ENV === 'development') { - console.info('当前运行环境为开发者工具,不上报数据。'); - return - } - } + // if (this._platform === 'devtools') { + // if (process.env.NODE_ENV === 'development') { + // console.info('当前运行环境为开发者工具,不上报数据。') + // return + // } + // } let emVal = ''; if (!em.message) { emVal = JSON.stringify(em); } else { emVal = em.stack; } + + let route = ''; + try { + route = get_route(); + }catch(e){ + // 未获取到页面路径 + route = ''; + } + let options = { ak: this.statData.ak, uuid: this.statData.uuid, p: this.statData.p, lt: '31', + url:route, ut: this.statData.ut, ch: this.statData.ch, mpsdk: this.statData.mpsdk, diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js index 598a2dd79004dd1f8687ae49545abd1a8f55bf04..458ab5b96a54c154454ecb4d3bca42981e15315c 100644 --- a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js @@ -44,7 +44,7 @@ describe('codegen', () => { it('generate text with multiple statements', () => { assertCodegen( `{{obj.param1}}123123{{obj.param1}}123123{{obj.param1}} -{{obj.param3}}---{{obj.param3}} {{obj.param2}}aaaa{{obj.param2}}aaaa{{obj.param2}}`, - `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v((_$g(0,'t0-0'))+"123123"+(_$g(0,'t0-1'))+"123123"+(_$g(0,'t0-2'))),_c('v-uni-text',{attrs:{"_i":1}},[_v("-"+(_$g(1,'t0-0'))+"---"+(_$g(1,'t0-1')))]),_v((_$g(0,'t2-0'))+"aaaa"+(_$g(0,'t2-1'))+"aaaa"+(_$g(0,'t2-2')))],1)}` + `with(this){return _c('uni-view',{attrs:{"_i":0}},[_v((_$g(0,'t0-0'))+"123123"+(_$g(0,'t0-1'))+"123123"+(_$g(0,'t0-2'))),_c('v-uni-text',{attrs:{"_i":1}},[_v("-"+(_$g(1,'t0-0'))+"---"+(_$g(1,'t0-1')))]),_v((_$g(0,'t2-0'))+"aaaa"+(_$g(0,'t2-1'))+"aaaa"+(_$g(0,'t2-2')))],1)}` ) }) it('generate v-slot', () => { @@ -68,18 +68,18 @@ describe('codegen', () => { '

', `with(this){return _c('p',{wxsProps:{"change:prop":"pos"},attrs:{"change:prop":swipe.sizeReady,"prop":_$gc(0,'change:pos'),"_i":0},on:{"touchstart":function($event){$event = $handleWxsEvent($event);swipe.touchstart($event, $getComponentDescriptor())},"touchmove":function($event){$event = $handleWxsEvent($event);swipe.touchmove($event, $getComponentDescriptor())},"touchend":function($event){$event = $handleWxsEvent($event);swipe.touchend($event, $getComponentDescriptor())},"change":function($event){return $handleViewEvent($event)}}})}` ) - }) - - it('generate staticClass and id', () => { - assertCodegen( - '', - `with(this){return _c('custom',{attrs:{"id":"id","_i":0}})}` - ) - assertCodegen( - '', - `with(this){return _c('custom',{attrs:{"id":_$g(0,'a-id'),"_i":0}})}` - ) - }) + }) + + it('generate staticClass and id', () => { + assertCodegen( + '', + `with(this){return _c('custom',{attrs:{"id":"id","_i":0}})}` + ) + assertCodegen( + '', + `with(this){return _c('custom',{attrs:{"id":_$g(0,'a-id'),"_i":0}})}` + ) + }) // TODO 后续优化dataset // it('generate dataset', () => { @@ -91,11 +91,11 @@ describe('codegen', () => { it('generate dataset', () => { assertCodegen( '', - `with(this){return _c('v-uni-view',{attrs:{"data-a":"1","data-b":_$g(0,'a-data-b'),"_i":0}})}` - ) - assertCodegen( - '', - `with(this){return _c('custom',{attrs:{"data-a":"1","data-b":_$g(0,'a-data-b'),"_i":0}})}` + `with(this){return _c('uni-view',{attrs:{"data-a":"1","data-b":_$g(0,'a-data-b'),"_i":0}})}` + ) + assertCodegen( + '', + `with(this){return _c('custom',{attrs:{"data-a":"1","data-b":_$g(0,'a-data-b'),"_i":0}})}` ) }) it('generate v-if directive', () => { @@ -141,12 +141,12 @@ describe('codegen', () => { it('generate text trim', () => { assertCodegen( 'text', - `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v("text")])}` + `with(this){return _c('uni-view',{attrs:{"_i":0}},[_v("text")])}` ) assertCodegen( ' text ', - `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v("text")])}` + `with(this){return _c('uni-view',{attrs:{"_i":0}},[_v("text")])}` ) assertCodegen( @@ -179,42 +179,42 @@ describe('codegen', () => { assertCodegen( ' text text ', - `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v("text text")])}` + `with(this){return _c('uni-view',{attrs:{"_i":0}},[_v("text text")])}` ) assertCodegen( 'text {{text}} text', - `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v("text "+(_$g(0,'t0-0'))+" text")])}` + `with(this){return _c('uni-view',{attrs:{"_i":0}},[_v("text "+(_$g(0,'t0-0'))+" text")])}` ) assertCodegen( ' text {{text}} 文本 ', - `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v("text "+(_$g(0,'t0-0'))+" 文本")])}` + `with(this){return _c('uni-view',{attrs:{"_i":0}},[_v("text "+(_$g(0,'t0-0'))+" 文本")])}` ) assertCodegen( '{{text}} text text ', - `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v((_$g(0,'t0-0'))+" text text")])}` + `with(this){return _c('uni-view',{attrs:{"_i":0}},[_v((_$g(0,'t0-0'))+" text text")])}` ) assertCodegen( ' {{text}} text text ', - `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v((_$g(0,'t0-0'))+" text text")])}` + `with(this){return _c('uni-view',{attrs:{"_i":0}},[_v((_$g(0,'t0-0'))+" text text")])}` ) assertCodegen( '{{text}} text text {{text}}', - `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v((_$g(0,'t0-0'))+" text text "+(_$g(0,'t0-1')))])}` + `with(this){return _c('uni-view',{attrs:{"_i":0}},[_v((_$g(0,'t0-0'))+" text text "+(_$g(0,'t0-1')))])}` ) assertCodegen( ' {{text}} text text {{text}} ', - `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v((_$g(0,'t0-0'))+" text text "+(_$g(0,'t0-1')))])}` - ) - }) - it('generate bool attr', () => { - assertCodegen( - '