redpac.js
4.47 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* Created by qiujun on 2018/10/9.
* 双11红包雨相关接口
*/
const INVALID_PARAMS = '参数错误';
const RedpacModel = require('../models/redpac');
const _sender = global.yoho.sender;
/**
*
* @param len 长度
* @param radix 基数(chars取字符的长度,例如16,就是取前16个字符作为udid基础字符)
* @returns {string}
*/
const getUdid = (len, radix) => {
let chars = '0123456789abcdefghijklmnopqrstuvwxyz'.split('');
let uuid = [], i;
radix = radix || chars.length;
if (len) {
// Compact form
for (i = 0; i < len; i++) {
uuid[i] = chars[0 | Math.random() * radix];
}
} else {
// rfc4122, version 4 form
let r;
// rfc4122 requires these characters
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
uuid[14] = '4';
// Fill in random data. At i==19 set the high bits of clock sequence as
// per rfc4122, sec. 4.1.5
for (i = 0; i < 36; i++) {
if (!uuid[i]) {
r = 0 | Math.random() * 16;
uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r];
}
}
}
return uuid.join('');
};
/**
* 上报
* @param type 0: packrain 1: turntable
* @param tags 一些基础参数
* @param fields 红包的金额
* @param isFail 接口是否失败
*/
const sendMessage = (type, tags, fields, isFail) => {
if (!_sender) {
return;
}
const hostname = (require('os').hostname());
let typeName = ['packrain', 'turntable'][type || 0];
let initTags = {
hostname,
pid: process.pid || 0,
app: global.yoho.config.appName,
type: typeName
};
tags = Object.assign({}, initTags, tags);
isFail = isFail ? 1 : 0;
fields = Object.assign({}, fields);
let mesurement = ['monitor_success_report', 'monitor_error_report'][isFail];
try {
_sender.addMessage({
measurement: mesurement,
tags,
fields
});
} catch (e) {
console.error(e);
}
};
const redpac = {
/**
* 从接口获取红包的token
* @param req
* @param res
*/
getRedpacToken(req, res) {
let params = req.query;
let udid = params.udid || getUdid(40, 16);
let actSource = params.source || 1002;
return req.ctx(RedpacModel).getRedpacToken(udid, actSource).then(result => {
res.json(result);
});
},
/**
* 领取红包
* @param req
* @param res
* @param next
* @returns {Promise.<TResult>}
*/
getRedpac(req, res, next) {
let params = req.body;
console.log(params);
let uid = {
toString: () => {
return parseInt(params.uid, 10);
},
sessionKey: params.sessionKey,
appVersion: params.appVersion,
appSessionType: params.sessionType
};
let udid = params.udid || '';
let redpacToken = params.token || '';
let actSource = params.source || 1002;
if (!params.udid || !params.uid || !params.token) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
return req.ctx(RedpacModel).getRedpac(uid, udid, actSource, redpacToken).then(result => {
if (result && result.data) {
sendMessage(0, {}, {succeedTimes: result.data.amount || 0}, 0);
} else {
sendMessage(0, {}, {succeedTimes: 0}, 0);
}
return res.json(result || {});
}).catch(e => {
console.log('MyError:', e);
sendMessage(0, {}, {message: e.message || 'interface error', stack: e}, 1);
next(e);
});
},
/**
* 获取我的红包列表
* @param req
* @param res
* @param next
*/
getMyRedpac(req, res, next) {
let params = req.query;
let uid = {
toString: () => {
return parseInt(params.uid, 10);
},
sessionKey: params.sessionKey,
appVersion: params.appVersion,
appSessionType: params.sessionType
};
if (!params.uid) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
return req.ctx(RedpacModel).getMyRedpac(uid).then(result => {
return res.json(result);
}).catch(next);
}
};
module.exports = redpac;