prod-image.js 1.25 KB
/**
 * Created by TaoHuang on 2017/5/12.
 */
import _ from 'lodash';

function sknImage({skn, sku, size}) {
    let baseUrl = '/Api/platform/getRemoteImageUrlBySku';
    let params = [];

    if (skn) {
        params.push(`skn_id=${skn}`);
    }

    if (sku) {
        params.push(`sku_id=${sku}`);
    }

    params.push(size ? `size=${size}` : 'size=80x80');

    params.push(`t=${new Date().getTime()}`);

    return `${baseUrl}?${params.join('&')}`;
}

export default {
    bind(el, binding) {
        // v-prod-img.skn="{ val: 'fdsa', size: '121X123'}"  skn
        // v-prod-img.sku="{ val: 'fdsa', size: '121X123'}"  sku
        // v-prod-img.sku="row.productSkn"  sku
        // v-prod-img.skn="row.productSkn"  skn
        // v-prod-img="row.productSkn"  skn

        let data = null;
        let key = null;

        if (!_.isEmpty(binding.modifiers)) {
            key = _.first(_.keys(binding.modifiers));
        } else {
            key = 'skn';
        }

        if (_.isPlainObject(binding.value)) {
            data = {
                [key]: binding.value.val,
                size: binding.value.size
            };
        } else {
            data = {
                [key]: binding.value
            };
        }

        el.src = sknImage(data);
    }
};