residence.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { includeList } from './router.config.js' // 配置信息
  2. import ajaxService from '@/services/ajax.service.js'
  3. import UserService from '@/services/user.service'
  4. const UserApi = new UserService(ajaxService)
  5. // 校验是否为配置的路径
  6. const isInclude = (url) => {
  7. if (!url) return false
  8. return includeList.some(item => url.indexOf(item.url) > -1)
  9. }
  10. // 校验返回页面类型
  11. const isIncludeType = (url) => {
  12. if (!url) return false
  13. return includeList.find(item => url === item.url)
  14. }
  15. // 参数
  16. const defaultParams = {
  17. accessClient: 1, // 来源 0 网站 1 小程序
  18. pagePath: '', //页面路径
  19. accessDuration: 0, //浏览时长初始值为 0
  20. pageType: '', //页面类型
  21. pageLabel: '', //页面标签
  22. userId: 0, //用户Id
  23. productId: 0, //商品Id
  24. behaviorType: 1 // 统计类型
  25. }
  26. // 页面进入
  27. const enter = async (current) => {
  28. await userBehavior(current)
  29. current.meta.enterTime = Date.now()
  30. }
  31. // 页面离开
  32. const leave = (prev) => {
  33. prev.meta.leaveTime = Date.now()
  34. }
  35. // 页面切换
  36. const routting = async (current, prev) => {
  37. // await userBehavior(current, prev)
  38. }
  39. /* 用户停留时间 */
  40. async function userBehavior(current) {
  41. try {
  42. // if (process.env.NODE_ENV !== 'production') { return }
  43. if (!current) return
  44. if (!isInclude(current.path)) return
  45. console.log('\n')
  46. console.log('------------------------')
  47. console.log('当前页面:', current.path)
  48. // console.log('离开页面:', prev.path)
  49. // 接口参数设置
  50. setTimeout(()=>{
  51. setingSysParams(current)
  52. },1000)
  53. // 清除设置缓存
  54. clearsSysParams(current)
  55. console.log('\n')
  56. console.log('------------------------')
  57. console.log('\n')
  58. } catch (e) {
  59. console.log(e)
  60. console.log('---用户行为轨迹记录异常---')
  61. }
  62. }
  63. // 接口参数设置
  64. const setingSysParams = async (current) => {
  65. const sysParams = Object.assign({}, defaultParams)
  66. const userSync = uni.getStorageSync('userInfo')
  67. const pageData = isIncludeType(current.path)
  68. // 筛除掉协销不做统计
  69. if (userSync.userIdentity === 1) return
  70. // 筛除掉以下页面路径的重复统计
  71. if (current.path === '/pages/goods/product' || current.path === '/pages/search/search' ||
  72. current.path === '/pages/supplier/user/my-shop' || current.path === '/pages/search/search-supplier'
  73. ) {
  74. return
  75. }
  76. // 参数设置
  77. sysParams.userId = userSync.userId ? userSync.userId : 0
  78. sysParams.pagePath = current.fullPath
  79. sysParams.pageType = pageData ? pageData.pageType : ''
  80. sysParams.behaviorType = uni.getStorageSync('behaviorType') ? uni.getStorageSync('behaviorType') : 1
  81. // 根据path获取不同的参数
  82. if (current.path === '/pages/supplier/user/my-shop') {
  83. sysParams.shopId = current.query.shopId ? current.query.shopId : 0
  84. }
  85. if (current.path === '/pages/goods/product' || current.path === '/pages/second/product/product-details') {
  86. sysParams.productId = current.query.id ? current.query.id : 0
  87. sysParams.pageLabel = uni.getStorageSync('productLabel')
  88. } else {
  89. sysParams.pageLabel = uni.getStorageSync('pageLabel') ? uni.getStorageSync('pageLabel') : pageData.pageLabel
  90. }
  91. console.log('记录路径:', sysParams.pagePath, '标签:', sysParams.pageLabel)
  92. // 统计接口调用
  93. await UserApi.userRecordStatistics(sysParams)
  94. console.log('---用户行为轨迹记录成功---')
  95. }
  96. // 清除设置缓存
  97. const clearsSysParams = async (current) => {
  98. if (current.path === '/pages/goods/product' || current.path === '/pages/second/product/product-details') {
  99. uni.removeStorageSync('productLabel')
  100. } else {
  101. uni.removeStorageSync('pageLabel')
  102. }
  103. uni.removeStorageSync('behaviorType')
  104. }
  105. export default { enter, leave, routting }