cmSysMixins.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. if(GLOBAL_TOKEN){ this.cmSysParams.userId = GLOBAL_USER_ID; }
  24. this.cmSysParams.pagePath = window.location.href;
  25. this.clearTimeSet = setInterval(() => {
  26. _self.cmSysParams.accessDuration++;
  27. console.log(`页面路径:${_self.cmSysParams.pagePath}`,`停留:${_self.cmSysParams.accessDuration}s`,`标签:${_self.cmSysParams.pageLabel}`)
  28. }, 1000);
  29. },
  30. userRecordStatistics(params) {
  31. var _this = this;
  32. PublicApi.userRecordStatistics(params,function (response) {
  33. if(response.code === 0){
  34. alert('111111111111111')
  35. console.log('<-------上送用户行为记录成功------>')
  36. }else{
  37. console.log('<-------上送用户行为记录异常------>')
  38. }
  39. })
  40. },
  41. beforeunloadHandler(e) {
  42. debugger
  43. this.userRecordStatistics(this.cmSysParams); // 上送后台接口,将浏览时长等信息传到后台,离开当前路由后调用
  44. console.log('关闭窗口之后',this.cmSysParams)
  45. clearInterval(this.clearTimeSet); // 离开页面后清除定时器
  46. }
  47. },
  48. mounted() {
  49. // 页面加载完成后开始计时
  50. this.setTime();
  51. // 绑定窗口关闭[监听]事件
  52. window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
  53. },
  54. destroyed() { // 解除窗口关闭[监听]事件
  55. window.removeEventListener('beforeunload', e => this.beforeunloadHandler(e))
  56. }
  57. }
  58. }();