zhuyifan

refactor(config): 重构配置管理并优化相关服务

package com.infoloop.tianting.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "business-config")
@Data
@PropertySource(encoding = "UTF-8", value = "classpath:application.properties", ignoreResourceNotFound = true)
public class BusinessConfig {
private String hisCustomerQueryUrl;
private String clientCustomerQueryUrl;
private String payUrl;
}
......@@ -2,6 +2,7 @@ package com.infoloop.tianting.service.client;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.infoloop.tianting.config.BusinessConfig;
import com.infoloop.tianting.model.dto.ClientCustomerDbDTO.CurrentClientCustomerDto;
import com.infoloop.tianting.model.dto.ClientCustomerDbDTO.CurrentClientCustomerListDto;
import lombok.RequiredArgsConstructor;
......@@ -18,18 +19,20 @@ import java.util.List;
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ClientCustomerServiceHttpClient {
private final BusinessConfig businessConfig;
private final OkHttpClient client = new OkHttpClient();
public List<CurrentClientCustomerDto> getCurrentClientCustomer(String token, boolean isHis) {
ObjectMapper objectMapper = new ObjectMapper();
try {
String url = "";
String url;
if (isHis) {
url = "https://nutri.amcare.com.cn/v3/hiscustomers/current/query";
url = businessConfig.getHisCustomerQueryUrl();
} else {
url = "https://nutri.amcare.com.cn/v3/clientcustomers/current/query";
url = businessConfig.getClientCustomerQueryUrl();
}
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(mediaType, "{}");
Request request = new Request.Builder()
......
package com.infoloop.tianting.service.client;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.infoloop.tianting.config.BusinessConfig;
import com.infoloop.tianting.model.dto.PayDTO.PayInformRequestDto;
import com.infoloop.tianting.model.dto.PayDTO.PayResponseDto;
import com.infoloop.tianting.model.dto.PayDTO.ThirdPartyPayInformRequestDto;
import com.infoloop.tianting.model.dto.WxUserDto.WxUserPhoneResponseDto;
import com.infoloop.tianting.utils.DateUtil;
import com.infoloop.tianting.utils.EncryptionUtil;
import java.io.IOException;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy;
import org.springframework.jmx.export.naming.IdentityNamingStrategy;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PayServiceClient {
private final OkHttpClient client = new OkHttpClient();
private final OkHttpClient client = new OkHttpClient();
public PayResponseDto payInform(PayInformRequestDto payRequestDto) {
try {
String url = "http://10.2.5.162:21051/API/Microapp/CreateOrder?appid=Order0001";
String clientId = "Order0001";
ObjectMapper objectMapper = new ObjectMapper();
// objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
ThirdPartyPayInformRequestDto jsonData = new ThirdPartyPayInformRequestDto();
jsonData.setClientId(clientId);
jsonData.setTradeId(payRequestDto.getOrderId()); //payRequestDto.getOrderId()
String requestDate = DateUtil.formatDate(LocalDateTime.now(), DateUtil.YMDHMS);
jsonData.setRequestDate(requestDate);
jsonData.setRequestSource("OFWXApplet");
jsonData.setPayerOpenId(payRequestDto.getOpenId());
jsonData.setHospitalCode("AMCAREBJLD"); //payRequestDto.getHospitalCode()
jsonData.setPID(payRequestDto.getPhone());
jsonData.setRemark("");
jsonData.setTransAmount(payRequestDto.getPrice());
jsonData.setTradeDesc("");
String signStr = EncryptionUtil.sha1Encryption(
clientId+payRequestDto.getOrderId()+requestDate+"Order0001");
jsonData.setSign(signStr);
String jsonDataString = objectMapper.writeValueAsString(jsonData);
private final BusinessConfig businessConfig;
// 设置请求体的媒体类型为JSON
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(mediaType, jsonDataString);
Request getPhoneRequest = new Request.Builder()
.url(url)
.post(body)
.build();
Response payResponse = client.newCall(getPhoneRequest).execute();
if(payResponse.code() != 200) {
System.out.println(payResponse.message());
throw new IllegalArgumentException("支付通知失败");
}
String payJsonData = payResponse.body().string();
System.out.println("pay json:"+payJsonData);
ObjectMapper objectMapper2 = new ObjectMapper();
objectMapper2.configure(Feature.ALLOW_MISSING_VALUES, true);
objectMapper2.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper2.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES, false);
public PayResponseDto payInform(PayInformRequestDto payRequestDto) {
try {
String url = businessConfig.getPayUrl();
String clientId = "Order0001";
ObjectMapper objectMapper = new ObjectMapper();
// objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
PayResponseDto payResponseDto = objectMapper2.readValue(payJsonData, PayResponseDto.class);
return payResponseDto;
} catch (Exception e) {
System.out.println(e.getStackTrace().toString());
throw new IllegalArgumentException("支付通知失败");
ThirdPartyPayInformRequestDto jsonData = new ThirdPartyPayInformRequestDto();
jsonData.setClientId(clientId);
jsonData.setTradeId(payRequestDto.getOrderId()); //payRequestDto.getOrderId()
String requestDate = DateUtil.formatDate(LocalDateTime.now(), DateUtil.YMDHMS);
jsonData.setRequestDate(requestDate);
jsonData.setRequestSource("OFWXApplet");
jsonData.setPayerOpenId(payRequestDto.getOpenId());
jsonData.setHospitalCode("AMCAREBJLD"); //payRequestDto.getHospitalCode()
jsonData.setPID(payRequestDto.getPhone());
jsonData.setRemark("");
jsonData.setTransAmount(payRequestDto.getPrice());
jsonData.setTradeDesc("");
String signStr = EncryptionUtil.sha1Encryption(clientId + payRequestDto.getOrderId() + requestDate + "Order0001");
jsonData.setSign(signStr);
String jsonDataString = objectMapper.writeValueAsString(jsonData);
// 设置请求体的媒体类型为JSON
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(mediaType, jsonDataString);
Request getPhoneRequest = new Request.Builder()
.url(url)
.post(body)
.build();
Response payResponse = client.newCall(getPhoneRequest).execute();
if (payResponse.code() != 200) {
System.out.println(payResponse.message());
throw new IllegalArgumentException("支付通知失败");
}
String payJsonData = payResponse.body().string();
System.out.println("pay json:" + payJsonData);
ObjectMapper objectMapper2 = new ObjectMapper();
objectMapper2.configure(Feature.ALLOW_MISSING_VALUES, true);
objectMapper2.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper2.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES, false);
return objectMapper2.readValue(payJsonData, PayResponseDto.class);
} catch (Exception e) {
System.out.println(e.getStackTrace().toString());
throw new IllegalArgumentException("支付通知失败");
}
}
}
}
......
......@@ -2,36 +2,39 @@ package com.infoloop.tianting.service.client;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.infoloop.tianting.constant.ConfigConstants;
import com.infoloop.tianting.model.dto.WxUserDto;
import com.infoloop.tianting.model.dto.WxUserDto.WxUserCodeDto;
import com.infoloop.tianting.model.dto.WxUserDto.WxUserOpenIdDto;
import com.infoloop.tianting.model.dto.WxUserDto.WxUserOpenIdDto1;
import com.infoloop.tianting.model.dto.WxUserDto.WxUserPhoneResponseDto;
import com.infoloop.tianting.model.dto.WxUserDto.WxUserTokenDto;
import com.infoloop.tianting.utils.JsonUtil;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName.Form;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import static com.infoloop.tianting.constant.ConfigConstants.MINI_PROGRAM_APP_ID;
import static com.infoloop.tianting.constant.ConfigConstants.MINI_PROGRAM_APP_SECRET;
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class WxMiniProgramClient {
@Value(MINI_PROGRAM_APP_ID)
private String appId;
@Value(MINI_PROGRAM_APP_SECRET)
private String appSecret;
private final OkHttpClient client = new OkHttpClient();
public WxUserPhoneResponseDto getUserPhoneInfoByCode(String code) {
try {
String getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx40ec496d73b58ec6"
+"&secret=5b309517bf918abf760b2195e91b99f3";
String getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId
+"&secret=" + appSecret;
Request getTokenRequest = new Request.Builder()
.url(getTokenUrl)
.build();
......@@ -40,7 +43,6 @@ public class WxMiniProgramClient {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String tokenJsonData = getTokenResponse.body().string();
WxUserTokenDto wxUserTokenDto = objectMapper.readValue(tokenJsonData, WxUserTokenDto.class);
System.out.println("token:" + wxUserTokenDto.getAccess_token());
String getPhoneUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="
+ wxUserTokenDto.getAccess_token();
WxUserCodeDto jsonData = new WxUserCodeDto();
......@@ -56,7 +58,6 @@ public class WxMiniProgramClient {
String phoneJsonData = getPhoneResponse.body().string();
WxUserPhoneResponseDto wxUserPhoneResponseDto = objectMapper.readValue(phoneJsonData, WxUserPhoneResponseDto.class);
return wxUserPhoneResponseDto;
} catch (Exception e) {
System.out.println(e.getMessage());
throw new IllegalArgumentException("获取用户手机号失败");
......@@ -67,14 +68,12 @@ public class WxMiniProgramClient {
try {
String url = "https://api.weixin.qq.com/sns/jscode2session" +
"?appid=wx40ec496d73b58ec6" + "&secret=5b309517bf918abf760b2195e91b99f3" + "&js_code=" + code + "&grant_type=authorization_code";
"?appid=" + appId + "&secret=" + appSecret + "&js_code=" + code + "&grant_type=authorization_code";
System.out.println("url:" +url);
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
String jsonData = response.body().string();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
......
......@@ -76,6 +76,10 @@ grpc.meizhongyihe-service.port=3040
miniprogram.appId=wx40ec496d73b58ec6
miniprogram.appSecret=5b309517bf918abf760b2195e91b99f3
business-config.hisCustomerQueryUrl = https://nutri.amcare.com.cn/v3/hiscustomers/current/query
business-config.clientCustomerQueryUrl = https://nutri.amcare.com.cn/v3/clientcustomers/current/query
business-config.payUrl = http://10.2.5.162:21051/API/Microapp/CreateOrder?appid=Order0001
##Actuator\u914D\u7F6E
management.server.port=8088
management.endpoints.web.exposure.include=*
......