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

'use strict';

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

const index = (req, res) => {

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

    gtModel.getResource(contentCode).then(data=>{

      if (!data.data) {
        res.jsonp({
          code: 200,
          data: '没有数据',
          message: 'User info'
        });
      } else {

        let banner = '';
        let md = data.data[0];

        if (md && md.data) {
          if (md.template_name === 'single_image') {
            banner = md.data[0];
          } else if (md.template_name === 'single_name_image')
          {
            banner = md.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: 200,
            data: banner
          });
        }
      }
    });
};

module.exports = {
    index
};