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.
 
 
 

73 regels
1.7 KiB

  1. import request from '@/utils/request.js'
  2. import API from '@/api/config.js'
  3. import { getUA } from '@/utils/util.js'
  4. import { getUser } from '@/utils/auth.js'
  5. let lastReportKey = ''
  6. let lastReportAt = 0
  7. function stringifyQuery(query = {}) {
  8. return Object.keys(query)
  9. .filter(key => query[key] !== undefined && query[key] !== null && query[key] !== '')
  10. .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(query[key])}`)
  11. .join('&')
  12. }
  13. function getCurrentPageInfo() {
  14. const pages = getCurrentPages()
  15. const page = pages[pages.length - 1]
  16. if (!page) return null
  17. const route = page.route || ''
  18. const query = page.options || {}
  19. const queryString = stringifyQuery(query)
  20. const path = route ? `/${route}` : '/'
  21. const url = queryString ? `${path}?${queryString}` : path
  22. return {
  23. route,
  24. path,
  25. url
  26. }
  27. }
  28. export function reportPagePv(extra = {}) {
  29. const page = getCurrentPageInfo()
  30. if (!page) return
  31. const now = Date.now()
  32. const key = extra.key ? `${page.url}::${extra.key}` : page.url
  33. if (key === lastReportKey && now - lastReportAt < 800) return
  34. lastReportKey = key
  35. lastReportAt = now
  36. const user = getUser() || {}
  37. const referrer = uni.getStorageSync('pvcv_last_url') || ''
  38. uni.setStorageSync('pvcv_last_url', page.url)
  39. request({
  40. url: API.INTERFACE_PVCV_ADD,
  41. method: 'POST',
  42. params: {
  43. source: uni.getStorageSync('source') || '',
  44. referrer,
  45. url: page.url,
  46. path: page.path,
  47. page_key: page.route,
  48. user_id: user.user_id || 0,
  49. user_agent: getUA(),
  50. ...extra
  51. }
  52. }).catch(() => {
  53. // 统计失败不能影响页面主流程
  54. })
  55. }
  56. export function reportCv(key, extra = {}) {
  57. if (!key) return
  58. reportPagePv({
  59. ...extra,
  60. key
  61. })
  62. }