Blame view

apps/common/controllers/passport.js 3.11 KB
1 2 3 4 5 6 7 8 9 10 11
/**
 * 获取资源位controller
 * @author: gxh<xuhui.ge@yoho.cn>
 * @date: 2016/12/01
 */

'use strict';

const _ = require('lodash');
const Promise = require('bluebird');
const co = Promise.coroutine;
OF1706 authored
12
const fp = require('lodash/fp');
13 14
const helpers = global.yoho.helpers;
OF1706 authored
15 16 17
// NOTE: 这里修改了图片质量的参数
helpers.image = _.flow(helpers.image, fp.replace(/\/quality\/\d*$/, '/quality/90'));
18 19
const passportModel = require('../models/passport');
20
const index = (req, res, next) => {
21 22 23

    return co(function *() {
OF1706 authored
24
        // 是否调用个人信息数字接口
25 26 27 28
        let userInfo = {};
        let uid = req.user.uid;

        if (uid) {
OF1706 authored
29
            // 获取个人信息VIP资料
王水玲 authored
30
            let profile = yield req.ctx(passportModel).getUserProfile(uid).then(ret => {
OF1706 authored
31
                if (ret && ret.code === 200) {
32 33 34 35 36
                    return ret.data;
                }
                return null;
            });
王水玲 authored
37
            let userNum = yield req.ctx(passportModel).getUserInfoNum(uid).then(ret => {
OF1706 authored
38
                if (ret && ret.code === 200) {
39 40 41 42
                    return ret.data;
                }
            });
OF1706 authored
43
            // 个人信息调用失败返回
44 45
            if (profile) {
OF1706 authored
46
                userInfo.result = 1;
47
OF1706 authored
48 49
                let curYearCost = parseInt(_.get(profile, 'vip_info.curYearCost', 0)); //eslint-disable-line
                let nextVipNeedCost = parseInt(_.get(profile, 'vip_info.nextVipNeedCost', 0)); //eslint-disable-line
OF1706 authored
50
OF1706 authored
51
                Object.assign(userInfo, {
52
53 54
                    // 个人资料
                    profileName: _.get(profile, 'profile_name', ''),
OF1706 authored
55
                    headIco: _.get(profile, 'head_ico') ? helpers.image(_.get(profile, 'head_ico'), 63, 63) : '',
OF1706 authored
56
                    curTitle: _.get(profile, 'vip_info.title', 0),
57
58
                    // VIP信息
OF1706 authored
59
                    curYearCost: curYearCost,
60 61
                    nextLevel: _.get(profile, 'vip_info.next_level', 0),
                    nextVipTitle: _.get(profile, 'vip_info.nextVipTitle', 0),
OF1706 authored
62
                    nextVipNeedCost: nextVipNeedCost,
63 64

                    // 升级百分比
OF1706 authored
65
                    curYearCostPer: nextVipNeedCost ?
OF1706 authored
66
                        Math.round(curYearCost / (nextVipNeedCost + curYearCost) * 100) : 0,
67
68 69
                    // 待处理订单
                    order: _.get(userNum, 'wait_pay_num + wait_cargo_num + send_cargo_num', 0),
70
71 72
                    // 我的收藏
                    favorite: _.get(userNum, 'brand_favorite_total', 0),
OF1706 authored
73
74 75
                    // 我的优惠券
                    coupon: _.get(userNum, 'coupon_num', 0),
OF1706 authored
76
77 78
                    // 我的有货币
                    coin: _.get(userNum, 'yoho_coin_num', 0),
OF1706 authored
79
80 81 82 83
                    // 我的退换货
                    return: _.get(userNum, 'refund_exchange_num', 0)

                });
84 85 86 87 88 89 90

                return res.jsonp({
                    code: 200,
                    data: userInfo
                });
            }
        }
91
        return res.jsonp({
92 93 94
            code: 400,
            data: '',
            message: '加载失败'
OF1706 authored
95
        });
OF1706 authored
96
    })().catch(next);
97 98 99 100 101 102
};

module.exports = {
    index
};