coupon511.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const _ = require('lodash');
const aes = require('../../../utils/aes');
const mySqlCli = global.yoho.utils.mysqlCli;
const UfoApi = global.yoho.UfoAPI;
const TABLE_PRIZE = 'act_prize_user_log';
const TABLE_USER_DETAIL = 'act_user_detail_info';
const logger = global.yoho.logger;
const mysqlCli = global.yoho.utils.mysqlCli;
const coupons = ['57008016-f16a-4814-854b-2ed8c8cab3b5', '418b5d9b-6574-4d62-8457-d74da6ec94e2'];
class Coupon511 extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
this.redis = global.yoho.redis;
this.client = this.redis.client;
}
_getCoupon(uid, inx) {
const token = coupons[inx - 1];
return this.get({
url: 'coupon',
data: {
method: 'ufo.coupons.send',
uid: uid,
coupon_tokens: token
},
api: UfoApi
});
}
concatPhone(uid) {
let phone = '';
let appStrLen = 11 - uid.toString().length;
for (let i = 0; i < appStrLen; i++) {
phone += '0';
}
phone += uid.toString();
return phone;
}
_saveUserInfo(uid) {
let actId = 9999, name = '000', birthday = '', gender = '', email = 'u@yoho.cn', province = '', city = '';
let phone = this.concatPhone(uid);
let strFind = `SELECT COUNT(*) AS user_count
FROM ${TABLE_USER_DETAIL}
WHERE act_id=:actId
AND user_phone=:phone`;
let strInsert = `INSERT INTO ${TABLE_USER_DETAIL}
(act_id, user_name, user_birthday, user_gender,
user_phone, user_email, user_province, user_city)
VALUES (:actId, :name, :birthday, :gender, :phone, :email, :province, :city)`;
return mySqlCli.query(strFind, {
actId,
phone
}).then(ret => {
if (ret && ret[0].user_count <= 0) {
return mySqlCli.insert(strInsert, {
actId,
name,
birthday,
gender,
phone,
email,
province,
city
}).then(ret2 => {
return Promise.resolve({
code: 200,
message: '用户信息添加成功',
data: ret2
});
});
} else {
return Promise.resolve({
code: 203,
message: '已存在的用户信息'
});
}
});
}
async getStatus(uid) {
const hasInx = await this.client.getAsync(`coupon511_${uid}`);
if (hasInx) {
return {
code: 200,
data: hasInx
};
}
return {
code: 204,
};
}
async takeCoupon(obj, inx) {
const hasInx = await this.client.getAsync(`coupon511_${obj.uid}`);
if (hasInx) {
return {
code: 200,
message: '已领取过 不可重复领取'
};
}
let uid = {
toString: () => {
return _.parseInt(obj.uid);
},
sessionKey: obj.sessionKey,
appVersion: obj.appVersion,
appSessionType: obj.sessionType
};
let result;
if (inx < 3) {
result = await this._getCoupon(uid, inx);
} else {
this._saveUserInfo(obj.uid);
result = {
code: 200,
message: '领取成功'
};
}
if (result.code === 200 || result.code === 401) {
this.client.set(`coupon511_${obj.uid}`, inx.toString());
}
return result;
}
}
module.exports = Coupon511;