getBanner.js 1.32 KB
/**
 * 获取资源位 banner广告 controller
 * @author: gxh<xuhui.ge@yoho.cn>
 * @date: 2016/11/30
 */

'use strict';

const getBannerModel = require('../models/getBanner');
const helpers = global.yoho.helpers;

const index = (req, res, next) => {

    let contentCode = req.query.content_code || '';
    let width = req.query.width || '';
    let height = req.query.height || '';

    req.ctx(getBannerModel).getResource(contentCode).then(data => {

        let banner = '';

        if (data.data) {

            // 获取banner数据
            let result = data.data[0];

            if (result && result.data) {

                if (result.template_name === 'single_image') {

                    banner = result.data[0];

                } else if (result.template_name === 'single_name_image') {

                    banner = result.data;
                }

                if (banner) {
                    if (!width || !height) {
                        width = 2600;
                        height = 60;
                    }
                }

                banner.src = helpers.image(banner.src, width, height, 2);
                banner.url = banner.url;

            }
        }

        res.jsonp({
            code: banner ? 200 : 400,
            data: banner
        });

    }).catch(next);
};

module.exports = {
    index
};