residence.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. }
  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. const sysParams = Object.assign({}, defaultParams)
  40. try {
  41. if (!prev) return
  42. if (!isInclude(prev.path)) return
  43. //协销不记录
  44. if (userSync.userIdentity === 1) return
  45. console.log('\n')
  46. console.log('------------------------')
  47. // 停留时长参数设置
  48. sysParams.accessDuration = prev.meta.leaveTime - prev.meta.enterTime
  49. console.log('当前页面:', current.path)
  50. console.log('离开页面:', prev.path)
  51. // 接口参数设置
  52. const pageData = isIncludeType(prev.path)
  53. sysParams.pagePath = prev.fullPath
  54. sysParams.pageType = pageData ? pageData.pageType : ''
  55. if (prev.path === '/pages/goods/product' || prev.path === '/pages/second/product/product-details') {
  56. sysParams.productId = prev.query.id ? prev.query.id : 0
  57. sysParams.pageLabel = uni.getStorageSync('productLabel')
  58. }else{
  59. sysParams.pageLabel = uni.getStorageSync('pageLabel')
  60. }
  61. // 调用接口
  62. console.log('记录路径:', prev.path, '停留时间:', sysParams.accessDuration, 'ms', '标签:', sysParams.pageLabel)
  63. await UserApi.userRecordStatistics(sysParams)
  64. uni.removeStorageSync('pageLabel')
  65. // 删除标记标签名
  66. console.log('---用户行为轨迹记录成功---')
  67. console.log('------------------------')
  68. console.log('\n')
  69. } catch (e) {
  70. console.log(e)
  71. console.log('---用户行为轨迹记录异常---')
  72. }
  73. }
  74. export default { enter, leave, routting }