index.js 4.08 KB
'use strict';
const api = global.yoho.API;
const serviceAPI = global.yoho.ServiceAPI;
const favApi = global.yoho.FavAPI;
const Promise = require('bluebird');
const helpers = global.yoho.helpers;

/**
 * 查询用户信息
 *
 * @param int uid 用户ID
 * @return Object 个人详情数据
 */
const _getUserProfileData = exports.getUserProfileData = (uid) => {
    if (!uid) {
        return Promise.resolve({});
    }

    return api.get('', {
        uid: uid,
        method: 'app.passport.profile'
    }, {
        code: 200
    }).then(result => {
        return result.data || {};
    });
};

/**
 * 个人中心页面收藏及订单等的数目数据
 *
 * @param int uid 用户ID
 * @return Object 处理之后的数据
 */
const _getInfoNumData = (uid) => {
    if (!uid) {
        return Promise.resolve({});
    }

    return api.all([api.get('', {
        uid: uid,
        method: 'app.home.getInfoNum' // 收藏及订单数目数据
    }, {
        code: 200
    }), api.get('', {
        uid: uid,
        method: 'app.address.gethidden' // 用户地址的数据
    }, {
        code: 200
    }), favApi.get('brower', {
        method: 'app.favorite.getFavoriteCount', // 获取用户收藏商品总数
        uid: uid
    }, {
        code: 200
    })]).then(data => {
        const res = {
            wait_pay_num: '',
            wait_cargo_num: '',
            send_cargo_num: '',
            refund_exchange_num: '',
            product_favorite_total: '',
            brand_favorite_total: '',
            yoho_coin_num: ''
        };

        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[2] && data[2].data && data[2].data.product_favorite_total > 0) {
            res.product_favorite_total = data[2].data.product_favorite_total > 99 ? '99+' :
                data[2].data.product_favorite_total;
        }


        return Object.assign(res, {
            address_num: data[1].data && data[1].data.length ? data[1].data.length : ''
        });
    });
};


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

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

    return api.get('', infoData, {
        code: 200
    }).then(result => {
        const formatData = [];
        let helpData = result.data;

        helpData = helpData || [];
        helpData.forEach(function(item) {
            formatData.push({
                name: item.caption,
                code: item.code,
                url: helpers.urlFormat('/help', {
                    id: item.code,
                    caption: item.caption,
                })
            });
        });
        return formatData;
    });
};

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

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

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

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

/**
 * 保存个人信息数据
 *
 * @param data
 */
exports.saveMydetails = (params) => {
    let defaultParam = {
            method: 'app.passport.modifyBase'
        },
        data = Object.assign(defaultParam, params);

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