residence.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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')
  44. console.log('◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆')
  45. console.log('页面离开')
  46. defaultParams.pageType = isIncludeType(route.path).pageType
  47. defaultParams.accessDuration = Date.now() - route.time
  48. defaultParams.pageLabel = uni.getStorageSync('pageLabel')
  49. userRecordStatistics(defaultParams)
  50. console.log('◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆')
  51. console.log('\n')
  52. }
  53. export default { onEnter, onLeave }