helpers.js 8.53 KB
'use strict';

const url = require('url');
const _ = require('lodash');
const config = require('../config/common');
const assetUrl = config.assetUrl;

const unitfromat = function(value, params) {
    if (params.rem) {
        value = Number(value) / (params.width || 40) + 'rem';
    } else if (params.percent) {
        value = value + '%';
    } else {
        value = value + 'px';
    }

    return value;
};

module.exports = {
    getAnalysis: function(data, index) {
        if (!data) {
            return '';
        }

        let fp = {
            F_ID: data.id,
            F_INDEX: data.order,
            F_NM: data.param.name,
            I_INDEX: index
        };

        if (data.component[index] && data.component[index].name) {
            fp.I_NM = data.component[index].name;
        }
        return JSON.stringify(fp);
    },
    isLazyLoad: function(type, index, opt) {
        if (type !== 'fix' && index > 8) { // 活动页中 不是浮动层及8张图后面的采用懒加载
            return opt.fn(this); // eslint-disable-line
        } else {
            return opt.inverse(this); // eslint-disable-line
        }
    },
    styleFormat: function(styleObj, opts) {
        let style = '';

        if (styleObj && _.isObject(styleObj)) {
            let params = opts.hash;
            let keys = ['width', 'height', 'top', 'left', 'right', 'bottom'];

            keys.forEach(function(k) {
                if (styleObj[k]) {
                    style += k + ':' + unitfromat(styleObj[k], params) + ';';
                }
            });
        }
        return style;
    },
    tabName: function(index, tabnames) {
        return tabnames[index].link;
    },
    tabStyle: function(index, count) {
        let width = (100 / Number(count)).toFixed(2);
        let style = 'height:100%;width:' + width + '%;';

        style += 'left:' + (Number(index) * Number(width)) + '%;';
        return style;
    },
    stringify: function(obj) {
        if (!obj) {
            return '';
        }
        return JSON.stringify(obj);
    },
    repeat: function(n, options) {
        if (n) {
            let str = '';

            for (let i = 0; i < n; i++) {
                let opt = {
                    index: i,
                    first: i === 0
                };

                str += options.fn(options, {
                    data: _.merge(opt, options)
                });
            }
            return str;
        }
        return options.inverse(this);
    },
    imgSrc: function(imgSrc) {
        return url.resolve(assetUrl, imgSrc);
    },
    image2: function(imageUrl, opts) {
        if (imageUrl && _.isString(imageUrl)) {
            let params = opts.hash;
            let urls = imageUrl.split('?');
            let query = urls[1] || '';
            let uri = urls[0];

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

            if (query) {
                query = query.replace(/{width}/g, params.w)
                    .replace(/{height}/g, params.h)
                    .replace(/{mode}/g, (params.mode || 2));

                if (query.indexOf('imageView2') === 0) {
                    if (params.q && query.indexOf('/q/') > 0) {
                        query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
                    } else if (params.q) {
                        query += '/q/' + params.q;
                    }
                } else if (query.indexOf('imageMogr2') === 0) {
                    if (params.q && query.indexOf('/quality/') > 0) {
                        query = query.replace(/\/quality\/\d+/g, '/quality/' + params.q);
                    } else if (params.q) {
                        query += '/quality/' + params.q;
                    }
                } else if (query.indexOf('imageView/') === 0) {
                    if (params.q && query.indexOf('/q/') > 0) {
                        query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
                    } else if (params.q) {
                        query += '/q/' + params.q;
                    }

                    if (params.mode) {
                        query = query.replace(/imageView\/\d{1}\//, 'imageView/' + params.mode + '/');
                    }
                }

                if (query.indexOf('/background') > -1) {
                    query = query.replace(/(\/background\/[^\/]+\/)/g, '/');
                }
            } else {
                query = 'imageView2/2/interlace/1/q/' + (params.q || 75);
            }
            return uri + '?' + query;
        } else {
            return '';
        }
    },

    /**
     * 图片质量调整
     */
    imageslim: function(imageUrl) {
        if (imageUrl && _.isString(imageUrl)) {
            let urls = imageUrl.split('?');
            let uri = urls[0];

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

            return uri + '?imageslim';
        } else {
            return '';
        }
    },
    isEqualOr: function() {
        let args = Array.prototype.slice.call(arguments);
        let v1 = args[0];
        let opt = args[args.length - 1];
        let isTrue = false;

        for (let i = 1; i < args.length - 1; i++) {
            if (v1 === args[i]) {
                isTrue = true;
                break;
            }
        }

        if (isTrue) {
            return opt.fn(this); // eslint-disable-line
        } else {
            return opt.inverse(this); // eslint-disable-line
        }
    },

    notEqualEither: function() {
        let args = Array.prototype.slice.call(arguments);
        let v1 = args[0];
        let opt = args[args.length - 1];
        let isTrue = false;

        for (let i = 1; i < args.length - 1; i++) {
            if (v1 === args[i]) {
                isTrue = true;
                break;
            }
        }

        if (!isTrue) {
            return opt.fn(this); // eslint-disable-line
        } else {
            return opt.inverse(this); // eslint-disable-line
        }
    },

    ifand: function() {
        let args = Array.prototype.slice.call(arguments);
        let opt = args[args.length - 1];
        let isTrue = true;

        for (let i = 0; i < args.length - 1; i++) {
            if (!args[i]) {
                isTrue = false;
                break;
            }
        }

        if (isTrue) {
            return opt.fn(this);
        } else {
            return opt.inverse(this);
        }
    },
    ifor: function() {
        let args = Array.prototype.slice.call(arguments);
        let opt = args[args.length - 1];
        let isTrue = false;

        for (let i = 0; i < args.length - 1; i++) {

            if (args[i]) {
                isTrue = true;
                break;
            }
        }

        if (isTrue) {
            return opt.fn(this);
        } else {
            return opt.inverse(this);
        }
    },
    unlessor: function() {
        let args = Array.prototype.slice.call(arguments);
        let opt = args[args.length - 1];
        let isTrue = true;

        for (let i = 0; i < args.length - 1; i++) {

            if (args[i]) {
                isTrue = false;
                break;
            }
        }

        if (isTrue) {
            return opt.fn(this);
        } else {
            return opt.inverse(this);
        }
    },

    /**
     * 小于某zhi
     *
     * @param letiable
     * @param number
     */
    within: function(letiable, number, opt) {
        if (letiable < number) {
            return opt.fn(this);
        } else {
            return opt.inverse(this);
        }
    },

    /**
     * 字段图片包含http
     */
    httpContent: function(contentData) {
        if (contentData) {
            const imgReg = /<img [^>]*src=['"]([^'"]+)[^>]*>/gi;

            contentData = (contentData || '').replace(imgReg, function(match, newUrl) {
                return match.replace(newUrl, newUrl.replace('http:', ''));
            });
        }
        return contentData;
    },

    /**
     * 特殊符号转译
     */
    htmlEncode: function(str) {
        const re = /(\r\n)|["\'<>]/g;

        str = str + '' || '';
        return str.replace(re, function(s) {
            switch (s) {
                case '"':
                    return '&quot;';
                case '\'':
                    return '&apos;';
                case '<':
                    return '&lt;';
                case '>':
                    return '&gt;';
                default:
                    return s;
            }
        });
    },
    getKeyValue(obj = {}, key) {
        return obj[key] || '';
    }
};