DefaultWeiXinBeanDefinition.java
3.67 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.topdraw.business.process.domian.weixin;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.topdraw.exception.BadRequestException;
import com.topdraw.utils.StringUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Date;
import java.util.UUID;
@Data
@Component
public class DefaultWeiXinBeanDefinition implements WeiXinBeanDefinition {
//
private String appid;
private String openId;
private String code;
private String token;
private String secret;
private String unionId;
private String nickname;
private String headImgUrl;
private JSONObject userInfo;
private String phoneNumber;
@Value("${file.upload:upload}")
private String filePath;
public DefaultWeiXinBeanDefinition() {
}
public DefaultWeiXinBeanDefinition(String appId, String code, String unionId, String openId, JSONObject userInfoWxJo, String phone) {
this.userInfo = userInfoWxJo;
if (userInfo != null) {
if (StringUtils.isNotBlank(userInfoWxJo.getString("unionId"))) {
unionId = userInfoWxJo.getString("unionId");
}
if (StringUtils.isNotBlank(userInfoWxJo.getString("openId"))) {
openId = userInfoWxJo.getString("openId");
}
headImgUrl = userInfoWxJo.getString("avatarUrl");
if (StringUtils.isNotBlank(userInfoWxJo.getString("nickName"))) {
nickname = Base64.getEncoder().encodeToString(userInfoWxJo.getString("nickName").getBytes(StandardCharsets.UTF_8));
}
String phoneNumber = userInfoWxJo.getString("phoneNumber");
if (StringUtils.isBlank(phoneNumber)) {
throw new BadRequestException("phoneNumber is null...");
}
this.phoneNumber = phoneNumber;
if (StringUtils.isNotBlank(headImgUrl)) {
new Thread(() -> {
String s = UUID.randomUUID().toString();
File file = new File(System.getProperty("user.dir") + "/" + filePath + "/icon/" + DateUtil.format(new Date(), "yyyy-MM-dd"));
if (!file.exists()) {
file.mkdirs();
}
HttpUtil.downloadFile(headImgUrl, new File(System.getProperty("user.dir") + "/" + filePath + "/icon/" + DateUtil.format(new Date(), "yyyy-MM-dd") + "/" + s + ".jpg"));
headImgUrl = filePath + "/icon/" + DateUtil.format(new Date(), "yyyy-MM-dd") + "/" + s + ".jpg";
}).start();
}
}
this.unionId = unionId;
this.phoneNumber = phone;
this.openId = openId;
this.appid = appId;
this.code = code;
}
@Override
public String getAppId() {
return this.appid;
}
@Override
public String getCode() {
return this.code;
}
@Override
public String getToken() {
return this.token;
}
@Override
public String getSecret() {
return this.secret;
}
@Override
public String getOpenId() {
return this.openId;
}
@Override
public String getUnionId() {
return this.unionId;
}
@Override
public String getNickname() {
return this.nickname;
}
@Override
public String getHeadImgUrl() {
return this.headImgUrl;
}
@Override
public JSONObject getUserInfo() {
return this.userInfo;
}
}