Commit d3ccabbf authored by Junling Bu's avatar Junling Bu
Browse files

feat[litemall-wx, litemall-wx-api]: 小商场前端支持兑换码优惠券

parent cd24f3d6
...@@ -31,5 +31,7 @@ public class WxResponseCode { ...@@ -31,5 +31,7 @@ public class WxResponseCode {
public static final int COUPON_EXCEED_LIMIT = 740; public static final int COUPON_EXCEED_LIMIT = 740;
public static final int COUPON_RECEIVE_FAIL= 741; public static final int COUPON_RECEIVE_FAIL= 741;
public static final int COUPON_CODE_INVALID= 742;
} }
...@@ -21,6 +21,7 @@ import org.springframework.web.bind.annotation.*; ...@@ -21,6 +21,7 @@ import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -113,14 +114,14 @@ public class WxCouponController { ...@@ -113,14 +114,14 @@ public class WxCouponController {
couponVo.setMin(coupon.getMin().toPlainString()); couponVo.setMin(coupon.getMin().toPlainString());
couponVo.setDiscount(coupon.getDiscount().toPlainString()); couponVo.setDiscount(coupon.getDiscount().toPlainString());
Short days = coupon.getDays(); Short timeType = coupon.getTimeType();
if (days == 0) { if (timeType.equals(CouponConstant.TIME_TYPE_TIME)) {
couponVo.setStartTime(coupon.getStartTime()); couponVo.setStartTime(coupon.getStartTime());
couponVo.setEndTime(coupon.getEndTime()); couponVo.setEndTime(coupon.getEndTime());
} }
else{ else{
couponVo.setStartTime(coupon.getAddTime()); couponVo.setStartTime(coupon.getAddTime());
couponVo.setEndTime(coupon.getAddTime().plusDays(days)); couponVo.setEndTime(coupon.getAddTime().plusDays(coupon.getDays()));
} }
couponVoList.add(couponVo); couponVoList.add(couponVo);
} }
...@@ -231,6 +232,9 @@ public class WxCouponController { ...@@ -231,6 +232,9 @@ public class WxCouponController {
if(type.equals(CouponConstant.TYPE_REGISTER)){ if(type.equals(CouponConstant.TYPE_REGISTER)){
return ResponseUtil.fail(WxResponseCode.COUPON_RECEIVE_FAIL, "新用户优惠券自动发送"); return ResponseUtil.fail(WxResponseCode.COUPON_RECEIVE_FAIL, "新用户优惠券自动发送");
} }
else if(type.equals(CouponConstant.TYPE_CODE)){
return ResponseUtil.fail(WxResponseCode.COUPON_RECEIVE_FAIL, "优惠券只能兑换");
}
else if(!type.equals(CouponConstant.TYPE_COMMON)){ else if(!type.equals(CouponConstant.TYPE_COMMON)){
return ResponseUtil.fail(WxResponseCode.COUPON_RECEIVE_FAIL, "优惠券类型不支持"); return ResponseUtil.fail(WxResponseCode.COUPON_RECEIVE_FAIL, "优惠券类型不支持");
} }
...@@ -240,7 +244,77 @@ public class WxCouponController { ...@@ -240,7 +244,77 @@ public class WxCouponController {
if(status.equals(CouponConstant.STATUS_OUT)){ if(status.equals(CouponConstant.STATUS_OUT)){
return ResponseUtil.fail(WxResponseCode.COUPON_EXCEED_LIMIT, "优惠券已领完"); return ResponseUtil.fail(WxResponseCode.COUPON_EXCEED_LIMIT, "优惠券已领完");
} }
if(status.equals(CouponConstant.STATUS_EXPIRED)){ else if(status.equals(CouponConstant.STATUS_EXPIRED)){
return ResponseUtil.fail(WxResponseCode.COUPON_RECEIVE_FAIL, "优惠券已经过期");
}
// 用户领券记录
LitemallCouponUser couponUser = new LitemallCouponUser();
couponUser.setCouponId(couponId);
couponUser.setUserId(userId);
couponUserService.add(couponUser);
return ResponseUtil.ok();
}
/**
* 优惠券兑换
*
* @param userId 用户ID
* @param body 请求内容, { code: xxx }
* @return 操作结果
*/
@PostMapping("exchange")
public Object exchange(@LoginUser Integer userId, @RequestBody String body) {
if (userId == null) {
return ResponseUtil.unlogin();
}
String code = JacksonUtil.parseString(body, "code");
if(code == null){
return ResponseUtil.badArgument();
}
LitemallCoupon coupon = couponService.findByCode(code);
if(coupon == null){
return ResponseUtil.fail(WxResponseCode.COUPON_CODE_INVALID, "优惠券不正确");
}
Integer couponId = coupon.getId();
// 当前已领取数量和总数量比较
Integer total = coupon.getTotal();
Integer totalCoupons = couponUserService.countCoupon(couponId);
if((total != 0) && (totalCoupons >= total)){
return ResponseUtil.fail(WxResponseCode.COUPON_EXCEED_LIMIT, "优惠券已兑换");
}
// 当前用户已领取数量和用户限领数量比较
Integer limit = coupon.getLimit().intValue();
Integer userCounpons = couponUserService.countUserAndCoupon(userId, couponId);
if((limit != 0) && (userCounpons >= limit)){
return ResponseUtil.fail(WxResponseCode.COUPON_EXCEED_LIMIT, "优惠券已兑换");
}
// 优惠券分发类型
// 例如注册赠券类型的优惠券不能领取
Short type = coupon.getType();
if(type.equals(CouponConstant.TYPE_REGISTER)){
return ResponseUtil.fail(WxResponseCode.COUPON_RECEIVE_FAIL, "新用户优惠券自动发送");
}
else if(type.equals(CouponConstant.TYPE_COMMON)){
return ResponseUtil.fail(WxResponseCode.COUPON_RECEIVE_FAIL, "优惠券只能领取,不能兑换");
}
else if(!type.equals(CouponConstant.TYPE_CODE)){
return ResponseUtil.fail(WxResponseCode.COUPON_RECEIVE_FAIL, "优惠券类型不支持");
}
// 优惠券状态,已下架或者过期不能领取
Short status = coupon.getStatus();
if(status.equals(CouponConstant.STATUS_OUT)){
return ResponseUtil.fail(WxResponseCode.COUPON_EXCEED_LIMIT, "优惠券已兑换");
}
else if(status.equals(CouponConstant.STATUS_EXPIRED)){
return ResponseUtil.fail(WxResponseCode.COUPON_RECEIVE_FAIL, "优惠券已经过期"); return ResponseUtil.fail(WxResponseCode.COUPON_RECEIVE_FAIL, "优惠券已经过期");
} }
......
...@@ -93,6 +93,7 @@ module.exports = { ...@@ -93,6 +93,7 @@ module.exports = {
CouponMyList: WxApiRoot + 'coupon/mylist', //我的优惠券列表 CouponMyList: WxApiRoot + 'coupon/mylist', //我的优惠券列表
CouponSelectList: WxApiRoot + 'coupon/selectlist', //当前订单可用优惠券列表 CouponSelectList: WxApiRoot + 'coupon/selectlist', //当前订单可用优惠券列表
CouponReceive: WxApiRoot + 'coupon/receive', //优惠券领取 CouponReceive: WxApiRoot + 'coupon/receive', //优惠券领取
CouponExchange: WxApiRoot + 'coupon/exchange', //优惠券兑换
StorageUpload: WxApiRoot + 'storage/upload', //图片上传, StorageUpload: WxApiRoot + 'storage/upload', //图片上传,
......
...@@ -6,6 +6,7 @@ var app = getApp(); ...@@ -6,6 +6,7 @@ var app = getApp();
Page({ Page({
data: { data: {
couponList: [], couponList: [],
code: '',
status: 0, status: 0,
page: 1, page: 1,
size: 10, size: 10,
...@@ -78,13 +79,6 @@ Page({ ...@@ -78,13 +79,6 @@ Page({
showPage: false, showPage: false,
couponList: [] couponList: []
}); });
// 页面渲染完成
wx.showToast({
title: '加载中...',
icon: 'loading',
duration: 2000
});
util.request(api.CouponMyList, { util.request(api.CouponMyList, {
status: that.data.status, status: that.data.status,
page: that.data.page, page: that.data.page,
...@@ -99,20 +93,40 @@ Page({ ...@@ -99,20 +93,40 @@ Page({
count: res.data.count count: res.data.count
}); });
} }
wx.hideToast();
}); });
}, },
goExchange: function() { bindExchange: function (e) {
wx.showToast({ this.setData({
title: '目前不支持', code: e.detail.value
icon: 'none', });
duration: 2000 },
clearExchange: function () {
this.setData({
code: ''
}); });
}, },
goUse: function() { goExchange: function() {
wx.reLaunch({ if (this.data.code.length === 0) {
url: '/pages/index/index' util.showErrorToast("请输入兑换码");
return;
}
let that = this;
util.request(api.CouponExchange, {
code: that.data.code
}, 'POST').then(function (res) {
if (res.errno === 0) {
that.getCouponList();
that.clearExchange();
wx.showToast({
title: "领取成功",
duration: 2000
})
}
else{
util.showErrorToast(res.errmsg);
}
}); });
}, },
nextPage: function(event) { nextPage: function(event) {
......
...@@ -16,8 +16,8 @@ ...@@ -16,8 +16,8 @@
<view class="coupon-form" wx:if="{{status == 0}}"> <view class="coupon-form" wx:if="{{status == 0}}">
<view class="input-box"> <view class="input-box">
<input class="coupon-sn" placeholder="请输入优惠码" /> <input class="coupon-sn" placeholder="请输入优惠码" value="{{code}}" bindinput="bindExchange"/>
<image class="clear-icon" src="http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/icon-normal/clear-fb-dd9d604f86.png"></image> <image class="clear-icon" wx:if="{{ code.length > 0 }}" src="/static/images/clear_input.png" catchtap="clearExchange"></image>
</view> </view>
<view class="add-btn" bindtap='goExchange'>兑换</view> <view class="add-btn" bindtap='goExchange'>兑换</view>
</view> </view>
......
...@@ -84,10 +84,13 @@ page { ...@@ -84,10 +84,13 @@ page {
.container .b .clear-icon { .container .b .clear-icon {
position: absolute; position: absolute;
top: 21rpx; top: 10rpx;
right: 30rpx; right: 18rpx;
width: 28rpx; z-index: 2;
height: 28rpx; display: block;
background: #fff;
height: 44rpx;
width: 44rpx;
} }
.container .b .add-btn { .container .b .add-btn {
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
"list": [] "list": []
}, },
"miniprogram": { "miniprogram": {
"current": 37, "current": 22,
"list": [ "list": [
{ {
"id": -1, "id": -1,
...@@ -164,9 +164,9 @@ ...@@ -164,9 +164,9 @@
"query": "orderId=2&type=0&valueId=1116011" "query": "orderId=2&type=0&valueId=1116011"
}, },
{ {
"id": -1, "id": 22,
"name": "我的优惠券", "name": "我的优惠券",
"pathName": "pages/ucenter/coupon/coupon", "pathName": "pages/ucenter/couponList/couponList",
"query": "" "query": ""
}, },
{ {
...@@ -256,7 +256,8 @@ ...@@ -256,7 +256,8 @@
{ {
"id": -1, "id": -1,
"name": "优惠券列表", "name": "优惠券列表",
"pathName": "pages/coupon/coupon" "pathName": "pages/coupon/coupon",
"query": ""
} }
] ]
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment