Authored by zhangwenxue

feat(feature/couponSend): add request restriction to UDID

... ... @@ -2,6 +2,7 @@
const model = require('../models/feature');
const _ = require('lodash');
const stringProcess = require('../../../utils/string-process');
const { cache, logger} = global.yoho;
exports.index = function(req, res, next) {
let qcdn = _.get(req.app.locals, 'wap.qcloud_cdn');
... ... @@ -67,6 +68,7 @@ exports.bottombar = function(req, res, next) {
}).catch(next);
};
const DefaultCouponExpireSeconds = 3600 * 24 * 7; // 7d
/**
* 获取优惠券
... ... @@ -103,18 +105,46 @@ exports.couponSend = (req, res, next) => {
if (uid === '' || uid === 0 || token === '' || token === 0) {
return res.jsonp(resultData);
}
const promise = coupontype === 'ufo' ?
req.ctx(model).couponSendUFO(uid, token) :
req.ctx(model).couponSend(uid, token);
promise.then(result => {
res.set({
'Cache-Control': 'no-cache',
Pragma: 'no-cache',
Expires: (new Date(1900, 0, 1, 0, 0, 0, 0)).toUTCString()
});
res.jsonp(result);
}).catch(next);
// 一个token只能领一次
const udid = req.cookies.udid || uid;
const redisCacheKey = `af_couponSend-${udid}-${coupontype}-${token}`;
cache.get(redisCacheKey).then(ret => {
if (ret === null) {
const promise = coupontype === 'ufo' ?
req.ctx(model).couponSendUFO(uid, token) :
req.ctx(model).couponSend(uid, token);
return promise.then(result => {
if (result && (result.code === 200 || result.code === 401)) { // 如果领取成功(200),或已经通过其它途经领取(401)
return cache.set(redisCacheKey, Date.now(), DefaultCouponExpireSeconds)
.then(() => {
return result;
});
}
return result;
}).then(result => {
res.set({
'Cache-Control': 'no-cache',
Pragma: 'no-cache',
Expires: (new Date(1900, 0, 1, 0, 0, 0, 0)).toUTCString()
});
res.jsonp(result);
});
} else {
// 已领或在领取中
res.jsonp({
code: 401,
message: '优惠券已经领取',
data: ''
});
}
}).catch(err => {
logger.error('couponSend %s, %j', redisCacheKey, err);
next(err);
});
};
exports.redenvelope = (req, res, next) => {
... ...