新增hr系统外部接口、电表云平台外部接口
parent
8c89048468
commit
9115dc753f
@ -0,0 +1,49 @@
|
||||
package com.op.open.common;
|
||||
|
||||
|
||||
import feign.Logger;
|
||||
import feign.Request;
|
||||
import feign.Response;
|
||||
import feign.Util;
|
||||
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author 018301
|
||||
*/
|
||||
@Component
|
||||
public class CustomFeignLogger extends Logger {
|
||||
|
||||
private static final String KEY_OUTSIDE_TRACE_ID = "outsideTraceId";
|
||||
|
||||
@Override
|
||||
protected void log(String configKey, String format, Object... args) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void logRequest(String configKey, Level logLevel, Request request) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Response logAndRebufferResponse(String configKey, Level logLevel, Response response, long elapsedTime) throws IOException {
|
||||
int status = response.status();
|
||||
if (response.body() != null && !(status == 204 || status == 205)) {
|
||||
byte[] bodyData = Util.toByteArray(response.body().asInputStream());
|
||||
|
||||
MDC.remove(KEY_OUTSIDE_TRACE_ID);
|
||||
return response.toBuilder().body(bodyData).build();
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
package com.op.open.common;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Description: 负责处理Feign接口消息体转换
|
||||
*
|
||||
* @author : huangjinxian
|
||||
* @version : 1.0
|
||||
* @since : 2024/9/13
|
||||
*/
|
||||
@SuppressWarnings({"unused"})
|
||||
public class FeignHttpMessageConverter extends MappingJackson2HttpMessageConverter {
|
||||
public FeignHttpMessageConverter() {
|
||||
List<MediaType> mediaTypes = new ArrayList<>();
|
||||
String utf8Suf = ";charset=UTF-8";
|
||||
mediaTypes.add(MediaType.valueOf(MediaType.TEXT_EVENT_STREAM_VALUE + utf8Suf));
|
||||
mediaTypes.add(MediaType.valueOf(MediaType.TEXT_PLAIN_VALUE + utf8Suf));
|
||||
mediaTypes.add(MediaType.valueOf(MediaType.TEXT_HTML_VALUE + utf8Suf));
|
||||
setSupportedMediaTypes(mediaTypes);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
package com.op.open.common;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.ObjectFactory;
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
||||
|
||||
/**
|
||||
* Description: Feign接口消息体转换工厂类
|
||||
*
|
||||
* @author : huangjinxian
|
||||
* @version : 1.0
|
||||
* @since : 2024/9/13
|
||||
*/
|
||||
@SuppressWarnings({"unused"})
|
||||
public class FeignHttpMessageConvertersFactory implements ObjectFactory<HttpMessageConverters> {
|
||||
@Override
|
||||
public HttpMessageConverters getObject() throws BeansException {
|
||||
return new HttpMessageConverters(new FeignHttpMessageConverter());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,78 @@
|
||||
package com.op.open.config;
|
||||
|
||||
|
||||
import com.op.open.common.CustomFeignLogger;
|
||||
import com.op.open.common.FeignHttpMessageConvertersFactory;
|
||||
import com.op.open.hr.HrClient;
|
||||
import com.op.open.hr.HrRequestInterceptor;
|
||||
import com.op.open.nhApi.NhApiClient;
|
||||
import com.op.open.nhApi.NhApiRequestInterceptor;
|
||||
import feign.*;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
|
||||
import org.springframework.cloud.openfeign.support.SpringDecoder;
|
||||
import org.springframework.cloud.openfeign.support.SpringEncoder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
|
||||
/**
|
||||
* Description:
|
||||
*
|
||||
* @author : huangjinxian
|
||||
* @version : 1.0
|
||||
* @since : 2024/9/23
|
||||
*/
|
||||
@Configuration
|
||||
public class OutsideApiConfig {
|
||||
|
||||
@Value("${nhApi.domain}")
|
||||
private String nhAipDomain;
|
||||
|
||||
@Value("${nhApi.key}")
|
||||
private String nhAipKey;
|
||||
|
||||
@Value("${newHr.domain}")
|
||||
private String hrDomain;
|
||||
|
||||
|
||||
private final CustomFeignLogger customFeignLogger;
|
||||
|
||||
public OutsideApiConfig(CustomFeignLogger customFeignLogger) {
|
||||
this.customFeignLogger = customFeignLogger;
|
||||
}
|
||||
|
||||
|
||||
private Feign.Builder getDefaultFeignClient() {
|
||||
return Feign.builder()
|
||||
.decoder(new ResponseEntityDecoder(new SpringDecoder(new FeignHttpMessageConvertersFactory())))
|
||||
.options(new Request.Options(60, TimeUnit.SECONDS, 60, TimeUnit.SECONDS, true))
|
||||
.contract(new Contract.Default())
|
||||
.logLevel(Logger.Level.FULL)
|
||||
.logger(customFeignLogger)
|
||||
.dismiss404();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NhApiClient nhApiClient() {
|
||||
return getDefaultFeignClient()
|
||||
.encoder(new SpringEncoder(new FeignHttpMessageConvertersFactory()))
|
||||
.retryer(new Retryer.Default(100, SECONDS.toMillis(1), 3))
|
||||
.requestInterceptor(new NhApiRequestInterceptor(nhAipKey))
|
||||
.target(NhApiClient.class, nhAipDomain);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public HrClient hrClient() {
|
||||
return getDefaultFeignClient()
|
||||
.encoder(new SpringEncoder(new FeignHttpMessageConvertersFactory()))
|
||||
.retryer(new Retryer.Default(100, SECONDS.toMillis(1), 3))
|
||||
.requestInterceptor(new HrRequestInterceptor())
|
||||
.target(HrClient.class, hrDomain);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,150 @@
|
||||
package com.op.open.config;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* spring上下文综合应用
|
||||
*
|
||||
* @author huangjinxian
|
||||
* @version 1.0
|
||||
* @date 2024/9/8
|
||||
*/
|
||||
@Component
|
||||
public class SpringContext implements ApplicationContextAware {
|
||||
//上下文
|
||||
private static ApplicationContext applicationContext = null;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
SpringContext.init(applicationContext);
|
||||
}
|
||||
|
||||
public static void init(ApplicationContext context) throws BeansException {
|
||||
if (applicationContext == null) {
|
||||
applicationContext = context;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Spring配置环境变量
|
||||
*
|
||||
* @return: org.springframework.core.env.Environment
|
||||
* @auther huangjinxian
|
||||
* @see Environment
|
||||
*/
|
||||
public static Environment getEnvironment() {
|
||||
return applicationContext.getEnvironment();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上下文
|
||||
*
|
||||
* @return: org.springframework.context.ApplicationContext
|
||||
* @auther huangjinxian
|
||||
* @see ApplicationContext
|
||||
*/
|
||||
public static ApplicationContext self() {
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送事件通知
|
||||
*
|
||||
* @param event
|
||||
*/
|
||||
public static void publishEvent(Object event) {
|
||||
applicationContext.publishEvent(event);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取java bean
|
||||
*
|
||||
* @param name: bean名
|
||||
* @return: java.lang.Object
|
||||
* @auther huangjinxian
|
||||
* @see Object
|
||||
*/
|
||||
public static Object getBean(String name) throws BeansException {
|
||||
return applicationContext.getBean(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取java bean
|
||||
*
|
||||
* @param clazz: bean类
|
||||
* @return: T
|
||||
* @auther huangjinxian
|
||||
* @see T
|
||||
*/
|
||||
public static <T> T getBean(Class<T> clazz) throws BeansException {
|
||||
return applicationContext.getBean(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取java bean
|
||||
*
|
||||
* @param name: bean名
|
||||
* @param clazz: bean类
|
||||
* @return: T
|
||||
* @auther huangjinxian
|
||||
* @see T
|
||||
*/
|
||||
public static <T> T getBean(String name, Class<T> clazz) throws BeansException {
|
||||
return applicationContext.getBean(name, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据类型获取BeanMap
|
||||
*
|
||||
* @param clazz: bean类
|
||||
* @return: java.util.Map<java.lang.String, T>
|
||||
* @auther huangjinxian
|
||||
* @see Map < String,T>
|
||||
*/
|
||||
public static <T> Map<String, T> getBeanMapOfType(Class<T> clazz) {
|
||||
return applicationContext.getBeansOfType(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据类型获取BeanList
|
||||
*
|
||||
* @param clazz: bean类
|
||||
* @return: java.util.Map<java.lang.String, T>
|
||||
* @auther huangjinxian
|
||||
* @see Map< String,T>
|
||||
*/
|
||||
public static <T> List<T> getBeanListOfType(Class<T> clazz) {
|
||||
return Lists.newArrayList(applicationContext.getBeansOfType(clazz).values());
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动装配这个类型的依赖
|
||||
*
|
||||
* @param t: 类
|
||||
* @return: void
|
||||
* @auther huangjinxian
|
||||
*/
|
||||
public static <T> void autowireBean(T t) {
|
||||
applicationContext.getAutowireCapableBeanFactory().autowireBean(t);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断IOC容器中是否匹配了名字为name的bean
|
||||
*
|
||||
* @param name: bean名
|
||||
* @return: boolean
|
||||
* @auther huangjinxian
|
||||
* @see boolean
|
||||
*/
|
||||
public static boolean containsBean(String name) {
|
||||
return applicationContext.containsBean(name);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,75 @@
|
||||
package com.op.open.hr;
|
||||
|
||||
|
||||
import com.op.open.hr.domain.HrQuery;
|
||||
import com.op.open.hr.domain.UserSign;
|
||||
import org.codehaus.jackson.JsonNode;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.codehaus.jackson.type.TypeReference;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* remark
|
||||
*
|
||||
* @author 019117
|
||||
* @date
|
||||
*/
|
||||
@Component
|
||||
public class HrApi {
|
||||
private final HrClient hrClient;
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
public HrApi(HrClient hrClient) {
|
||||
this.hrClient = hrClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重试参数
|
||||
*/
|
||||
private int reloadValue = 0;
|
||||
|
||||
public List<UserSign> getAttRecord( HrQuery params ) {
|
||||
try {
|
||||
String httpResult = hrClient.getAttRecord(params);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode jsonNode = objectMapper.readTree(httpResult);
|
||||
|
||||
String code = jsonNode.get("iserror").asText();
|
||||
if (!"0".equals(code)) {
|
||||
|
||||
if ("无效的v8apitoken".equals(jsonNode.get("errormsg").asText())){
|
||||
if (reloadValue < 3) {
|
||||
reloadValue++;
|
||||
return getAttRecord(params);
|
||||
}
|
||||
}
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
reloadValue = 0;
|
||||
|
||||
|
||||
return convert(jsonNode.get("data"), UserSign.class);
|
||||
|
||||
}catch (Exception e){
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static <T> List<T> convert(JsonNode jsonNode, Class<T> elementType) throws Exception {
|
||||
// 如果是数组节点,直接转换
|
||||
if (jsonNode.isArray()) {
|
||||
// 使用TypeReference来指定泛型类型
|
||||
return objectMapper.readValue(
|
||||
objectMapper.writeValueAsString(jsonNode),
|
||||
new TypeReference<List<T>>() {}
|
||||
);
|
||||
}
|
||||
throw new IllegalArgumentException("JsonNode is not an array");
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
package com.op.open.hr;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.op.open.hr.domain.HrQuery;
|
||||
import feign.Headers;
|
||||
import feign.QueryMap;
|
||||
import feign.RequestLine;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* remark
|
||||
*
|
||||
* @author 019117
|
||||
* @date
|
||||
*/
|
||||
public interface HrClient {
|
||||
|
||||
@RequestLine("GET /v8api/api/login/getDynV8ApiToken")
|
||||
@Headers({"Content-Type: application/json"})
|
||||
String getToken();
|
||||
|
||||
@RequestLine("GET /v8api/v8api/att/getAttRecord")
|
||||
@Headers({"Content-Type: application/json"})
|
||||
String getAttRecord(@QueryMap HrQuery params);
|
||||
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
package com.op.open.hr;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.op.open.config.SpringContext;
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* remark
|
||||
*
|
||||
* @author 019117
|
||||
* @date
|
||||
*/
|
||||
public class HrRequestInterceptor implements RequestInterceptor {
|
||||
|
||||
|
||||
private String getToken() {
|
||||
HrClient hrClient = SpringContext.getBean(HrClient.class);
|
||||
return hrClient.getToken();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(RequestTemplate template) {
|
||||
if ("/v8api/api/login/getDynV8ApiToken".equals(template.url())) {
|
||||
return;
|
||||
}
|
||||
|
||||
String token = getToken();
|
||||
|
||||
template.header("v8apitoken", token);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
package com.op.open.hr.domain;
|
||||
|
||||
/**
|
||||
* remark
|
||||
*
|
||||
* @author 019117
|
||||
* @date
|
||||
*/
|
||||
public class HrQuery {
|
||||
|
||||
private String bd;
|
||||
|
||||
private String ed;
|
||||
|
||||
private String empcode;
|
||||
|
||||
public String getBd() {
|
||||
return bd;
|
||||
}
|
||||
|
||||
public void setBd(String bd) {
|
||||
this.bd = bd;
|
||||
}
|
||||
|
||||
public String getEd() {
|
||||
return ed;
|
||||
}
|
||||
|
||||
public void setEd(String ed) {
|
||||
this.ed = ed;
|
||||
}
|
||||
|
||||
public String getEmpcode() {
|
||||
return empcode;
|
||||
}
|
||||
|
||||
public void setEmpcode(String empcode) {
|
||||
this.empcode = empcode;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,48 @@
|
||||
package com.op.open.hr.domain;
|
||||
|
||||
/**
|
||||
* remark
|
||||
*
|
||||
* @author 019117
|
||||
* @date
|
||||
*/
|
||||
public class UserSign {
|
||||
|
||||
private String empname;
|
||||
private String empcode;
|
||||
private String strdata;
|
||||
private String kqdate;
|
||||
|
||||
|
||||
public String getEmpname() {
|
||||
return empname;
|
||||
}
|
||||
|
||||
public void setEmpname(String empname) {
|
||||
this.empname = empname;
|
||||
}
|
||||
|
||||
public String getEmpcode() {
|
||||
return empcode;
|
||||
}
|
||||
|
||||
public void setEmpcode(String empcode) {
|
||||
this.empcode = empcode;
|
||||
}
|
||||
|
||||
public String getStrdata() {
|
||||
return strdata;
|
||||
}
|
||||
|
||||
public void setStrdata(String strdata) {
|
||||
this.strdata = strdata;
|
||||
}
|
||||
|
||||
public String getKqdate() {
|
||||
return kqdate;
|
||||
}
|
||||
|
||||
public void setKqdate(String kqdate) {
|
||||
this.kqdate = kqdate;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package com.op.open.nhApi;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.op.open.nhApi.domain.NhQuery;
|
||||
import feign.Headers;
|
||||
import feign.RequestLine;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
/**
|
||||
* remark
|
||||
*
|
||||
* @author 019117
|
||||
* @date
|
||||
*/
|
||||
public interface NhApiClient {
|
||||
|
||||
@RequestLine("POST /WebApi/NH2/VoltageCurrent")
|
||||
@Headers({"Content-Type: application/json"})
|
||||
JSONObject getVoltageCurrent(@RequestBody NhQuery params);
|
||||
|
||||
@RequestLine("POST /WebApi/NH2/ElectricPower")
|
||||
@Headers({"Content-Type: application/json"})
|
||||
JSONObject getElectricPower(@RequestBody NhQuery params);
|
||||
|
||||
@RequestLine("POST /WebApi/NH2/ElectricQuantity")
|
||||
@Headers({"Content-Type: application/json"})
|
||||
JSONObject getElectricQuantity(@RequestBody NhQuery params);
|
||||
|
||||
@RequestLine("POST /WebApi/NH2/FrozenElectricity")
|
||||
@Headers({"Content-Type: application/json"})
|
||||
JSONObject getFrozenElectricity(@RequestBody NhQuery params);
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package com.op.open.nhApi;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
|
||||
/**
|
||||
* remark
|
||||
*
|
||||
* @author 019117
|
||||
* @date
|
||||
*/
|
||||
public class NhApiRequestInterceptor implements RequestInterceptor {
|
||||
|
||||
private final String token;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* **/
|
||||
public NhApiRequestInterceptor(String token) {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(RequestTemplate template) {
|
||||
template.header("API_KEY", token);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
package com.op.open.nhApi.domain;
|
||||
|
||||
/**
|
||||
* remark
|
||||
*
|
||||
* @author 019117
|
||||
* @date
|
||||
*/
|
||||
public class NhQuery {
|
||||
|
||||
private String dateType;
|
||||
|
||||
private String startTime;
|
||||
|
||||
private String endTime;
|
||||
|
||||
private String valueType;
|
||||
|
||||
public String getDateType() {
|
||||
return dateType;
|
||||
}
|
||||
|
||||
public void setDateType(String dateType) {
|
||||
this.dateType = dateType;
|
||||
}
|
||||
|
||||
public String getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public void setStartTime(String startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
public String getEndTime() {
|
||||
return endTime;
|
||||
}
|
||||
|
||||
public void setEndTime(String endTime) {
|
||||
this.endTime = endTime;
|
||||
}
|
||||
|
||||
public String getValueType() {
|
||||
return valueType;
|
||||
}
|
||||
|
||||
public void setValueType(String valueType) {
|
||||
this.valueType = valueType;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue