From 49b992c00c02b53c5881a141b430953c0dc3f72f Mon Sep 17 00:00:00 2001 From: julismme <11083725@qq.com> Date: Tue, 6 Jan 2026 13:23:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(pay):=20=E6=8D=90=E5=8A=A9=E5=90=8E?= =?UTF-8?q?=E5=8F=AA=E5=8F=91=E6=94=BE=E6=96=B0=E5=A2=9E=E7=9A=84=E5=8B=8B?= =?UTF-8?q?=E7=AB=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../processor/WeChatPayProcessor.java | 130 ++++++++++-------- 1 file changed, 76 insertions(+), 54 deletions(-) diff --git a/src/main/java/org/b3log/symphony/processor/WeChatPayProcessor.java b/src/main/java/org/b3log/symphony/processor/WeChatPayProcessor.java index 991b8906a..01d543609 100644 --- a/src/main/java/org/b3log/symphony/processor/WeChatPayProcessor.java +++ b/src/main/java/org/b3log/symphony/processor/WeChatPayProcessor.java @@ -26,7 +26,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.b3log.latke.Keys; -import org.b3log.latke.Latkes; import org.b3log.latke.http.Dispatcher; import org.b3log.latke.http.Request; import org.b3log.latke.http.RequestContext; @@ -47,11 +46,11 @@ import org.b3log.symphony.util.Sessions; import org.b3log.symphony.util.StatusCodes; import org.b3log.symphony.util.Symphonys; +import org.json.JSONArray; import org.json.JSONObject; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; import java.util.*; import static org.apache.logging.log4j.core.util.NameUtil.md5; @@ -100,7 +99,7 @@ synchronized public void wechatCall(final RequestContext context) { // 校验签名 String sign = paramsMap.get("sign"); - Map params2 = new HashMap<>(); + Map params2 = new HashMap<>(); params2.put("code", paramsMap.get("code")); params2.put("timestamp", paramsMap.get("timestamp")); params2.put("mch_id", paramsMap.get("mch_id")); @@ -151,30 +150,41 @@ synchronized public void wechatCall(final RequestContext context) { sponsorService.add(record); // 统计用户总积分 double sum = sponsorService.getSum(userId); - // 三清 - cloudService.removeMedal(userId, L1_NAME); - cloudService.removeMedal(userId, L2_NAME); - cloudService.removeMedal(userId, L3_NAME); - cloudService.removeMedal(userId, L4_NAME); - // 赋予勋章 - if (sum >= 4096) { - int rank = TopProcessor.getDonateRankByUserId(userId); - String rankChinese = NumberChineseFormatter.format(rank, false); - cloudService.giveMedal(userId, L4_NAME, "", "", sum + ";" + rankChinese + ";" + getNo(userId, 4)); - cloudService.giveMedal(userId, L3_NAME, "", "", getNo(userId, 3) + ""); - cloudService.giveMedal(userId, L2_NAME, "", "", getNo(userId, 2) + ""); - cloudService.giveMedal(userId, L1_NAME, "", "", getNo(userId, 1) + ""); - } else if (sum >= 1024) { - cloudService.giveMedal(userId, L3_NAME, "", "", getNo(userId, 3) + ""); - cloudService.giveMedal(userId, L2_NAME, "", "", getNo(userId, 2) + ""); - cloudService.giveMedal(userId, L1_NAME, "", "", getNo(userId, 1) + ""); - } else if (sum >= 256) { - cloudService.giveMedal(userId, L2_NAME, "", "", getNo(userId, 2) + ""); - cloudService.giveMedal(userId, L1_NAME, "", "", getNo(userId, 1) + ""); - } else if (sum >= 16) { - cloudService.giveMedal(userId, L1_NAME, "", "", getNo(userId, 1) + ""); + // 获取用户当前已有的勋章 + String enabledMedalsJson = cloudService.getEnabledMedal(userId); + JSONObject enabledMedals = new JSONObject(enabledMedalsJson); + JSONArray medalsList = enabledMedals.optJSONArray("list"); + // 创建已拥有勋章名称的集合 + Set ownedMedals = new HashSet<>(); + if (medalsList != null) { + for (int i = 0; i < medalsList.length(); i++) { + JSONObject medal = medalsList.optJSONObject(i); + if (medal != null) ownedMedals.add(medal.optString("name")); + } + } + // 根据总捐赠金额计算应该获得的勋章 + List shouldHaveMedals = new ArrayList<>(); + if (sum >= 16) shouldHaveMedals.add(L1_NAME); + if (sum >= 256) shouldHaveMedals.add(L2_NAME); + if (sum >= 1024) shouldHaveMedals.add(L3_NAME); + if (sum >= 4096) shouldHaveMedals.add(L4_NAME); + // 只授予用户还没有的勋章 + for (String medalName : shouldHaveMedals) { + if (!ownedMedals.contains(medalName)) { + if (L4_NAME.equals(medalName)) { + int rank = TopProcessor.getDonateRankByUserId(userId); + String rankChinese = NumberChineseFormatter.format(rank, false); + cloudService.giveMedal(userId, medalName, "", "", sum + ";" + rankChinese + ";" + getNo(userId, 4)); + } else { + int level = L1_NAME.equals(medalName) ? 1 : + L2_NAME.equals(medalName) ? 2 : + L3_NAME.equals(medalName) ? 3 : 0; + if (level > 0) { + cloudService.giveMedal(userId, medalName, "", "", getNo(userId, level) + ""); + } + } + } } - final Response response = context.getResponse(); response.sendString("SUCCESS"); } @@ -215,28 +225,40 @@ public static void manual(String userId, String total_amount, String memo) { sponsorService.add(record); // 统计用户总积分 double sum = sponsorService.getSum(userId); - // 三清 - cloudService.removeMedal(userId, L1_NAME); - cloudService.removeMedal(userId, L2_NAME); - cloudService.removeMedal(userId, L3_NAME); - cloudService.removeMedal(userId, L4_NAME); - // 赋予勋章 - if (sum >= 4096) { - int rank = TopProcessor.getDonateRankByUserId(userId); - String rankChinese = NumberChineseFormatter.format(rank, false); - cloudService.giveMedal(userId, L4_NAME, "", "", sum + ";" + rankChinese + ";" + getNo(userId, 4)); - cloudService.giveMedal(userId, L3_NAME, "", "", getNo(userId, 3) + ""); - cloudService.giveMedal(userId, L2_NAME, "", "", getNo(userId, 2) + ""); - cloudService.giveMedal(userId, L1_NAME, "", "", getNo(userId, 1) + ""); - } else if (sum >= 1024) { - cloudService.giveMedal(userId, L3_NAME, "", "", getNo(userId, 3) + ""); - cloudService.giveMedal(userId, L2_NAME, "", "", getNo(userId, 2) + ""); - cloudService.giveMedal(userId, L1_NAME, "", "", getNo(userId, 1) + ""); - } else if (sum >= 256) { - cloudService.giveMedal(userId, L2_NAME, "", "", getNo(userId, 2) + ""); - cloudService.giveMedal(userId, L1_NAME, "", "", getNo(userId, 1) + ""); - } else if (sum >= 16) { - cloudService.giveMedal(userId, L1_NAME, "", "", getNo(userId, 1) + ""); + // 获取用户当前已有的勋章 + String enabledMedalsJson = cloudService.getEnabledMedal(userId); + JSONObject enabledMedals = new JSONObject(enabledMedalsJson); + JSONArray medalsList = enabledMedals.optJSONArray("list"); + // 创建已拥有勋章名称的集合 + Set ownedMedals = new HashSet<>(); + if (medalsList != null) { + for (int i = 0; i < medalsList.length(); i++) { + JSONObject medal = medalsList.optJSONObject(i); + if (medal != null) ownedMedals.add(medal.optString("name")); + } + } + // 根据总捐赠金额计算应该获得的勋章 + List shouldHaveMedals = new ArrayList<>(); + if (sum >= 16) shouldHaveMedals.add(L1_NAME); + if (sum >= 256) shouldHaveMedals.add(L2_NAME); + if (sum >= 1024) shouldHaveMedals.add(L3_NAME); + if (sum >= 4096) shouldHaveMedals.add(L4_NAME); + // 只授予用户还没有的勋章 + for (String medalName : shouldHaveMedals) { + if (!ownedMedals.contains(medalName)) { + if (L4_NAME.equals(medalName)) { + int rank = TopProcessor.getDonateRankByUserId(userId); + String rankChinese = NumberChineseFormatter.format(rank, false); + cloudService.giveMedal(userId, medalName, "", "", sum + ";" + rankChinese + ";" + getNo(userId, 4)); + } else { + int level = L1_NAME.equals(medalName) ? 1 : + L2_NAME.equals(medalName) ? 2 : + L3_NAME.equals(medalName) ? 3 : 0; + if (level > 0) { + cloudService.giveMedal(userId, medalName, "", "", getNo(userId, level) + ""); + } + } + } } } @@ -271,7 +293,7 @@ public void pay(final RequestContext context) { String mchId = Symphonys.get("pay.wechat.mch_id"); String key = Symphonys.get("pay.wechat.key"); - Map params = new HashMap<>(); + Map params = new HashMap<>(); params.put("mch_id", mchId); params.put("out_trade_no", String.valueOf(time)); params.put("total_fee", total_amount); @@ -297,13 +319,13 @@ public void pay(final RequestContext context) { context.renderJSON(new JSONObject().put("QRcode_url", ret.optJSONObject("data").optString("QRcode_url"))); } - public static String packageSign(Map < String, String > params, boolean urlEncoder) { + public static String packageSign(Map params, boolean urlEncoder) { // 先将参数以其参数名的字典序升序进行排序 - TreeMap < String, String > sortedParams = new TreeMap< String, String >(params); + TreeMap sortedParams = new TreeMap(params); // 遍历排序后的字典,将所有参数按"key=value"格式拼接在一起 StringBuilder sb = new StringBuilder(); boolean first = true; - for (Map.Entry< String, String > param: sortedParams.entrySet()) { + for (Map.Entry param : sortedParams.entrySet()) { String value = param.getValue(); if (value.isEmpty()) { continue; @@ -328,7 +350,7 @@ public static String urlEncode(String src) throws UnsupportedEncodingException { return URLEncoder.encode(src, Charsets.UTF_8.name()).replace("+", "%20"); } - public static String createSign(Map < String, String > params, String partnerKey) { + public static String createSign(Map params, String partnerKey) { // 生成签名前先去除sign params.remove("sign"); String stringA = packageSign(params, false); @@ -336,7 +358,7 @@ public static String createSign(Map < String, String > params, String partnerKey return md5(stringSignTemp).toUpperCase(); } - public static String getParam(Map < String, String > params, String partnerKey) { + public static String getParam(Map params, String partnerKey) { String stringA = packageSign(params, false); return stringA; }