util.js 13.1 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
'use strict'
// 逛帮助
import util from '../../../../utils/util';
import Promise from '../../../../vendors/es6-promise';
import {parseBrandListData} from '../../../../utils/productList';
import objectAssign from '../../../../vendors/object-assign';

// 获取列表数据
function _getList(options, GET, API_HOST) {
    let param = {
        page: options.page || 1,
        gender: options.gender,
        sort_id: options.sort_id || 0,
        tag: options.tag,
        author_id: options.author_id,
        limit: options.limit || 6,
    };

    return GET(API_HOST + '/guang/api/v2/article/getList', param).then(json => {
        if(!(json && json.code && json.code == 200)) {
            return [];
        }
        return json.data;
    }).catch(error => {
        return [];
    });
}
// 获取收藏等数据
function _getSimpleArticleList(articleIds, GET, API_HOST) {
    if (articleIds.length === 0) {
        return new Promise(function(resolve, reject){
            resolve({});
        });
    }

    let param = {
        articleIds: articleIds.join(',')
    };

    return GET(API_HOST + '/guang/api/v6/article/getSimpleArticleList', param).then(json => {
        if(!(json && json.code && json.code == 200)) {
            return {};
        }
        return json.data;
    }).catch(error => {
        return {};
    });
}
// 获取作者数据
function _getAuthor(options, GET, API_HOST) {
    let param = {
        author_id: options.author_id
    };

    return GET(API_HOST + '/guang/service/v1/author/getAuthor', param).then(json => {
        if(!(json && json.code && json.code == 200)) {
            return {};
        }
        return json.data;
    }).catch(error => {
        return {};
    });
}
// 获取当前文章
function _getArticle(options, GET, API_HOST) {
    let param = {
        article_id: options.article_id,
    };

    return GET(API_HOST + '/guang/service/v2/article/getArticle', param).then(json => {
        if(!(json && json.code && json.code == 200)) {
            return {};
        }
        return json.data;
    }).catch(error => {
        return {};
    });
}
// 获取文章详情
function _getArticleContent(options, GET, API_HOST) {
    let param = {
        article_id: options.article_id,
    };

    return GET(API_HOST + '/guang/service/v2/article/getArticleContent', param).then(json => {
        if(!(json && json.code && json.code == 200)) {
            return [];
        }
        return json.data;
    }).catch(error => {
        return [];
    });
}
// 获取相关品牌
function _getBrand(options, GET, API_HOST) {
    let param = {
        article_id: options.article_id,
    };

    return GET(API_HOST + '/guang/service/v2/article/getBrand', param).then(json => {
        if(!(json && json.code && json.code == 200)) {
            return [];
        }
        json.data = json.data || [];
        
        let showBrands = json.data.filter(item => {
            return item.isGlobal === 'N'
        });
        return showBrands;
    }).catch(error => {
        return [];
    });
}
// 获取当前文章关联文章
function _getOtherArticle(options, GET, API_HOST) {
    let param = {
        article_id: options.article_id,
        tags: options.tags || '',
        offset: options.offset || 0,
        limit: options.limit || 3,
    };

    return GET(API_HOST + '/guang/service/v2/article/getOtherArticle', param).then(json => {
        if(!(json && json.code && json.code == 200)) {
            return [];
        }
        return json.data;
    }).catch(error => {
        return [];
    });
}
// 通过skn获取商品详情
function _recomandLi(options, GET, API_HOST) {
    let param = {
        method: 'app.search.recomandLi',
        order: 's_t_desc',
        query: options.skns.join(','),
        limit: options.skns.length || 1,
    };

    return GET(API_HOST, param).then(json => {
        if(!(json && json.code && json.code == 200)) {
            return {};
        }
        return json.data;
    }).catch(error => {
        return {};
    });
}

function _get(object, path, defaultValue) {
    return (!Array.isArray(path) ? path.replace(/\[/g, '.').replace(/\]/g, '').split('.') : path)
        .reduce((o, k) => (o || {})[k], object) || defaultValue;
}
// 格式化列表数据
function _formatArticle(data){
    let navResult = [];
    let rdata = [];

    (data || []).map(item => {
        switch (item.category_id) {
            case '1': // 话题
                item.isTopic = true;
                break;
            case '2': // 搭配
                item.isCollocation = true;
                break;
            case '3': // 潮人
                item.isFashionMan = true;
                break;
            case '4': // 潮品
                item.isFashionGood = true;
                break;
            case '5': // 小贴士
                item.isTip = true;
                break;
            case '19': // 专题
                item.isSpecialTopic = true;
                break;
            case '22': // 视频
                item.isVideo = true;
                break;
            case '24': // Show
            case '9999': // Show
                item.isShow = true;
                break;
            default:
                break;
        }
        item.src = util.formatImageUrl(item.src, 750, 480);
        if (_get(item, 'author.avatar', false)) {
            item.author.avatar = util.formatImageUrl(item.author.avatar, 50, 50);
        }
        // 商品列表
        (item.product_list || []).map(sub => {
            sub.url = util.formatImageUrl(sub.pic_url, 106, 140);
            sub.price = parseFloat(sub.sales_price, 10).toFixed(2);
            delete sub.pic_url;
            delete sub.sales_price;
        });

        navResult.push(item);
    });

    return navResult;
}
// 逛详情页数据处理
function _processArticleDetail(articleContent) {
    let finalDetail = [];
    let allGoods = [];
    let replyTexts = [];

    articleContent.map((value, index) => {
        // 文字
        let text = _get(value, 'text.data.text', false);
        if (text) {
            finalDetail.push({
                text: text
            });
            replyTexts.push(text);
        }

        // 一张图片
        if (_get(value, 'singleImage.data', []).length) {
            let tagList = _get(value, 'singleImage.data[0].tagList', []);
            let tagListGlobal = _get(value, 'singleImage.data[0].tagListGlobal', []);
            let tagListLimit = _get(value, 'singleImage.data[0].tagListLimit', []);

            // 普通商品
            // if (tagList.length) {
            //     articleContent.map((tag, tagIndex) => {
            //         tag.isOrdinary = true;
            //     });
            // }

            // 全球购商品
            // if (tagListGlobal.length) {
            //     tagListGlobal.map(tagGlobal => {
            //         tagGlobal.isGlobal = true;
            //         tagList.push(tagGlobal);
            //     });
            // }

            // 限定商品
            if (tagListLimit.length) {
                tagListLimit.map(tagLimit => {
                    tagLimit.isLimit = true;
                    tagList.push(tagLimit);
                });
            }

            finalDetail.push({
                bigImage: util.formatImageUrl(_get(value, 'singleImage.data[0].src', ''), 750, 480),
                tagList: tagList
            });
        }

        // 两张小图
        if (_get(value, 'smallPic.data', []).length > 1) {
            finalDetail.push({smallImage: [
                {
                    src: util.formatImageUrl(_get(value, 'smallPic.data[0].src', ''), 345, 215)
                },
                {
                    src: util.formatImageUrl(_get(value, 'smallPic.data[1].src', ''), 345, 215)
                }
            ]});
        }

        // 改变关联商品显示形式,false:默认单列多行,true:改成俩列多行
        let changeDisplayType = _get(value, 'goods.display_template') === '2';

        // 相关商品
        let goodsData = _get(value, 'goods.data', []);
        let goodsDataGlobal = _get(value, 'goods.dataGlobal', []);
        let goodsDataLimit = _get(value, 'goods.dataLimit', []);
        if (goodsData.length ||
            goodsDataGlobal.length ||
            goodsDataLimit.length) {

            // 普通商品
            goodsData.map(item => {
                if (changeDisplayType) {
                    item.src = util.formatImageUrl(item.src, 320, 430);
                } else {
                    item.src = util.formatImageUrl(item.src, 150, 200);
                }
                allGoods.push(item.id);
            });

           /* // 全球购商品
            goodsDataGlobal.map(item => {
                if (changeDisplayType) {
                    item.src = util.formatImageUrl(item.src, 320, 430);
                } else {
                    item.src = util.formatImageUrl(item.src, 150, 200);
                }
                allGoods.push(item.id);
                goodsData.push(item);
            });*/

            // 限定商品
            goodsDataLimit.map(item => {
                if (changeDisplayType) {
                    item.src = util.formatImageUrl(item.src, 320, 430);
                } else {
                    item.src = util.formatImageUrl(item.src, 150, 200);
                }
                item.isLimit = true;
                allGoods.push(item.id);
                goodsData.push(item);
            });

            finalDetail.push({relatedReco: {
                goods: goodsData,
                changeDisplayType: changeDisplayType
            }});
        }

        /*// 更多商品链接
        if (_get(value, 'link', false)) {
            finalDetail.push({
                moreLink: _get(value, 'link.data[0].url', '')
            });
        }*/

        // 推荐商品
        if (_get(value, 'recommend_products', false)) {
            let recommendProducts = _get(value, 'recommend_products.data', []);
            let recommendProductsGlobal = _get(value, 'recommend_products.dataGlobal', []);
            let recommendProductsLimit = _get(value, 'recommend_products.dataLimit', []);

            // 普通商品
            recommendProducts.map((goods, goodsKey) => {
                goods.isOrdinary = true;
                goods.pic_url = util.formatImageUrl(goods.pic_url, 170, 230);
                goods.price = parseFloat(goods.sales_price).toFixed(2);
            });

            /*// 全球购商品
            recommendProductsGlobal.map(goodsGlobal => {
                goodsGlobal.isGlobal = true;
                goodsGlobal.pic_url = util.formatImageUrl(goodsGlobal.pic_url, 170, 230);
                goodsGlobal.price = parseFloat(goodsGlobal.sales_price).toFixed(2);
                recommendProducts.push(goodsGlobal);
            });*/

            // 限定商品
            recommendProductsLimit.map(goodsLimit => {
                goodsLimit.isLimit = true;
                goodsLimit.pic_url = util.formatImageUrl(goodsLimit.pic_url, 170, 230);
                goodsLimit.price = parseFloat(goodsLimit.sales_price).toFixed(2);
                recommendProducts.push(goodsLimit);
            });

            finalDetail.push({
                recommendProducts: recommendProducts
            });
        }

        // 视频
        if (_get(value, 'video', false)) {
            let src = _get(value, 'video.data.src', '');

            finalDetail.push({
                video: {
                    src: src,
                    cover_image: _get(value, 'video.data.cover_image', '')
                }
            });
        }
    });

    return {
        finalDetail: finalDetail,
        replyTexts: replyTexts,
        allGoods: allGoods
    };
}

// 重新获取商品数据
function _pushGoodsInfo(finalDetail, product_lists) {
    let goodsObj = {};

    product_lists = parseBrandListData(product_lists, 160, 215);

    product_lists.map(item => {
        goodsObj[item.product_skn] = item;
    });

    (finalDetail || []).map((fitem, findex) => {
        if (fitem.relatedReco && fitem.relatedReco.goods) {
            let goods = [];

            fitem.relatedReco.goods.map(good => {
                if (goodsObj[good.id]) {
                    let default_images = fitem.relatedReco.changeDisplayType ?
                        goodsObj[good.id].default_images : util.formatImageUrl(good.src, 345, 215);

                    goods.push(objectAssign(good, goodsObj[good.id], {default_images}));
                }
            });

            fitem.relatedReco.moreNum = 4;// 改成第一屏幕最多展示4个
            fitem.relatedReco.goods = goods;
        }
    });

    return finalDetail;
}

// 获取当前文章分享内容
function _getShareDate(options, GET, API_HOST) {
    return GET(API_HOST + '/guang/api/v1/share/guang', options).then(json => {
        if(!(json && json.code && json.code == 200)) {
            return {};
        }
        return json.data;
    }).catch(error => {
        return {};
    });
}

module.exports = {
    _get,
    _getList,
    _getSimpleArticleList,
    _getAuthor,
    _formatArticle,
    _getArticle,
    _getArticleContent,
    _getBrand,
    _getOtherArticle,
    _recomandLi,
    _processArticleDetail,
    _pushGoodsInfo,
    _getShareDate
}