cmSysMixins.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // 统计数据
  2. // 统计类型 1:首页banner;2:直播模块;3:最新活动;4:热门文章;5:新品橱窗;6:活动列表
  3. var cmSysVitaMixins = function () {
  4. return {
  5. data() {
  6. return {
  7. browseTime: 0, // 浏览时长初始值为 0
  8. clearTimeSet: null,
  9. cmSysParams:{
  10. pagePath:'',//页面路径
  11. accessDuration:0,//停留时间
  12. pageType:'',//页面类型
  13. pageLabel:'',//页面标签
  14. userId:0,//用户Id
  15. productId:0//商品Id
  16. }
  17. }
  18. },
  19. methods: {
  20. setTime() {
  21. //设置定时器
  22. let _self = this;
  23. const globalUserData = JSON.parse(localStorage.getItem('userInfo'));
  24. if(globalUserData){ this.cmSysParams.userId = globalUserData.userId * 1; }
  25. this.cmSysParams.pagePath = window.location.href;
  26. this.clearTimeSet = setInterval(() => {
  27. _self.browseTime++;
  28. }, 1000);
  29. },
  30. userRecordStatistics(url,data) {// navigator.sendBeacon 方式 发送统计数据
  31. const blob = new Blob([JSON.stringify(data)], {
  32. type: 'application/json; charset=UTF-8',
  33. });
  34. navigator.sendBeacon(url, blob);
  35. },
  36. beforeunloadHandler(e) {
  37. this.cmSysParams.accessDuration = this.browseTime*1000
  38. console.log(`页面路径:${this.cmSysParams.pagePath}`,`停留:${this.cmSysParams.accessDuration}s`,`标签:${this.cmSysParams.pageLabel}`)
  39. var NODE_ENV_BASE_URL = $("#coreServer").val();
  40. var url = `${NODE_ENV_BASE_URL}/user/record/StatisticsPc`
  41. this.userRecordStatistics(url,this.cmSysParams); // 上送后台接口,将浏览时长等信息传到后台,离开当前路由后调用
  42. clearInterval(this.clearTimeSet); // 离开页面后清除定时器
  43. },
  44. handleWindow(type){
  45. const isBrowser = isPC ? 1 : 0;
  46. const map = {
  47. 0: 'pagehide',
  48. 1: 'beforeunload',
  49. };
  50. const FunMap = {
  51. 0:window.addEventListener(map[isBrowser], e => this.beforeunloadHandler(e)), // 绑定
  52. 1:window.removeEventListener(map[isBrowser], e => this.beforeunloadHandler(e)) // 接触绑定
  53. }
  54. return FunMap[type];
  55. }
  56. },
  57. mounted() {
  58. // 页面加载完成后开始计时
  59. this.setTime();
  60. // 绑定窗口[监听]事件
  61. this.handleWindow(0)
  62. },
  63. destroyed() {
  64. // 解除窗口关闭[监听]事件
  65. this.handleWindow(1)
  66. }
  67. }
  68. }();