me-gift-service.js
3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* 我的礼品卡列表
* @author xiaoxiao <xiaoxiao.hao@yoho.cn>
* @date: 2017/9/1
*/
'use strict';
const Promise = require('bluebird');
const _ = require('lodash');
const helpers = global.yoho.helpers;
const MeGiftAPi = require('./me-gift-api');
const setPager = require(`${global.utils}/pager`).setPager;
const crypto = global.yoho.crypto;
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
this.meGiftAPi = new MeGiftAPi(this.ctx);
}
headTab(params) {
let type = params.type;
let tabs = [{
name: '使用中',
url: helpers.urlFormat('/home/megift'),
active: false
}, {
name: '已过期',
url: helpers.urlFormat('/home/megift', {type: 1}),
active: false
}, {
name: '已用完',
url: helpers.urlFormat('/home/megift', {type: 2}),
active: false
}, {
name: '已冻结',
url: helpers.urlFormat('/home/megift', {type: 3}),
active: false
}, {
name: '添加礼品卡',
class: 'add-gift',
active: false
}];
tabs[type].active = true;
return tabs;
}
// 礼品卡列表-status=[1-可使用,2-已冻结,3-已过期,4-已用完]
getList(params, uid) {
let status = 1;
params.type = Number(params.type > -1 && params.type < 4 ? params.type : 0);
params.page = Number(params.page) || 1;
switch (params.type) {
case 1: status = 3; break;
case 2: status = 4; break;
case 3: status = 2; break;
default: status = 1;
}
return Promise.all([
this.meGiftAPi.getList(uid, status, params.page),
this.verifyBinMobile(uid)
]).then(rlist => {
let giftData = {};
giftData.data = _.get(rlist[0], 'data.giftCardInfoBOList', []);
giftData.pager = Object.assign({
count: _.get(rlist[0], 'data.total', 0),
curPage: params.page,
totalPages: _.get(rlist[0], 'data.pageSize', 0)
}, setPager(_.get(rlist[0], 'data.pageSize', 0), {
page: params.page
}));
return {
giftData: giftData,
userInfo: rlist[1],
tabs: this.headTab(params)
};
});
}
// 礼品卡列表
detailList(params) {
return Promise.resolve({tabs: this.headTab(params)});
}
// 礼品卡列表
verifyBinMobile(uid) {
let userInfo = {
isBinMobile: _.get(this.ctx, 'req.session.isBinMobile', false)
};
if (userInfo.isBinMobile) {
return Promise.resolve(userInfo);
}
return this.meGiftAPi.getProfile(uid).then(lres => {
lres = _.get(lres, 'data', {});
let isBinMobile = Number(!!lres.verify_mobile);
_.set(this.ctx, 'req.session.isBinMobile', isBinMobile);
return Object.assign({}, userInfo, {
isBinMobile: isBinMobile,
email: lres.verify_email,
mobile: lres.verify_mobile
});
});
}
// 激活礼品卡
activateGift(params, uid) {
return this.meGiftAPi.activateGift(uid, params.cardCode, crypto.encryption('yoho9646yoho9646', params.cardPwd));
}
};