residence.js 2.6 KB

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