myCurrency.js 2.28 KB
'use strict';

const serviceAPI = global.yoho.ServiceAPI;
const api = global.yoho.API;
const _ = require('lodash');
const logger = global.yoho.logger;

const _yohoCoin = (uid) => {
    return api.get('', {
        method: 'app.yoho.yohocoin',
        uid: uid
    }).then((result) => {

        if (result && result.code === 200) {
            return result.data;
        } else {
            logger.error('youhocoin code no 200');
        }
    });
};

const bannerData = (contentCode) => {

    return serviceAPI.get('operations/api/v5/resource/get', {
        content_code: contentCode
    }).then((result) => {

        if (result && result.code === 200) {

            return result.data;

        } else {
            logger.error('banner is not 200');
            return {};
        }
    });
};

const myCurrency = (uid, contentCode) => {
    return Promise.all([_yohoCoin(uid), bannerData(contentCode)])
        .then((result) => {

            return {
                yohoCoin: result[0],
                banner: result[1]
            };
        });
};

const currencyDetail = (uid, page, limit) => {
    return api.get('', {
        method: 'app.yohocoin.lists',
        uid: uid,
        page: page,
        limit: limit
    }).then((result) => {

        if (result && result.code === 200) {
            let total = parseInt(result.data.page_total, 10) + 1;

            _.forEach(_.get(result, 'data.coinlist', []), perCoin => {
                if (perCoin.num > 0) {
                    perCoin.num = '+' + perCoin.num;
                }
            });

            if (page && page <= total) {
                return _yohoCoin(uid).then(list => {

                    if (list.yohocoin_num && list.yohocoin_num !== 0) {
                        result.data = _.assign(result.data, {
                            money: true
                        });
                    } else {
                        result.data = _.assign(result.data, {
                            money: false
                        });
                    }

                    return result.data;
                });
            } else {
                return;
            }

        } else {
            logger.error('youholist code no 200');
        }

    });
};

module.exports = {
    myCurrency,
    currencyDetail,
    bannerData
};