daily-check-in.js
2.54 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
/**
* Created by qiujun on 2019/5/7.
*/
const _ = require('lodash');
const aes = require('../../../utils/aes');
const PARENT_TABLE_NAME = 'dailysignin';
const TABLE_NAME = PARENT_TABLE_NAME + ':activity'; // 签到活动表
class ActDailyCheckInModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
this.redis = global.yoho.redis;
this.client = this.redis.client;
}
// 获取签到活动列表
async activityList() {
let length = await this.client.llenAsync(TABLE_NAME);
return this.client.lrangeAsync(TABLE_NAME, 0, length - 1).then(acts => {
let finalResult = [];
acts.filter(act => act).map(act => {
finalResult.unshift(JSON.parse(act)); // 向前插入,使数组倒序
});
return finalResult;
});
}
// 创建签到活动
async createActivity(data) {
let length = await this.client.llenAsync(TABLE_NAME);
data.id = length + 1;
data.encryptedId = aes.encryptUid(length + 1);
return this.client.rpushAsync(TABLE_NAME, JSON.stringify(data));
}
/**
* 获取活动信息
* @param actId
* @returns {Promise.<void>}
*/
async actInfo(actId) {
let idx = parseInt(aes.decryptUid(actId), 10) - 1;
if (idx) {
return this.client.lrangeAsync(TABLE_NAME, idx, idx).then(act => {
return JSON.parse(act[0] || {});
});
} else {
return {
code: 201,
message: '无活动信息'
};
}
}
/**
* 删除活动
* @param id
* @returns {Promise.<*>}
*/
async actDelete(id) {
id = parseInt(aes.decryptUid(id), 10);
return this.client.lsetAsync(TABLE_NAME, id - 1, '');
}
/**
* 获取中奖信息
* @param id
* @returns {Promise.<void>}
*/
async getWinnerList(id) {
if (id) {
let actId = parseInt(aes.decryptUid(id), 10);
let prizeListKey = `${PARENT_TABLE_NAME}:${actId}:prizelist`;
let result = await this.client.zrevrangeAsync(prizeListKey, 0, -1);
let finalResult = [];
for (let i = 0; i < result.length; i++) {
let ret = {};
let prize = JSON.parse(result[i]);
let uid = prize.uid;
let userInfoKey = `${PARENT_TABLE_NAME}:${actId}:user:${uid}:info`;
let userInfo = await this.client.zrangeAsync(userInfoKey, 0, 0);
if (userInfo && userInfo.length > 0) {
let phone = JSON.parse(userInfo[0]).phone;
Object.assign(ret, prize, {phone: phone});
finalResult.push(ret);
}
}
return finalResult;
}
}
}
module.exports = ActDailyCheckInModel;