Blame view

apps/product/models/shop-handler.js 10.1 KB
周少峰 authored
1 2 3 4 5 6 7
/*
 * @Author: sefon
 * @Date: 2016-07-31 22:13:24
 */

'use strict';
const _ = require('lodash');
htoooth authored
8
const Fn = require('lodash/fp');
htoooth authored
9
const qs = require('querystring');
周少峰 authored
10
周少峰 authored
11 12
const helpers = global.yoho.helpers;
周少峰 authored
13 14 15 16
// const queryString = require('querystring');

const newProductsName = '新品上架 NEW';
const hotProductsName = '人气单品 HOT';
yyq authored
17
const shopListUrl = '/product/shoplist';
周少峰 authored
18 19 20 21 22

/**
 * 新品上架
 */
const newProducts = (data) => {
yyq authored
23
    let dest = {};
周少峰 authored
24
yyq authored
25 26 27 28 29 30 31 32 33 34
    if (!_.isEmpty(data)) {
        Object.assign(dest, {
            name: newProductsName,
            list: []
        });
        _.forEach(data, (value) => {
            dest.list.push({
                productId: value.productId,
                title: value.productName,
                productSkn: value.productSkn,
yyq authored
35 36
                price: ${value.salesPrice}`,
                img: value.src,
m  
OF1706 authored
37
                url: helpers.getUrlBySkc(value.productSkn)
yyq authored
38
            });
周少峰 authored
39
        });
yyq authored
40 41
    }
周少峰 authored
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    return dest;
};

/**
 * 人气单品
 */
const hotProducts = (data) => {
    let dest = {
        name: hotProductsName,
        list: []
    };

    _.forEach(data, (value, key) => {
        dest.list.push({
            productId: value.productId,
            title: value.productName,
            productSkn: value.productSkn,
yyq authored
59
            price: ${value.salesPrice}`,
周少峰 authored
60
            img: value.src,
m  
OF1706 authored
61
            url: helpers.getUrlBySkc(value.productSkn),
周少峰 authored
62 63 64 65 66 67 68 69 70 71 72
            index: key + 1
        });
    });
    return dest;
};

/**
 * tabBar
 */
const goodsTabBar = (data, shopId) => {
    let dest = {
yyq authored
73 74 75
        hot: [],
        new: []
    };
周少峰 authored
76
yyq authored
77 78 79 80
    _.forEach(_.sortBy(data.hot, o => {
        return -o.position;
    }), (value) => {
        if (value.url && value.position) {
周少峰 authored
81 82
            dest.hot.push({
                name: value.name,
yyq authored
83
                url: value.url
周少峰 authored
84 85 86 87 88
            });
        }

    });
yyq authored
89 90 91 92
    _.forEach(_.sortBy(data.new, o => {
        return -o.position;
    }), (value) => {
        if (value.url && value.position) {
周少峰 authored
93 94
            dest.new.push({
                name: value.name,
yyq authored
95
                url: value.url
周少峰 authored
96 97 98
            });
        }
    });
yyq authored
99 100
    dest.hot.push({name: 'MORE', url: `${shopListUrl}?navBar=2&order=s_n_desc&shopId=${shopId}`});
    dest.new.push({name: 'MORE', url: `${shopListUrl}?navBar=3&order=s_t_desc&shopId=${shopId}`});
周少峰 authored
101 102 103 104 105 106 107 108 109
    return dest;
};

/**
 * 店铺经典模板banner
 * @param data 装修数据
 * @returns {{}}
 */
const shopTopBanner = (data) => {
yyq authored
110 111 112
    let src = data[0].shopSrc;
    let str = _.split(src, '?').length ? '?' : '&';
周少峰 authored
113 114
    return {
        shopTopBanner: {
yyq authored
115
            bannerHeight: 200,
yyq authored
116
            banner: `${src}${str}imageView2/1/w/{width}/h/{height}`,
周少峰 authored
117 118
            detailSrc: data[0].detailSrc || '',
            isShowShopName: data[0].isShowShopName === 'Y'
周少峰 authored
119 120 121 122 123
        }
    };

};
yyq authored
124 125 126 127 128 129 130 131 132 133
const shopTopBannerBase = (data) => {
    return {
        shopTopBannerBase: {
            bannerHeight: 200,
            banner: helpers.getForceSourceUrl(data[0].shopSrc),
            showShopName: data[0].isShowShopName === 'Y'
        }
    };
};
周少峰 authored
134 135 136 137 138
/**
 * 导航
 * @param data 装修数据
 * @returns {{}}
 */
yyq authored
139 140 141 142
const navigationBar = (data, shopId, params) => {
    params = params || {};

    const gender = params.gender ? `&gender=${params.gender}` : '';
周少峰 authored
143 144 145
    let shopNav = [
        {
            name: '店铺首页',
yyq authored
146
            url: `/?navBar=0&shopId=${shopId}${gender}`
周少峰 authored
147 148 149
        },
        {
            name: '全部商品',
yyq authored
150
            url: `${shopListUrl}/?navBar=1&shopId=${shopId}${gender}`
周少峰 authored
151 152 153
        },
        {
            name: '人气单品',
yyq authored
154
            url: `${shopListUrl}/?navBar=2&order=s_n_desc&shopId=${shopId}${gender}`
周少峰 authored
155 156 157
        },
        {
            name: '新品上架',
yyq authored
158
            url: `${shopListUrl}/?navBar=3&order=s_t_desc&shopId=${shopId}${gender}`
周少峰 authored
159 160 161
        }
    ];
yyq authored
162 163 164 165 166 167 168 169 170 171
    _.forEach(data, (value) => {
        if (value.url) {
            shopNav.push({
                name: value.name,
                url: `${value.url}&navBar=${shopNav.length}`
            });
        }
    });

    return {navigationBar: shopNav};
周少峰 authored
172 173 174
};

/**
周少峰 authored
175 176 177 178
 * 资源位大图
 * @param type $data
 * @return type []
 */
htoooth authored
179
const largeSlideImg = (data) => {
周少峰 authored
180 181 182
    let dest = [];

    _.forEach(data, (value) => {
yyq authored
183
        value = _.get(value, 'data[0]', {});
周少峰 authored
184
        dest.push({
yyq authored
185
            img: value.src,
htoooth authored
186
            url: value.url
周少峰 authored
187 188 189 190 191 192 193
        });
    });

    return {largeSlideImg: dest};
};

/**
周少峰 authored
194 195 196 197
 * 资源位小图
 * @param data 装修数据
 * @returns {{}}
 */
htoooth authored
198
const oneRowTwoColImages = (data) => {
周少峰 authored
199 200 201
    let dest = [];

    _.forEach(data, (value) => {
yyq authored
202
        value = _.get(value, 'data[0]', {});
周少峰 authored
203
        dest.push({
yyq authored
204
            img: value.src,
htoooth authored
205
            url: value.url
周少峰 authored
206 207
        });
    });
周少峰 authored
208 209
    return {oneRowTwoColImages: dest};
};
周少峰 authored
210
周少峰 authored
211 212 213 214 215
/**
 * 经典推荐
 * @param type $data
 * @return type []
 */
htoooth authored
216
const recommend = (data) => {
周少峰 authored
217 218 219 220 221 222 223 224
    let dest = [];

    _.forEach(data, (value) => {
        dest.push({
            enName: value.enName,
            name: value.name,
            img: value.src,
            title: value.title,
htoooth authored
225
            url: value.url
周少峰 authored
226 227 228 229 230
        });
    });

    return {
        recommend: {
yyq authored
231
            title: dest[0].title,
周少峰 authored
232
            list: dest
周少峰 authored
233
        }
周少峰 authored
234
    };
周少峰 authored
235 236 237
};

/**
yyq authored
238
 * 品牌集合
周少峰 authored
239 240 241
 * @param type $data
 * @return type []
 */
yyq authored
242 243 244 245
const brandBrowse = (data, params) => {
    let brand = _.get(params, 'brand', '');
    let resData = {};
yyq authored
246
    if (!_.isEmpty(data) && data.length > 1) {
yyq authored
247 248 249 250
        let list = [];

        _.forEach(data, value => {
            list.push({
yyq authored
251
                url: `${shopListUrl}?shopId=${params.shopId || ''}&brand=${value.id}&title=${value.title}$navBar=1`,
yyq authored
252
                brandName: value.brandName,
yyq authored
253
                title: value.title,
yyq authored
254 255 256 257 258
                cur: (brand === +value.id)
            });
        });

        resData.brandBrowse = {
yyq authored
259
            title: list[0].title,
yyq authored
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
            list: list
        };
    }

    return resData;
};

/**
 * 热门推荐
 * @param type $data
 * @return type []
 */
const hotRecommend = (data) => {
    let resData = {};

    if (!_.isEmpty(data)) {
        let list = [];

        _.forEach(data, value => {
            list.push({
yyq authored
280
                title: value.title,
yyq authored
281 282 283 284
                img: value.src,
                url: value.url
            });
        });
周少峰 authored
285
yyq authored
286
        resData.hotRecommend = {
yyq authored
287
            title: list[0].title,
yyq authored
288 289 290 291 292 293
            list: list
        };
    }

    return resData;
};
周少峰 authored
294 295

/**
yyq authored
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
 * 水牌
 */
const signboard = (data) => {
    let list = [];

    _.forEach(data, value => {
        if (value.data) {
            _.forEach(value.data, val => {
                list.push({
                    img: helpers.image(val.src, 160, 240),
                    url: val.url
                });
            });
        }
    });
    return {
        title: _.get(list, '[0].title', ''),
        list: list
    };
};
htoooth authored
317 318

// 销售类目
yyq authored
319 320 321 322
const _handleSaleCategory = (shopId, baseUrl, resourceObj) => {
    baseUrl = baseUrl || '';

    const thisShop = (categoryId) => baseUrl + '?' + qs.stringify({
htoooth authored
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
        productPool: categoryId,
        shopId: shopId,
        navBar: -1
    });

    let hasSaleCategory = Fn.pipe(Fn.prop('linkType'), Fn.eq('1'));

    if (hasSaleCategory(resourceObj)) {
        return Object.assign(resourceObj, {url: thisShop(resourceObj.categoryId)});
    }

    _(resourceObj).forEach((value) => {
        if (_.has(value, 'data')) {
            _.forEach(value.data, (it) => {
                if (hasSaleCategory(it)) {
                    Object.assign(it, {url: thisShop(it.categoryId)});
                }
            });
        }

        if (hasSaleCategory(value)) {
            Object.assign(value, {url: thisShop(value.categoryId)});
        }
    });

    return resourceObj;
};
yyq authored
351
/**
周少峰 authored
352 353 354 355
 * 店铺装修楼层数据
 * @param data 装修数据
 * @returns {{}}
 */
yyq authored
356 357
exports.getShopDecorator = (data, params, shopId, base) => {
    let dest = {};
周少峰 authored
358
yyq authored
359 360
    if (base) {
        _.forEach(data.list, (value) => {
361
            let info = Fn.pipe(JSON.parse, _.partial(_handleSaleCategory, shopId, ''))(value.resource_data || '[]');
yyq authored
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378

            switch (value.resource_name) {
                case 'signboard':
                    dest.signboard = signboard(info);
                    break;
                case 'shopTopBanner_base':
                    Object.assign(dest, shopTopBannerBase(info));
                    break;
                default:
                    break;
            }
        });
    } else {
        Object.assign(dest, {
            newArrivel: {},
            hotSingle: {}
        });
htoooth authored
379
yyq authored
380
        _.forEach(data.list, (value) => {
381
            let info = Fn.pipe(JSON.parse, _.partial(_handleSaleCategory, shopId, shopListUrl))(value.resource_data || '[]'); // eslint-disable-line
yyq authored
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
            let tabBar;

            switch (value.resource_name) {
                case 'newProducts':
                    Object.assign(dest.newArrivel, newProducts(info));
                    break;
                case 'hotProducts':
                    Object.assign(dest.hotSingle, hotProducts(info));
                    break;
                case 'goodsTabBar':
                    tabBar = goodsTabBar(info, shopId);
                    Object.assign(dest.newArrivel, {navs: tabBar.new});
                    Object.assign(dest.hotSingle, {navs: tabBar.hot});
                    break;
                case 'shopTopBanner':
                    Object.assign(dest, shopTopBanner(info));
                    break;
                case 'navigationBar':
yyq authored
400
                    Object.assign(dest, navigationBar(info, shopId, params));
yyq authored
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
                    break;
                case 'largeSlideImg':
                    Object.assign(dest, largeSlideImg(info, shopId));
                    break;
                case 'oneRowTwoColImages':
                    Object.assign(dest, oneRowTwoColImages(info, shopId));
                    break;
                case 'recommend':
                    Object.assign(dest, recommend(info, shopId));
                    break;
                case 'brandBrowse':
                    Object.assign(dest, brandBrowse(info, params));
                    break;
                case 'hotRecommend':
                    Object.assign(dest, hotRecommend(info));
                    break;
                default:
                    break;
            }
        });
    }
周少峰 authored
422 423 424

    return dest;
};