|
|
@@ -23,7 +23,6 @@ import java.net.URLEncoder;
|
|
|
import java.net.http.HttpRequest;
|
|
|
import java.net.http.HttpResponse;
|
|
|
import java.util.List;
|
|
|
-import java.util.Optional;
|
|
|
|
|
|
/*
|
|
|
*项目名: netflix
|
|
|
@@ -40,8 +39,6 @@ public class WxCorpOpsImpl implements WxCorpOps {
|
|
|
@Autowired
|
|
|
private RedisService redisService;
|
|
|
|
|
|
- private final static String remoteUrl = "wx-ops.liuliangbang.vip";
|
|
|
-
|
|
|
@Override
|
|
|
public CorpExternalContactInfo getExternalContact(String corpId, String userId) throws Exception {
|
|
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get?access_token=" + getAccessToken(corpId) + "&external_userid=" + userId;
|
|
|
@@ -60,7 +57,7 @@ public class WxCorpOpsImpl implements WxCorpOps {
|
|
|
|
|
|
@Override
|
|
|
public String getAccessToken(String corpId) throws Exception {
|
|
|
- String accessToken = redisService.getStr(RedisKey.ACCESS_TOKEN_KEY + corpId);
|
|
|
+ String accessToken = redisService.getStr(RedisKey.CORP_ACCESS_TOKEN_KEY + corpId);
|
|
|
if (StringUtils.isNotBlank(accessToken)) {
|
|
|
return accessToken;
|
|
|
}
|
|
|
@@ -172,9 +169,9 @@ public class WxCorpOpsImpl implements WxCorpOps {
|
|
|
|
|
|
@Override
|
|
|
public String getJsTicket(String corpId) throws Exception {
|
|
|
- String jsTicket = redisService.getStr(RedisKey.JS_TICKET_KEY + corpId);
|
|
|
+ String jsTicket = redisService.getStr(RedisKey.CORP_JS_TICKET_KEY + corpId);
|
|
|
if (StrUtil.isBlank(jsTicket)) {
|
|
|
- return js_ticket(this.getAccessToken(corpId));
|
|
|
+ return js_ticket(corpId);
|
|
|
}
|
|
|
return jsTicket;
|
|
|
}
|
|
|
@@ -189,10 +186,11 @@ public class WxCorpOpsImpl implements WxCorpOps {
|
|
|
}
|
|
|
|
|
|
public String access_token(String corpId) throws Exception {
|
|
|
- corpId = Optional.ofNullable(corpId).orElse(yhlXWxCorpConfig.getCorpId());
|
|
|
-
|
|
|
- String url = "http://" + remoteUrl + "/wx/corp/access_token" +
|
|
|
- "?corpId=" + corpId;
|
|
|
+ BaseWxCorpConfig corpConfig = WxCorpFactory.getBaseWxCorpConfig(corpId);
|
|
|
+ if (corpId == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("{0}企业微信不存在", corpId);
|
|
|
+ }
|
|
|
+ String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s", corpConfig.getCorpId(), corpConfig.getCorpSecret());
|
|
|
HttpResponse<byte[]> res =
|
|
|
J11HttpC.custom()
|
|
|
.ofGet()
|
|
|
@@ -204,16 +202,24 @@ public class WxCorpOpsImpl implements WxCorpOps {
|
|
|
throw BusinessRuntimeException.getInstance("获取access_token失败: " + IoKit.toString(res.body()));
|
|
|
}
|
|
|
|
|
|
- Callback callback = Jsons.parseObject(res.body(), Callback.class);
|
|
|
- if (!callback.isSuccess()) {
|
|
|
- throw BusinessRuntimeException.getInstance(callback.getMessage());
|
|
|
+ WechatAccessToken token = Jsons.parseObject(res.body(), WechatAccessToken.class);
|
|
|
+ if (null == token) {
|
|
|
+ throw BusinessRuntimeException.getInstance("获取企业微信access_token失败: " + IoKit.toString(res.body()));
|
|
|
}
|
|
|
- return (String) callback.getValue();
|
|
|
+
|
|
|
+ final Integer errcode = token.getErrcode();
|
|
|
+ if (null != errcode && 0 != errcode) {
|
|
|
+ throw BusinessRuntimeException.getInstance("获取企业微信access_token失败: " + token.getErrmsg() + "[" + errcode + "]");
|
|
|
+ }
|
|
|
+ //存入redis
|
|
|
+ String accessToken = token.getAccessToken();
|
|
|
+ // 设置过期时间 比微信官方给的时间提前300秒过期
|
|
|
+ redisService.set(RedisKey.CORP_ACCESS_TOKEN_KEY + corpId, accessToken, token.getExpiresIn() - 300L);
|
|
|
+ return accessToken;
|
|
|
}
|
|
|
|
|
|
public String js_ticket(String corpId) throws Exception {
|
|
|
- String url = "http://" + remoteUrl + "/wx/corp/js_ticket" +
|
|
|
- "?corpId=" + corpId;
|
|
|
+ String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=%s", this.getAccessToken(corpId));
|
|
|
HttpResponse<byte[]> res =
|
|
|
J11HttpC.custom()
|
|
|
.ofGet()
|
|
|
@@ -222,19 +228,25 @@ public class WxCorpOpsImpl implements WxCorpOps {
|
|
|
.send(HttpResponse.BodyHandlers.ofByteArray());
|
|
|
|
|
|
if (200 != res.statusCode()) {
|
|
|
- throw BusinessRuntimeException.getInstance("获取access_token失败: " + IoKit.toString(res.body()));
|
|
|
+ throw BusinessRuntimeException.getInstance("获取js_ticket失败: " + IoKit.toString(res.body()));
|
|
|
}
|
|
|
|
|
|
- Callback callback = Jsons.parseObject(res.body(), Callback.class);
|
|
|
- if (!callback.isSuccess()) {
|
|
|
- throw BusinessRuntimeException.getInstance(callback.getMessage());
|
|
|
+ OffiaccountJsTicket ticket = Jsons.parseObject(res.body(), OffiaccountJsTicket.class);
|
|
|
+ if (null == ticket) {
|
|
|
+ throw BusinessRuntimeException.getInstance("获取js_ticket失败: " + IoKit.toString(res.body()));
|
|
|
}
|
|
|
- return (String) callback.getValue();
|
|
|
+
|
|
|
+ final Integer errcode = ticket.getErrcode();
|
|
|
+ if (null != errcode && 0 != errcode) {
|
|
|
+ throw BusinessRuntimeException.getInstance("获取js_ticket失败: " + ticket.getErrmsg() + "[" + errcode + "]");
|
|
|
+ }
|
|
|
+ String jsTicket = ticket.getTicket();
|
|
|
+ redisService.set(RedisKey.CORP_JS_TICKET_KEY + corpId, jsTicket, ticket.getExpiresIn() - 300L);
|
|
|
+ return jsTicket;
|
|
|
}
|
|
|
|
|
|
private String agentJsTicket(String corpId) throws Exception {
|
|
|
- String url = "http://" + remoteUrl + "/wx/corp/agent_js_ticket" +
|
|
|
- "?corpId=" + corpId;
|
|
|
+ String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=%s&type=agent_config", this.getAccessToken(corpId));
|
|
|
HttpResponse<byte[]> res =
|
|
|
J11HttpC.custom()
|
|
|
.ofGet()
|
|
|
@@ -246,10 +258,16 @@ public class WxCorpOpsImpl implements WxCorpOps {
|
|
|
throw BusinessRuntimeException.getInstance("获取agent_js_ticket失败: " + IoKit.toString(res.body()));
|
|
|
}
|
|
|
|
|
|
- Callback callback = Jsons.parseObject(res.body(), Callback.class);
|
|
|
- if (!callback.isSuccess()) {
|
|
|
- throw BusinessRuntimeException.getInstance(callback.getMessage());
|
|
|
+ OffiaccountJsTicket ticket = Jsons.parseObject(res.body(), OffiaccountJsTicket.class);
|
|
|
+ if (null == ticket) {
|
|
|
+ throw BusinessRuntimeException.getInstance("获取agent_js_ticket失败: " + IoKit.toString(res.body()));
|
|
|
+ }
|
|
|
+ final Integer errcode = ticket.getErrcode();
|
|
|
+ if (null != errcode && 0 != errcode) {
|
|
|
+ throw BusinessRuntimeException.getInstance("获取agent_js_ticket失败: " + ticket.getErrmsg() + "[" + errcode + "]");
|
|
|
}
|
|
|
- return (String) callback.getValue();
|
|
|
+ String jsTicket = ticket.getTicket();
|
|
|
+ redisService.set(RedisKey.CORP_AGENT_JS_TICKET_KEY + corpId, jsTicket, ticket.getExpiresIn() - 300L);
|
|
|
+ return jsTicket;
|
|
|
}
|
|
|
}
|