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

export default {
    isApp() {
        let qs = this.queryString();
        let isApp = !!qs.app_version || (qs.openrefer === 'app' && qs.uid);

        return !!isApp;
    },
    queryString() {
        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;
    },
    encode(obj) {
        let stringifyPrimitive = function(v) {
            switch (typeof v) {
                case 'string':
                    return v;
                case 'boolean':
                    return v ? 'true' : 'false';
                case 'number':
                    return isFinite(v) ? v : '';
                default:
                    return '';
            }
        };

        let sep = sep || '&';
        let eq = eq || '=';
        if (obj === null) {
            obj = undefined;
        }

        if (typeof obj === 'object') {
            return Object.keys(obj).map(function(k) {
                let ks = encodeURIComponent(stringifyPrimitive(k)) + eq;
                if (Array.isArray(obj[k])) {
                    return obj[k].map(function(v) {
                        return ks + encodeURIComponent(stringifyPrimitive(v));
                    }).join(sep);
                } else {
                    return ks + encodeURIComponent(stringifyPrimitive(obj[k]));
                }
            }).join(sep);
        }
        return '';
    },
    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').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();
    }
};