helpers.js 3.75 KB
'use strict';

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

module.exports = {
  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);
        query = query.replace(/{height}/g, params.h);
        query = query.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 + '/');
          }
        }
      } else {
        query = 'imageView2/2/interlace/1/q/' + (params.q || 75);
      }
      return uri + '?' + query;
    } else {
      return '';
    }
  },
  ifor: function() {
    var args = Array.prototype.slice.call(arguments);
    var opt = args[args.length - 1];
    var 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);
    }
  },

  /**
     * 小于某zhi
     *
     * @param variable
     * @param number
     */
  within: function(variable, number, opt) {
    if (variable < number) {
      return opt.fn(this);
    } else {
      return opt.inverse(this);
    }
  },
  eq: function(a, b, options) {
    if (arguments.length === 2) {
      options = b;
      b = options.hash.compare;
    }
    if (a === b) {
      return options.fn(this);
    }
    return options.inverse(this);
  },
  goodsUrl: (product, kind) => {
    let productId, goodsId, cnAlphabet;

    switch (kind) {
      case 'collection':
        productId = product.productId;
        goodsId = product.goodsId;
        cnAlphabet = product.cnAlphabet;
        break;
      default:
        productId = product.productId;
        goodsId = product.goodsList[0].goodsId;
        cnAlphabet = product.cnAlphabet;
    }

    return `/product/pro_${productId}_${goodsId}/${cnAlphabet}.html`;
  },
  toFixed: (value, num) => {
    if (typeof value === 'undefined') {
      return;
    }
    return Number(value).toFixed(num || 2);
  },
  resizeImage: (src, width, height, mode) => {
    return src ? src.replace(/(\{width}|\{height}|\{mode})/g, function($0) {
      const dict = {
        '{width}': width || 300,
        '{height}': height || 300,
        '{mode}': mode || 2
      };

      return dict[$0];
    }).replace(/https?:/, '') + '/interlace/1' : '';
  },
  pxToRem: (px) => {
    const rootValue = 40;

    if (typeof px !== 'number') {
      px = parseInt(`0${px}`);
    }

    if (px > 2) {
      return (px / rootValue).toFixed(2) + 'rem';
    } else {
      return px + 'px';
    }
  }
};