residence.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. pagePath: '', //页面路径
  18. accessDuration: 0, //浏览时长初始值为 0
  19. pageType: '', //页面类型
  20. pageLabel: '', //页面标签
  21. userId: 0, //用户Id
  22. productId: 0, //商品Id
  23. behaviorType: 1 // 统计类型
  24. }
  25. // 页面进入
  26. const enter = (current) => {
  27. current.meta.enterTime = Date.now()
  28. }
  29. // 页面离开
  30. const leave = (prev) => {
  31. prev.meta.leaveTime = Date.now()
  32. }
  33. // 页面切换
  34. const routting = async (current, prev) => {
  35. await userBehavior(current, prev)
  36. }
  37. /* 用户停留时间 */
  38. async function userBehavior(current, prev) {
  39. try {
  40. if (!prev) return
  41. if (!isInclude(prev.path)) return
  42. console.log('\n')
  43. console.log('------------------------')
  44. console.log('当前页面:', current.path)
  45. console.log('离开页面:', prev.path)
  46. // 接口参数设置
  47. setingSysParams(prev)
  48. // 清除设置缓存
  49. clearsSysParams(prev)
  50. console.log('\n')
  51. console.log('------------------------')
  52. console.log('\n')
  53. } catch (e) {
  54. console.log(e)
  55. console.log('---用户行为轨迹记录异常---')
  56. }
  57. }
  58. // 接口参数设置
  59. const setingSysParams = async (prev) => {
  60. const sysParams = Object.assign({}, defaultParams)
  61. const userSync = uni.getStorageSync('userInfo')
  62. const pageData = isIncludeType(prev.path)
  63. // 参数设置
  64. sysParams.userId = userSync.userId ? userSync.userId : 0
  65. sysParams.accessDuration = prev.meta.leaveTime - prev.meta.enterTime
  66. sysParams.pagePath = prev.fullPath
  67. sysParams.pageType = pageData ? pageData.pageType : ''
  68. sysParams.behaviorType = uni.getStorageSync('behaviorType') ? uni.getStorageSync('behaviorType') : 1
  69. // 根据path获取不同的参数
  70. if (prev.path === '/pages/goods/product' || prev.path === '/pages/second/product/product-details') {
  71. sysParams.productId = prev.query.id ? prev.query.id : 0
  72. sysParams.pageLabel = uni.getStorageSync('productLabel')
  73. } else {
  74. sysParams.pageLabel = uni.getStorageSync('pageLabel')
  75. }
  76. console.log('记录路径:', prev.path, '停留时间:', sysParams.accessDuration, 'ms', '标签:', sysParams.pageLabel)
  77. // 协销不记录
  78. if (userSync.userIdentity === 1) return
  79. // 统计接口调用
  80. await UserApi.userRecordStatistics(sysParams)
  81. console.log('---用户行为轨迹记录成功---')
  82. }
  83. // 清除设置缓存
  84. const clearsSysParams = async (prev) => {
  85. if (prev.path === '/pages/goods/product' || prev.path === '/pages/second/product/product-details') {
  86. uni.removeStorageSync('productLabel')
  87. } else {
  88. uni.removeStorageSync('pageLabel')
  89. }
  90. uni.removeStorageSync('behaviorType')
  91. }
  92. export default { enter, leave, routting }