From 8239ef4a06c3b511592937571e33a4b465229e59 Mon Sep 17 00:00:00 2001 From: "Stormeye.Wu" Date: Tue, 25 Sep 2018 14:04:39 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=BE=AE=E4=BF=A1=E6=94=AF=E4=BB=98body?= =?UTF-8?q?=E8=B6=85=E9=95=BF=E5=A4=84=E7=90=86][=E4=BF=AE=E5=A4=8D]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/WxOrderServiceImpl.java | 11 ++++++- .../main/java/com/iformall/utils/Utility.java | 29 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxOrderServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxOrderServiceImpl.java index 31c569c04..80db0298b 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxOrderServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxOrderServiceImpl.java @@ -14,6 +14,7 @@ import com.iformall.service.WxCUserService; import com.iformall.service.WxOrderService; import com.iformall.service.WxPayOrderService; import com.iformall.utils.RedisLock; +import com.iformall.utils.Utility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -231,7 +232,15 @@ public class WxOrderServiceImpl implements WxOrderService { // body // tenant_id + merchant_id + title + subtitle - String bodyStr = coupon.getTitle() + "-" + coupon.getSubTitle(); + String bodyStr = ""; + try { + bodyStr = Utility.substring(wxMerchant.getName(), Math.min(64, wxMerchant.getName().getBytes().length), "utf-8") + + Utility.substring(coupon.getTitle(), Math.min(62, coupon.getTitle().getBytes().length), "utf-8"); + } catch (Exception e) { + logger.error("body:" + e.getMessage()); + bodyStr = wxMerchant.getName() + "-" + EnumCouponType.getEnum(coupon.getType()); + } + WxOrder record = new WxOrder(); record.setId(orderNumber); diff --git a/mallinkService/src/main/java/com/iformall/utils/Utility.java b/mallinkService/src/main/java/com/iformall/utils/Utility.java index 8dd600543..582a403f1 100644 --- a/mallinkService/src/main/java/com/iformall/utils/Utility.java +++ b/mallinkService/src/main/java/com/iformall/utils/Utility.java @@ -572,4 +572,33 @@ public final class Utility { } return false; } + + /** + * @param text + * 目标字符串 + * @param length + * 截取长度 + * @param encode + * 采用的编码方式 + * @return + * @throws UnsupportedEncodingException + */ + + public static String substring(String text, int length, String encode) + throws UnsupportedEncodingException { + if (text == null) { + return null; + } + StringBuilder sb = new StringBuilder(); + int currentLength = 0; + for (char c : text.toCharArray()) { + currentLength += String.valueOf(c).getBytes(encode).length; + if (currentLength <= length) { + sb.append(c); + } else { + break; + } + } + return sb.toString(); + } }