seckill-detail.js 12.4 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
/**
 * [商品获取数据]
 */
'use strict';
const _ = require('lodash');
const commentModel = require('./consult-comment');
const bundle = require('./bundle');
const productProcess = require(`${utils}/product-process`);


const api = global.yoho.API;
const helpers = global.yoho.helpers;

const tool = {
    prodessDetailData(origin) {
        let dest = {}; // 结果输出

        // 商品名称
        if (!origin.product_name) {
            return dest;
        }

        dest.goodsName = origin.product_name;

        // 是否是虚拟商品
        // dest.virtualGoods = (origin.attribute * 1 === 3);

        // 活动促销短语
        origin.market_phrase && origin.market_phrase !== ' ' && (dest.marketPhrase = origin.market_phrase);

        // 商品促销短语
        origin.sales_phrase && origin.sales_phrase !== ' ' && (dest.goodsSubtitle = origin.sales_phrase);

        // 商品标签
        if (origin.tags) {
            let productTags = {};

            _.forEach(origin.tags, function(value) {
                productTags[value] = true;
            });

            dest.tags = productTags;
        }

        // 商品价格
        let goodsPrice = {
            currentPrice: origin.format_sales_price === '0' ? origin.format_market_price : origin.format_sales_price
        };

        if (origin.format_sales_price !== '0' && origin.format_market_price !== origin.format_sales_price) {
            goodsPrice.previousPrice = origin.format_market_price;
        }

        dest.goodsPrice = goodsPrice;

        // 商品返回 YOHO 币
        // origin.yohoCoinNum && (dest.commodityReturn = origin.yohoCoinNum);

        // 上市期
        origin.expect_arrival_time && (dest.periodOfMarket = origin.expect_arrival_time);


        // 品牌信息
        if (origin.shop_id) {
            let extra = `?productSkn=${origin.product_skn}&shopId=${origin.shop_id}`;

            dest.preferenceUrl = `/product/detail/preference${extra}`;
        }

        // dest.brandId = origin.brand_info && origin.brand_info.brand_id || 0;
        dest.brandId = _.get(origin, 'brand_info.brand_id', 0);
        dest.productSkn = origin.product_skn;
        dest.id = origin.product_id;

        dest.goodsId = origin.goods_id;
        dest.isDepositAdvance = origin.is_deposit_advance === 'Y'; // 是否定金预售
        dest.isSeckill = origin.is_secKill === 'Y'; // 是否秒杀
        dest.isLimitBuy = origin.isLimitBuy; // 是否 限购
        dest.isPresale = Boolean(origin.expect_arrival_time); // 是否普通预售
        dest.bundleType = origin.bundle_type; // 商品活动标记

        // 自定义 属性
        dest.showCoupon = !(
            dest.isDepositAdvance || dest.isSeckill || dest.isLimitBuy || dest.isPresale
        ); // 商品有限购、秒杀、定金预售、普通预售 不显示领

        // 20170113 要求关闭商品详情页面领券功能
        // dest.showCoupon = false;

        // 商品信息
        if (origin.goods_list.length) {
            let goodsGroup = [];

            // pagecache重构
            _.forEach(origin.goods_list, function(value) {

                // 商品分组
                if (value.images_list) {
                    _.forEach(value.images_list, function(good) {
                        goodsGroup.push({
                            goodsId: value.goods_id,
                            img: good.image_url
                        });
                    });
                }
            });


            // 商品图:多个
            if (goodsGroup.length > 1) {
                let bannerList = [];

                _.forEach(goodsGroup, function(value) {
                    value.img = _.replace(value.img, '/quality/80', '/quality/70');

                    bannerList.push({
                        img: value.img
                    });
                });

                dest.bannerTop = {
                    list: bannerList
                };
            } else if (goodsGroup[0]) {
                dest.bannerTop = {
                    img: goodsGroup[0].img
                };
            }
        }

        // 底部简介URL链接
        dest.introUrl = '/product/detail/intro/' + origin.product_skn;

        return dest;
    },

    /**
     * 处理品牌关联店铺信息
     * @param  {array}
     * @return {array}
     */
    processShopsInfo(data) {
        let enterStore = [];

        _.forEach(data, function(value) {
            let shopInfo = {
                img: value.brand_ico,
                storeName: value.brand_name
            };

            if (value.shop_id) {
                shopInfo.url = helpers.urlFormat('/product/index/brand', {
                    shop_id: value.shop_id
                });
            } else {
                shopInfo.url = helpers.urlFormat('', null, value.brand_domain);
            }

            enterStore.push(shopInfo);
        });
        return enterStore;
    },

    /**
     * 处理商品 feedback
     * @param data feedback要处理数据
     * {
     *  comment, defaultConsult, userConsult
     * }
     * @param productId 商品id
     * @return feedback
     */
    processFeedback(data, productId) {
        // let {comment, defaultConsult, userConsult} = data;
        let comment = data.comment;
        let defaultConsult = data.defaultConsult;
        let userConsult = data.userConsult;

        let feedbacks = {consults: [], consultsNum: 0};

        Object.assign(feedbacks, comment);

        // 商品评价
        feedbacks.commentsUrl = helpers.urlFormat('/product/detail/comments', {
            product_id: productId
        });

        /* 如果有用户咨询,显示用户咨询,否则显示常见问题 */
        let obj = {};

        if (userConsult.total) {
            obj = {
                commonConsults: false,
                consultsNum: parseInt(userConsult.total, 10),
                consults: _.take(userConsult.list, 2)
            };
        } else if (!_.isEmpty(defaultConsult) && !_.get(comment, 'consultsNum', 0)) {
            obj = {
                commonConsults: true,
                consultsNum: true,
                consults: _.take(defaultConsult.faq, 2)
            };
        }

        Object.assign(feedbacks, obj);

        if (_.get(feedbacks, 'consultsNum')) {
            feedbacks.consultsUrl = helpers.urlFormat('/product/detail/consults', {
                product_id: productId,
                total: feedbacks.consultsNum
            });
        } else {
            feedbacks.consultsUrl = helpers.urlFormat('/product/detail/consultform', {
                product_id: productId
            });
        }
        return feedbacks;
    },

    /**
     * 套餐数据处理
     * @param bundleData
     * @param skn
     * @returns {{}}
     */
    processBundle(bundleData, skn, gid) {
        let subPrice = _.get(bundleData, 'bundleInfo.subPrice', 0);

        return {
            title: '优惠套装',
            href: helpers.urlFormat('/product/bundle/detail', {skn: skn, gid: gid}),
            description: subPrice ? '立省¥' + subPrice : 0,
            productList: productProcess.processProductList(bundleData && bundleData.productList)
        };
    },

    /**
     * 处理量贩数据
     * @param finalResult
     * @param bundleData
     */
    processDiscount(finalResult, bundleData) {
        finalResult.discountBuy = {
            num: _.get(bundleData, 'bundleInfo.bundleCount', 1),
            promotionPhrase: _.get(bundleData, 'bundleInfo.promotionPhrase', ''),
            discount: _.get(bundleData, 'bundleInfo.discount', 1)
        };

        let oldPromotion = finalResult.promotion;

        finalResult.promotion = [{
            promotionTitle: _.get(bundleData, 'bundleInfo.promotionPhrase', ''),
            promotionType: '量贩'
        }];

        _.forEach(oldPromotion, value => {
            finalResult.promotion.push(value);
        });
    }
};

const newDetail = {
    getProductData(data) {
        let params = {
            method: 'app.product.data'
        };

        if (data.id) { // 通过 productId 获取商品详情
            Object.assign(params, {
                product_id: data.id
            });
        } else if (data.productSkn) { // 通过 productSkn 获取商品详情
            Object.assign(params, {
                product_skn: data.productSkn
            });
        }

        return api.get('', params, {
            code: 200,
            cache: true
        }).then(result => {
            if (!result || result.code === 500 || !result.data) {
                return {};
            }

            result.data.goods_id = data.goodsId;

            return tool.prodessDetailData(result.data);
        });
    },

    /*
        获取 促销,评论,咨询,品牌信息
    */
    querySknData(params) {
        let brandId = params.brandId;
        let productId = params.productId;
        let skn = params.skn;
        let gid = params.gid;

        let apiArray = [
            this.queryShopsByBrandId(brandId), // 0 品牌信息
            commentModel.getCommentInfo({
                productId: productId
            }), //  1 评论信息
            commentModel.getCommonConsult(), // 2 默认咨询信息
            commentModel.getConsults(productId, 1, 2), // 3 咨询列表
            this.queryPromotion(skn), // 4 促销活动
        ];

        if (params.bundleType) {
            apiArray.push(bundle.getBundleBySkn(skn));
        }

        return Promise.all(apiArray).then(info => {
            let finalResult = {
                promotion: null,
                enterStore: null,
                feedbacks: {
                    consults: [],
                    consultsNum: 0
                },
            };

            finalResult.promotion = info[4];
            finalResult.enterStore = info[0];
            finalResult.feedbacks = tool.processFeedback({
                comment: info[1],
                defaultConsult: info[2],
                userConsult: info[3]
            }, productId);

            let bundleData = _.get(info[5], 'data', null);

            /* 套装 */
            if (bundleData && _.get(bundleData, 'bundleInfo.discountType', null) === 1) {
                finalResult.bundleData = tool.processBundle(bundleData, skn, gid);
            }

            /* 量贩 */
            if (bundleData && _.get(bundleData, 'bundleInfo.discountType', null) === 2) {
                tool.processDiscount(finalResult, bundleData);
            }

            return finalResult;
        });
    },

    // ------------------------------接口

    /*
    接口: 根据BrandID, 查询品牌信息
    */
    queryShopsByBrandId(brandId) {
        let params = {
            method: 'app.shop.queryShopsByBrandId',
            brand_id: brandId
        };

        let cacheConf = {
            cache: true
        };

        return api.get('', params, cacheConf)
            .then(result => {
                if (result && result.code === 200) {
                    return tool.processShopsInfo(result.data);
                }
                return [];
            }, () => []);
    },

    /**
     *  接口: 商品 促销活动
     */
    queryPromotion(skn) {
        let params = {
            method: 'app.product.promotion',
            product_skn: skn
        };

        let cacheConf = {
            cache: true
        };

        return api.get('', params, cacheConf)
            .then(result => {
                if (result && result.code === 200) {
                    return result.data;
                }

                return {};
            });
    },

    /**
     * 获取秒杀列表
     */
    getSeckillData(param) {
        let params = {
            method: 'app.seckill.data',
            product_skn: param.productskn
        };

        return api.get('', params, {
            code: 200
        })

        // mock
        .catch()
        .then(result => {
            let data = {};

            if (result.data) {
                data = result.data;
                if (data.storageSum > 0) {
                    data.storageSum = 1;
                }
                if (data.secKillSku) {
                    _.forEach(data.secKillSku, item => {
                        if (item.storageNum > 0) {
                            item.storageNum = 1;
                        }
                    });
                }
            } else {
                data.status = 0;
            }
            return data;
        });
    }
};

module.exports = newDetail;