ソースを参照

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

Junling Bu 7 年 前
コミット
d3ccabbf81

+ 2 - 0
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/util/WxResponseCode.java

@@ -31,5 +31,7 @@ public class WxResponseCode {
 
     public static final int COUPON_EXCEED_LIMIT = 740;
     public static final int COUPON_RECEIVE_FAIL= 741;
+    public static final int COUPON_CODE_INVALID= 742;
+
 
 }

+ 78 - 4
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxCouponController.java

@@ -21,6 +21,7 @@ import org.springframework.web.bind.annotation.*;
 
 import javax.validation.constraints.NotNull;
 import java.math.BigDecimal;
+import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -113,14 +114,14 @@ public class WxCouponController {
             couponVo.setMin(coupon.getMin().toPlainString());
             couponVo.setDiscount(coupon.getDiscount().toPlainString());
 
-            Short days = coupon.getDays();
-            if (days == 0) {
+            Short timeType = coupon.getTimeType();
+            if (timeType.equals(CouponConstant.TIME_TYPE_TIME)) {
                 couponVo.setStartTime(coupon.getStartTime());
                 couponVo.setEndTime(coupon.getEndTime());
             }
             else{
                 couponVo.setStartTime(coupon.getAddTime());
-                couponVo.setEndTime(coupon.getAddTime().plusDays(days));
+                couponVo.setEndTime(coupon.getAddTime().plusDays(coupon.getDays()));
             }
             couponVoList.add(couponVo);
         }
@@ -231,6 +232,9 @@ public class WxCouponController {
         if(type.equals(CouponConstant.TYPE_REGISTER)){
             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)){
             return ResponseUtil.fail(WxResponseCode.COUPON_RECEIVE_FAIL, "优惠券类型不支持");
         }
@@ -240,7 +244,77 @@ public class WxCouponController {
         if(status.equals(CouponConstant.STATUS_OUT)){
             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, "优惠券已经过期");
         }
 

+ 1 - 0
litemall-wx/config/api.js

@@ -93,6 +93,7 @@ module.exports = {
   CouponMyList: WxApiRoot + 'coupon/mylist', //我的优惠券列表
   CouponSelectList: WxApiRoot + 'coupon/selectlist', //当前订单可用优惠券列表
   CouponReceive: WxApiRoot + 'coupon/receive', //优惠券领取
+  CouponExchange: WxApiRoot + 'coupon/exchange', //优惠券兑换
 
   StorageUpload: WxApiRoot + 'storage/upload', //图片上传,
 

+ 30 - 16
litemall-wx/pages/ucenter/couponList/couponList.js

@@ -6,6 +6,7 @@ var app = getApp();
 Page({
   data: {
     couponList: [],
+    code: '',
     status: 0,
     page: 1,
     size: 10,
@@ -78,13 +79,6 @@ Page({
       showPage: false,
       couponList: []
     });
-    // 页面渲染完成
-    wx.showToast({
-      title: '加载中...',
-      icon: 'loading',
-      duration: 2000
-    });
-
     util.request(api.CouponMyList, {
       status: that.data.status,
       page: that.data.page,
@@ -99,20 +93,40 @@ Page({
           count: res.data.count
         });
       }
-      wx.hideToast();
     });
 
   },
-  goExchange: function() {
-    wx.showToast({
-      title: '目前不支持',
-      icon: 'none',
-      duration: 2000
+  bindExchange: function (e) {
+    this.setData({
+      code: e.detail.value
+    });
+  },
+  clearExchange: function () {
+    this.setData({
+      code: ''
     });
   },
-  goUse: function() {
-    wx.reLaunch({
-      url: '/pages/index/index'
+  goExchange: function() {
+    if (this.data.code.length === 0) {
+      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) {

+ 2 - 2
litemall-wx/pages/ucenter/couponList/couponList.wxml

@@ -16,8 +16,8 @@
 
     <view class="coupon-form" wx:if="{{status == 0}}">
       <view class="input-box">
-        <input class="coupon-sn" placeholder="请输入优惠码" />
-        <image class="clear-icon" src="http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/icon-normal/clear-fb-dd9d604f86.png"></image>
+        <input class="coupon-sn" placeholder="请输入优惠码" value="{{code}}" bindinput="bindExchange"/>
+        <image class="clear-icon" wx:if="{{ code.length > 0 }}" src="/static/images/clear_input.png" catchtap="clearExchange"></image>
       </view>
       <view class="add-btn" bindtap='goExchange'>兑换</view>
     </view>

+ 7 - 4
litemall-wx/pages/ucenter/couponList/couponList.wxss

@@ -84,10 +84,13 @@ page {
 
 .container .b .clear-icon {
   position: absolute;
-  top: 21rpx;
-  right: 30rpx;
-  width: 28rpx;
-  height: 28rpx;
+  top: 10rpx;
+  right: 18rpx;
+  z-index: 2;
+  display: block;
+  background: #fff;
+  height: 44rpx;
+  width: 44rpx;
 }
 
 .container .b .add-btn {

+ 5 - 4
litemall-wx/project.config.json

@@ -29,7 +29,7 @@
 			"list": []
 		},
 		"miniprogram": {
-			"current": 37,
+			"current": 22,
 			"list": [
 				{
 					"id": -1,
@@ -164,9 +164,9 @@
 					"query": "orderId=2&type=0&valueId=1116011"
 				},
 				{
-					"id": -1,
+					"id": 22,
 					"name": "我的优惠券",
-					"pathName": "pages/ucenter/coupon/coupon",
+					"pathName": "pages/ucenter/couponList/couponList",
 					"query": ""
 				},
 				{
@@ -256,7 +256,8 @@
 				{
 					"id": -1,
 					"name": "优惠券列表",
-					"pathName": "pages/coupon/coupon"
+					"pathName": "pages/coupon/coupon",
+					"query": ""
 				}
 			]
 		}