pageTime.uts 3.5 KB
Newer Older
M
mehaotian 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
import { dbGet } from "./db"
import { PAGE_PVER_TIME, APP_PVER_TIME } from '../config.uts';
import { ResidenceTimeReturn } from '../../interface.uts'
import { dbSet, dbRemove } from "./db";

// 首次访问时间
const FIRST_VISIT_TIME_KEY = '__first__visit__time'
// 最后访问时间
const LAST_VISIT_TIME_KEY = '__last__visit__time'
// 访问总数
const TOTAL_VISIT_COUNT = '__total__visit__count'
const FIRST_TIME = '__first_time'

// 页面停留时间记录key
const PAGE_RESIDENCE_TIME = '__page__residence__time'
let First_Page_Residence_Time = 0
let Last_Page_Residence_Time = 0

/**
 * 获取当前时间
 */
export const get_time = () : number => {
  return Math.floor(new Date().getTime() / 1000)
}


/**
 * 设置页面首次访问时间,用户获取页面/应用停留时常
 */
export const set_first_time = () : number => {
	// 获取当前时间 ,以下代码获取到是毫秒级时间戳 ,实际上用到是秒级时间戳,所以需要除以1000
	// const time = new Date().getTime()
	let time = get_time()
	dbSet(FIRST_TIME, time)
	return time
}



/**
 * 获取首次访问时间
 */
export const get_first_visit_time = () : number => {
	const timeStorge = dbGet<number>(FIRST_VISIT_TIME_KEY)
	let time : number
	if (timeStorge != null && timeStorge != 0) {
		time = timeStorge as number
	} else {
		time = get_time()
		dbSet(FIRST_VISIT_TIME_KEY, time)
		// 首次访问需要 将最后访问时间置 0
		dbRemove(LAST_VISIT_TIME_KEY)
	}
	return time
}

/**
 * 最后访问时间
 */
export const get_last_visit_time = () : number => {
	const timeStorge = dbGet<number>(LAST_VISIT_TIME_KEY)
M
mehaotian 已提交
62
	let time = 0
M
mehaotian 已提交
63 64
	if (timeStorge != null && timeStorge != 0) {
		time = timeStorge as number
M
mehaotian 已提交
65 66 67
	}
	
	dbSet(LAST_VISIT_TIME_KEY, get_time())
M
mehaotian 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	return time
}


/**
 * 获取总访问次数
 */
export const get_total_visit_count = () : number => {
	const timeStorge = dbGet<number>(TOTAL_VISIT_COUNT)
	let count = 1
	if (timeStorge != null) {
		count = timeStorge as number
		count++
	}
	dbSet(TOTAL_VISIT_COUNT, count)
	return count
}

/**
 * 获取页面 \ 应用停留时间
 */
export const get_residence_time = (type : string) : ResidenceTimeReturn => {
	let residenceTime = 0
	const last_time = get_time()
	const first_time = (dbGet(FIRST_TIME) ?? last_time) as number
	if (first_time != 0) {
		residenceTime = last_time - first_time
	}
	// 将毫秒级时间戳转换为秒级时间戳,因为直接获取的是秒级时间戳,所以不需要转换
	// residenceTime = parseInt(residenceTime / 1000)
	residenceTime = residenceTime < 1 ? 1 : residenceTime
	let timeData : ResidenceTimeReturn = {
		residenceTime: residenceTime,
		overtime: false,
	}
	if (type === 'app') {
		let overtime = residenceTime > APP_PVER_TIME ? true : false
		timeData.overtime = overtime
		return timeData
	}
	if (type === 'page') {
		let overtime = residenceTime > PAGE_PVER_TIME ? true : false
		timeData.overtime = overtime
		return timeData
	}
	return timeData
}

/**
 * 设置页面停留时间
 */
export const set_page_residence_time = () : number => {
	First_Page_Residence_Time = get_time()
	dbSet(PAGE_RESIDENCE_TIME, First_Page_Residence_Time)
	return First_Page_Residence_Time
}

/**
 * 获取页面停留时间
 */
export const get_page_residence_time = () : number => {
	Last_Page_Residence_Time = get_time()
	First_Page_Residence_Time = dbGet<number>(PAGE_RESIDENCE_TIME) ?? 0
	const diff : number = Last_Page_Residence_Time - First_Page_Residence_Time
	return diff
}