router.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import Vue from 'vue'
  2. import UniRouteGuards from '../js_sdk/pocky-route-gurads/lib/index.js' // 路由导航守卫
  3. import { includeList } from './router.config.js' // 配置信息
  4. import ajaxService from '@/services/ajax.service.js'
  5. import UserService from '@/services/user.service'
  6. const UserApi = new UserService(ajaxService)
  7. // 路由导航守卫
  8. Vue.use(UniRouteGuards)
  9. const map = new Map()
  10. const bucketStack = []
  11. const guard = new UniRouteGuards()
  12. // 截取路径参数
  13. const getUrlParams = (appPath) => {
  14. const queryArr = appPath.split('?')
  15. const queryStr = queryArr[1]
  16. let query = queryStr.split('&')
  17. let params = {}
  18. for (let i = 0; i < query.length; i++) {
  19. let q = query[i].split('=')
  20. if (q.length === 2) {
  21. params[q[0]] = q[1]
  22. }
  23. }
  24. return params
  25. }
  26. // 校验是否为配置的路径
  27. const isInclude = (url) => {
  28. if (!url) return false
  29. return includeList.some(item => url.indexOf(item.url) > -1)
  30. }
  31. // 校验返回页面类型
  32. const isIncludeType = (url) => {
  33. if (!url) return false
  34. return includeList.find(item => url === item.url)
  35. }
  36. // 参数
  37. const userSync = uni.getStorageSync('userInfo')
  38. const defaultParams = {
  39. pagePath: '', //页面路径
  40. accessDuration: 0, //浏览时长初始值为 0
  41. pageType: '', //页面类型
  42. pageLabel: '', //页面标签
  43. userId: userSync ? userSync.userId : 0, //用户Id
  44. productId: 0 //商品Id
  45. }
  46. // 上送接口Api
  47. const userRecordStatistics = (params) => {
  48. UserApi.userRecordStatistics(params)
  49. .then(response => {
  50. console.log('◆◇◆◇上送用户行为记录成功◇◆◇◆')
  51. })
  52. .catch(error => {
  53. console.log('◇◆◇◆上送用户行为记录异常◇◆◇◆')
  54. return
  55. })
  56. }
  57. // 跳过路由白名单拦截
  58. guard.beforeEach((to, from, next) => {
  59. console.log('\n')
  60. console.log('◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆')
  61. console.log('guard.beforeEach')
  62. console.log('to:', to)
  63. console.log('from:', from)
  64. console.log('页面拦截状态:', isInclude(to.url))
  65. // if (to.action !== 'navigateBack') {
  66. // bucketStack.push(to.url)
  67. // console.log('入栈:', to.url)
  68. // }
  69. if (isInclude(to.url)) {
  70. console.log('stay time started')
  71. defaultParams.pagePath = to.url
  72. if (to.url.indexOf('?') != -1) {
  73. map.set(to.url.split('?')[0], Date.now())
  74. const urlParams = getUrlParams(to.url)
  75. if (urlParams) {
  76. defaultParams.productId = urlParams ? urlParams.id : 0
  77. }
  78. }else{
  79. map.set(to.url, Date.now())
  80. }
  81. }
  82. // map.set(to.url.split('?')[0], Date.now())
  83. console.log('◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆')
  84. console.log('\n')
  85. next()
  86. })
  87. guard.afterEach((to, from) => {
  88. console.log('\n')
  89. console.log('◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆')
  90. console.log('guard.afterEach')
  91. console.log('to:', to)
  92. console.log('from:', from)
  93. // if (to.action === 'navigateBack') {
  94. // const lastUrl = bucketStack.pop()
  95. // map.set(lastUrl.split('?')[0], Date.now())
  96. // const current = bucketStack[bucketStack.length - 1]
  97. // if(current){
  98. // map.set(current.split('?')[0], Date.now())
  99. // }
  100. // console.log('未关闭页面栈列表:', bucketStack)
  101. // }
  102. if (map.has(from.url)) {
  103. const beginTime = map.get(from.url)
  104. defaultParams.pageType = isIncludeType(from.url).pageType
  105. defaultParams.accessDuration = Date.now() - beginTime
  106. defaultParams.pageLabel = uni.getStorageSync('pageLabel')
  107. console.log('页面停留时间:', (Date.now() - beginTime), '毫秒')
  108. // api
  109. console.log('api is action ...')
  110. userRecordStatistics(defaultParams)
  111. }
  112. console.log('◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆')
  113. console.log('\n')
  114. })