12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- import { createBrowseHistory, createBrowseRecord } from '@/core/libs/requests'
- import { useUserStore } from '@/store'
- import { storeToRefs } from 'pinia'
- import dayjs from 'dayjs'
- export enum AnalysisEventType {
- LaunchApp = 4,
- /**
- * 发布圈子
- */
- PublishCircle = 5,
- /**
- * 浏览页面
- */
- ViewPage = 6,
- }
- export const useAnalysis = (automatic: boolean, isApp = false) => {
- const userStore = useUserStore()
- const { userInfo, isLogined } = storeToRefs(userStore)
- const viewDuration = ref(0)
- const viewStartAt = ref<Date | null>(null);
- const totalUsageTime = ref(0);
- const option = ref<Record<string, any>>({})
- const report = async (type) => {
- if (!isLogined.value) return
- const duration = automatic
- ? (viewDuration.value = dayjs().diff(viewStartAt.value, 'second'))
- : 0
- await createBrowseRecord({
- stylistId: userInfo.value.userId,
- bizType: type,
- duration,
- ...option.value,
- })
- viewDuration.value = 0
- viewStartAt.value = undefined
- option.value = {}
- }
- onLaunch(async () => {
- if (automatic) {
- if (isApp) {
- await report(AnalysisEventType.LaunchApp)
- }
- }
- })
- onLoad(() => {
- console.log('analysis')
- })
- onShow(() => {
- console.log('show')
- if (automatic) {
- viewStartAt.value = new Date()
- // if (isApp) {
- //
- // }
- }
- })
- onHide(async () => {
- if (automatic && viewStartAt.value) {
- const usageTime = dayjs().diff(viewStartAt.value, 'second'); // 本次使用时长(秒)
- totalUsageTime.value += usageTime; // 累计到总时长
- console.log(`本次使用时长:${usageTime} 秒`);
- console.log(`累计使用时长:${totalUsageTime.value} 秒`);
- await report(AnalysisEventType.ViewPage); // 上报页面浏览记录
- }
- })
- onUnload(async () => {
- // if (automatic) {
- // await report(AnalysisEventType.ViewPage)
- // }
- })
- return { option, report }
- }
|