goods-show-yas-rpter.js 2.28 KB
import qs from 'yoho-qs';
import yasTools from 'common/yas-tools';

class GoodsShowYasRpter {
    constructor(params) {
        this.timer = null;
        this.goodsContainer = $(document);
        this.goodsSelectorStr = '.good-info';

        if (params) {
            this.goodsContainer = params.goodsContainer ? $(params.goodsContainer) : this.goodsContainer;
            this.goodsSelectorStr = params.goodsSelectorStr || '.good-info';
        }

        this.reportParams = {
            C_ID: qs.physical_channel || 1,
            DATA: []
        };
        this.lastShowSkns = [];

        window.onscroll = () => {
            window.requestAnimationFrame(() => {
                this.reportCtl();
            });
        };

        this.report();
    }

    reportCtl() {
        if (this.timer) {
            clearTimeout(this.timer);
        }

        this.timer = setTimeout(() => {
            this.reportParams.DATA = [];
            this.report();
        }, 1000);
    }

    report() {
        let cacheTheShowSkns = []; // 暂存此次上报的商品,上报后复制给 lastReportSkns

        this.goodsContainer.find(this.goodsSelectorStr).each((gindex, theGoods) => {
            let $theGoods = $(theGoods);
            let proSkn = $theGoods.data('id');

            if ($.inviewport($theGoods, { threshold: 0 })) {
                let indexInLast = this.lastShowSkns.indexOf(proSkn);

                if (indexInLast < 0) {
                    this.reportParams.DATA.push({
                        P_NAME: yasTools.getPname(),
                        P_PARAM: location.href.split('?')[0],
                        I_INDEX: gindex + 1,
                        PRD_SKN: proSkn
                    });
                }

                cacheTheShowSkns.push(proSkn);
            }
        });

        this.lastShowSkns = cacheTheShowSkns;

        if (typeof _yas !== 'undefined' && this.reportParams.DATA && this.reportParams.DATA.length) {
            setTimeout(function() {
                window._yas.sendCustomInfo && window._yas.sendCustomInfo({
                    op: 'YB_SHOW_EVENT',
                    appop: 'YB_SHOW_EVENT',
                    param: JSON.stringify(this.reportParams)
                }, true);
            }, 1000);

        }
    }

}

export default GoodsShowYasRpter;