passport.js 3.11 KB
/**
 * 获取资源位controller
 * @author: gxh<xuhui.ge@yoho.cn>
 * @date: 2016/12/01
 */

'use strict';

const _ = require('lodash');
const Promise = require('bluebird');
const co = Promise.coroutine;
const fp = require('lodash/fp');
const helpers = global.yoho.helpers;

// NOTE: 这里修改了图片质量的参数
helpers.image = _.flow(helpers.image, fp.replace(/\/quality\/\d*$/, '/quality/90'));

const passportModel = require('../models/passport');

const index = (req, res, next) => {

    return co(function *() {

        // 是否调用个人信息数字接口
        let userInfo = {};
        let uid = req.user.uid;

        if (uid) {
            // 获取个人信息VIP资料
            let profile = yield req.ctx(passportModel).getUserProfile(uid).then(ret => {
                if (ret && ret.code === 200) {
                    return ret.data;
                }
                return null;
            });

            let userNum = yield req.ctx(passportModel).getUserInfoNum(uid).then(ret => {
                if (ret && ret.code === 200) {
                    return ret.data;
                }
            });

            // 个人信息调用失败返回
            if (profile) {

                userInfo.result = 1;

                let curYearCost = parseInt(_.get(profile, 'vip_info.curYearCost', 0)); //eslint-disable-line
                let nextVipNeedCost = parseInt(_.get(profile, 'vip_info.nextVipNeedCost', 0)); //eslint-disable-line

                Object.assign(userInfo, {

                    // 个人资料
                    profileName: _.get(profile, 'profile_name', ''),
                    headIco: _.get(profile, 'head_ico') ? helpers.image(_.get(profile, 'head_ico'), 63, 63) : '',
                    curTitle: _.get(profile, 'vip_info.title', 0),

                    // VIP信息
                    curYearCost: curYearCost,
                    nextLevel: _.get(profile, 'vip_info.next_level', 0),
                    nextVipTitle: _.get(profile, 'vip_info.nextVipTitle', 0),
                    nextVipNeedCost: nextVipNeedCost,

                    // 升级百分比
                    curYearCostPer: nextVipNeedCost ?
                        Math.round(curYearCost / (nextVipNeedCost + curYearCost) * 100) : 0,

                    // 待处理订单
                    order: _.get(userNum, 'wait_pay_num + wait_cargo_num + send_cargo_num', 0),

                    // 我的收藏
                    favorite: _.get(userNum, 'brand_favorite_total', 0),

                    // 我的优惠券
                    coupon: _.get(userNum, 'coupon_num', 0),

                    // 我的有货币
                    coin: _.get(userNum, 'yoho_coin_num', 0),

                    // 我的退换货
                    return: _.get(userNum, 'refund_exchange_num', 0)

                });

                return res.jsonp({
                    code: 200,
                    data: userInfo
                });
            }
        }
        return res.jsonp({
            code: 400,
            data: '',
            message: '加载失败'
        });
    })().catch(next);
};

module.exports = {
    index
};