detail.js 17.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
/**
 * 商品详情models
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/5/6
 */

'use strict';

const library = '../../../library';
const API = require(`${library}/api`);
const sign = require(`${library}/sign`);
const _ = require('lodash');
const HELPERS = require(`${library}/helpers`);

var api = new API();
var helpers = new HELPERS();

/**
 * 处理品牌关联店铺信息
 * @param  {array}
 * @return {array}
 */
const getShopsInfo = (data) => {
    var enterStore = {};

    _.forEach(data, function(value, key) {
        enterStore[key] = {};
        enterStore[key].img = helpers.getImageUrl(value.brand_ico, 47, 47);
        enterStore[key].storeName = value.brand_name;

        if (value.shop_id !== null && value.shop_id !== undefined) {
            let params = {};

            params.shop_id = value.shop_id;
            enterStore[key].url =
                helpers.url('/product/index/brand', params);
        } else {
            enterStore[key].url =
                helpers.url('', null, value.brand_domain);
        }
    });
    return enterStore;
};

/**
 * 处理限购商品的有关按钮状态(或取现购买以及底部商品购买按钮)
 * @param  {array} 需要处理的数据
 * @param  {int} 限购商品的关联状态
 * @param  {Boolean} 限购商品是否已开售
 * @return {array}
 */
const procShowStatus = (data, showStatus, isBeginSale) => {
    switch (showStatus) {
        case 1:// 开售前/后,立即分享获得限购码(用户未领取限购码)
            // 显示获取限购码按钮
            data.canGetLimitCode = true;

            if (isBeginSale) { // 开售后
                data.cartInfo.noLimitCode = true;
            } else {
                data.cartInfo.limitNotForSale = true;
            }
            break;
        case 2: // 开售后,限购码已抢光(用户未领取限购码)
            data.codeEmpty = true;
            data.cartInfo.noLimitCode = true;
            break;
        case 3: // 开售后,商品已经售罄
            data.noLimitGoodsBtn = true;
            data.cartInfo.soldOut = true;
            break;
        case 4: // 开售后,立即购买(用户已领取限购码)
            data.gotCode = true;
            data.cartInfo.canBuyLimit = true;
            break;
        case 5: // 开售前,限购码已被抢光(用户未领取限购码)
            data.codeEmpty = true;
            data.cartInfo.limitNotForSale = true;
            break;
        case 6: // 开售前,即将开售(用户已领取限购码)
            data.gotCode = true;
            data.cartInfo.limitNotForSale = true;
            break;
        case 7: // 开售后,用户已经用获得的限购码购买过商品
            data.gotCode = true;
            data.cartInfo.noLimitCode = true;
            break;
        default:
            break;
    }

    return data;
};

/**
 * 根据设备类型获得限购商品跳转app的url 待处理
 * @param  {[string]} productCode 限购商品码
 * @param  {[string]} skn 限购商品skn
 * @return {[string]} 限购商品跳转url
 */
const getLimitCodeUrl = (productCode, skn, ua) => {
    var url = 'yohoapp://yoho.app/openwith?limit_product_code=' + productCode + '&product_skn=' + skn;

    return url;
};

/**
 * 详情页数据格式化
 * @param origin Object 原始数据
 * @return dest Object 格式化数据
 */
const detailDataPkg = (origin, uid, vipLevel, ua) => {
    var dest = {}, // 结果输出
        colorGroup = {},
        thumbImageList = {},
        sizeGroup = {},
        totalStorageNum = 0;

    // 商品名称
    if (origin.productName === null || origin.productName === undefined) {
        return dest;
    }

    dest.goodsName = origin.productName;

    // 用户未登录时 待处理
    if (uid === null || uid === undefined) {
        let params = {};

        params.refer = helpers.url('/product/show_' + origin.erpProductId + '.html');
        dest.loginUrl = helpers.url('/signin.html', params);
    }

    // 商品促销短语
    if (origin.salesPhrase !== null && origin.salesPhrase !== undefined) {
        dest.goodsSubtitle = origin.salesPhrase;
    }

    // 商品标签
    if (origin.productTagBoList !== null && origin.productTagBoList !== undefined) {
        _.forEach(origin.productTagBoList, function(value) {
            switch (value.tagLabel) {
                case 'is_soon_sold_out':
                    dest.tags.is_soon_sold_out = true;
                    break;
                case 'is_new':
                    dest.tags.is_new = true;
                    break;
                case 'is_discount':
                    dest.tags.is_discount = true;
                    break;
                case 'is_limited':
                    dest.tags.is_limited = true;
                    break;
                case 'is_yohood':
                    dest.tags.is_yohood = true;
                    break;
                case 'is_advance':
                    dest.tags.is_advance = true;
                    break;
                default:
                    break;
            }
        });
    }

    // 商品价格
    dest.goodsPrice = {};

    if (origin.productPriceBo !== null && origin.productPriceBo !== undefined) {
        dest.goodsPrice.currentPrice = origin.productPriceBo.formatSalesPrice;
        if (origin.productPriceBo.formatMarketPrice !== origin.productPriceBo.formatSalesPrice) {
            dest.goodsPrice.previousPrice = origin.productPriceBo.formatMarketPrice;
        }

        // 商品返回 YOHO 币
        if (origin.productPriceBo.yohoCoinNum !== null && origin.productPriceBo.yohoCoinNum !== 0) {
            dest.commodityReturn = dest.productPriceBo.yohoCoinNum;
        }
    }

    // VIP 商品价格
    dest.vipLevel = {};
    dest.vipLevel.list = {};

    if (origin.productPriceBo.vipPrices !== null && origin.productPriceBo.vipPrices !== undefined) {
        _.forEach(origin.productPriceBo.vipPrices, function(value, key) {
            dest.vipLevel.list[key] = {};
            dest.vipLevel.list[key].level = value.vipLevel;
            dest.vipLevel.list[key].text = value.vipPrice;
            dest.vipLevel.list[key].currentLevel = (value.vipLevel === vipLevel) ? true : false;
        });
    }

    // 上市期
    if (origin.expectArrivalTime !== null && origin.expectArrivalTime !== undefined) {
        dest.periodOfMarket = origin.expectArrivalTime + '月';
    }

    // 促销信息
    dest.goodsDiscount = {};
    dest.goodsDiscount.list = {};

    if (origin.promotionBoList !== null && origin.promotionBoList !== undefined) {
        _.forEach(origin.promotionBoList, function(value, key) {
            dest.goodsDiscount.list[key] = {};
            dest.goodsDiscount.list[key].text = '【' + value.promotionType + '】' +
                value.promotionTitle;
        });
    }

    // 商品咨询
    dest.feedbacks = {};
    dest.feedbacks.consults = {};
    dest.feedbacks.consultsNum = 0;

    if (origin.consultBoWrapper !== null && origin.consultBoWrapper !== undefined) {
        dest.feedbacks.consultsNum = origin.consultBoWrapper.consultTotal;
        _.forEach(origin.consultBoWrapper.consultBoList, function(value, key) {
            dest.feedbacks.consults[key] = {};
            dest.feedbacks.consults[key].question = value.ask;
            dest.feedbacks.consults[key].time = value.askTime;
            dest.feedbacks.consults[key].answer = value.answer;
        });

        let params = {};

        params.product_id = origin.id;
        params.total = dest.feedbacks.consultsNum;

        dest.feedbacks.consultsUrl = helpers.url('/product/detail/consults', params);

    } else { // 暂无咨询

        let params = {};

        params.product_id = origin.id;

        dest.feedbacks.consultsUrl = helpers.url('/product/detail/consultform', params);
    }

    // 商品评价
    dest.feedbacks.commentsNum = 0;
    if (origin.commentBoWrapper !== null && origin.commentBoWrapper !== undefined) {
        dest.feedbacks.commentsNum = origin.commentBoWrapper.commentTotal;
        dest.feedbacks.comments = {};

        _.forEach(origin.commentBoWrapper.commentBoList, function(value, key) {
            dest.feedbacks.comments[key] = {};
            dest.feedbacks.comments[key].userName = value.nickName;
            dest.feedbacks.comments[key].desc = value.colorName +
                '/' + value.sizeName;
            dest.feedbacks.comments[key].content = (value.content !== null &&
                value.content !== undefined) ? value.content : '';
            dest.feedbacks.comments[key].time = value.createTime;
        });

        let params = {};

        params.product_id = origin.id;
        params.total = dest.feedbacks.commentsNum;

        dest.feedbacks.commentsUrl = helpers.url('/product/detail/comments', params);
    }

    // 品牌信息
    if (origin.brand !== null && origin.brand !== undefined) {

        // dest.enterStore[0] = {};
        // dest.enterStore[0].img =
        //     helpers.getImageUrl(origin.brand.brandIco, 47, 47);
        // dest.enterStore[0].storeName = origin.brand.brandName;
        // dest.enterStore[0].url = helpers.url('', null, origin.brand.brandDomain);

        // 为你优选的链接
        let params = {};

        params.productSkn = origin.erpProductId;
        params.brandId = origin.brand.id;
        dest.preferenceUrl = helpers.url('/product/detail/preference', params, '');
    }

    dest.productSkn = origin.erpProductId;

    // 商品信息
    if (origin.goodsList !== null && origin.goodsList !== undefined) {
        let goodsGroup = {},
            sizeName = '',
            colorList = {},
            sizeList = {},
            allSizeList = {},
            colorStorageGroup = {},
            colorStorageNum = 0;

        _.forEach(origin.goodsList, function(value, key) {
            if (value.status === 0) {
                return true;
            }

            colorStorageNum = 0;

            // 商品分组
            if (value.goodsImagesList !== null && value.goodsImagesList !== undefined) {
                _.forEach(value.goodsImagesList, function(good, keyForGood) {
                    goodsGroup[keyForGood] = {};
                    goodsGroup[keyForGood].goodsId = good.goodsId;
                    goodsGroup[keyForGood].img = good.imageUrl;
                });
            }

            // 商品的尺码列表
            colorStorageGroup[value.productSkc] = {};
            if (value.goodsSizeBoList !== null && value.goodsSizeBoList !== undefined) {

                _.forEach(value.goodsSizeBoList, function(size, keyForSize) {
                    sizeList[value.productSkc] = {};
                    sizeList[value.productSkc][keyForSize] = {};
                    sizeList[value.productSkc][keyForSize].id = size.id;
                    sizeList[value.productSkc][keyForSize].skuId = size.goodsSizeSkuId;
                    sizeList[value.productSkc][keyForSize].goodsId = size.goodsId;
                    sizeList[value.productSkc][keyForSize].colorId = size.colorId;
                    sizeList[value.productSkc][keyForSize].name = size.sizeName;
                    sizeList[value.productSkc][keyForSize].sizeNum = size.goodsSizeStorageNum;

                    sizeName = size.sizeName;

                    // 所有尺码列表,赋值用于前端展示默认尺码的时候
                    // 判断出没有库存则显示灰色
                    let build = {};

                    build.storage = size.goodsSizeStorageNum;
                    build.id = size.id;
                    allSizeList[sizeName] = (allSizeList[sizeName] === null ||
                        allSizeList[sizeName] === undefined) ? build :
                        allSizeList[sizeName];

                    colorStorageNum += parseInt(size.goodsSizeStorageNum);
                    colorStorageGroup[value.productSkc][sizeName] = parseInt(size.goodsSizeStorageNum);
                });

                // 颜色分组
                colorList[key] = {};
                colorList[key].id = value.colorId;
                colorList[key].skcId = value.productSkc;
                colorList[key].name = value.colorName;
                colorList[key].goodsName = value.goodsName;
                colorList[key].colorNum = colorStorageNum;
            }

            // 缩略图
            let params = {};

            params.img = helpers.getImageUrl(value.colorImage, 60, 60);
            thumbImageList[key] = params;

            // 商品库存总数
            totalStorageNum += colorStorageNum;
        });

        // 遍历所有尺码,构建颜色显示数据
        let i = 1;

        _.forEach(allSizeList, function(value, key) {

            // 默认尺码
            sizeGroup[0] = {};

            sizeGroup[0].size = {};
            sizeGroup[0].size[key] = {};
            sizeGroup[0].size[key].name = sizeName;
            sizeGroup[0].size[key].sizeNum = (value.storage === undefined || value.storage === null) ? false : true;
            sizeGroup[0].size[key].id = value.id;

            // 各个颜色的尺码, 每行显示一个尺码对应的颜色
            _.forEach(colorList, function(colorArr, keyForColorArr) {
                colorGroup[i] = {};
                colorGroup[i].color = {};
                colorGroup[i].color[keyForColorArr] = {};
                colorGroup[i].color[keyForColorArr].colorNum =
                (colorStorageGroup[colorArr.skcId][sizeName] !== null &&
                colorStorageGroup[colorArr.skcId][sizeName] !== undefined) ?
                colorStorageGroup[colorArr.skcId][sizeName] : 0;
            });
            colorGroup[i].id = value.id;

            ++i;
        });

        // 遍历所有颜色, 构建尺码显示数据

        i = 1;
        _.forEach(colorList, function(value, key) {
            // 各个尺码的颜色, 每行显示一个颜色的对应尺码
            sizeGroup[i] = {};
            sizeGroup[i].size = sizeList[value.skcId];
            sizeGroup[i].colorId = value.skcId;

            // 默认颜色
            colorGroup[0] = {};
            colorGroup[0].color = {};
            colorGroup[0].color[key] = value;

            ++i;
        });

        // 商品图:多个
        if (goodsGroup[1] !== null && goodsGroup[1] !== undefined) {
            _.forEach(goodsGroup, function(value, key) {
                dest.bannerTop = {};

                dest.bannerTop.list = {};
                let params = {};

                params.img = helpers.getImageUrl(value.img, 450, 600);
                dest.bannerTop.list[key] = params;
            });
        } else if (goodsGroup[0] !== null && goodsGroup[0] !== undefined) {
            dest.bannerTop = {};
            dest.bannerTop.img = helpers.getImageUrl(goodsGroup[0].img, 450, 600);
        }
    }

    // 悬浮的购物车信息
    dest.cartInfo = {};
    dest.cartInfo.cartUrl = helpers.url('/cart/index/index', null);
    dest.cartInfo.numInCart = 0;
    dest.cartInfo.goodsInstore = origin.storage;

    let soldOut = (origin.storage === 0) || (origin.status === 0 || totalStorageNum === 0);
    let notForSale = origin.attribute === 2;

    // 显示加入购物车链接
    if (!soldOut && !notForSale) {
        _.orderBy(colorGroup);

        dest.cartInfo = {};
        dest.cartInfo.productId = origin.id;
        dest.cartInfo.thumbs = thumbImageList;
        dest.cartInfo.name = (dest.goodsName !== null && dest.goodsName !== undefined) ? dest.goodsName : '';
        dest.cartInfo.price = (dest.goodsPrice.previousPrice !== null &&
            dest.goodsPrice.previousPrice !== undefined) ? dest.goodsPrice.previousPrice : '';
        dest.cartInfo.salePrice = (dest.goodsPrice.currentPrice !== null &&
            dest.goodsPrice.currentPrice !== undefined) ? dest.goodsPrice.currentPrice : '';
        dest.cartInfo.totalNum = totalStorageNum;
        dest.cartInfo.colors = colorGroup;
        dest.cartInfo.sizes = sizeGroup;

       // 限购商品
        if (origin.isLimitBuy === 'Y') {
            // 是否开售
            let isBeginSale = (origin.saleStatus !== null && origin.saleStatus !== undefined &&
                origin.saleStatus === 1);

            // 限购商品有关的展示状态
            let showStatus = 1;

            if (origin.showStatus !== null && origin.showStatus !== undefined) {
                showStatus = parseInt(origin.showStatus);
            }

            // 处理限购商品有关的按钮状态
            dest = procShowStatus(dest, showStatus, isBeginSale);

            dest.cartInfo.limitProductCode = origin.limitProductCode;
            dest.cartInfo.limitCodeUrl = getLimitCodeUrl(origin.limitProductCode, origin.erpProductId, ua);
            dest.cartInfo.limitProductPay = helpers.url('/cart/index/orderEnsure');
        } else {
            dest.cartInfo.addToCartUrl = helpers.url('/product/buy_' + origin.id + '_' +
                origin.goodsList.id + '.html'); // 待处理 确认 goodsId 的含义
        }
    } else if (notForSale) {
        dest.cartInfo.notForSale = true;
    } else if (soldOut) {
        dest.cartInfo.soldOut = true;
    }

    // 是否收藏

    dest.isCollect = false;
    if (origin.isCollect !== null && origin.isCollect !== undefined && origin.isCollect === 'Y') {
        dest.isCollect = true;
    }

    // 底部简介的URL链接
    dest.introUrl = helpers.url('/product/intro_' + origin.erpProductId + '/' + origin.cnAlphabet + '.html', null, '');
    dest.id = origin.id;

    return dest;
};

module.exports = (data) => {
    var finalResult;

    return api.get('', sign.apiSign({
        productId: data.id,
        method: 'h5.product.data'
    })).then(result => {
        finalResult = detailDataPkg(result, null, 0, data.ua);

        return api.get('', sign.apiSign({
            method: 'app.shop.queryShopsByBrandId',
            brand_id: result.brand.id
        })).then(shops => {
            if (shops.code === 200) {
                finalResult.enterStore = getShopsInfo(shops.data);
            }
            return finalResult;
        });
    });
};