Blame view

utils/helpers.js 8.46 KB
姜枫 authored
1 2
'use strict';
lijing authored
3
const url = require('url');
4
const _ = require('lodash');
lijing authored
5 6 7
const config = require('../config/common');
const assetUrl = config.assetUrl;
沈志敏 authored
8 9 10 11 12 13 14 15 16 17 18 19
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;
};
lijing authored
20
module.exports = {
沈志敏 authored
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    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);
    },
沈志敏 authored
38
    isLazyLoad: function(type, index, opt) {
沈志敏 authored
39 40 41
        if (type !== 'fix' && index > 8) { // 活动页中 不是浮动层及8张图后面的采用懒加载
            return opt.fn(this); // eslint-disable-line
        } else {
lijing authored
42
            return opt.inverse(this); // eslint-disable-line
沈志敏 authored
43 44
        }
    },
沈志敏 authored
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    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;
    },
沈志敏 authored
60
    tabName: function(index, tabnames) {
TaoHuang authored
61
        return tabnames[index].link;
沈志敏 authored
62
    },
沈志敏 authored
63 64 65 66 67 68 69 70
    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) {
沈志敏 authored
71
        if (!obj) {
ccbikai(👎🏻🍜) authored
72 73
            return '';
        }
沈志敏 authored
74 75 76 77
        return JSON.stringify(obj);
    },
    repeat: function(n, options) {
        if (n) {
沈志敏 authored
78
            let str = '';
沈志敏 authored
79
沈志敏 authored
80 81
            for (let i = 0; i < n; i++) {
                let opt = {
沈志敏 authored
82 83 84 85 86 87 88 89 90 91 92 93
                    index: i,
                    first: i === 0
                };

                str += options.fn(options, {
                    data: _.merge(opt, options)
                });
            }
            return str;
        }
        return options.inverse(this);
    },
lijing authored
94 95
    imgSrc: function(imgSrc) {
        return url.resolve(assetUrl, imgSrc);
姜枫 authored
96
    },
97
    image2: function(imageUrl, opts) {
98
        if (imageUrl && _.isString(imageUrl)) {
姜枫 authored
99
            let params = opts.hash;
100
            let urls = imageUrl.split('?');
姜枫 authored
101
            let query = urls[1] || '';
姜枫 authored
102 103 104 105 106
            let uri = urls[0];

            if (uri.indexOf('http:') === 0) {
                uri = uri.replace('http:', '');
            }
姜枫 authored
107 108

            if (query) {
陈峰 authored
109 110 111
                query = query.replace(/{width}/g, params.w)
                    .replace(/{height}/g, params.h)
                    .replace(/{mode}/g, (params.mode || 2));
姜枫 authored
112 113 114 115

                if (query.indexOf('imageView2') === 0) {
                    if (params.q && query.indexOf('/q/') > 0) {
                        query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
姜枫 authored
116
                    } else if (params.q) {
姜枫 authored
117 118 119 120 121
                        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);
姜枫 authored
122
                    } else if (params.q) {
姜枫 authored
123 124
                        query += '/quality/' + params.q;
                    }
沈志敏 authored
125
                } else if (query.indexOf('imageView/') === 0) {
126 127
                    if (params.q && query.indexOf('/q/') > 0) {
                        query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
姜枫 authored
128
                    } else if (params.q) {
129 130 131 132 133 134
                        query += '/q/' + params.q;
                    }

                    if (params.mode) {
                        query = query.replace(/imageView\/\d{1}\//, 'imageView/' + params.mode + '/');
                    }
姜枫 authored
135
                }
王水玲 authored
136 137 138 139

                if (query.indexOf('/background') > -1) {
                    query = query.replace(/(\/background\/[^\/]+\/)/g, '/');
                }
姜枫 authored
140 141 142
            } else {
                query = 'imageView2/2/interlace/1/q/' + (params.q || 75);
            }
姜枫 authored
143
            return uri + '?' + query;
姜枫 authored
144
        } else {
145
            return '';
姜枫 authored
146 147
        }
    },
郭成尧 authored
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

    /**
     * 图片质量调整
     */
    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 '';
        }
    },
沈志敏 authored
166
    isEqualOr: function() {
陈峰 authored
167 168 169 170
        let args = Array.prototype.slice.call(arguments);
        let v1 = args[0];
        let opt = args[args.length - 1];
        let isTrue = false;
沈志敏 authored
171
陈峰 authored
172
        for (let i = 1; i < args.length - 1; i++) {
沈志敏 authored
173 174 175 176 177 178 179 180 181
            if (v1 === args[i]) {
                isTrue = true;
                break;
            }
        }

        if (isTrue) {
            return opt.fn(this); // eslint-disable-line
        } else {
lijing authored
182
            return opt.inverse(this); // eslint-disable-line
沈志敏 authored
183 184
        }
    },
郭成尧 authored
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

    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
        }
    },
沈志敏 authored
206
    ifand: function() {
陈峰 authored
207 208 209
        let args = Array.prototype.slice.call(arguments);
        let opt = args[args.length - 1];
        let isTrue = true;
沈志敏 authored
210
陈峰 authored
211
        for (let i = 0; i < args.length - 1; i++) {
沈志敏 authored
212 213 214 215 216 217 218 219 220 221 222 223
            if (!args[i]) {
                isTrue = false;
                break;
            }
        }

        if (isTrue) {
            return opt.fn(this);
        } else {
            return opt.inverse(this);
        }
    },
姜枫 authored
224
    ifor: function() {
陈峰 authored
225 226 227
        let args = Array.prototype.slice.call(arguments);
        let opt = args[args.length - 1];
        let isTrue = false;
姜枫 authored
228
陈峰 authored
229
        for (let i = 0; i < args.length - 1; i++) {
zhangxiaoru authored
230
姜枫 authored
231 232 233 234 235 236 237 238 239 240 241
            if (args[i]) {
                isTrue = true;
                break;
            }
        }

        if (isTrue) {
            return opt.fn(this);
        } else {
            return opt.inverse(this);
        }
徐炜 authored
242
    },
zhangxiaoru authored
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    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);
        }
    },
徐炜 authored
262 263 264 265

    /**
     * 小于某zhi
     *
陈峰 authored
266
     * @param letiable
徐炜 authored
267 268
     * @param number
     */
陈峰 authored
269 270
    within: function(letiable, number, opt) {
        if (letiable < number) {
徐炜 authored
271 272 273 274
            return opt.fn(this);
        } else {
            return opt.inverse(this);
        }
李靖 authored
275
    },
李靖 authored
276 277 278 279

    /**
     * 字段图片包含http
     */
李靖 authored
280 281 282 283 284 285 286 287 288
    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;
王水玲 authored
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    },

    /**
     * 特殊符号转译
     */
    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;
            }
        });
lijing authored
312
    }
lijing authored
313
};