Authored by 李奇

商品列表上报组件修改

@@ -27,23 +27,29 @@ @@ -27,23 +27,29 @@
27 data() { 27 data() {
28 return { 28 return {
29 visible: false, 29 visible: false,
30 - waitingReport: [] 30 + waitingReport: [],
  31 + isVisible: false
31 }; 32 };
32 }, 33 },
33 methods: { 34 methods: {
34 - record() {  
35 - const param = {  
36 - P_NAME: this.pageName, // 页面名称  
37 - P_PARAM: this.pageParams, // 页面参数  
38 - I_INDEX: this.index, // 内部item的序号  
39 - PRD_SKN: this.productSkn // 商品SKN (可选)  
40 - }; 35 + record(visible) {
  36 + // 元素由不可见变为可见则记录,否则不记录
  37 + if (!this.isVisiable && visible) {
  38 + const param = {
  39 + P_NAME: this.pageName, // 页面名称
  40 + P_PARAM: this.pageParams, // 页面参数
  41 + I_INDEX: this.index, // 内部item的序号
  42 + PRD_SKN: this.productSkn // 商品SKN (可选)
  43 + };
41 44
42 - this.waitingReport.push(param); 45 + console.log('push')
  46 + this.waitingReport.push(param);
  47 + }
  48 + this.isVisiable = visible;
43 }, 49 },
44 report() { 50 report() {
45 if (this.waitingReport.length) { 51 if (this.waitingReport.length) {
46 - console.log('>>>>>>>>><<<<<<<<<', this.waitingReport) 52 + console.log('>>>>>>>>>report<<<<<<<<<', this.waitingReport)
47 this.$yas.event('YB_SHOW_EVENT', this.waitingReport); 53 this.$yas.event('YB_SHOW_EVENT', this.waitingReport);
48 this.reset(); 54 this.reset();
49 } 55 }
@@ -11,70 +11,60 @@ @@ -11,70 +11,60 @@
11 export default { 11 export default {
12 name: 'Exposure', 12 name: 'Exposure',
13 props: { 13 props: {
14 - viewRect: {  
15 - type: Object,  
16 - default() {  
17 - return {  
18 - top: 0,  
19 - left: 0,  
20 - bottom: 0,  
21 - right: 0  
22 - }  
23 - }  
24 - },  
25 - itemClass: {  
26 - type: String,  
27 - default() {  
28 - return 'exposure-item';  
29 - }  
30 - } 14 + topClassName: String
31 }, 15 },
32 data() { 16 data() {
33 return { 17 return {
  18 + viewArea: {
  19 + top: 0,
  20 + bottom: 0
  21 + }
34 }; 22 };
35 }, 23 },
36 watch: { 24 watch: {
37 }, 25 },
38 methods: { 26 methods: {
39 - checkReport(evt, isInit) { 27 + checkReport() {
40 let children = _.filter(this.$children, child => { 28 let children = _.filter(this.$children, child => {
41 return child.$options.name === 'ExposureItem' 29 return child.$options.name === 'ExposureItem'
42 }); 30 });
43 31
44 - _.each(children, (item, index) => {  
45 - const visible = this.isVisible(item.$el, this.$scrollEl);  
46 -  
47 -// if (visible && (this.componentStatus[index] !== visible || isInit)) {  
48 -// this.report(component, index + 1);  
49 -// }  
50 -// this.componentStatus[index] = visible; 32 + _.each(children, child => {
  33 + return child.record(this.isVisible(child.$el))
51 }); 34 });
52 }, 35 },
53 - report() {  
54 -  
55 - },  
56 isVisible($el) { 36 isVisible($el) {
57 const rect = $el.getBoundingClientRect(); 37 const rect = $el.getBoundingClientRect();
58 -// return ((parentRect.top >= rect.top && parentRect.top <= (rect.top + rect.height)) ||  
59 -// ((parentRect.top + parentRect.height) >= rect.top && (parentRect.top + parentRect.height) <= (rect.top + rect.height)) ||  
60 -// (rect.top >= parentRect.top && (rect.top + rect.height) <= (parentRect.top + parentRect.height)));  
61 - }  
62 38
  39 + return ((rect.top > this.viewArea.top && rect.top < this.viewArea.bottom) ||
  40 + rect.bottom > this.viewArea.top && rect.bottom < this.viewArea.bottom)
  41 + }
63 }, 42 },
64 created() { 43 created() {
65 }, 44 },
66 mounted() { 45 mounted() {
  46 + const filterRect = document.querySelector(`.${this.topClassName}`).getBoundingClientRect();
  47 +
  48 + // 可见区域顶部距离视口的上边的距离
  49 + this.viewArea.top = filterRect.bottom;
  50 +
  51 + // 可见区域底部距离视口的上边的距离
  52 + this.viewArea.bottom = window.screen.height;
  53 +
67 this.$scrollEl = window; 54 this.$scrollEl = window;
68 this.scrollEvent = util.throttle(500, this.checkReport); 55 this.scrollEvent = util.throttle(500, this.checkReport);
69 if (this.$scrollEl) { 56 if (this.$scrollEl) {
70 this.$scrollEl.addEventListener('scroll', this.scrollEvent); 57 this.$scrollEl.addEventListener('scroll', this.scrollEvent);
71 - this.checkReport(void 0, true);  
72 - }  
73 58
  59 + let timer = setInterval(() => {
  60 + if (this.$children.length) {
  61 + clearInterval(timer);
  62 + this.checkReport(void 0, true);
  63 + }
  64 + }, 500);
  65 + }
74 setInterval(() => { 66 setInterval(() => {
75 - let children = _.filter(this.$children, child => child.$options.name === 'ExposureItem');  
76 - _.each(children, child => child.report());  
77 - this.report(); 67 + _.each(_.filter(this.$children, child => child.$options.name === 'ExposureItem'), child => child.report());
78 }, 3000); 68 }, 3000);
79 } 69 }
80 }; 70 };