me-gift-service.js
5.02 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* 我的礼品卡列表
* @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), params));
return {
giftData: giftData,
userInfo: rlist[1],
tabs: this.headTab(params)
};
});
}
// 获取礼品卡消费记录
consumeList(params, uid) {
let types = ['类型', '消费', '退款'];
params.page = Number(params.page) || 1;
params.type = Number(params.type) || 0;
return this.meGiftAPi.consumeList(params, uid).then(rlist => {
let giftData = {};
giftData.data = _.get(rlist, 'data', {});
giftData.pager = Object.assign({
count: _.get(rlist, 'data.total', 0),
curPage: params.page,
totalPages: _.get(rlist, 'data.pageSize', 0)
}, setPager(_.get(rlist, 'data.pageSize', 0), params));
_.set(giftData, 'data.types', types[params.type] || types[0]);
return giftData;
});
}
// 验证手机是否绑定
verifyBinMobile(uid) {
let userInfo = {
isBinMobile: Number(!!_.get(this.ctx, 'req.user.mobile', false))
};
if (userInfo.isBinMobile) {
return Promise.resolve(userInfo);
}
return this.meGiftAPi.getProfile(uid).then(d => {
let mobile = _.get(d, 'data.mobile', '');
if (mobile) {
_.set(this.ctx, 'req.session.USER_MOBILE', mobile);
}
Object.assign(userInfo, {isBinMobile: Number(!!mobile), email: _.get(d, 'data.email', '')});
return userInfo;
});
}
// 发送邮箱验证码
sendEmailCode(params, uid) {
return this.meGiftAPi.sendEmailCode(params.email, uid);
}
// 验证邮箱验证码
verifyEmail(params, uid) {
return this.meGiftAPi.verifyEmail(params.code, uid);
}
// 发验手机证码
smsBind(params) {
return this.meGiftAPi.checkIsCanBind(params.area, params.mobile).then(res => {
if (_.get(res, 'data.isCanBind') === 'N') {
return {
code: 401,
message: '<p>绑定失败,该手机号已被绑定</p><p>请更换手机号</p>'
};
}
return this.meGiftAPi.smsbind(params.area, params.mobile);
});
}
// 修改绑定的手机号
changeMobile(params, uid) {
return this.meGiftAPi.changeMobile(params.area, params.mobile, params.code, uid).then(res => {
if (res.code === 200) {
_.set(this.ctx, 'req.session.USER_MOBILE', params.mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'));
}
return res;
});
}
// 激活礼品卡
activateGift(params, uid) {
return this.meGiftAPi.activateGift(uid, params.cardCode, crypto.encryption('yoho9646yoho9646', params.cardPwd));
}
};