|
@@ -0,0 +1,120 @@
|
|
|
+package com.zzyd.request.base;
|
|
|
+
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
+import com.fasterxml.jackson.databind.JavaType;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
|
|
+import com.fasterxml.jackson.databind.type.TypeFactory;
|
|
|
+import com.zzyd.response.base.CommonResponse;
|
|
|
+import com.zzyd.util.SQBHttpClient;
|
|
|
+
|
|
|
+import java.io.Serializable;
|
|
|
+import java.lang.reflect.ParameterizedType;
|
|
|
+import java.lang.reflect.Type;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+public interface IBaseRequest<T extends IBaseRequest,R> extends Serializable {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 方法路径后缀
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ String apiPathSuffix();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回值是否为集合
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ default boolean ifList(){
|
|
|
+ return false;
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ default boolean level1Data(){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否激活接口
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ default boolean activate(){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ default CommonResponse<R> doExecute() {
|
|
|
+ Type genericInterface = this.getClass().getGenericInterfaces()[0];
|
|
|
+ Class<R> responseClass;
|
|
|
+ if (genericInterface instanceof ParameterizedType) {
|
|
|
+ Type[] actualTypeArguments = ((ParameterizedType) genericInterface).getActualTypeArguments();
|
|
|
+ if(actualTypeArguments.length==1){
|
|
|
+ responseClass = (Class<R>) String.class;
|
|
|
+ }
|
|
|
+ Type actualType = actualTypeArguments[1];
|
|
|
+ responseClass = (Class<R>) actualType;
|
|
|
+ if (actualType instanceof ParameterizedType) {
|
|
|
+ Type rawType = ((ParameterizedType) actualType).getRawType();
|
|
|
+ //处理是否集合
|
|
|
+ if (List.class.equals(rawType)) {
|
|
|
+ Type[] listTypeArguments = ((ParameterizedType) actualType).getActualTypeArguments();
|
|
|
+ if (listTypeArguments.length > 0) {
|
|
|
+ responseClass = (Class<R>) listTypeArguments[0];
|
|
|
+ } else {
|
|
|
+ throw new IllegalStateException("无法获取泛型类型R的Class对象");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new IllegalStateException("无法获取泛型类型R的Class对象");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
+ // 设置命名策略为蛇形转驼峰
|
|
|
+ mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
|
|
|
+ // 使用 responseClass 构建 JavaType
|
|
|
+ JavaType javaType;
|
|
|
+ if(ifList()){
|
|
|
+ JavaType listType = TypeFactory.defaultInstance().constructCollectionType(List.class, responseClass);
|
|
|
+ // 构建包含 List 的 CommonResponse 的 JavaType
|
|
|
+ javaType = mapper.getTypeFactory().constructParametricType(CommonResponse.class, listType);
|
|
|
+
|
|
|
+ }else{
|
|
|
+ javaType = mapper.getTypeFactory().constructParametricType(CommonResponse.class, responseClass);
|
|
|
+ }
|
|
|
+ // 执行文件请求并获取 JSON 字符串
|
|
|
+ String responseJson = SQBHttpClient.doExecute(this);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(responseJson);
|
|
|
+ Object error = jsonObject.get("error_code");
|
|
|
+ if(!Objects.isNull(error) && error.toString().length()>0){
|
|
|
+ throw new RuntimeException((String) error);
|
|
|
+ }
|
|
|
+ JSONObject response = JSONObject.parseObject(JSONObject.toJSONString(jsonObject.get("biz_response")));
|
|
|
+ if(!Objects.isNull(response.get("error_code")) && response.get("error_code").toString().length()>0){
|
|
|
+ throw new RuntimeException((String) response.get("error_message"));
|
|
|
+ }
|
|
|
+ if(level1Data()){
|
|
|
+ SQBHttpClient.terminalSn = response.getString("terminal_sn");
|
|
|
+ SQBHttpClient.terminalKey = response.getString("terminal_key");
|
|
|
+ javaType = TypeFactory.defaultInstance().constructType(responseClass);
|
|
|
+ CommonResponse<R> commonResponse = new CommonResponse<>();
|
|
|
+ commonResponse.setResultCode("200");
|
|
|
+ commonResponse.setBizResponse(mapper.readValue(response.toJSONString(), javaType));
|
|
|
+ return commonResponse;
|
|
|
+ }
|
|
|
+ // 将 JSON 字符串反序列化为 CommonResponse 对象
|
|
|
+ return mapper.readValue(response.toJSONString(), javaType);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|