WxMiniProgramClient.java
3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.infoloop.tianting.service.client;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.infoloop.tianting.model.dto.WxUserDto.WxUserCodeDto;
import com.infoloop.tianting.model.dto.WxUserDto.WxUserOpenIdDto;
import com.infoloop.tianting.model.dto.WxUserDto.WxUserPhoneResponseDto;
import com.infoloop.tianting.model.dto.WxUserDto.WxUserTokenDto;
import lombok.RequiredArgsConstructor;
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.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=" + appId
+"&secret=" + appSecret;
Request getTokenRequest = new Request.Builder()
.url(getTokenUrl)
.build();
Response getTokenResponse = client.newCall(getTokenRequest).execute();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String tokenJsonData = getTokenResponse.body().string();
WxUserTokenDto wxUserTokenDto = objectMapper.readValue(tokenJsonData, WxUserTokenDto.class);
String getPhoneUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="
+ wxUserTokenDto.getAccess_token();
WxUserCodeDto jsonData = new WxUserCodeDto();
jsonData.setCode(code);
String jsonDataString = objectMapper.writeValueAsString(jsonData);
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(mediaType, jsonDataString);
Request getPhoneRequest = new Request.Builder()
.url(getPhoneUrl)
.post(body)
.build();
Response getPhoneResponse = client.newCall(getPhoneRequest).execute();
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("获取用户手机号失败");
}
}
public WxUserOpenIdDto getUserOpenIdByCode(String code) {
try {
String url = "https://api.weixin.qq.com/sns/jscode2session" +
"?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);
return objectMapper.readValue(jsonData, WxUserOpenIdDto.class);
} catch (Exception e) {
System.out.println("error"+e.getMessage());
throw new IllegalArgumentException("获取用户openid失败");
}
}
}