Authored by 沈志敏

重构领券中心 代码

... ... @@ -22,8 +22,17 @@ exports.index = (req, res, next) => {
}).catch(next);
};
exports.getCouponStatus = (req, res, next) => {
couponModel.getCouponStatus({
uid: req.user.uid,
contentCode: req.query.contentCode
}).then(result => {
res.json(result);
}).catch(next);
};
exports.sendcoupon = (req, res, next) => {
couponModel.sendcoupon(req.query.id, req.user.uid).then(result => {
res.json(result);
}).catch(next);
};
};
\ No newline at end of file
... ...
... ... @@ -64,27 +64,64 @@ exports.getCouponData = (channel, params) => {
};
val.data.forEach(function(item) {
const cou = {
obj.coupons.push({
id: crypto.encryption('yoho9646abcdefgh', item.couponID), // 加密优惠券号
img: helpers.image(item.image.src, 0, 0), // 优惠券图片
url: item.image.url.replace('http:', '') // 去逛逛链接
};
});
});
// 领取状态判断
switch (item.status) {
case 3:
cou.got = true; // 优惠券已领取
break;
case 2:
result.categories.push(obj);
i++;
}
});
}
while (false);
return result;
})();
};
exports.getCouponStatus = (params) => {
return Promise.coroutine(function*() {
const coupon = yield api.get('', Object.assign(params, {
method: 'app.promotion.queryCouponCenter'
}));
const result = {
code: coupon.code,
categories: []
};
do {
if (!coupon.data || !Array.isArray(coupon.data) || coupon.data.length === 0) {
break;
}
let i = 0;
coupon.data.forEach(function(val, index) {
if (val.template_name === 'getCoupon') {
// 优惠券楼层
if (!coupon.data[index - 1].data || !coupon.data[index - 1].data.text) {
return;
}
val.data.forEach(function(item) {
const status = Number(item.status);
if ([2, 3].indexOf(status) > -1) {
const cou = {
id: crypto.encryption('yoho9646abcdefgh', item.couponID) // 加密优惠券号
};
if (status === 2) {
cou.empty = true; // 优惠券已抢光
break;
default:
cou.normal = true; // 可领取优惠券
} else if (status === 3) {
cou.got = true; // 优惠券已领取
}
result.categories.push(cou);
}
obj.coupons.push(cou);
});
result.categories.push(obj);
i++;
}
});
... ...
... ... @@ -19,6 +19,7 @@ router.get(/^\/special\/(\d+)_(.*)\.html$/, specialController.special);
// 领券中心
router.get('/coupon/index', coupon.index);
router.get('/coupon/couponstatus', coupon.getCouponStatus);
router.get('/coupon/sendcoupon', auth, coupon.sendcoupon);
module.exports = router;
\ No newline at end of file
... ...
... ... @@ -7,39 +7,24 @@
</div>
{{# coupons}}
<div class="coupon">
<a {{#if normal}}
class="enable"
{{/if}}
href="{{url}}"
target="_blank"
href="javascript:void(0);"
data-id="{{id}}">
<a href="{{url}}" target="_blank" href="javascript:void(0);" data-id="{{id}}">
<img src="{{img}}">
{{#if empty}}
<div class="coupon-mask"></div>
{{/if}}
<div class="info">
{{#if normal}}
<div class="enable info" id="{{id}}">
<div class="normal">
<p>点击</p>
<p>领取</p>
</div>
{{/if}}
{{#if got}}
<div class="got">
<div class="got hidden">
<p>已领取</p>
<p class="guang">去使用</p>
</div>
{{/if}}
{{#if empty}}
<div class="empty">
<div class="empty hidden">
<p>已抢光</p>
<p class="guang">去逛逛</p>
</div>
{{/if}}
</div>
</a>
</div>
... ...
... ... @@ -162,8 +162,61 @@ function requestCoupon(id) {
});
}
$('.enable .info').on('click', function(e) {
e.preventDefault();
requestCoupon($(this).closest('a').data('id'));
redirect.gunangSrc = $(this).closest('a').get(0).href;
function getCouponStatus() {
var hash,
data = {},
search = window.location.search,
hashes = search ? decodeURIComponent(search).slice(1).split('&') : [];
for (i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
data[hash[0]] = hash[1];
}
if (!data.contentCode) {
return;
}
$.ajax({
type: 'GET',
url: '/coupon/couponstatus',
data: data,
success: function(res) {
if (res.code === 200) {
var cates = res.categories || [];
cates.forEach(function(obj) {
var e = document.getElementById(obj.id);
var child = e.children;
if (!child.length) {
return;
}
e.classList.remove('enable');
for (var i = 0; i < child.length; i++) {
if (child[i].className === 'normal') {
child[i].classList.add('hidden');
}
if ((obj.got && child[i].className.indexOf('got') > -1) || (obj.empty && child[i].className.indexOf('empty') > -1)) {
child[i].classList.remove('hidden');
}
}
})
}
}
});
}
// 获取领券状态
getCouponStatus();
$('.info').on('click', function(e) {
if (this.className.indexOf('enable') > -1) {
e.preventDefault();
requestCoupon($(this).closest('a').data('id'));
redirect.gunangSrc = $(this).closest('a').get(0).href;
}
});
\ No newline at end of file
... ...
... ... @@ -136,6 +136,10 @@
margin-top: -19px;
}
> .hidden {
display: none;
}
p {
margin-bottom: 5px;
}
... ...