sweet/assets/scriptes/cloudData.js
2020-08-07 09:55:56 +08:00

88 lines
2.3 KiB
JavaScript

var storageUtil = require("storageutils");
let stringUtil = require('stringUtil');
const REDPACK_DAY_COUNT = 'redpack_day_count';
const REDPACK_INFO = 'redpack_info';
const DAY_GIFT_TAG = 'day_gift_tag';
// 每日已获取红包
let dayCount = -1;
// 已获得红包信息
let packInfo;
module.exports = {
// 从存档获取当日已获得红包数量
redpackTodayCount: function (cb, fcb) {
if (dayCount > -1) {
return cb(dayCount);
}
storageUtil.getStorage(REDPACK_DAY_COUNT, (res) => {
dayCount = res ? parseInt(res) : 0;
cb && cb(dayCount);
}, () => {
console.log('error get user readpack today count');
fcb && fcb()
});
},
increaseTodayCount() {
(dayCount === -1) || (dayCount = 0);
dayCount += 1;
storageUtil.setStorage(REDPACK_DAY_COUNT, dayCount, function () {
console.log('success update user readpack today count', dayCount)
}, function () {
console.log('error update user readpack today count', dayCount)
}, 1)
},
// 获取已获得红包信息
redpackTotal: function (cb, fcb) {
if (packInfo) {
return cb(packInfo);
}
storageUtil.getStorage(REDPACK_INFO, (res) => {
if (res) {
try {
packInfo = JSON.parse(res);
} catch (err) {
}
}
cb && cb(packInfo);
}, () => {
console.log('error get user readpack info');
fcb && fcb()
});
},
// 更新用户红包信息
updateRedpackInfo({count, money}) {
packInfo = {count, money};
storageUtil.setStorage(REDPACK_INFO, JSON.stringify(packInfo), function () {
console.log('success update user readpack info', packInfo)
}, function () {
console.log('error update user readpack info', packInfo)
})
},
// 更新每日道具奖励状态
updateDayGiftStatus(cb) {
storageUtil.setStorage(DAY_GIFT_TAG, 1, () => {
cb && cb(true);
}, () => {
console.log('error get user readpack today count');
cb && cb(false);
}, 1, 0);
},
// 获取每日道具奖励状态
getDayGiftStatus(cb) {
// if (CC_DEBUG) {
// cb && cb(false);
// } else
{
storageUtil.getStorage(DAY_GIFT_TAG, (res) => {
cb && cb(stringUtil.isTrue(res));
}, () => {
cb(true);
})
}
}
}