188 lines
6.0 KiB
JavaScript
188 lines
6.0 KiB
JavaScript
import Vue from 'vue'
|
|
import { pushSystemStatic } from '@/api/public'
|
|
import { pageViewDataCacheKey } from '@/config/index'
|
|
|
|
// 日期格式化
|
|
export function formatterDate(date, flag = '-', mode = 'date') {
|
|
const year = new Date(date).getFullYear()
|
|
const month = new Date(date).getMonth() + 1
|
|
const day = new Date(date).getDate()
|
|
return `${year}${flag}${month > 9 ? month : '0' + month}${flag}${day > 9 ? day : '0' + day}`
|
|
}
|
|
|
|
export function formatContent(html) {
|
|
// 去掉img标签里的style、width、height属性
|
|
let contentData = html.replace(/<img[^>]*>/gi, function(match, capture) {
|
|
match = match.replace(`style=""`, '')
|
|
match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '')
|
|
match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '')
|
|
match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '')
|
|
return match
|
|
}) // 修改所有style里的width属性为max-width:100%
|
|
contentData = contentData.replace(/style="[^"]+"/gi, function(match, capture) {
|
|
match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;'); return match
|
|
}) // 去掉<br/>标签
|
|
contentData = contentData.replace(/<br[^>]*\/>/gi, '') // img标签添加style属性:max-width:100%;height:auto
|
|
contentData = contentData.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:0 auto;"')
|
|
return contentData
|
|
}
|
|
|
|
/**
|
|
* desc: 监听页面访问次数
|
|
* auth: lifizer
|
|
* date: 2024-05-16
|
|
* @param key-页面唯一标识
|
|
* @param cb-回调函数
|
|
* @returns {null}
|
|
*/
|
|
export function pageToWatchShowCount(key, cb = '') {
|
|
if (!key) {
|
|
return
|
|
}
|
|
if (!Vue.prototype.$pageToWatchData[key]) {
|
|
Vue.prototype.$pageToWatchData[key] = []
|
|
}
|
|
console.log('页面监听-----%s', key)
|
|
cb && cb()
|
|
}
|
|
|
|
/**
|
|
* desc: 监听页面访问时长
|
|
* auth: lifizer
|
|
* date: 2024-05-16
|
|
* @param key-页面唯一标识
|
|
* @returns {null}
|
|
*/
|
|
export function pageToWatchShowTimer(key, data) {
|
|
if (!key) {
|
|
return
|
|
}
|
|
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
|
|
}
|
|
|
|
/**
|
|
* desc: 获取当前系统时间
|
|
* auth: lifizer
|
|
* date: 2024-05-16
|
|
* @returns {String}
|
|
*/
|
|
export function getCurrDateTime() {
|
|
const date = new Date()
|
|
let str = ''
|
|
str += date.getFullYear() + '-'
|
|
str += (date.getMonth() + 1).toString().padStart(2, '0') + '-'
|
|
str += (date.getDate()).toString().padStart(2, '0') + ' '
|
|
str += (date.getHours()).toString().padStart(2, '0') + ':'
|
|
str += (date.getMinutes()).toString().padStart(2, '0') + ':'
|
|
str += (date.getSeconds()).toString().padStart(2, '0')
|
|
return str
|
|
}
|
|
|
|
/**
|
|
* desc: 计算两个时间的相差的秒数
|
|
* auth: lifizer
|
|
* date: 2024-05-16
|
|
* @param startValue-开始时间
|
|
* @param endValue-结束时间
|
|
* @returns {null}
|
|
*/
|
|
export function TimeDifference(startValue, endValue) {
|
|
const startYear = parseInt(startValue.substring(0, 4))
|
|
const startMonth = parseInt(startValue.substring(5, 7)) - 1
|
|
const startDate = parseInt(startValue.substring(8, 10))
|
|
const startHours = parseInt(startValue.substring(11, 13))
|
|
const startMinutes = parseInt(startValue.substring(14, 16))
|
|
const startSeconds = parseInt(startValue.substring(17, 19))
|
|
const endYear = parseInt(endValue.substring(0, 4))
|
|
const endMonth = parseInt(endValue.substring(5, 7)) - 1
|
|
const endDate = parseInt(endValue.substring(8, 10))
|
|
const endHours = parseInt(endValue.substring(11, 13))
|
|
const endMinutes = parseInt(endValue.substring(14, 16))
|
|
const endSeconds = parseInt(endValue.substring(17, 19))
|
|
const startTime = (new Date(startYear, startMonth, startDate, startHours, startMinutes, startSeconds)).getTime()
|
|
const endTime = (new Date(endYear, endMonth, endDate, endHours, endMinutes, endSeconds)).getTime()
|
|
return (endTime - startTime) / 1000
|
|
}
|
|
|
|
/**
|
|
* 清楚所有的定时器
|
|
* @returns {null}
|
|
*/
|
|
export function clearAllInterval() {
|
|
const endTimerId = setInterval(function() {
|
|
console.log('clear all timer...')
|
|
}, 1000)
|
|
for (let i = 1; i <= endTimerId; i++) {
|
|
clearInterval(i)
|
|
}
|
|
pushSysBySecondsHandle()
|
|
}
|
|
|
|
/**
|
|
* 定时推送埋点
|
|
* @returns {null}
|
|
*/
|
|
function pushSysBySecondsHandle() {
|
|
setInterval(() => {
|
|
const userInfo = uni.getStorageSync('userInfo')
|
|
if (!userInfo) {
|
|
return
|
|
}
|
|
let statsData = []
|
|
for (const key in Vue.prototype.$pageToWatchData) {
|
|
if (Vue.prototype.$pageToWatchData[key]) {
|
|
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
|
|
const osName = uni.getSystemInfoSync().osName
|
|
console.log(deviceType)
|
|
console.log(osName)
|
|
const postData = {
|
|
uid: userInfo.uid,
|
|
deviceType: deviceType === 'phone' ? osName : 'unknown',
|
|
statsData
|
|
}
|
|
// console.log('开始上报数据-------------------')
|
|
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)
|
|
}
|