currency.js 2.61 KB
/**
 * [个人中心]有货币
 * @author: jiangmin
 * @date: 2016/07/11
 */

'use strict';
const mcHandler = require('../models/menu-crumb-handler');

const currencyModel = require('../models/currency');


/**
 * 时间格式化
 * @param time
 * @param a
 * @returns {string}
 */
const getTime = (time, a)=> {
    let newDate = new Date(time.getFullYear(), time.getMonth() + 1, time.getDate() - 30 * a);

    return newDate.getFullYear() + '-' + newDate.getMonth() + '-' + newDate.getDate();
};

/**
 * 取时间的秒数
 * @param time
 * @returns {number}
 */
const getDate = (time)=> {
    if (typeof (time) === 'string') {
        return new Date(time).getTime() / 1000;
    } else {
        return new Date(time.getFullYear() + '-' + time.getMonth() + '-' + time.getDate()).getTime() / 1000;
    }
};

/**
 * 有货币页面加载
 */
const index = (req, res, next) => {
    let uid = req.user.uid;
    let page = parseInt(req.query.page, 10) || 1;
    let queryType = parseInt(req.query.queryType, 10) || 0;
    let beginTime = req.query.beginTime || getTime(new Date(), 3);
    let date1 = getDate(new Date());
    let date2 = getDate(beginTime);
    let selectIndex = parseInt((date1 - date2) / (90 * 3600 * 24), 10);

    Promise.all([currencyModel.getIndexData(uid, page, queryType, beginTime), mcHandler.getMeThumb()]).then(result => {
        const thumb = result[1];

        result[0].list.tabs[queryType].isActive = true;
        result[0].list.coinList.forEach(function(x) {
            x.date = x.date.replace(/\-/g, '.');
        });
        result[0].list.selects[selectIndex > 2 ? 2 : selectIndex].isSelected = 'selected';
        if (result[0].list.paginationOpts) {
            result[0].list.paginationOpts.queryParams = {
                queryType: queryType,
                beginTime: beginTime
            };
        }

        res.display('index', {
            module: 'me',
            page: 'currency',
            isMe: true,
            content: {
                nav: mcHandler.getMeCrumb('我的有货币'),
                navigation: mcHandler.getSideMenu('我的有货币'),
                banner: thumb,
                currency: true,
                tabs: result[0].list.tabs,
                title: '我的有货币',
                currentYear: new Date().getFullYear(),
                data: result[0].list.coinList,
                paginationOpts: result[0].list.paginationOpts,
                num: result[0].num.data,
                selects: result[0].list.selects,
                total: result[0].list.total ? result[0].list.total : 0
            }
        });
    }).catch(next);
};


module.exports = {
    index
};