utils.js 4.84 KB
import tip from '../css/featuretip.css'; //eslint-disable-line
import cointip from '../css/featuretip-coin.css'; //eslint-disable-line
import $ from 'jquery';

const isMobile = {
    Android() {
        return navigator.userAgent.match(/Android/i) ? true : false;
    },
    BlackBerry() {
        return navigator.userAgent.match(/BlackBerry/i) ? true : false;
    },
    iOS() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true : false;
    },
    Windows() {
        return navigator.userAgent.match(/IEMobile/i) ? true : false;
    },
    any() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());
    }
};
const mycurrency = isMobile.any() ? '//m.yohobuy.com/home/mycurrency' : '//www.yohobuy.com/home/currency';
const $tipTmpl = $('<div class="featuretip tip-wrap"><div class="tip"><div class="title"></div><div class="content"></div><a class="button" href="">返回</a></div></div>'); // eslint-disable-line
const $cointipTmpl = $('<div class="feature-coin tip-wrap"><div class="tip"><div class="tip-close">&times;</div><div class="title"></div><div class="content"></div><div class="bottom-button"><a class="button" href="">去逛逛</a><a class="coin" href=\'' + mycurrency + '?openby:yohobuy={"action":"go.mine"}\'>查看有货币</a></div></div></div>'); // eslint-disable-line

let _queryString = function() {
    if (!window._jssdkQS) {
        let vars = {},
            hash,
            i,
            hashes = window.location.search.slice(1).split('&');

        for (i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars[hash[0]] = hash[1];
        }

        window._jssdkQS = vars;
    }

    return window._jssdkQS;
};

let _sParamByIframe = function() {
    let search = window.location.search;
    if (search.indexOf('&expires=') === -1) {
        if (!search) {
            search = '?expires=' + (24 * 60 * 60 * 1000);
        } else {
            search = search + '&expires=' + (24 * 60 * 60 * 1000);    
        }
    }
    $('<iframe style="display:none;" src="//m.yohobuy.com/activity/wechat/1111' + search + '"></iframe>').prependTo('body');
};

let _bindEvent = function() {
    let $body = $('body');
    $body.on('click', '.feature-coin .close,.feature-coin .tip-close', function(e) {
        $cointipTmpl.fadeOut();
        e.preventDefault();
    });
    $body.on('click', '.feature-coin.tip-wrap', function(e) {
        if ('feature-coin tip-wrap' === e.target.className) {
            $cointipTmpl.fadeOut();
            e.preventDefault();
        }
    });
    $body.on('click', '.featuretip .close', function(e) {
        $tipTmpl.fadeOut();
        e.preventDefault();
    });
    $body.on('click', '.featuretip .refresh', function() {
        location.reload();
    });
    $body.on('click', '.featuretip.tip-wrap', function(e) {
        if ('featuretip tip-wrap' === e.target.className) {
            $tipTmpl.fadeOut();
            e.preventDefault();
        }
    });
};

export default {
    queryString: _queryString,
    init() {
        //发送活动页参数
        _sParamByIframe();

        // 绑定事件
        _bindEvent();
    },
    isApp() {
        let qs = _queryString();
        let isApp = !!qs.app_version || (qs.openrefer === 'app' && qs.uid);

        return !!isApp;
    },
    image(url, width, height, mode, quality) {
        mode = !isNaN(Number(mode)) ? mode : 2;
        url = url || '';
        url = url.replace(/{width}/g, width).replace(/{height}/g, height).replace(/{mode}/g, mode);
        if (url.indexOf('imageView2') > 0) {
            quality = quality || 90;
            url += '/q/' + quality;
        }
        return url.replace('http:', '');
    },
    showTip(data) {
        data = data || {
            title: '',
            content: '',
            close: true
        };

        $tipTmpl.find('.title').html(data.title);
        $tipTmpl.find('.content').html(data.content);

        if (data.close) {
            $tipTmpl.find('.button').addClass('close');
        } else {
            $tipTmpl.find('.button').addClass('refresh').html('刷新');
        }

        $('body').append($tipTmpl);
        $tipTmpl.show();
    },
    showCoinTip(data) {
        data = data || {
            title: '',
            content: '',
            close: true
        };

        $cointipTmpl.find('.title').html(data.title);
        $cointipTmpl.find('.content').html(data.content);

        if (data.close) {
            $cointipTmpl.find('.button').addClass('close');
        }

        if (data.coin) {
            $cointipTmpl.find('.coin').css('display', 'inline-block');
        } else {
            $cointipTmpl.find('.coin').hide();
        }

        if (data.img) {
            $cointipTmpl.find('.tip').css('background-image', 'url(' + data.img + ')');
        }

        $('body').append($cointipTmpl);
        $cointipTmpl.show();
    }
};