guang-new.js 3.28 KB
'use strict';

const _ = require('lodash');
const helpers = global.yoho.helpers;

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

    _indexRes(params) {
        let options = {
            url: '/operations/api/v5/resource/get',
            data: {
                content_code: '5523199f921ee2297b509397a224d9e8',
                gender: params.gender
            },
            api: global.yoho.ServiceAPI
        };

        return this.get(options).then(result => {
            return result;
        });
    }

    list(params) {
        let options = {
            url: '/guang/api/v1/article/getHomePageList',
            data: {
                page: params.page || 1,
                limit: params.limit || 10
            },
            api: global.yoho.ServiceAPI
        };

        return this.get(options).then(result => {
            let resu = {
                list: []
            };

            if (_.get(result, 'data.guangList')) {
                resu.list = result.data.guangList;

                _.forEach(resu.list, (val) => {
                    val.play = val.hasVideoFlag === 1;
                });
            }
            return resu;
        });
    }

    index(params) {
        return Promise.all([
            this._indexRes(params)
        ]).then((result) => {
            let resu = {
                floorRes: {},
                gender: encodeURIComponent(params.gender)
            };

            if (_.get(result, '[0].data')) {
                _.forEach(result[0].data, (val) => {
                    if (val.template_name === 'focus' && _.get(val, 'data')) {
                        resu.floorRes.focus = _.get(val, 'data');
                        _.forEach(val.data, (d) => {
                            d.src = helpers.image(d.src, 750, 480);
                        });
                    }
                    if (val.template_name === '3:4ImageListFloor' && _.get(val, 'data.list')) {
                        resu.floorRes.type = _.get(val, 'data.list');
                        _.forEach(val.data.list, (d) => {
                            d.src = helpers.image(d.src, 200, 640);
                        });
                    }
                    if (val.template_name === 'GuangRqFloor' && _.get(val, 'data.list')) {
                        resu.floorRes.renQi = _.get(val, 'data.list');
                        _.forEach(val.data.list, (d) => {
                            d.src = helpers.image(d.src, 690, 430);
                        });
                    }
                    if (val.template_name === 'GuangDpFloor' && _.get(val, 'data.list')) {
                        resu.floorRes.daPei = _.get(val, 'data.list');
                        _.forEach(val.data.list, (d) => {
                            d.src = helpers.image(d.src, 590, 370);
                        });
                    }
                    if (val.template_name === 'GuangHhFloor' && _.get(val, 'data.list')) {
                        resu.floorRes.haoHuo = _.get(val, 'data.list');
                        _.forEach(val.data.list, (d) => {
                            d.src = helpers.image(d.src, 360, 224);
                        });
                    }
                });
            }

            return resu;
        });
    }
}

module.exports = newGuang;