helper.wxs 3.72 KB
/* global getRegExp */
var regExpWidth = getRegExp('{width}', 'g');
var regExpHeight = getRegExp('{height}', 'g');
var regExpMode = getRegExp('{mode}', 'g');
var regExpQg = getRegExp('/q/d+', 'g');
var regExpQ = getRegExp('/q/d+');
var regExpQuality = getRegExp('/quality/d+');
var regExpQualityg = getRegExp('/quality/d+', 'g');
var regExpImageView = getRegExp('imageView');
var regExpImageMogr = getRegExp('imageMogr');

var defaultQuality = 75;

function image(imgUrl, w, h, mode, q) {
    var urls,
        query,
        url;

    var params = {
        w: w,
        h: h,
        mode: mode || 2,
        q: q || defaultQuality
    };

    if (imgUrl && (typeof imgUrl === 'string')) {
        urls = imgUrl.split('?');
        query = urls[1] || '';
        url = urls[0];

        if (url.indexOf('http:') === 0) {
            url = url.replace('http:', 'https:');
        }

        if (!query || query === 'imageslim') {
            url += params.q === defaultQuality ? '?imageslim' : '?imageView2/0/interlace/1/q/' + params.q;
            imgUrl = url;
        } else {
            imgUrl = imgUrl.replace(regExpWidth, params.w)
                .replace(regExpHeight, params.h)
                .replace(regExpMode, (params.mode));

            if (regExpImageView.test(query)) { // imageView2 || imageView
                if (!regExpQ.test(query)) {
                    imgUrl += '/q/' + params.q;
                } else {
                    imgUrl = imgUrl.replace(regExpQg, '/q/' + params.q);
                }
            } else if (regExpImageMogr.test(query)) {
                if (!regExpQuality.test(query)) {
                    imgUrl += '/quality/' + params.q;
                } else {
                    imgUrl = imgUrl.replace(regExpQualityg, '/quality/' + params.q);
                }
            }
        }
        return imgUrl;
    } else {
        return '';
    }
}

// 如果图片没有imageView2,则默认添加imageView2
function imgView(imgSrc, w, h, mode, q) {
    var imgUrl = imgSrc || '';
    var indexOf = imgUrl.indexOf('?');

    if (!imgUrl) {
        return '';
    }

    if (imgUrl.indexOf('http://') === imgUrl.indexOf('https://')) {
        return imgUrl;
    }

    if (indexOf === -1) {
        imgUrl += '?imageView2/{mode}/w/{width}/h/{height}';
        return image(imgUrl, w, h, mode, q);
    }

    if (indexOf > -1) {
        return image(imgUrl, w, h, mode, q);
    }

    return imgUrl;
}

function dateFormat(times, format) {
    var date, year, month, day, hour, minute, second;

    if (getRegExp('^[0-9]*$').test(times)) {
        times = times.toString().length === 10 ? times * 1000 : times;
    } else {
        times = times && times.replace(getRegExp('-', 'g'), '/');
    }

    // ios 日期格式为2017/05/06 12:10:30
    date = times ? getDate(times) : getDate();
    year = date.getFullYear();
    month = date.getMonth() + 1;
    day = date.getDate();

    hour = date.getHours();
    minute = date.getMinutes();
    second = date.getSeconds();

    format = format || 'YYYY/MM/DD HH:mm:ss';
    [year, month, day, hour, minute, second] = [year, month, day, hour, minute, second].map(function(n) {
        n = n.toString();
        return n[1] ? n : '0' + n;
    });

    return format.replace('YYYY', year).replace('MM', month).replace('DD', day)
        .replace('HH', hour).replace('mm', minute).replace('ss', second);
}

function strBlur(str) {
    str = (str || '').replace(getRegExp('.(?=.)', 'g'), '*');

    return str;
}

function mobileBlur(mobile) {
    mobile = (mobile || '').toString();
    mobile = mobile.substring(0, 3) + '****' + mobile.substring(7);

    return mobile;
}

module.exports = {
    dateFormat: dateFormat,
    image: image,
    imgView: imgView,
    strBlur: strBlur,
    mobileBlur: mobileBlur
};