vue-filter.js 5.34 KB
const util = require('common/util');

module.exports = (Vue) => {
    /**
     * 替换参数
     *
     * @example
     *   value = /{width}/{height}/{mode}
     *
     *   {value | resize 100 200 2}  ==> /100/200/2
     */
    Vue.filter('resize', (value, width, height, mode) => util.getImgUrl(value, width, height, mode));

    /**
     * 性别款式
     *
     * @example
     *
     *  {value | gender}
     */
    Vue.filter('clothingGenderIdentity', (value) => {
        let ret = null;

        switch (value) {
            case 1:
                ret = '男款';
                break;
            case 2:
                ret = '女款';
                break;
            default:
                ret = '通用';
        }

        return ret;
    });

    /**
     * 品牌URL
     *
     * @param value brand domain
     */
    Vue.filter('brandUrl', (value, kind) => {
        let url = `/product/shop/${value}`;

        if (kind === 'shop') {
            url = `/product/shop/favorite?id=${value || ''}`;
        }
        return url;
    });

    /**
     * 产品 URL
     */
    Vue.filter('goodsUrl', (product, kind) => {
        let productId, goodsId, cnAlphabet;

        switch (kind) {
            case 'collection':
                productId = product.product_id;
                goodsId = product.goods_id;
                cnAlphabet = product.cn_alphabet;
                break;
            default:
                productId = product.product_id;
                goodsId = product.goods_list.length ? product.goods_list[0].goods_id : '';
                cnAlphabet = product.cn_alphabet;
        }

        return `/product/pro_${productId}_${goodsId}/${cnAlphabet}.html`;
    });

    /**
     * 订单状态
     * @example
     *  {value | order}
     *  状态 0:待付款,1-3:待发货,4-5:待收货(0:未付款,1:已付款,2:备货中,3:配货中,4:已发货,5:运输中,6:已完成)
     */
    Vue.filter('convertOrderState', (value) => {
        let stateTxt = '';

        if (typeof value !== 'undefined') {
            value = parseInt(value, 10);
        }
        switch (value) {
            case 0:
                stateTxt = '待付款';
                break;
            case 1:
                stateTxt = '待发货';
                break;
            case 2:
                stateTxt = '待发货';
                break;
            case 3:
                stateTxt = '待发货';
                break;
            case 4:
                stateTxt = '待收货';
                break;
            case 5:
                stateTxt = '待收货';
                break;
            case 6:
                stateTxt = '交易成功';
                break;
            default:
                stateTxt = '';
                break;
        }
        return stateTxt;
    });

    Vue.filter('toFixed', (value, num) => {
        if (typeof value === 'undefined') {
            return;
        }
        return Number(value).toFixed(num || 2);
    });

    /**
     * 转换时间
     *      yyyy-MM-dd hh:mm:ss
     */
    Vue.filter('convertTime', (value) => {
        if (typeof value === 'undefined') {
            return;
        }
        let date = new Date(parseFloat(value) * 1000);
        let Y = date.getFullYear() + '-';
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
        let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();

        return Y + M + D + h + m + s;
    });


    /**
     * 格式化时间
     */
    Vue.filter('formatUnixTime', (value) => {
        // return moment.unix(value).format(format || 'YYYY-MM-DD HH:mm:ss');

        if (typeof value === 'undefined') {
            return;
        }

        let date = new Date(parseFloat(value) * 1000);

        // let Y = date.getFullYear() + '-';
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
        let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
        let h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
        let m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();

        // let s = date.getSeconds();

        // return M + '.' + D + ' ' + h + ' ' + ' ' + m + ' ' + s;
        return `${M}.${D} ${h}:${m}`;
    });

    const zhDict = {
        color: '颜色',
        gender: '性别',
        size: '尺寸',
        brand: '品牌',
        priceRange: '价格',
        group_sort: '品类',
        discount: '折扣',
        style: '风格',
        ageLevel: '年龄'
    };

    const enDict = {
        color: 'Color',
        gender: 'Gender',
        size: 'Size',
        brand: 'Brand',
        priceRange: 'Price',
        group_sort: 'Category',
        discount: 'Sale',
        style: 'Style',
        ageLevel: 'Age'
    };

    Vue.filter('filter-cn', (value, prefix) => {
        prefix = prefix || '';
        value = zhDict[value] || '';
        return prefix + value || '';
    });

    Vue.filter('filter-en-cn', value => {
        const cn = zhDict[value] || '';
        const en = enDict[value] || '';

        return en + cn;
    });
};