util.js 937 Bytes
const yoho = require('yoho');

const getImgHost = function(url) {
    let urlArr = url.split('/'),
        num = urlArr[urlArr.length - 1].substr(1, 1),
        domain = 'static.yhbimg.com/goodsimg';

    url = domain + url;
    if (num === '1') {
        return '//img11.' + url;
    } else {
        return '//img12.' + url;
    }
};

const visibilitychange = function() {
    document.addEventListener('visibilitychange', function() {
        if (yoho.isApp && !document.hidden) {
            yoho.showLoading(false);
        }
    });

};

const getImgUrl = function(src, width = 300, height = 300, mode = 2) {
    return src ? src.replace(/(\{width}|\{height}|\{mode})/g, function($0) {
        const dict = {
            '{width}': width,
            '{height}': height,
            '{mode}': mode || 2
        };

        return dict[$0];
    }) : '';
};

module.exports = {
    getImgHost,
    getImgUrl,
    visibilitychange
};