diff --git a/backend/src/main/java/io/metersphere/commons/utils/UnicodeConvertUtils.java b/backend/src/main/java/io/metersphere/commons/utils/UnicodeConvertUtils.java new file mode 100644 index 0000000000..dc5b5eb377 --- /dev/null +++ b/backend/src/main/java/io/metersphere/commons/utils/UnicodeConvertUtils.java @@ -0,0 +1,74 @@ +package io.metersphere.commons.utils; + +import java.util.regex.Pattern; + +public class UnicodeConvertUtils { + + // 单个字符的正则表达式 + private static final String singlePattern = "[0-9|a-f|A-F]"; + // 4个字符的正则表达式 + private static final String pattern = singlePattern + singlePattern + + singlePattern + singlePattern; + + + /** + * 把 \\u 开头的单字转成汉字,如 \\u6B65 -> 步 + * @param str + * @return + */ + private static String ustartToCn(final String str) { + StringBuilder sb = new StringBuilder().append("0x") + .append(str.substring(2, 6)); + Integer codeInteger = Integer.decode(sb.toString()); + int code = codeInteger.intValue(); + char c = (char)code; + return String.valueOf(c); + } + + /** + * 字符串是否以Unicode字符开头。约定Unicode字符以 \\u开头。 + * @param str 字符串 + * @return true表示以Unicode字符开头. + */ + private static boolean isStartWithUnicode(final String str) { + if (null == str || str.length() == 0) { + return false; + } + if (!str.startsWith("\\u")) { + return false; + } + // \u6B65 + if (str.length() < 6) { + return false; + } + String content = str.substring(2, 6); + + boolean isMatch = Pattern.matches(pattern, content); + return isMatch; + } + + /** + * 字符串中,所有以 \\u 开头的UNICODE字符串,全部替换成汉字 + * @return + */ + public static String unicodeToCn(final String str) { + // 用于构建新的字符串 + StringBuilder sb = new StringBuilder(); + // 从左向右扫描字符串。tmpStr是还没有被扫描的剩余字符串。 + // 下面有两个判断分支: + // 1. 如果剩余字符串是Unicode字符开头,就把Unicode转换成汉字,加到StringBuilder中。然后跳过这个Unicode字符。 + // 2.反之, 如果剩余字符串不是Unicode字符开头,把普通字符加入StringBuilder,向右跳过1. + int length = str.length(); + for (int i = 0; i < length;) { + String tmpStr = str.substring(i); + if (isStartWithUnicode(tmpStr)) { // 分支1 + sb.append(ustartToCn(tmpStr)); + i += 6; + } else { // 分支2 + sb.append(str.substring(i, i + 1)); + i++; + } + } + return sb.toString(); + } +} \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/track/issue/client/ZentaoClient.java b/backend/src/main/java/io/metersphere/track/issue/client/ZentaoClient.java index 0bc39c248b..45a827cf08 100644 --- a/backend/src/main/java/io/metersphere/track/issue/client/ZentaoClient.java +++ b/backend/src/main/java/io/metersphere/track/issue/client/ZentaoClient.java @@ -1,11 +1,11 @@ package io.metersphere.track.issue.client; import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.LogUtil; +import io.metersphere.commons.utils.UnicodeConvertUtils; import io.metersphere.i18n.Translator; import io.metersphere.track.issue.domain.zentao.*; import org.apache.commons.lang3.StringUtils; @@ -85,7 +85,11 @@ public abstract class ZentaoClient extends BaseClient { MSException.throwException(e.getMessage()); } AddIssueResponse addIssueResponse = (AddIssueResponse) getResultForObject(AddIssueResponse.class, response); - return JSONObject.parseObject(addIssueResponse.getData(), AddIssueResponse.Issue.class); + AddIssueResponse.Issue issue = JSONObject.parseObject(addIssueResponse.getData(), AddIssueResponse.Issue.class); + if (issue == null) { + MSException.throwException(UnicodeConvertUtils.unicodeToCn(response.getBody())); + } + return issue; } public void updateIssue(String id, MultiValueMap paramMap) {