|
@@ -0,0 +1,182 @@
|
|
|
+package info.aspirecn.iov.sjjh.supplier.common.service;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import info.aspirecn.iov.sjjh.supplier.common.util.Constants;
|
|
|
+import info.aspirecn.iov.sjjh.supplier.common.util.SupplierProperties;
|
|
|
+import info.aspirecn.iov.sjjh.supplier.common.vo.ChannelTypeHandleResponseObject;
|
|
|
+import info.aspirecn.iov.sjjh.supplier.common.vo.RequestObject;
|
|
|
+import info.aspirecn.iov.sjjh.supplier.common.vo.ResponseObject;
|
|
|
+import info.aspirecn.iov.sjjh.supplier.common.vo.TokenResponseObject;
|
|
|
+import info.aspirecn.rdc.aspirecloud.node.except.utils.ErrorUtils;
|
|
|
+import okhttp3.*;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.net.SocketTimeoutException;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @author bzh
|
|
|
+ *
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class SupplierCommonServiceImpl implements SupplierCommonService {
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ObjectMapper objectMapper;
|
|
|
+ @Value("#{'${common.errorCode}'.split(',')}")
|
|
|
+ List<String> errorCode;
|
|
|
+ @Autowired
|
|
|
+ SupplierProperties supplierProperties;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ StringRedisTemplate stringRedisTemplate;
|
|
|
+
|
|
|
+ private OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
|
|
|
+ @Override
|
|
|
+ public String getToken() {
|
|
|
+ String appId = supplierProperties.getAppId();
|
|
|
+ String appSecret = supplierProperties.getAppSecret();
|
|
|
+ String key = "sjjhCore:"+appId+"-"+appSecret;
|
|
|
+ String token="";
|
|
|
+ try{
|
|
|
+ token = stringRedisTemplate.boundValueOps(key).get();
|
|
|
+ if(StringUtils.isEmpty(token)){
|
|
|
+ String requestUrl = supplierProperties.getTokenUrl() + "?appId=" + appId + "&appSecret="+appSecret;
|
|
|
+ log.info("supplierCommon.getToken接口requestUrl={}", requestUrl);
|
|
|
+
|
|
|
+ Request okRequest = new Request.Builder().get().url(requestUrl).build();
|
|
|
+ OkHttpClient client = okHttpClient.newBuilder().connectTimeout(supplierProperties.getOutTime(), TimeUnit.MILLISECONDS)
|
|
|
+ .readTimeout(supplierProperties.getOutTime(), TimeUnit.MILLISECONDS).writeTimeout(supplierProperties.getOutTime(), TimeUnit.MILLISECONDS)
|
|
|
+ .build();
|
|
|
+ Response response = client.newCall(okRequest).execute();
|
|
|
+ String responseContext = response.body().string();
|
|
|
+ log.info("supplierCommon.getToken接口responseContext={}",responseContext);
|
|
|
+
|
|
|
+ boolean flag = response.isSuccessful();
|
|
|
+ if(flag){
|
|
|
+
|
|
|
+ TokenResponseObject responseObject = objectMapper.readValue(responseContext, TokenResponseObject.class);
|
|
|
+
|
|
|
+ int code = responseObject.getCode();
|
|
|
+
|
|
|
+ if(code==1||code==2000){
|
|
|
+ token= responseObject.getToken();
|
|
|
+ stringRedisTemplate.boundValueOps(key).set(token);
|
|
|
+ stringRedisTemplate.expire(key,20,TimeUnit.MINUTES);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ log.info("supplierCommon.getToken接口Exception={}", e);
|
|
|
+
|
|
|
+ }
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean deleteToken(String appId,String appSecret) {
|
|
|
+ String key = "sjjhCore:"+appId+"-"+appSecret;
|
|
|
+ return stringRedisTemplate.delete(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ChannelTypeHandleResponseObject route(HttpServletRequest request, String customBody, int outTime, String token, String productId) {
|
|
|
+
|
|
|
+ ChannelTypeHandleResponseObject ret = new ChannelTypeHandleResponseObject();
|
|
|
+ ret.setIsCharge(Constants.IS_NOT_CHARGE);
|
|
|
+ ret.setCode(Constants.SUCCESS);
|
|
|
+ String upstreamCode = Constants.LOG_UPSTREAM_DEFAULT_RESPONSE_CODE;
|
|
|
+ try{
|
|
|
+ RequestObject requestObject = new RequestObject();
|
|
|
+ requestObject.setAccessId(request.getHeader("x-b3-traceid"));
|
|
|
+ requestObject.setCustomBody(objectMapper.readValue(customBody, Map.class));
|
|
|
+ requestObject.setProductId(productId);
|
|
|
+ String body = objectMapper.writeValueAsString(requestObject);
|
|
|
+ log.info("requestBody:{},url:{}",body,supplierProperties.getUrl());
|
|
|
+ RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), body);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Request okRequest = new Request.Builder()
|
|
|
+ .post(requestBody)
|
|
|
+ .header("token",token)
|
|
|
+ .url(supplierProperties.getUrl())
|
|
|
+ .build();
|
|
|
+ OkHttpClient client = okHttpClient.newBuilder()
|
|
|
+ .connectTimeout(outTime, TimeUnit.MILLISECONDS)
|
|
|
+ .readTimeout(outTime, TimeUnit.MILLISECONDS)
|
|
|
+ .writeTimeout(outTime, TimeUnit.MILLISECONDS)
|
|
|
+ .build();
|
|
|
+
|
|
|
+
|
|
|
+ Response response = client.newCall(okRequest).execute();
|
|
|
+ String commonResponseContext = response.body().string();
|
|
|
+ boolean flag =response.isSuccessful();
|
|
|
+
|
|
|
+
|
|
|
+ log.info("supplierCommon.route接口responseContext={}",commonResponseContext);
|
|
|
+ if(flag){
|
|
|
+ Set<String> names = response.headers().names();
|
|
|
+ /*for(String name: names){
|
|
|
+ log.info("name:{}",name);
|
|
|
+ }*/
|
|
|
+ List<String> headers = response.headers("isCharge");
|
|
|
+ String isCharge = "0";
|
|
|
+ if(headers !=null &&headers.size()>=1){
|
|
|
+ isCharge = headers.get(0);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ ResponseObject responseObject = objectMapper.readValue(commonResponseContext, ResponseObject.class);
|
|
|
+
|
|
|
+ int code = responseObject.getResultCode();
|
|
|
+ upstreamCode = String.valueOf(code);
|
|
|
+ if(errorCode.contains(code)){
|
|
|
+ ret.setResultCode(Constants.OTHER_ERROR_CODE);
|
|
|
+ ret.setResultBody("查询错误");
|
|
|
+ ret.setResultDesc("查询错误");
|
|
|
+ }else {
|
|
|
+ ret.setResultCode(code);
|
|
|
+ ret.setResultBody(responseObject.getResultBody());
|
|
|
+ ret.setIsCharge(Integer.parseInt(isCharge));
|
|
|
+ }
|
|
|
+
|
|
|
+ }else{
|
|
|
+ log.info("发送请求没有成功,flag:{}",flag);
|
|
|
+ ret.setCode(Constants.FAIL);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (SocketTimeoutException ste) {
|
|
|
+ ErrorUtils.captureException(ste);
|
|
|
+ log.info("supplierCommon.route接口SocketTimeoutException={}", ste);
|
|
|
+ ret.setCode(Constants.REQUEST_TIMEOUT);
|
|
|
+ } catch (Exception e) {
|
|
|
+ ErrorUtils.captureException(e);
|
|
|
+ log.info("supplierCommon.route接口Exception={}", e);
|
|
|
+ ret.setCode(Constants.FAIL);
|
|
|
+ }
|
|
|
+ request.setAttribute(Constants.LOG_UPSTREAM_RESPONSE_CODE, upstreamCode);
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+}
|