|
|
/**
|
|
|
* 签到活动model
|
|
|
* Created by qiujun on 2019/5/7.
|
|
|
*/
|
|
|
const _ = require('lodash');
|
|
|
const aes = require('../../../utils/aes');
|
|
|
const WheelSurfModel = require('./wheel-surf');
|
|
|
const PARENT_TABLE_NAME = 'dailysignin';
|
|
|
const TABLE_NAME = PARENT_TABLE_NAME + ':activity';
|
|
|
|
|
|
class ActDailyCheckIn extends global.yoho.BaseModel {
|
|
|
class DailyCheckInModel extends global.yoho.BaseModel {
|
|
|
constructor(ctx) {
|
|
|
super(ctx);
|
|
|
this.redis = global.yoho.redis;
|
|
|
this.client = this.redis.client;
|
|
|
this.wheelSurfModel = new WheelSurfModel(ctx);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取活动信息
|
|
|
* @param actId
|
|
|
* @returns {Promise.<void>}
|
|
|
*/
|
|
|
async getActInfo(actId) {
|
|
|
let idx = 0;
|
|
|
|
|
|
try {
|
|
|
idx = parseInt(aes.decryptUid(actId), 10) - 1;
|
|
|
} catch (err) {
|
|
|
return {
|
|
|
code: 203,
|
|
|
message: '无活动信息'
|
|
|
};
|
|
|
}
|
|
|
|
|
|
if (idx) {
|
|
|
return this.client.lrangeAsync(TABLE_NAME, idx, idx).then(act => {
|
|
|
return JSON.parse(act[0]);
|
|
|
});
|
|
|
} else {
|
|
|
return {
|
|
|
code: 203,
|
|
|
message: '无活动信息'
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 每日签到
|
|
|
* @param data
|
|
|
* @returns {Promise.<void>}
|
|
|
*/
|
|
|
async checkInNormal(data) {
|
|
|
if (data.uid && data.actId) {
|
|
|
let actInfo = await this.getActInfo(data.actId);
|
|
|
|
|
|
if (actInfo.code && actInfo.code === 203) { // 无活动信息,返回
|
|
|
return actInfo;
|
|
|
}
|
|
|
let now = new Date(); // 今天的日期用于判断
|
|
|
|
|
|
if (now.getTime() < parseInt(actInfo.startTime, 10) * 1000) {
|
|
|
return {
|
|
|
code: 203,
|
|
|
message: '活动未开始'
|
|
|
};
|
|
|
}
|
|
|
|
|
|
if (now.getTime() > parseInt(actInfo.endTime, 10) * 1000) {
|
|
|
return {
|
|
|
code: 203,
|
|
|
message: '活动已结束'
|
|
|
};
|
|
|
}
|
|
|
|
|
|
let normalWheelId = actInfo.normalWheelId;
|
|
|
let checkInKey = `${PARENT_TABLE_NAME}:${actInfo.id}:user:${data.uid}:checkin`;
|
|
|
|
|
|
let params = {
|
|
|
act_id: normalWheelId,
|
|
|
uid: data.uid,
|
|
|
sessionKey: data.sessionKey,
|
|
|
appVersion: data.appVersion,
|
|
|
appSessionType: data.appSessionType
|
|
|
};
|
|
|
let getUserCheckInData = await this.client.zrevrangeAsync(checkInKey, 0, 0); // 查询用户最近一次签到信息
|
|
|
let isChecked = false;
|
|
|
|
|
|
if (getUserCheckInData && getUserCheckInData.length > 0) { // 如果有签到信息,判断是否是今日签到的
|
|
|
let checkInData = JSON.parse(getUserCheckInData[0]);
|
|
|
let date = new Date(checkInData.createTime);
|
|
|
|
|
|
if ((new Date(date.toLocaleDateString())).getTime() >= (new Date(now.toLocaleDateString())).getTime()) {
|
|
|
isChecked = true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (!isChecked) { // 没有签到的情况下,签到
|
|
|
return this.wheelSurfModel.goPrize(params).then(prize => {
|
|
|
// console.log(prize);
|
|
|
if (prize.name && prize.type) { // 从大转盘接口返回的奖品信息
|
|
|
|
|
|
let score = (new Date()).getTime();
|
|
|
let checkInData = {
|
|
|
actId: data.actId,
|
|
|
wheelId: normalWheelId,
|
|
|
prizeId: prize.id,
|
|
|
prizeImg: prize.img,
|
|
|
prizeDesc: prize.desc,
|
|
|
prizeType: prize.type,
|
|
|
createTime: score
|
|
|
};
|
|
|
|
|
|
// console.log('checkInData=', checkInData);
|
|
|
|
|
|
return this.client.zaddAsync(checkInKey, score, JSON.stringify(checkInData)).then(() => {
|
|
|
return Object.assign({}, {code: 200}, checkInData);
|
|
|
});
|
|
|
} else {
|
|
|
return {
|
|
|
code: 205,
|
|
|
message: '未中奖',
|
|
|
data: prize
|
|
|
};
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
|
return {
|
|
|
code: 201,
|
|
|
message: '今天已经签到过咯~'
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 对于实物中奖的用户,保存其中奖信息
|
|
|
* @param data
|
|
|
* @returns {Promise.<void>}
|
|
|
*/
|
|
|
async saveUserInfo(data) {
|
|
|
if (data.uid && data.actId) {
|
|
|
let actId;
|
|
|
|
|
|
try {
|
|
|
actId = parseInt(aes.decryptUid(data.actId), 10);
|
|
|
} catch (err) {
|
|
|
return {
|
|
|
code: 203,
|
|
|
message: '无活动信息'
|
|
|
};
|
|
|
}
|
|
|
let userPhoneKey = `${PARENT_TABLE_NAME}:${actId}:user:${data.uid}:info`;
|
|
|
let allPrizeKey = `${PARENT_TABLE_NAME}:${actId}:prizelist`;
|
|
|
let userCumulativeData = await this.getUserCumulativeData({uid: data.uid, actId: data.actId});
|
|
|
|
|
|
|
|
|
if (userCumulativeData.cumPrize.length > 0) {
|
|
|
let userInfo = await this.getUserInfo(data);
|
|
|
|
|
|
if (!userInfo.hasPhoneNum) {
|
|
|
let hasRealPrize = false; // 是否存在实物奖品
|
|
|
let realPrizeArr = []; // 临时存储中到的实物奖品
|
|
|
|
|
|
for (let i = 0; i < userCumulativeData.cumPrize.length; i++) {
|
|
|
|
|
|
let prize = userCumulativeData.cumPrize[i];
|
|
|
|
|
|
if (prize.prizeType === 4) {
|
|
|
hasRealPrize = true;
|
|
|
realPrizeArr.push(prize);
|
|
|
}
|
|
|
}
|
|
|
// console.log('real:', realPrizeArr)
|
|
|
|
|
|
if (hasRealPrize) { // 有实物奖品的情况下
|
|
|
let score = (new Date()).getTime();
|
|
|
let phoneData = {
|
|
|
phone: data.phone.toString(),
|
|
|
nickname: data.nickname,
|
|
|
uid: data.uid
|
|
|
};
|
|
|
|
|
|
return this.client.zaddAsync(userPhoneKey, score, JSON.stringify(phoneData)).then(async() => { // 往用户信息表添加信息
|
|
|
let finalResult = [];
|
|
|
|
|
|
for (let j = 0; j < realPrizeArr.length; j++) {
|
|
|
let prize = realPrizeArr[j];
|
|
|
let prizeData = {
|
|
|
id: parseInt(phoneData.uid, 10).toString(16), // 隐藏一下真实uid
|
|
|
nickname: phoneData.nickname,
|
|
|
daysCount: prize.daysCount,
|
|
|
phone: phoneData.phone.substring(0, 3) + '****' + phoneData.phone.substring(7, 11),
|
|
|
prizeName: prize.prizeDesc.replace('恭喜您获得', '')
|
|
|
};
|
|
|
|
|
|
let result = await this.client.zaddAsync(allPrizeKey, score, JSON.stringify(prizeData)); // 往中奖公布列表添加数据
|
|
|
|
|
|
finalResult.push(result);
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
code: 200,
|
|
|
data: finalResult,
|
|
|
message: '保存成功'
|
|
|
};
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
return {
|
|
|
code: 204,
|
|
|
message: '无实物奖品中奖信息'
|
|
|
};
|
|
|
}
|
|
|
} else {
|
|
|
return {
|
|
|
code: 203,
|
|
|
message: '您已填写过手机号,请勿重复提交'
|
|
|
};
|
|
|
}
|
|
|
} else {
|
|
|
return {
|
|
|
code: 204,
|
|
|
message: '无中奖信息'
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取用户是否填写过手机号信息
|
|
|
* @param data
|
|
|
* @returns {Promise.<void>}
|
|
|
*/
|
|
|
async getUserInfo(data) {
|
|
|
if (data.uid && data.actId) {
|
|
|
let actId;
|
|
|
|
|
|
try {
|
|
|
actId = parseInt(aes.decryptUid(data.actId), 10);
|
|
|
} catch (err) {
|
|
|
return {
|
|
|
code: 203,
|
|
|
message: '无活动信息'
|
|
|
};
|
|
|
}
|
|
|
let userPhoneKey = `${PARENT_TABLE_NAME}:${actId}:user:${data.uid}:info`;
|
|
|
let userInfo = await this.client.zrangeAsync(userPhoneKey, 0, 0);
|
|
|
|
|
|
if (userInfo && userInfo.length > 0) {
|
|
|
return {
|
|
|
code: 200,
|
|
|
hasPhoneNum: 1
|
|
|
};
|
|
|
} else {
|
|
|
return {
|
|
|
code: 203,
|
|
|
hasPhoneNum: 0
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取此次活动所有获得实物奖品的人员列表
|
|
|
* @param data
|
|
|
* @returns {Promise.<void>}
|
|
|
*/
|
|
|
async getWinnerList(data) {
|
|
|
if (data.actId) {
|
|
|
let actId;
|
|
|
|
|
|
try {
|
|
|
actId = parseInt(aes.decryptUid(data.actId), 10);
|
|
|
} catch (err) {
|
|
|
return {
|
|
|
code: 203,
|
|
|
message: '无活动信息'
|
|
|
};
|
|
|
}
|
|
|
|
|
|
let prizeListKey = `${PARENT_TABLE_NAME}:${actId}:prizelist`;
|
|
|
|
|
|
return this.client.zrevrangeAsync(prizeListKey, 0, -1).then(result => {
|
|
|
return result.map(ret => {
|
|
|
return JSON.parse(ret);
|
|
|
});
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 累计签到
|
|
|
* @param data
|
|
|
* @returns {Promise.<void>}
|
|
|
*/
|
|
|
async checkInCumulative(data) {
|
|
|
if (data.uid && data.actId) {
|
|
|
let actInfo = await this.getActInfo(data.actId);
|
|
|
let userCumulativeData = await this.getUserCumulativeData({uid: data.uid, actId: data.actId});
|
|
|
|
|
|
if (actInfo.code && actInfo.code === 203) { // 无活动信息,返回
|
|
|
return actInfo;
|
|
|
}
|
|
|
|
|
|
|
|
|
if (actInfo.prizeDayArr && actInfo.prizeDayArr.length > 0) {
|
|
|
let totalDays = userCumulativeData.cumDays.length; // 累计已签到天数
|
|
|
let prizeKey = `${PARENT_TABLE_NAME}:${actInfo.id}:user:${data.uid}:prize`;
|
|
|
|
|
|
if (totalDays > 0) {
|
|
|
for (let i = 0; i < actInfo.prizeDayArr.length; i++) {
|
|
|
let day = actInfo.prizeDayArr[i]; // 配置的累计天数信息
|
|
|
let daysCount = parseInt(day.count, 10); // 当前循环中累计天数的数量
|
|
|
let wheelId = day.wheelId; // 累计天数信息中配置的大转盘id
|
|
|
let params = {
|
|
|
act_id: wheelId,
|
|
|
uid: data.uid,
|
|
|
sessionKey: data.sessionKey,
|
|
|
appVersion: data.appVersion,
|
|
|
appSessionType: data.appSessionType
|
|
|
};
|
|
|
|
|
|
|
|
|
// console.log('totalDays:', totalDays, '&daysCount:', daysCount, '&day:', day);
|
|
|
// console.log('userCumulativeData:', userCumulativeData.cumPrize);
|
|
|
|
|
|
if (totalDays >= daysCount) { // 比较累计签到的天数是否大于活动设定的累计签到天数
|
|
|
if (userCumulativeData.cumPrize.length > 0) { // 如果获得过签到奖品,则判断是否是已领取过对应的累计奖励
|
|
|
let isGet = false;
|
|
|
|
|
|
for (let j = 0; j < userCumulativeData.cumPrize.length; j++) {
|
|
|
let prizeGet = userCumulativeData.cumPrize[j];
|
|
|
|
|
|
if (parseInt(prizeGet.daysCount, 10) === daysCount) { // 如果获得的累计奖品的天数和当前循环的累计天数相同
|
|
|
isGet = true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (!isGet) {
|
|
|
return this.wheelSurfModel.goPrize(params).then(prize => { // 调用对应的大转盘获取奖品返回
|
|
|
// console.log('prize_' + totalDays + '_' + daysCount, prize);
|
|
|
if (prize.name && prize.type) {
|
|
|
let score = (new Date()).getTime();
|
|
|
let checkInData = {
|
|
|
actId: data.actId,
|
|
|
wheelId: wheelId,
|
|
|
prizeId: prize.id,
|
|
|
prizeImg: prize.img,
|
|
|
prizeDesc: prize.desc,
|
|
|
prizeType: prize.type,
|
|
|
daysCount: daysCount,
|
|
|
createTime: score
|
|
|
};
|
|
|
|
|
|
return this.client.zaddAsync(prizeKey, score, JSON.stringify(checkInData)).then(() => {
|
|
|
return Object.assign({}, {code: 200}, checkInData);
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
} else { // 如果没有获得过累计签到的奖品
|
|
|
return this.wheelSurfModel.goPrize(params).then(prize => { // 调用对应的大转盘获取奖品返回
|
|
|
// console.log('prize:', prize);
|
|
|
if (prize.name && prize.type) {
|
|
|
let score = (new Date()).getTime();
|
|
|
let checkInData = {
|
|
|
actId: data.actId,
|
|
|
wheelId: wheelId,
|
|
|
prizeId: prize.id,
|
|
|
prizeImg: prize.img,
|
|
|
prizeDesc: prize.desc,
|
|
|
prizeType: prize.type,
|
|
|
daysCount: daysCount,
|
|
|
createTime: score
|
|
|
};
|
|
|
|
|
|
return this.client.zaddAsync(prizeKey, score, JSON.stringify(checkInData)).then(() => {
|
|
|
return Object.assign({}, {code: 200}, checkInData);
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
} else {
|
|
|
return {
|
|
|
code: 202,
|
|
|
message: '未满足累计天数要求'
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 循环内全部未返回,则默认返回
|
|
|
return {
|
|
|
code: 202,
|
|
|
message: '未满足累计天数要求'
|
|
|
};
|
|
|
} else { // 累计签到天数为0返回
|
|
|
return {
|
|
|
code: 202,
|
|
|
message: '未满足累计天数要求'
|
|
|
};
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
return {
|
|
|
code: 201,
|
|
|
message: '无累计抽奖可用'
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取签到累计天数
|
|
|
* @param data
|
|
|
* @returns {Promise.<{cumDays: *, cumGeted: (*|Array)}>}
|
|
|
*/
|
|
|
async getUserCumulativeData(data) {
|
|
|
if (data.uid && data.actId) {
|
|
|
let actId = 0;
|
|
|
|
|
|
try {
|
|
|
actId = parseInt(aes.decryptUid(data.actId), 10);
|
|
|
} catch (err) {
|
|
|
return {
|
|
|
code: 203,
|
|
|
message: '无活动信息'
|
|
|
};
|
|
|
}
|
|
|
|
|
|
if (actId) {
|
|
|
let checkInKey = `${PARENT_TABLE_NAME}:${actId}:user:${data.uid}:checkin`;
|
|
|
let cumPrizeKey = `${PARENT_TABLE_NAME}:${actId}:user:${data.uid}:prize`;
|
|
|
|
|
|
let cumDays = await this.client.zrangeAsync(checkInKey, 0, -1); // 已签到天数
|
|
|
let cumPrizeGeted = await this.client.zrangeAsync(cumPrizeKey, 0, -1); // 已获得的累计签到奖励
|
|
|
let cumDaysResult = [];
|
|
|
let cumPrizeResult = [];
|
|
|
|
|
|
cumDays.map(day => {
|
|
|
cumDaysResult.push(JSON.parse(day));
|
|
|
});
|
|
|
|
|
|
cumPrizeGeted.map(prize => {
|
|
|
cumPrizeResult.push(JSON.parse(prize));
|
|
|
});
|
|
|
|
|
|
return {
|
|
|
code: 200,
|
|
|
cumDays: cumDaysResult,
|
|
|
cumPrize: cumPrizeResult
|
|
|
};
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
module.exports = ActDailyCheckIn; |
|
|
module.exports = DailyCheckInModel; |
...
|
...
|
|