zhuyifan

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

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