Skip to content

Commit 6f7bc7c

Browse files
yuanqixun袁启勋
andauthored
🎨 #1450 企业微信优化获取三方授权码的接口实现
* 重新实现企业微信三方授权,获取永久授权码返回信息 * 1、增加获取服务商三方预授权URL 2、修复授权后应用图标字段为空的bug Co-authored-by: 袁启勋 <xun.yuan@infoship.cn>
1 parent 868545c commit 6f7bc7c

File tree

6 files changed

+367
-0
lines changed

6 files changed

+367
-0
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTpService.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import me.chanjar.weixin.common.util.http.RequestHttp;
88
import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult;
99
import me.chanjar.weixin.cp.bean.WxCpTpCorp;
10+
import me.chanjar.weixin.cp.bean.WxCpTpPermanentCodeInfo;
1011
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
1112

1213
/**
@@ -88,8 +89,36 @@ public interface WxCpTpService {
8889
* @param authCode .
8990
* @return .
9091
*/
92+
@Deprecated
9193
WxCpTpCorp getPermanentCode(String authCode) throws WxErrorException;
9294

95+
/**
96+
* 获取企业永久授权码信息
97+
* <pre>
98+
* 原来的方法实现不全
99+
* </pre>
100+
*
101+
* @param authCode
102+
* @return
103+
*
104+
* @author yuan
105+
* @since 2020-03-18
106+
*
107+
* @throws WxErrorException
108+
*/
109+
WxCpTpPermanentCodeInfo getPermanentCodeInfo(String authCode) throws WxErrorException;
110+
111+
/**
112+
* <pre>
113+
* 获取预授权链接
114+
* </pre>
115+
* @param redirectUri 授权完成后的回调网址
116+
* @param state a-zA-Z0-9的参数值(不超过128个字节),用于第三方自行校验session,防止跨域攻击
117+
* @return
118+
* @throws WxErrorException
119+
*/
120+
String getPreAuthUrl(String redirectUri,String state) throws WxErrorException;
121+
93122
/**
94123
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求.
95124
*

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImpl.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.base.Joiner;
44
import com.google.gson.JsonObject;
55
import com.google.gson.JsonParser;
6+
import lombok.SneakyThrows;
67
import lombok.extern.slf4j.Slf4j;
78
import me.chanjar.weixin.common.WxType;
89
import me.chanjar.weixin.common.bean.WxAccessToken;
@@ -17,10 +18,14 @@
1718
import me.chanjar.weixin.cp.api.WxCpTpService;
1819
import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult;
1920
import me.chanjar.weixin.cp.bean.WxCpTpCorp;
21+
import me.chanjar.weixin.cp.bean.WxCpTpPermanentCodeInfo;
22+
import me.chanjar.weixin.cp.bean.WxCpTpPreauthCode;
2023
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
24+
import org.apache.commons.lang3.StringUtils;
2125

2226
import java.io.File;
2327
import java.io.IOException;
28+
import java.net.URLEncoder;
2429
import java.util.HashMap;
2530
import java.util.Map;
2631

@@ -123,6 +128,26 @@ public WxCpTpCorp getPermanentCode(String authCode) throws WxErrorException {
123128
return wxCpTpCorp;
124129
}
125130

131+
@Override
132+
public WxCpTpPermanentCodeInfo getPermanentCodeInfo(String authCode) throws WxErrorException{
133+
JsonObject jsonObject = new JsonObject();
134+
jsonObject.addProperty("auth_code", authCode);
135+
String result = post(configStorage.getApiUrl(GET_PERMANENT_CODE), jsonObject.toString());
136+
return WxCpTpPermanentCodeInfo.fromJson(result);
137+
}
138+
139+
@Override
140+
@SneakyThrows
141+
public String getPreAuthUrl(String redirectUri,String state) throws WxErrorException{
142+
String result = get(configStorage.getApiUrl(GET_PREAUTH_CODE),null);
143+
WxCpTpPreauthCode preauthCode = WxCpTpPreauthCode.fromJson(result);
144+
String preAuthUrl = "https://open.work.weixin.qq.com/3rdapp/install?suite_id="+configStorage.getSuiteId()+
145+
"&pre_auth_code="+preauthCode.getPreAuthCode()+"&redirect_uri="+ URLEncoder.encode(redirectUri,"utf-8");
146+
if(StringUtils.isNotBlank(state))
147+
preAuthUrl += "&state="+state;
148+
return preAuthUrl;
149+
}
150+
126151
@Override
127152
public String get(String url, String queryParam) throws WxErrorException {
128153
return execute(SimpleGetRequestExecutor.create(this), url, queryParam);
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
package me.chanjar.weixin.cp.bean;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
7+
8+
import java.util.List;
9+
10+
/**
11+
* 微信部门.
12+
*
13+
* @author Daniel Qian
14+
*/
15+
@Getter
16+
@Setter
17+
public class WxCpTpPermanentCodeInfo extends WxCpBaseResp {
18+
19+
private static final long serialVersionUID = -5028321625140879571L;
20+
21+
@SerializedName("access_token")
22+
private String accessToken;
23+
24+
@SerializedName("expires_in")
25+
private Long ExpiresIn;
26+
27+
@SerializedName("permanent_code")
28+
private String permanentCode;
29+
30+
/**
31+
* 授权企业信息
32+
*/
33+
@SerializedName("auth_corp_info")
34+
private AuthCorpInfo authCorpInfo;
35+
36+
/**
37+
* 授权信息。如果是通讯录应用,且没开启实体应用,是没有该项的。通讯录应用拥有企业通讯录的全部信息读写权限
38+
*/
39+
@SerializedName("auth_info")
40+
private AuthInfo authInfo;
41+
42+
/**
43+
* 授权用户信息
44+
*/
45+
@SerializedName("auth_user_info")
46+
private AuthUserInfo authUserInfo;
47+
48+
49+
@Getter
50+
@Setter
51+
public static class AuthCorpInfo {
52+
@SerializedName("corpid")
53+
private String corpId;
54+
55+
@SerializedName("corp_name")
56+
private String corpName;
57+
58+
@SerializedName("corp_type")
59+
private String corpType;
60+
61+
@SerializedName("corp_square_logo_url")
62+
private String corpSquareLogoUrl;
63+
64+
@SerializedName("corp_round_logo_url")
65+
private String corpRoundLogoUrl;
66+
67+
@SerializedName("corp_user_max")
68+
private String corpUserMax;
69+
70+
@SerializedName("corp_agent_max")
71+
private String corpAgentMax;
72+
73+
/**
74+
* 所绑定的企业微信主体名称(仅认证过的企业有)
75+
*/
76+
@SerializedName("corp_full_name")
77+
private String corpFullName;
78+
79+
/**
80+
* 认证到期时间
81+
*/
82+
@SerializedName("verified_end_time")
83+
private Long verifiedEndTime;
84+
85+
/**
86+
* 企业类型,1. 企业; 2. 政府以及事业单位; 3. 其他组织, 4.团队号
87+
*/
88+
@SerializedName("subject_type")
89+
private Integer subjectType;
90+
91+
/**
92+
* 授权企业在微工作台(原企业号)的二维码,可用于关注微工作台
93+
*/
94+
@SerializedName("corp_wxqrcode")
95+
private String corpWxqrcode;
96+
97+
@SerializedName("corp_scale")
98+
private String corpScale;
99+
100+
@SerializedName("corp_industry")
101+
private String corpIndustry;
102+
103+
@SerializedName("corp_sub_industry")
104+
private String corpSubIndustry;
105+
106+
@SerializedName("location")
107+
private String location;
108+
109+
}
110+
111+
/**
112+
* 授权信息
113+
*/
114+
@Getter
115+
@Setter
116+
public static class AuthInfo {
117+
118+
/**
119+
* 授权的应用信息,注意是一个数组,但仅旧的多应用套件授权时会返回多个agent,对新的单应用授权,永远只返回一个agent
120+
*/
121+
@SerializedName("agent")
122+
private List<Agent> agent;
123+
124+
}
125+
126+
@Getter
127+
@Setter
128+
public static class Agent {
129+
@SerializedName("agentid")
130+
private Integer agentid;
131+
132+
@SerializedName("name")
133+
private String name;
134+
135+
@SerializedName("round_logo_url")
136+
private String roundLogoUrl;
137+
138+
@SerializedName("square_logo_url")
139+
private String squareLogoUrl;
140+
141+
/**
142+
* 旧的多应用套件中的对应应用id,新开发者请忽略
143+
*/
144+
@SerializedName("appid")
145+
@Deprecated
146+
private String appid;
147+
148+
/**
149+
* 应用权限
150+
*/
151+
@SerializedName("privilege")
152+
private Privilege privilege;
153+
154+
}
155+
156+
/**
157+
* 授权人员信息
158+
*/
159+
@Getter
160+
@Setter
161+
public static class AuthUserInfo {
162+
@SerializedName("userid")
163+
private String userid;
164+
165+
@SerializedName("name")
166+
private String name;
167+
168+
@SerializedName("avatar")
169+
private String avatar;
170+
}
171+
172+
/**
173+
* 应用对应的权限
174+
*/
175+
@Getter
176+
@Setter
177+
public static class Privilege {
178+
179+
/**
180+
* 权限等级。
181+
* 1:通讯录基本信息只读
182+
* 2:通讯录全部信息只读
183+
* 3:通讯录全部信息读写
184+
* 4:单个基本信息只读
185+
* 5:通讯录全部信息只写
186+
*/
187+
@SerializedName("level")
188+
private Integer level;
189+
190+
@SerializedName("allow_party")
191+
private List<Integer> allowParty;
192+
193+
@SerializedName("allow_user")
194+
private List<String> allowUser;
195+
196+
@SerializedName("allow_tag")
197+
private List<Integer> allowTag;
198+
199+
@SerializedName("extra_party")
200+
private List<Integer> extraParty;
201+
202+
@SerializedName("extra_user")
203+
private List<String> extraUser;
204+
205+
@SerializedName("extra_tag")
206+
private List<Integer> extraTag;
207+
208+
209+
}
210+
211+
212+
public static WxCpTpPermanentCodeInfo fromJson(String json) {
213+
return WxCpGsonBuilder.create().fromJson(json, WxCpTpPermanentCodeInfo.class);
214+
}
215+
216+
public String toJson() {
217+
return WxCpGsonBuilder.create().toJson(this);
218+
}
219+
220+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package me.chanjar.weixin.cp.bean;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
7+
8+
/**
9+
* 预授权码返回
10+
* @author yqx
11+
* @date 2020/3/19
12+
*/
13+
@Getter
14+
@Setter
15+
public class WxCpTpPreauthCode extends WxCpBaseResp {
16+
17+
@SerializedName("pre_auth_code")
18+
String preAuthCode;
19+
20+
@SerializedName("expires_in")
21+
Long expiresIn;
22+
23+
public static WxCpTpPreauthCode fromJson(String json) {
24+
return WxCpGsonBuilder.create().fromJson(json, WxCpTpPreauthCode.class);
25+
}
26+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public static class Tp {
9393
public static final String GET_PERMANENT_CODE = "/cgi-bin/service/get_permanent_code";
9494
public static final String GET_SUITE_TOKEN = "/cgi-bin/service/get_suite_token";
9595
public static final String GET_PROVIDER_TOKEN = "/cgi-bin/service/get_provider_token";
96+
public static final String GET_PREAUTH_CODE = "/cgi-bin/service/get_pre_auth_code";
9697
}
9798

9899
public static class User {

0 commit comments

Comments
 (0)