You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

45 regels
1.2 KiB

  1. export function usePvcv() {
  2. const { post } = useApi()
  3. const { userInfo } = useUser()
  4. const route = useRoute()
  5. const sourceCookie = useCookie('source', { path: '/' })
  6. function getPagePayload(toRoute = route, extra = {}) {
  7. const path = toRoute.path || window.location.pathname || '/'
  8. const fullPath = toRoute.fullPath || path
  9. const url = import.meta.client ? `${window.location.origin}${fullPath}` : fullPath
  10. return {
  11. source: sourceCookie.value || '',
  12. referrer: extra.referrer || (import.meta.client ? document.referrer : ''),
  13. url,
  14. path,
  15. page_key: extra.page_key || String(toRoute.name || path).replace(/^\/+/, ''),
  16. user_id: userInfo.value?.user_id || 0,
  17. user_agent: import.meta.client ? navigator.userAgent : '',
  18. ...extra,
  19. }
  20. }
  21. async function reportPv(toRoute = route, extra = {}) {
  22. if (!import.meta.client) return
  23. try {
  24. await post('/common/pvcv', getPagePayload(toRoute, extra))
  25. } catch {
  26. // 统计不能影响主流程
  27. }
  28. }
  29. async function reportCv(key, extra = {}) {
  30. if (!key) return
  31. await reportPv(route, {
  32. ...extra,
  33. key,
  34. })
  35. }
  36. return {
  37. reportPv,
  38. reportCv,
  39. }
  40. }