From 19914b5b0d09892cf4ecc05b2a56874f005d0c64 Mon Sep 17 00:00:00 2001 From: lifizer <913626195@qq.com> Date: Sun, 16 Jun 2024 16:33:41 +0800 Subject: [PATCH] =?UTF-8?q?Feat(=E9=A1=B5=E9=9D=A2):=E6=9C=AA=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=97=B6=E9=A1=B5=E9=9D=A2=E5=9F=8B=E7=82=B9=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=86=99=E5=85=A5=E7=BC=93=E5=AD=98=E4=BB=A5=E4=BE=BF?= =?UTF-8?q?=E4=B8=8B=E6=AC=A1=E7=99=BB=E5=BD=95=E6=97=B6=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E4=B8=8A=E6=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/index.js | 2 ++ config/page.js | 6 ++++-- utils/store/cookie.js | 8 ++++---- utils/util.js | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/config/index.js b/config/index.js index 6084326..5c30e44 100644 --- a/config/index.js +++ b/config/index.js @@ -22,3 +22,5 @@ export const VUE_APP_RESOURCES_URL = "https://wxapp.xdd618.com/api/file/pic" export const STATIC_RESOURCE_URL = "https://resource.xdd618.com/image" export const WX_LIVE_APPID = "wx2b03c6e691cd7370"; + +export const pageViewDataCacheKey = 'pageViewDataCacheKey' diff --git a/config/page.js b/config/page.js index 2768c5a..79d35e6 100644 --- a/config/page.js +++ b/config/page.js @@ -1,3 +1,5 @@ -export const pageToWatchData = { +import { getPageViewDataToLocalCache } from '@/utils/util' + +export const pageToWatchData = Object.assign({ 'appIndex': [] -} +}, getPageViewDataToLocalCache()) diff --git a/utils/store/cookie.js b/utils/store/cookie.js index 4eeb230..958186d 100644 --- a/utils/store/cookie.js +++ b/utils/store/cookie.js @@ -1,4 +1,5 @@ import { trim, isType } from "@/utils"; +import { pageViewDataCacheKey } from '@/config/index' const doc = null; // const doc = window.document; @@ -30,15 +31,14 @@ function remove(key) { function clearAll() { // uni.clearStorage() - const res = uni.getStorageInfoSync(); + const res = uni.getStorageInfoSync() res.keys.map((item) => { - //不要清除spread和redirect - if (item == 'redirect' || item == 'spread') { + // 不要清除spread和redirect、页面埋点数据 + if ([pageViewDataCacheKey, 'redirect', 'spread'].includes(item)) { return } remove(item) }) - console.log(res) } function _has(key) { diff --git a/utils/util.js b/utils/util.js index de312fe..a8a24f4 100644 --- a/utils/util.js +++ b/utils/util.js @@ -1,5 +1,6 @@ import Vue from 'vue' import { pushSystemStatic } from '@/api/public' +import { pageViewDataCacheKey } from '@/config/index' // 日期格式化 export function formatterDate(date, flag = '-', mode = 'date') { @@ -54,6 +55,24 @@ export function pageToWatchShowTimer(key, data) { if (Vue.prototype.$pageToWatchData[key]) { Vue.prototype.$pageToWatchData[key].push(data) } + const userInfo = uni.getStorageSync('userInfo') + if (!userInfo) { + uni.setStorageSync(pageViewDataCacheKey, Vue.prototype.$pageToWatchData) + } +} + +/** + * desc: 获取缓存页面埋点数据 + * auth: lifizer + * date: 2024-06-16 + * @returns {data} + */ +export function getPageViewDataToLocalCache() { + let data = {} + if (uni.setStorageSync(pageViewDataCacheKey)) { + data = uni.setStorageSync(pageViewDataCacheKey) + } + return data } /** @@ -130,6 +149,10 @@ function pushSysBySecondsHandle() { statsData = statsData.concat(Vue.prototype.$pageToWatchData[key]) } } + if (statsData.length < 1) { + console.log('*************没有页面埋点数据则不用上报,节约网络请求') + return + } // 设备类型phone、pad、pc、unknow const deviceType = uni.getSystemInfoSync().deviceType // 系统名称ios、android @@ -142,7 +165,17 @@ function pushSysBySecondsHandle() { statsData } console.log('开始上报数据-------------------') - pushSystemStatic(postData).catch(err => { + pushSystemStatic(postData).then(res => { + const { success } = res + if (success) { + // 上报成功之后清空上一次的页面埋点数据 + for (const key in Vue.prototype.$pageToWatchData) { + Vue.prototype.$pageToWatchData[key] = [] + } + console.log('*************清除上次未登录缓存的页面埋点数据') + uni.removeStorageSync(pageViewDataCacheKey) + } + }).catch(err => { console.log(err) }) }, 30 * 1000)