residence.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 ? userSync.userId : 0, //用户Id
  23. productId: 0 //商品Id
  24. }
  25. // 上送接口Api
  26. const userRecordStatistics = (params) => {
  27. UserApi.userRecordStatistics(params)
  28. .then(response => {
  29. console.log('◆◇◆◇上送用户行为记录成功◇◆◇◆')
  30. })
  31. .catch(error => {
  32. console.log('◇◆◇◆上送用户行为记录异常◇◆◇◆')
  33. return
  34. })
  35. }
  36. // 页面进入
  37. const onEnter = (route, map, log) => {
  38. /* 需要什么东西就加载route里面 route.query可以获取到当前页面的参数 */
  39. log('页面进入:', route.path, route.query, map)
  40. }
  41. // 页面离开
  42. const onLeave = (route, map, log) => {
  43. console.log('\n',route)
  44. console.log('◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆')
  45. console.log('页面离开')
  46. defaultParams.pageType = isIncludeType(route.path) ? isIncludeType(route.path).pageType : ''
  47. defaultParams.productId = route.query.id ? route.query.id : 0
  48. defaultParams.accessDuration = Date.now() - route.time
  49. defaultParams.pageLabel = uni.getStorageSync('pageLabel')
  50. userRecordStatistics(defaultParams)
  51. console.log('◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆')
  52. console.log('\n')
  53. }
  54. export default { onEnter, onLeave }