material.js 2.12 KB
'use strict';

const platformApi = new global.yoho.ApiBase(global.yoho.config.domains.platformApi, {
    name: 'imCs',
    cache: global.yoho.cache,
    useCache: false
});
const _ = require('lodash');

class materialModel extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    canLogin(params) {
        return platformApi.get('/platform/product/material/canlogin', {
            uid: params.uid
        }).then(result => {
            if (result && result.code === 200) {
                return result.data.canLogin;
            }
        });
    }

    list(params) {
        return platformApi.get('/platform/product/material/getRecommendProductList', {
            page: params.page || 1
        }).then(result => {
            if (result && result.code === 200) {
                let resu = {
                    goods: []
                };

                if (result && result.data && result.data.product_list) {
                    let build = [];

                    _.forEach(result.data.product_list, (val) => {
                        let url = `//m.yohobuy.com/product/${val.productSkn}.html`;
                        let imgUrl = val.picImgUrl;

                        if (imgUrl.split('?')[0]) {
                            imgUrl = imgUrl.split('?')[0] + '?imageView2/0/w/323/h/431';
                        }
                        if (params.unionType) {
                            url = `//m.yohobuy.com/product/${val.productSkn}.html?union_type=${params.unionType}`;
                        }
                        if (params.isApp) {
                            url = `http:${url}?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":"${val.productSkn}"}}`;  // eslint-disable-line
                        }

                        build.push({
                            product_name: val.productName,
                            default_images: imgUrl,
                            url: url
                        });
                    });
                    resu.goods = build;
                }
                return resu;
            }
        });
    }
}

module.exports = materialModel;