Selaa lähdekoodia

[bug-4718] 字段“shi_ji_shi_jian_”是时间戳格式,在保存时会报错,在后端对这个字段特殊处理,将时间戳格式转化成时间格式

huangws 10 kuukautta sitten
vanhempi
sitoutus
57ae81f8cf

+ 33 - 0
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/untils/JsonUtil.java

@@ -4,6 +4,8 @@ import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -12,6 +14,7 @@ public class JsonUtil {
             Pattern.compile("\"(\\w+)\":\"(\\{.*?\\})\"");
 
     public static String fixNestedJson(String jsonStr) {
+        jsonStr = convertTimestamp(jsonStr);
         // 处理嵌套JSON结构
         Matcher matcher = NESTED_JSON_PATTERN.matcher(jsonStr);
         StringBuffer sb = new StringBuffer();
@@ -37,4 +40,34 @@ public class JsonUtil {
     public static JSONArray parseToArray(String jsonStr) {
         return JSONUtil.parseArray(fixNestedJson(jsonStr));
     }
+    /**
+     * [bug-4718] 冰箱温湿度记录、室内温湿度记录 删除sql解析失败,列表上没有显示请求操作时间
+     * 前端传入时间戳格式时间导致报错
+     * 如果shi_ji_shi_jian_为时间戳格式转换成时间格式
+     * */
+    public static String convertTimestamp(String jsonStr) {
+        try {
+            JSONObject jsonObj = new JSONObject(jsonStr.substring(1, jsonStr.length()-1));
+            if(jsonObj.isNull("shi_ji_shi_jian_") == false) {
+                Object timeValue = jsonObj.get("shi_ji_shi_jian_");
+                if(timeValue instanceof Number) {
+                    long timestamp = ((Number)timeValue).longValue();
+                    String timestampStr = String.valueOf(timestamp);
+                    if(timestampStr.length() == 10 || timestampStr.length() == 13) {
+                        if(timestampStr.length() == 10) {
+                            timestamp *= 1000;
+                        }
+                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+                        String mysqlTime = sdf.format(new Date(timestamp));
+                        jsonObj.put("shi_ji_shi_jian_", mysqlTime);
+                        return "[" + jsonObj.toString() + "]";
+                    }
+                }
+            }
+            return jsonStr;
+        } catch (Exception e) {
+            return jsonStr;
+        }
+    }
+
 }