show.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { reportLog } from '../logger';
  2. import { getActiveKeyAfterClick } from '@/uilts/help';
  3. import Report from "@/log-center/log"
  4. // 每个窗口共享一个Observer实例
  5. class ShowLogObserver {
  6. constructor() {
  7. this._observe = null;
  8. this.showLogMap = new Map();
  9. this.init();
  10. }
  11. init() {
  12. this._observe = new IntersectionObserver((entries, observer) => {
  13. entries.forEach((entry) => {
  14. if (entry.isIntersecting) {
  15. this.report(entry);
  16. // show-log-once === '1' && 曝光之后取消观察
  17. if (entry?.target?.getAttribute('show-log-once') === '1') {
  18. this.remove(entry.target);
  19. }
  20. }
  21. })
  22. }, {
  23. root: null,
  24. rootMargin: '0px',
  25. threshold: 1
  26. })
  27. }
  28. remove(el) {
  29. this._observe.unobserve(el);
  30. this.showLogMap.delete(el.denetShowLogkey);
  31. }
  32. add(el, binding) {
  33. this._observe.observe(el);
  34. this.showLogMap.set(el.denetShowLogkey, binding.value)
  35. }
  36. report(el) {
  37. const logData = this.showLogMap.get(el?.target?.denetShowLogkey);
  38. return logData && reportLog({
  39. businessType: Report.businessType.pageView,
  40. ...logData
  41. })
  42. }
  43. }
  44. export default new ShowLogObserver();