material-new.js 1.81 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');
const utils = '../../../utils';
const productProcess = require(`${utils}/product-process`);

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) {
        let options = {
            data: {
                method: 'app.search.recommendProduct',
                type: 'default'
            },
            param: {
                code: 200
            }
        };

        return this.get(options).then(result => {
            if (result && result.code === 200) {
                let resu = {
                    goods: []
                };

                if (_.get(result, 'data.product_list', [])) {
                    let build = [];

                    build = productProcess.processProductList(result.data.product_list || [], {
                        isApp: params.isApp,
                        showSimilar: false
                    });
                    if (params.unionType) {
                        _.forEach(build, (val) => {
                            let newUrl = val.url;

                            val.url = newUrl.replace('.html', `.html?union_type=${params.unionType}`);
                        });
                    }
                    resu.goods = build;
                }
                return resu;
            }
        });
    }
}

module.exports = materialModel;