Authored by yyq

优惠码兑换

... ... @@ -51,6 +51,13 @@ const getCoupons = (req, res, next) => {
}).catch(next);
};
// 获取优惠券列表
const convertCoupons = (req, res, next) => {
oeModel.convertCoupons(req.user.uid, req.query.code).then(data => {
res.send(data);
}).catch(next);
};
// 订单金额计算
const compute = (req, res, next) => {
let params = req.body;
... ... @@ -165,6 +172,7 @@ const submit = (req, res, next) => {
module.exports = {
index,
getCoupons,
convertCoupons,
compute,
submit
};
... ...
... ... @@ -35,7 +35,19 @@ const getOrderPaymentAsync = (uid, cartType, yohoCoinMode) => api.get('', {
*/
const getUesrCouponAsync = (uid) => api.get('', {
method: 'app.Shopping.listCoupon',
uid: uid
uid: uid,
is_group_frees: 'Y'
});
/**
* 优惠码兑换优惠券API
* @param uid [number] uid
* @param code [number] code
*/
const getCouponByCodeAsync = (uid, code) => api.get('', {
method: 'app.Shopping.useCoupon',
uid: uid,
coupon_code: code
});
/**
... ... @@ -193,6 +205,7 @@ module.exports = {
getUserProfileAsync,
getOrderPaymentAsync,
getUesrCouponAsync,
getCouponByCodeAsync,
getOrderComputeAsync,
orderSubmitAsync
};
... ...
... ... @@ -52,6 +52,10 @@ const getCoupons = (uid) => ensureApi.getUesrCouponAsync(uid).then(result => {
return result;
});
// 兑换优惠券
const convertCoupons = (uid, code) => ensureApi.getCouponByCodeAsync(uid, code);
// 订单计算
const compute = (uid, cartType, pa) => {
return ensureApi.getOrderComputeAsync(uid, cartType, pa.paymentType, pa.deliveryWay, pa).then(result => {
... ... @@ -104,6 +108,7 @@ const submit = (uid, cartType, p, remoteIp) => {
module.exports = {
index,
getCoupons,
convertCoupons,
compute,
submit
};
... ...
... ... @@ -29,7 +29,8 @@ router.post('/address/save', address.saveAddress); // 新增地址/更新地址
router.post('/address/setdefault', address.setDefault); // 设置默认地址
router.get('/ensure', auth, ensure.index); // 限购商品快捷结算页
router.get('/ensure/coupons', auth, ensure.getCoupons); // 限购商品快捷结算页
router.get('/ensure/coupons', auth, ensure.getCoupons); // 结算优惠券列表
router.get('/ensure/couponcode', auth, ensure.convertCoupons); // 优惠码兑换券
router.post('/ensure/compute', auth, ensure.compute); // 价格重新计算
router.post('/ensure/submit', auth, ensure.submit); // 价格重新计算
... ...
... ... @@ -176,23 +176,21 @@
<dt id="use-coupons" class="use-coupons">
<span class="locker-switch"></span>使用优惠券/优惠码
<div class="tip-box right">
<p>优惠码不可与优惠券同时使用</p>
<p class="hide">优惠码不可与优惠券同时使用</p>
<p class="coupon-err-tip"></p>
</div>
</dt>
<dd>
<div class="coupon-opt-title">
<label class="radio-btn on">使用优惠券</label>
<label class="radio-btn code">使用优惠码</label>
<div class="use-code">
<input type="text" class="coupon-code" placeholder="输入优惠码">
<label class="sure-convert fw300">兑换</label>
</div>
<span class="ticket-use-tip hide">
已成功使用<em class="red num">1</em>张优惠券,<em class="red price"></em>
<b class="cancel-use-ticket">取消使用</b>
</span>
<div class="opt-area right">
<input type="text" class="coupon-code">
<label class="sure-use-code fw300">直接使用</label>
<label class="sure-use-ticket fw300">确定</label>
</div>
<p class="tip-box red right">说明:普通优惠券可以与运费券同时使用</p>
</div>
<div class="coupon-list-wrap"></div>
</dd>
... ...
... ... @@ -150,28 +150,28 @@
\{{#if emptyUsable}}
<p class="empty-tip">暂无可用优惠券~</p>
\{{^}}
\{{#if usable_frees_coupons}}
<ul class="usable-free-list">
<li class="useable-title">可用运费券</li>
\{{#each usable_frees_coupons}}
<li>
\{{#if usable_coupons}}
<ul class="usable-list">
<li class="useable-title">普通优惠券</li>
\{{#each usable_coupons}}
<li \{{#if using}}class="red"\{{/if}}>
<span class="title">\{{coupon_name}}</span>
<span class="time">\{{coupon_validity}}</span>
<span class="money">¥ \{{round coupon_value 2}}</span>
<span class="coupon-radio" data-id="\{{coupon_code}}" data-price="\{{round coupon_value 2}}"></span>
<span class="coupon-radio \{{#if using}}on\{{/if}}" data-id="\{{coupon_code}}" data-price="\{{round coupon_value 2}}"></span>
</li>
\{{/each}}
</ul>
\{{/if}}
\{{#if usable_coupons}}
<ul class="usable-list">
<li class="useable-title">可用优惠券</li>
\{{#each usable_coupons}}
<li>
\{{#if usable_frees_coupons}}
<ul class="usable-free-list">
<li class="useable-title">运费券</li>
\{{#each usable_frees_coupons}}
<li \{{#if using}}class="red"\{{/if}}>
<span class="title">\{{coupon_name}}</span>
<span class="time">\{{coupon_validity}}</span>
<span class="money">¥ \{{round coupon_value 2}}</span>
<span class="coupon-radio" data-id="\{{coupon_code}}" data-price="\{{round coupon_value 2}}"></span>
<span class="coupon-radio \{{#if using}}on\{{/if}}" data-id="\{{coupon_code}}" data-price="\{{round coupon_value 2}}"></span>
</li>
\{{/each}}
</ul>
... ...
... ... @@ -491,7 +491,7 @@ coupon = {
this.getList();
this.eventBind();
},
getList: function() {
getList: function(choose) {
var that = this;
$.ajax({
... ... @@ -499,57 +499,66 @@ coupon = {
url: '/cart/ensure/coupons'
}).then(function(data) {
if (data.code === 200) {
$('.coupon-list-wrap', that.$couponWrap).html(that.couponsTpl(data.data));
$('.coupon-list-wrap', that.$couponWrap).html(that.couponsTpl(that.selectUsedCoupon(data.data)));
if (choose) { // 兑换券后默认使用该券
$('.coupon-radio', that.$couponWrap).each(function() {
$(this).data('id') === choose && $(this).trigger('click');
});
}
}
});
},
eventBind: function() {
useCode: function(code) {
var that = this;
this.$couponWrap.on('click', '.radio-btn', function() {
var $this = $(this);
if ($this.hasClass('on')) {
return;
}
$this.siblings('.radio-btn.on').removeClass('on');
$this.addClass('on').parent().toggleClass('using-code');
if (!code) {
return;
}
if ($this.hasClass('code')) {
that.cancelTicketUse();
that.$couponList.slideUp();
$.ajax({
type: 'GET',
url: '/cart/ensure/couponcode',
data: {code: code}
}).then(function(data) {
if (data.code === 200) {
that.getList(data.data.coupon_code);
} else {
that.cancelCodeUse();
that.$couponList.slideDown();
that.$errorTip.text(data.message || '兑换失败,请稍后再试');
}
}).on('click', '.cancel-use-ticket', function() {
that.cancelTicketUse();
}).on('change', '.coupon-code', function() {
that.$errorTip.empty();
}).on('click', '.sure-use-ticket', function() {
that.close();
}).on('click', '.sure-use-code', function() {
var $this = $(this);
var couponId;
couponId = $this.siblings('.coupon-code').val();
});
},
selectUsedCoupon: function(data) {
var i;
if (couponId) {
order.couponCode = couponId;
if (order.couponCode) {
data.usable_coupons = data.usable_coupons || [];
data.usable_frees_coupons = data.usable_frees_coupons || [];
// 重新计算订单价格
compute(0, function(resData) {
if (resData.code === 200) {
that.close();
} else {
that.$errorTip.text(resData.message);
for (i = 0; i < data.usable_coupons.length; i++) {
if (order.couponCode.indexOf(data.usable_coupons[i].coupon_code) > -1) {
data.usable_coupons[i].using = true;
}
}
if (order.couponCode) {
delete order.couponCode;
}
}
});
for (i = 0; i < data.usable_frees_coupons.length; i++) {
if (order.couponCode.indexOf(data.usable_frees_coupons[i].coupon_code) > -1) {
data.usable_frees_coupons[i].using = true;
}
}
}
return data;
},
eventBind: function() {
var that = this;
this.$couponWrap.on('click', '.cancel-use-ticket', function() {
that.cancelTicketUse();
}).on('change', '.coupon-code', function() {
that.$errorTip.empty();
}).on('click', '.sure-convert', function() {
that.useCode($(this).siblings('.coupon-code').val());
}).on('click', '.coupon-radio', function() {
var $this = $(this),
$par = $this.parent();
... ...
... ... @@ -924,66 +924,50 @@
padding: 0 30px;
background-color: #f0f0f0;
.ticket-use-tip {
margin-left: 20px;
.num {
margin: 0 4px;
}
.red {
color: $red;
}
.cancel-use-ticket {
color: #1987cf;
margin-left: 20px;
cursor: pointer;
}
.red {
color: $red;
}
.opt-area {
line-height: 1;
margin-top: 13px;
border: 1px solid #000;
background-color: #fff;
.use-code {
display: inline-block;
> input {
width: 174px;
width: 180px;
line-height: 24px;
border: none;
border: 1px solid #e0e0e0;
padding: 0 10px;
margin-right: 18px;
font-size: 14px;
display: none;
box-sizing: border-box;
}
> label {
.sure-convert {
color: #fff;
background-color: #000;
width: 78px;
width: 60px;
height: 24px;
line-height: 24px;
text-align: center;
cursor: pointer;
display: none;
display: inline-block;
}
}
.sure-use-code {
float: right;
}
.ticket-use-tip {
margin-left: 20px;
.sure-use-ticket {
display: inline-block;
.num {
margin: 0 4px;
}
}
&.using-code .opt-area {
> * {
display: inline-block;
.red {
color: $red;
}
.sure-use-ticket {
display: none;
.cancel-use-ticket {
color: #1987cf;
margin-left: 20px;
cursor: pointer;
}
}
}
... ...