Procházet zdrojové kódy

封装接口返回数据方法

crazycat před 2 roky
rodič
revize
bcfffe04bf

+ 37 - 0
src/main/java/com/welampiot/common/YamlPropertySourceFactory.java

@@ -0,0 +1,37 @@
+package com.welampiot.common;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.Properties;
+
+import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
+import org.springframework.core.env.PropertiesPropertySource;
+import org.springframework.core.env.PropertySource;
+import org.springframework.core.io.support.EncodedResource;
+import org.springframework.core.io.support.PropertySourceFactory;
+import org.springframework.lang.Nullable;
+
+public class YamlPropertySourceFactory implements PropertySourceFactory {
+
+    @Override
+    public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
+        Properties propertiesFromYaml = loadYamlIntoProperties(resource);
+        String sourceName = name != null ? name : resource.getResource().getFilename();
+        return new PropertiesPropertySource(sourceName, propertiesFromYaml);
+    }
+
+    private Properties loadYamlIntoProperties(EncodedResource resource) throws FileNotFoundException {
+        try {
+            YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
+            factory.setResources(resource.getResource());
+            factory.afterPropertiesSet();
+            return factory.getObject();
+        } catch (IllegalStateException e) {
+            // for ignoreResourceNotFound
+            Throwable cause = e.getCause();
+            if (cause instanceof FileNotFoundException)
+                throw (FileNotFoundException) e.getCause();
+            throw e;
+        }
+    }
+}

+ 19 - 0
src/main/java/com/welampiot/configuration/ResponseConfig.java

@@ -0,0 +1,19 @@
+package com.welampiot.configuration;
+
+import com.welampiot.common.YamlPropertySourceFactory;
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+
+@Component
+@PropertySource(factory = YamlPropertySourceFactory.class,value = "classpath:config/response.yml", encoding = "utf-8")
+@ConfigurationProperties(prefix = "response")
+@Data
+public class ResponseConfig {
+    private Map<String,String> msgCN;
+    private Map<String,String> msgEN;
+    private Map<String,String> msgRU;
+}

+ 42 - 0
src/main/java/com/welampiot/utils/ToolUtils.java

@@ -1,5 +1,7 @@
 package com.welampiot.utils;
 
+import com.welampiot.common.BaseResult;
+import com.welampiot.configuration.ResponseConfig;
 import com.welampiot.dto.GlobalLocationDTO;
 import com.welampiot.dto.SectionDTO;
 import com.welampiot.dto.UserDTO;
@@ -21,6 +23,9 @@ public class ToolUtils {
     private SectionService sectionService;
     @Autowired
     private GlobalLocationService globalLocationService;
+    @Autowired
+    private ResponseConfig responseConfig;
+
 
     /**
      * 获取用户所有路段 id
@@ -113,4 +118,41 @@ public class ToolUtils {
     public UserDTO getUser() {
         return user;
     }
+
+    /**
+     * 返回接口信息,不带数据
+     * @param code 状态码
+     * @param version 语言类型 0 中文,1 英文,2 俄语
+     * @return
+     */
+    public BaseResult response(String code,Integer version){
+        String msg;
+        if (version == 0){
+            msg = responseConfig.getMsgCN().containsKey(code) ? responseConfig.getMsgCN().get(code) : "";
+        } else if (version == 1) {
+            msg = responseConfig.getMsgEN().containsKey(code) ? responseConfig.getMsgEN().get(code) : "";
+        }else {
+            msg = responseConfig.getMsgRU().containsKey(code) ? responseConfig.getMsgRU().get(code) : "";
+        }
+        return new BaseResult<>(code,msg,new Object());
+    }
+
+    /**
+     * 返回接口信息,带数据
+     * @param code 状态码
+     * @param version 语言类型 0 中文,1 英文,2 俄语
+     * @param obj 返回数据内容
+     * @return
+     */
+    public BaseResult response(String code,Integer version,Object obj){
+        String msg;
+        if (version == 0){
+            msg = responseConfig.getMsgCN().containsKey(code) ? responseConfig.getMsgCN().get(code) : "";
+        } else if (version == 1) {
+            msg = responseConfig.getMsgEN().containsKey(code) ? responseConfig.getMsgEN().get(code) : "";
+        }else {
+            msg = responseConfig.getMsgRU().containsKey(code) ? responseConfig.getMsgRU().get(code) : "";
+        }
+        return new BaseResult<>(code,msg,obj);
+    }
 }

+ 7 - 0
src/main/resources/config/response.yml

@@ -0,0 +1,7 @@
+response:
+  msgCN: {'0000': '操作成功',
+          '0001': '参数异常'}
+  msgEN: { '0000': 'Operation successful',
+           '0001': 'Parameter abnormality' }
+  msgRU: { '0000': 'Операция прошла успешно',
+           '0001': 'Параметры аномалии' }