index.js 3.22 KB
'use strict';
const api = global.yoho.API;
const serviceAPI = global.yoho.ServiceAPI;
const Promise = require('bluebird');
const _ = require('lodash');
const helpers = global.yoho.helpers;

/**
 * 查询用户信息
 *
 * @param int uid 用户ID
 * @return Object 个人详情数据
 */
const _getUserProfileData = (uid) => {
    return api.get('', {
        uid: uid,
        method: 'app.passport.profile'
    }).then(result => {
        return result.data;
    });
};

/**
 * 个人中心页面收藏及订单等的数目数据
 *
 * @param int uid 用户ID
 * @return Object 处理之后的数据
 */
const _getInfoNumData = (uid) => {
    return api.all([api.get('', {
        uid: uid,
        method: 'app.home.getInfoNum' // 收藏及订单数目数据
    }), api.get('', {
        uid: uid,
        method: 'app.address.gethidden' // 用户地址的数据
    })]).then(data => {
        let res = {
            wait_pay_num: 0,
            wait_cargo_num: 0,
            send_cargo_num: 0,
            refund_exchange_num: 0,
            product_favorite_total: 0,
            brand_favorite_total: 0,
            yoho_coin_num: 0
        };

        if (data[0].data) {
            for (let k in data[0].data) {
                if (res[k] !== null) {
                    res[k] = data[0].data[k] ? data[0].data[k] : '';
                    if (k !== 'yoho_coin_num' && res[k] > 99) {
                        res[k] = '99+';
                    }
                }
            }
        }
        if (data[1].data) {
            res = _.merge(res, {
                address_num: data[1].data.length ? data[1].data.length : ''
            });
        }
        return res;
    });
};


/**
 * 个人中心主页数据
 *
 * @param int uid 用户ID
 * @return Object 处理之后的数据
 */
exports.getUserHomeData = (uid) => {
    return Promise.all([_getUserProfileData(uid), _getInfoNumData(uid)]);
};

/**
 * 帮助中心列表页
 *
 * @param data
 *
 */
exports.getHelpInfo = (data) => {
    var defaultParam = {
            method: 'app.help.li'
        },
        infoData = Object.assign(defaultParam, data);

    return api.get('', infoData).then(result => {
        var helpData = result.data;
        var formatData = [];

        helpData = helpData || [];
        _.forEach(helpData, (item) => {
            formatData.push({
                name: item.caption,
                code: item.code,
                url: helpers.urlFormat('/home/help-detail', {
                    code: item.code,
                    caption: item.caption,
                })
            });
        });
        return formatData;
    });
};

/**
 * 帮助中心详情页
 *
 * @param data
 */
exports.getHelpDetail = (data) => {
    var defaultParam = {
            method: 'app.help.detail',
            return_type: 'html'
        },
        detailData = Object.assign(defaultParam, data);

    return api.get('', detailData).then(result => {
        return result;
    });
};

/**
 * 保存意见反馈数据
 *
 * @param data
 */
exports.saveFeedback = (data) => {
    return serviceAPI.post('/suggest/api/v1/suggest/saveSuggest', data).then(result => {
        return {
            code: result.code,
            message: result.message
        };

        // 参考接口数据
    });
};