Browse Source

【异常提示标准化】以及一些参数返回值修改

wub 4 years ago
parent
commit
a87bf830c9

+ 4 - 2
src/main/java/com/uas/eis/core/WebAppConfig.java

@@ -29,9 +29,11 @@ public class WebAppConfig extends WebMvcConfigurationSupport{
 	public ApiSignLoginInterceptor apiSignLoginInterceptor(){
 		return new ApiSignLoginInterceptor();
 	}
-	
+
+	@Override
 	public void addInterceptors(InterceptorRegistry registry){
-		registry.addInterceptor(apiSignLoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/EIS/login");
+		registry.addInterceptor(apiSignLoginInterceptor()).addPathPatterns("/api/**","/mes/**")
+				.excludePathPatterns("/login", "/erp/**");
 		//registry.addInterceptor(loginInterceptor()).addPathPatterns("/**").excludePathPatterns("/EIS/login");
 		registry.addInterceptor(new DataSourceInterceptor()).addPathPatterns("/*/**");
 	}

+ 104 - 3
src/main/java/com/uas/eis/dao/BaseDao.java

@@ -10,6 +10,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import com.uas.eis.entity.Configs;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.dao.DataAccessException;
 import org.springframework.dao.EmptyResultDataAccessException;
@@ -26,10 +27,14 @@ import net.sf.json.JSONObject;
 
 @Repository
 public class BaseDao{
-	
+
+	static final String CREATE_SEQ = "CREATE SEQUENCE ?" + // 创建序列
+			" MINVALUE 1 MAXVALUE 99999999999 INCREMENT BY 1 START WITH 3000 CACHE 20 NOORDER NOCYCLE ";
+
 	@Autowired
 	private JdbcTemplate jdbcTemplate;
-	
+
+
 	public JdbcTemplate getJdbcTemplate() {
 		return jdbcTemplate;
 	}
@@ -579,5 +584,101 @@ public class BaseDao{
 		}
 		return datas;
 	}
-	
+	/**
+	 * 获取序列号
+	 *
+	 * @param seq
+	 *            指定的序列名
+	 */
+	public int getSeqId(String seq) {
+		try {
+			String sql = "select " + seq + ".nextval from dual";
+			SqlRowList rs = queryForRowSet(sql);
+			if (rs.next()) {
+				return rs.getInt(1);
+			} else {// 如果不存在就创建序列
+				int count = getCountByCondition("user_sequences", "Sequence_Name='" + seq.toUpperCase() + "'");
+				if (count == 0)
+					getJdbcTemplate().execute(CREATE_SEQ.replace("?", seq));
+				return getSeqId(seq);
+			}
+		} catch (Exception e) {
+			int count = getCountByCondition("user_sequences", "Sequence_Name='" + seq.toUpperCase() + "'");
+			if (count == 0)
+				getJdbcTemplate().execute(CREATE_SEQ.replace("?", seq));
+			return getSeqId(seq);
+		}
+	}
+
+	/**
+		* 获取编号序列
+	 *
+			 * @param myTable
+	 *            Caller
+	 * @param thisType
+	 *            2
+			 */
+	public synchronized String sGetMaxNumber(String myTable, int thisType) {
+		return callProcedure("Sp_GetMaxNumber", new Object[] { myTable, thisType });
+	}
+
+	/**
+	 * 一个字段,一条结果
+	 *
+	 * @param tableName
+	 *            对应要查询的表
+	 * @param field
+	 *            要查询的字段
+	 * @param condition
+	 *            查询条件
+	 * @return field对应的数据
+	 */
+	public <T> T getFieldValue(String tableName, String field, String condition, Class<T> requiredType) {
+		StringBuffer sql = new StringBuffer("SELECT ");
+		sql.append(field);
+		sql.append(" FROM ");
+		sql.append(tableName);
+		sql.append(" WHERE ");
+		sql.append(condition);
+		SqlRowList srs = queryForRowSet(sql.toString());
+		if (srs.next()) {
+			RowConvert<T> convert = new RowConvert<T>(requiredType);
+			return convert.convert(srs.getObject(1));
+		} else {
+			return null;
+		}
+	}
+
+	/**
+	 * 判断指定参数是否配置为“是”
+	 *
+	 * @param caller
+	 * @param code
+	 *            参数编号
+	 */
+	public boolean isDBSetting(String caller, String code) {
+		int count = getCount("select count(1)  from configs where caller='" + caller + "' and code='" + code + "'");
+		if (count==0){
+			return false;
+		}
+		Configs configs = jdbcTemplate.queryForObject("select *  from configs where caller=? and code=?",
+				new BeanPropertyRowMapper<Configs>(Configs.class), caller, code);
+		if (configs != null) {
+			String data = configs.getData();
+			if ("YN".equals(configs.getData_type())) {
+				return String.valueOf(Constant.YES).equals(data);
+			}
+			return data == null ? false : true;
+		}
+		return false;
+	}
+
+	public int getCount(String sql) {
+		SqlRowList rs = queryForRowSet(sql);
+		if (rs.next()) {
+			return rs.getInt(1);
+		}
+		return 0;
+	}
+
 }

+ 165 - 0
src/main/java/com/uas/eis/entity/Configs.java

@@ -0,0 +1,165 @@
+package com.uas.eis.entity;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * 系统参数配置
+ * 
+ * @author yingp
+ * 
+ */
+public class Configs implements Serializable {
+
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+	private int id;
+	private String caller;
+	private String code;
+	private String title;
+	private String data_type;
+	private String data;
+	private String class_;
+	private String method;
+	private String dbfind;// dbfindField,dbfindCaller
+	private Integer multi;
+	private Integer editable;
+	private String help;
+	private List<Properties> properties;
+
+	public int getId() {
+		return id;
+	}
+
+	public void setId(int id) {
+		this.id = id;
+	}
+
+	public String getCaller() {
+		return caller;
+	}
+
+	public void setCaller(String caller) {
+		this.caller = caller;
+	}
+
+	public String getCode() {
+		return code;
+	}
+
+	public void setCode(String code) {
+		this.code = code;
+	}
+
+	public String getTitle() {
+		return title;
+	}
+
+	public void setTitle(String title) {
+		this.title = title;
+	}
+
+	public String getData_type() {
+		return data_type;
+	}
+
+	public void setData_type(String data_type) {
+		this.data_type = data_type;
+	}
+
+	public String getData() {
+		return data;
+	}
+
+	public void setData(String data) {
+		this.data = data;
+	}
+
+	public String getClass_() {
+		return class_;
+	}
+
+	public void setClass_(String class_) {
+		this.class_ = class_;
+	}
+
+	public String getMethod() {
+		return method;
+	}
+
+	public void setMethod(String method) {
+		this.method = method;
+	}
+
+	public String getDbfind() {
+		return dbfind;
+	}
+
+	public void setDbfind(String dbfind) {
+		this.dbfind = dbfind;
+	}
+
+	public Integer getMulti() {
+		return multi;
+	}
+
+	public void setMulti(Integer multi) {
+		this.multi = multi;
+	}
+
+	public Integer getEditable() {
+		return editable;
+	}
+
+	public void setEditable(Integer editable) {
+		this.editable = editable;
+	}
+
+	public String getHelp() {
+		return help;
+	}
+
+	public void setHelp(String help) {
+		this.help = help;
+	}
+
+	public List<Properties> getProperties() {
+		return properties;
+	}
+
+	public void setProperties(List<Properties> properties) {
+		this.properties = properties;
+	}
+
+	public static class Properties {
+		private int config_id;
+		private String display;
+		private String value;
+
+		public int getConfig_id() {
+			return config_id;
+		}
+
+		public void setConfig_id(int config_id) {
+			this.config_id = config_id;
+		}
+
+		public String getDisplay() {
+			return display;
+		}
+
+		public void setDisplay(String display) {
+			this.display = display;
+		}
+
+		public String getValue() {
+			return value;
+		}
+
+		public void setValue(String value) {
+			this.value = value;
+		}
+	}
+}

+ 12 - 11
src/main/java/com/uas/eis/entity/ErrorMessage.java

@@ -7,26 +7,27 @@ import java.util.Objects;
  * 异常处理返回信息
  */
 public enum ErrorMessage {
-
-    TIMESTAMP_ILLEGAL(1001,"请求时间戳不合法"),
-    ACCESSKEY_ILLEGAL(1002,"加密KEY不合法"),
-    TIMEOUT_ILLEGAL(1003,"请求超时"),
-    REQUESTID_ILLEGAL(1004,"随机字符串不合法"),
-    SIGNATURE_ILLEGAL(1005,"签名错误");
-
-    private int code;
+    SYS_ILLEGAL("0001","请求异常"),
+    TIMESTAMP_ILLEGAL("1001","请求时间戳不合法"),
+    ACCESSKEY_ILLEGAL("1002","加密KEY不合法"),
+    TIMEOUT_ILLEGAL("1003","请求超时"),
+    REQUESTID_ILLEGAL("1004","随机字符串不合法"),
+    SIGNATURE_ILLEGAL("1005","签名错误"),
+    BUSINESS_ILLEGAL("2001","业务异常");
+
+    private String code;
     private String message;
 
-    ErrorMessage(int  code,String message){
+    ErrorMessage(String  code,String message){
         this.code = code;
         this.message = message;
     }
 
-    public int getCode() {
+    public String getCode() {
         return code;
     }
 
-    public void setCode(int code) {
+    public void setCode(String code) {
         this.code = code;
     }
 

+ 11 - 10
src/main/java/com/uas/eis/exception/ExceptionHandlerAdvice.java

@@ -1,10 +1,11 @@
 package com.uas.eis.exception;
 
-import com.uas.eis.entity.ErrorMsg;
+import com.uas.eis.entity.ErrorMessage;
 import com.uas.eis.sdk.entity.ApiResult;
 import org.apache.log4j.Logger;
 import org.springframework.http.HttpStatus;
 import org.springframework.ui.ModelMap;
+import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.ResponseBody;
@@ -31,9 +32,10 @@ public class ExceptionHandlerAdvice {
 		ModelMap map = new ModelMap();
 		//logger.error(ex);
 		ex.printStackTrace();
-		map.put("errCode", -1);
-		map.put("errMsg", "server error");
-		map.put("errDesc",ex.getMessage());
+		map.put("code", ErrorMessage.SYS_ILLEGAL.getCode());
+		map.put("message", StringUtils.isEmpty(ex.getMessage())?ErrorMessage.SYS_ILLEGAL.getMessage():ex.getMessage());
+		map.put("requestId",request.getHeader("RequestId"));
+		map.put("data",null);
 		return map;
 	}
 
@@ -49,10 +51,11 @@ public class ExceptionHandlerAdvice {
 	@ResponseBody
 	public ModelMap handleSystemError(SystemException ex, HttpServletRequest request) {
 		ModelMap map = new ModelMap();
-		ErrorMsg errorMsg = ex.getErrorMsg();
-		map.put("errCode", errorMsg.getErrCode());
-		map.put("errMsg", errorMsg.getErrMsg());
-		map.put("errDesc",errorMsg.getErrDesc());
+		ApiResult apiResult = ex.getApiResult();
+		map.put("code", apiResult.getCode());
+		map.put("message",apiResult.getMessage());
+		map.put("requestId",apiResult.getRequestId());
+		map.put("data",apiResult.getData());
 		return map;
 	}
 	
@@ -75,6 +78,4 @@ public class ExceptionHandlerAdvice {
 		return map;
 	}
 
-
-	
 }

+ 19 - 2
src/main/java/com/uas/eis/exception/SystemException.java

@@ -1,6 +1,7 @@
 package com.uas.eis.exception;
 
 import com.uas.eis.entity.ErrorMsg;
+import com.uas.eis.sdk.entity.ApiResult;
 
 /**
  * 系统程序执行异常
@@ -17,6 +18,8 @@ public class SystemException extends RuntimeException {
 	
 	private ErrorMsg errorMsg;
 
+	private ApiResult apiResult;
+
 	public SystemException() {
 	}
 
@@ -28,12 +31,18 @@ public class SystemException extends RuntimeException {
 		super(errorMsg.getErrDesc());
 		this.errorMsg = errorMsg;
 	}
-
 	public SystemException(ErrorMsg errorMsg, Throwable paramThrowable) {
 		super(errorMsg.getErrDesc(), paramThrowable);
 		this.errorMsg = errorMsg;
 	}
-
+	public SystemException(ApiResult apiResult) {
+		super(apiResult.getMessage());
+		this.apiResult = apiResult;
+	}
+	public SystemException(ApiResult apiResult, Throwable paramThrowable) {
+		super(apiResult.getMessage(), paramThrowable);
+		this.apiResult = apiResult;
+	}
 	public SystemException(Throwable paramThrowable) {
 		super(paramThrowable);
 	}
@@ -45,4 +54,12 @@ public class SystemException extends RuntimeException {
 	public void setErrorMsg(ErrorMsg errorMsg) {
 		this.errorMsg = errorMsg;
 	}
+
+	public ApiResult getApiResult() {
+		return apiResult;
+	}
+
+	public void setApiResult(ApiResult apiResult) {
+		this.apiResult = apiResult;
+	}
 }

+ 37 - 1
src/main/java/com/uas/eis/sdk/entity/ApiResult.java

@@ -10,7 +10,7 @@ public class ApiResult<T> {
     /**
      * 自定义业务码
      */
-    private int code;
+    private String code;
 
     /**
      * 自定义业务提示说明
@@ -25,6 +25,42 @@ public class ApiResult<T> {
      */
     private T data;
 
+    public String getCode() {
+        return code;
+    }
+
+    public ApiResult<T> setCode(String code) {
+        this.code = code;
+        return this;
+    }
+
+    public String getMessage() {
+        return message;
+    }
+
+    public ApiResult<T> setMessage(String message) {
+        this.message = message;
+        return this;
+    }
+
+    public String getRequestId() {
+        return requestId;
+    }
+
+    public ApiResult<T> setRequestId(String requestId) {
+        this.requestId = requestId;
+        return this;
+    }
+
+    public T getData() {
+        return data;
+    }
+
+    public ApiResult<T> setData(T data) {
+        this.data = data;
+        return this;
+    }
+
     @Override
     public String toString() {
         return "Result{" +

+ 54 - 0
src/main/java/com/uas/eis/sdk/resp/ApiResponse.java

@@ -0,0 +1,54 @@
+package com.uas.eis.sdk.resp;
+
+import com.uas.eis.sdk.entity.ApiResult;
+
+
+/**
+ * @author
+ * @email
+ * @date 2021-12-22 20:00
+ */
+public class ApiResponse {
+
+    private final static String SUCCESS = "success";
+
+    public static <T> ApiResult<T> successRsp(String requestId) {
+        return new ApiResult<T>().setRequestId(requestId);
+    }
+
+    public static <T> ApiResult<T> successRsp(String code, String msg) {
+        return new ApiResult<T>().setCode(code).setMessage(msg);
+    }
+
+    public static <T> ApiResult<T> successRsp(String code,String requestId, String msg) {
+        return new ApiResult<T>().setCode(code).setRequestId(requestId).setMessage(msg);
+    }
+
+    public static <T> ApiResult<T> successRsp(String code,String requestId,T data) {
+        return new ApiResult<T>().setRequestId(requestId).setData(data).setCode(code);
+    }
+
+    public static <T> ApiResult<T> successRsp(String code, String msg,String requestId,T data) {
+        return new ApiResult<T>().setCode(code).setMessage(msg).setRequestId(requestId).setData(data);
+    }
+
+
+    public static <T> ApiResult<T> failRsp(String code, String msg) {
+        return new ApiResult<T>().setCode(code).setMessage(msg);
+    }
+
+    public static <T> ApiResult<T> failRsp(String code,String requestId, String msg) {
+        return new ApiResult<T>().setCode(code).setMessage(msg).setRequestId(requestId);
+    }
+
+    public static <T> ApiResult<T> failRsp(String code, String msg, T data) {
+        return new ApiResult<T>().setCode(code).setMessage(msg).setData(data);
+    }
+    public static <T> ApiResult<T> failRsp(String requestId,T data) {
+        return new ApiResult<T>().setRequestId(requestId).setData(data);
+    }
+
+    public static <T> ApiResult<T> failRsp(String code, String msg,String requestId,T data) {
+        return new ApiResult<T>().setCode(code).setMessage(msg).setRequestId(requestId).setData(data);
+    }
+}

+ 16 - 1
src/main/java/com/uas/eis/utils/BaseUtil.java

@@ -151,5 +151,20 @@ public class BaseUtil {
 		json.put("grid", JSONArray.fromObject(gridList));
 		return json.toString();
 	}
-	
+
+	/**
+	 * 将formStore解析成一个map
+	 *
+	 * @param formStore
+	 *            字符串形式的表单数据
+	 * @return map形式的表单数据
+	 */
+	public static Map<Object, Object> parseFormStoreToMap(String formStore) {
+		try {
+			return FlexJsonUtil.fromJson(formStore);
+		} catch (Exception e) {
+			return JSONUtil.toMap(formStore);
+		}
+	}
+
 }

+ 2 - 2
src/main/java/com/uas/eis/utils/HttpUtil.java

@@ -28,7 +28,6 @@ import javax.net.ssl.TrustManager;
 import javax.net.ssl.X509TrustManager;
 
 import org.apache.http.Consts;
-import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.NameValuePair;
@@ -580,8 +579,9 @@ public class HttpUtil {
 				}
 			}
 			request.setEntity(new UrlEncodedFormEntity(nvps));
-			response = httpClient.execute(request);
 			System.out.println(request);
+			response = httpClient.execute(request);
+			System.out.println(response);
 			return Response.getResponse(response);
 		} finally {
 			request.releaseConnection();