daily-check-in.js
1.58 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
/**
* 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 => {
return acts.filter(act => act).map(act => {
return JSON.parse(act);
});
});
}
// 创建签到活动
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, '');
}
}
module.exports = ActDailyCheckInModel;