|
- export function usePvcv() {
- const { post } = useApi()
- const { userInfo } = useUser()
- const route = useRoute()
- const sourceCookie = useCookie('source', { path: '/' })
-
- function getPagePayload(toRoute = route, extra = {}) {
- const path = toRoute.path || window.location.pathname || '/'
- const fullPath = toRoute.fullPath || path
- const url = import.meta.client ? `${window.location.origin}${fullPath}` : fullPath
- return {
- source: sourceCookie.value || '',
- referrer: extra.referrer || (import.meta.client ? document.referrer : ''),
- url,
- path,
- page_key: extra.page_key || String(toRoute.name || path).replace(/^\/+/, ''),
- user_id: userInfo.value?.user_id || 0,
- user_agent: import.meta.client ? navigator.userAgent : '',
- ...extra,
- }
- }
-
- async function reportPv(toRoute = route, extra = {}) {
- if (!import.meta.client) return
- try {
- await post('/common/pvcv', getPagePayload(toRoute, extra))
- } catch {
- // 统计不能影响主流程
- }
- }
-
- async function reportCv(key, extra = {}) {
- if (!key) return
- await reportPv(route, {
- ...extra,
- key,
- })
- }
-
- return {
- reportPv,
- reportCv,
- }
- }
|