residence.js 2.9 KB

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