소스 검색

Merge remote-tracking branch 'origin/master'

bing 1 년 전
부모
커밋
1775624fc0

+ 5 - 1
ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/enums/BalanceUpdateEnum.java

@@ -29,7 +29,11 @@ public enum BalanceUpdateEnum {
     /**
      * 更新
      */
-    RECOVER(3,"更新");
+    RECOVER(3,"更新"),
+    /**
+     * 错扣补款
+     */
+    COMPENSATE(3,"补款");
 
     private final Integer code;
     private final String message;

+ 2 - 0
ruoyi-common/ruoyi-common-core/src/main/resources/i18n/messages_zh_CN.properties

@@ -59,3 +59,5 @@ tenant.number.not.blank=租户编号不能为空
 tenant.not.exists=对不起, 您的租户不存在,请联系管理员
 tenant.blocked=对不起,您的租户已禁用,请联系管理员
 tenant.expired=对不起,您的租户已过期,请联系管理员
+##钱包
+bag.balance.valid=账户余额校验失败

+ 26 - 17
ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/utils/EncryptUtils.java

@@ -5,8 +5,9 @@ import cn.hutool.core.util.ArrayUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.crypto.SecureUtil;
 import cn.hutool.crypto.SmUtil;
-import cn.hutool.crypto.asymmetric.*;
-import cn.hutool.crypto.digest.MD5;
+import cn.hutool.crypto.asymmetric.KeyType;
+import cn.hutool.crypto.asymmetric.RSA;
+import cn.hutool.crypto.asymmetric.SM2;
 import org.apache.commons.codec.digest.DigestUtils;
 
 import java.nio.charset.StandardCharsets;
@@ -116,6 +117,7 @@ public class EncryptUtils {
         if (StrUtil.isBlank(password)) {
             throw new IllegalArgumentException("DESede需要传入秘钥信息");
         }
+        byte[] temp = password.getBytes(StandardCharsets.UTF_8);
         return SecureUtil.desede(password.getBytes(StandardCharsets.UTF_8)).decryptStr(data, StandardCharsets.UTF_8);
     }
     /**
@@ -305,6 +307,20 @@ public class EncryptUtils {
         RSA rsa = SecureUtil.rsa(privateKey, null);
         return rsa.decryptStr(data, KeyType.PrivateKey, StandardCharsets.UTF_8);
     }
+    /**
+     * rsa私钥解密
+     *
+     * @param data       待加密数据
+     * @param publicKey 公钥
+     * @return 解密后字符串
+     */
+    public static String decryptByRsaPublicKey(String data, String publicKey) {
+        if (StrUtil.isBlank(publicKey)) {
+            throw new IllegalArgumentException("RSA需要传入公钥进行解密");
+        }
+        RSA rsa = SecureUtil.rsa(null, publicKey);
+        return rsa.decryptStr(data, KeyType.PublicKey, StandardCharsets.UTF_8);
+    }
 
     /**
      * md5加密
@@ -335,20 +351,13 @@ public class EncryptUtils {
     public static String encryptBySm3(String data) {
         return SmUtil.sm3(data);
     }
-
-    /**
-     * DESede(3Des解密)解密
-     *
-     * @param data     待解密数据
-     * @param password 秘钥字符串
-     * @return 解密后字符串
-     */
-    public static String encryptByDESede(String data, byte[] password) {
-        //if (password.length!=24) {
-        //    throw new IllegalArgumentException("DESede需要传入秘钥信息");
-        //}
-
-        return SecureUtil.desede(password).encryptHex(data, StandardCharsets.UTF_8);
+    public static byte[] hex(String key) {
+        String f = DigestUtils.md5Hex(key);
+        int enkLength = 24;
+        byte[] bKeys = f.getBytes();
+        byte[] enk = new byte[enkLength];
+        System.arraycopy(bKeys, 0, enk, 0, enkLength);
+        return enk;
+//        return new byte[1];
     }
-
 }

+ 24 - 5
ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/utils/YcEncryptUtil.java

@@ -130,35 +130,54 @@ public class YcEncryptUtil extends EncryptUtils {
     }
 
     /**
-     * 加密卡余
+     * 客户公钥加密卡余
      *
      * @param data      待加密余额
      * @param userId    待加密余额的用户Id
      * @param publicKey 客户公钥(对客户公钥进行Md5摘要计算后的16进制字符串)
      * @return 加密后的余额(16进制字符串)
      */
-    public static String encryptBagBalance(String data, String userId, String publicKey) {
+    public static String encryptBagBalanceByPublicKey(String data, String userId, String publicKey) {
         String secretKey = getBalanceSecretKey(publicKey, userId);
         byte[] key = hex(secretKey);
 
         return SecureUtil.desede(key).encryptHex(data, StandardCharsets.UTF_8);
     }
+    /**
+     * 客户公钥加密卡余
+     *
+     * @param data      待加密余额
+     * @param userId    待加密余额的用户Id
+     * @return 加密后的余额(16进制字符串)
+     */
+    public static String encryptBagBalanceByPublicKey(String data, String userId) {
+        return encryptBagBalanceByPublicKey(data, userId, RedisUtils.getCacheObject(CacheNames.CUSTOM_PUB_KEY).toString());
+    }
 
     /**
-     * 解密卡余
+     * 客户公钥解密卡余
      *
      * @param data      待解密余额
      * @param userId    待解密余额的用户Id
      * @param publicKey 客户公钥(对客户公钥进行Md5摘要计算后的16进制字符串)
      * @return 解密后的余额(16进制字符串)
      */
-    public static String decryptBagBalance(String data, String userId, String publicKey) {
+    public static String decryptBagBalanceByPublicKey(String data, String userId, String publicKey) {
         String secretKey = getBalanceSecretKey(publicKey, userId);
         byte[] key = hex(secretKey);
 
         return SecureUtil.desede(key).decryptStr(data, StandardCharsets.UTF_8);
     }
-
+    /**
+     * 客户公钥解密卡余
+     *
+     * @param data      待解密余额
+     * @param userId    待解密余额的用户Id
+     * @return 解密后的余额(16进制字符串)
+     */
+    public static String decryptBagBalanceByPublicKey(String data, String userId) {
+         return decryptBagBalanceByPublicKey(data, userId, RedisUtils.getCacheObject(CacheNames.CUSTOM_PUB_KEY).toString());
+    }
     /**
      * 16进制字符串转字节码
      *

+ 33 - 0
ruoyi-modules/ruoyi-backstage/src/main/java/org/dromara/backstage/business/bag/strategy/IUpdateStrategy.java

@@ -0,0 +1,33 @@
+package org.dromara.backstage.business.bag.strategy;
+
+import org.dromara.backstage.payment.domain.bo.PtBagBo;
+import org.dromara.backstage.payment.domain.vo.PtBagVo;
+
+/**
+ * name: IUpdateStrategy
+ * package: org.dromara.backstage.business.bag.Strategy
+ * description: 钱包更新策略
+ * date: 2024-08-19 16:31:27 16:31
+ *
+ * @author yubo
+ * @version 0.1
+ * @since JDK 1.8
+ */
+public interface IUpdateStrategy {
+    /**
+     * 校验账户钱包余额
+     * 校验规则:获取钱包的明文和密文卡余,如果两者一致则通过,否则抛出异常
+     *
+     * @param bo 账户钱包
+     */
+    void verification(PtBagBo bo);
+
+    /**
+     * 重新组装钱包逻辑
+     *
+     * @param bo 账户钱包
+     */
+    void reassembly(PtBagBo bo);
+
+    PtBagVo update(PtBagBo bo);
+}

+ 53 - 0
ruoyi-modules/ruoyi-backstage/src/main/java/org/dromara/backstage/business/bag/strategy/impl/UpdateStrategyImpl.java

@@ -0,0 +1,53 @@
+package org.dromara.backstage.business.bag.strategy.impl;
+
+import cn.hutool.core.util.StrUtil;
+import lombok.RequiredArgsConstructor;
+import org.dromara.backstage.business.bag.strategy.IUpdateStrategy;
+import org.dromara.backstage.payment.domain.bo.PtBagBo;
+import org.dromara.backstage.payment.domain.vo.PtBagVo;
+import org.dromara.backstage.payment.service.IPtBagService;
+import org.dromara.common.encrypt.utils.YcEncryptUtil;
+import org.springframework.stereotype.Service;
+
+import java.math.BigDecimal;
+
+/**
+ * name: UpdateStrategyImpl
+ * package: org.dromara.backstage.business.bag.strategy.impl
+ * description: 钱包更新策略接口实现
+ * date: 2024-08-19 16:55:27 16:55
+ *
+ * @author yubo
+ * @version 0.1
+ * @since JDK 1.8
+ */
+@RequiredArgsConstructor
+@Service
+public class UpdateStrategyImpl implements IUpdateStrategy {
+
+    private final IPtBagService bagService;
+
+    @Override
+    public void verification(PtBagBo bo) {
+        PtBagVo vo = bagService.queryById(bo.getBagId());
+
+        BigDecimal entryptValue = BigDecimal.ZERO;
+        if (StrUtil.isNotBlank(vo.getEncryptBalance())) {
+            String decryptValue = YcEncryptUtil.decryptBagBalanceByPublicKey(vo.getEncryptBalance(), vo.getUserId().toString());
+            entryptValue = new BigDecimal(decryptValue);
+        }
+        if(entryptValue.compareTo(vo.getBalance()) != 0){
+            String message = "";
+        }
+    }
+
+    @Override
+    public void reassembly(PtBagBo bo) {
+
+    }
+
+    @Override
+    public PtBagVo update(PtBagBo bo) {
+        return bagService.updateBalanceByBo(bo);
+    }
+}

+ 3 - 4
ruoyi-modules/ruoyi-backstage/src/main/java/org/dromara/backstage/payment/service/IPtBagService.java

@@ -1,6 +1,5 @@
 package org.dromara.backstage.payment.service;
 
-import org.dromara.backstage.payment.domain.PtBag;
 import org.dromara.backstage.payment.domain.vo.PtBagVo;
 import org.dromara.backstage.payment.domain.bo.PtBagBo;
 import org.dromara.common.mybatis.core.page.TableDataInfo;
@@ -92,10 +91,10 @@ public interface IPtBagService {
     Boolean rechargeByBo(PtBagBo bo);
 
     /**
-     * 账户钱包退款/直接更新余额
+     * 更新钱包
      *
      * @param bo 账户钱包
-     * @return 是否修改成功
+     * @return 更新后的钱包
      */
-    Boolean updateBalanceByBo(PtBagBo bo);
+    PtBagVo updateBalanceByBo(PtBagBo bo);
 }

+ 15 - 15
ruoyi-modules/ruoyi-backstage/src/main/java/org/dromara/backstage/payment/service/impl/PtBagServiceImpl.java

@@ -117,8 +117,7 @@ public class PtBagServiceImpl implements IPtBagService {
         PtBag update = MapstructUtils.convert(bo, PtBag.class);
 
         if (update != null && validBalance(update)) {
-            String encryptValue = YcEncryptUtil.encryptBagBalance(bo.getBalance().toString(),
-                bo.getUserId().toString(), RedisUtils.getCacheObject(CacheNames.CUSTOM_PUB_KEY).toString());
+            String encryptValue = YcEncryptUtil.encryptBagBalanceByPublicKey(bo.getBalance().toString(), bo.getUserId().toString());
             update.setEncryptBalance(encryptValue);
             return baseMapper.updateById(update) > 0;
         } else {
@@ -147,7 +146,7 @@ public class PtBagServiceImpl implements IPtBagService {
 
         BigDecimal entryptValue = BigDecimal.ZERO;
         if (StrUtil.isNotBlank(entity.getEncryptBalance())) {
-            String decryptValue = YcEncryptUtil.decryptBagBalance(entity.getEncryptBalance(), entity.getUserId().toString(), RedisUtils.getCacheObject(CacheNames.CUSTOM_PUB_KEY).toString());
+            String decryptValue = YcEncryptUtil.decryptBagBalanceByPublicKey(entity.getEncryptBalance(), entity.getUserId().toString(), RedisUtils.getCacheObject(CacheNames.CUSTOM_PUB_KEY).toString());
             entryptValue = new BigDecimal(decryptValue);
         }
         return entryptValue.compareTo(entity.getBalance()) == 0;
@@ -241,22 +240,23 @@ public class PtBagServiceImpl implements IPtBagService {
     }
 
     /**
-     * 账户钱包退款/直接更新余额
+     * 更新钱包
      *
      * @param bo 账户钱包
-     * @return 是否更新成功
+     * @return 更新后的账户钱包
      */
     @Override
-    public Boolean updateBalanceByBo(PtBagBo bo) {
-        //加密卡余
-        String encryptValue = YcEncryptUtil.encryptBagBalance(bo.getBalance().toString(),
-            bo.getUserId().toString(), RedisUtils.getCacheObject(CacheNames.CUSTOM_PUB_KEY).toString());
-
-        return baseMapper.update(null, new LambdaUpdateWrapper<PtBag>()
-            .set(PtBag::getBalance, bo.getBalance())
-            .set(PtBag::getEncryptBalance,encryptValue)
-            .eq(PtBag::getBagCode, bo.getBagCode())
-            .eq(PtBag::getUserId, bo.getUserId())) > 0;
+    public PtBagVo updateBalanceByBo(PtBagBo bo) {
+        PtBag update = MapstructUtils.convert(bo, PtBag.class);
+        if (update != null){
+            if(baseMapper.updateById(update)>0){
+                return queryById(update.getBagId());
+            } else{
+                return null;
+            }
+        } else {
+            return null;
+        }
     }
 
 }