TokenCreator新增uniqueid方法

This commit is contained in:
jinyu 2017-07-06 15:48:51 +08:00
parent 4ccc11d8cf
commit c8726ddc3f
22 changed files with 2090 additions and 2062 deletions

View File

@ -19,8 +19,7 @@ public class CacheManager<T extends Cacheable> {
protected final CacheStorager<T> cacheStorager; protected final CacheStorager<T> cacheStorager;
private final ReentrantLock lock = new ReentrantLock(); private final ReentrantLock lock = new ReentrantLock();
public CacheManager(CacheCreator<T> cacheCreator, public CacheManager(CacheCreator<T> cacheCreator, CacheStorager<T> cacheStorager) {
CacheStorager<T> cacheStorager) {
this.cacheCreator = cacheCreator; this.cacheCreator = cacheCreator;
this.cacheStorager = cacheStorager; this.cacheStorager = cacheStorager;
} }

View File

@ -13,7 +13,6 @@ import com.foxinmy.weixin4j.util.DigestUtil;
import com.foxinmy.weixin4j.util.MapUtil; import com.foxinmy.weixin4j.util.MapUtil;
import com.foxinmy.weixin4j.util.RandomUtil; import com.foxinmy.weixin4j.util.RandomUtil;
import com.foxinmy.weixin4j.util.StringUtil; import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
/** /**
* JSSDK配置类 * JSSDK配置类
@ -51,17 +50,6 @@ public class JSSDKConfigurator {
return this; return this;
} }
/**
* 公众号的唯一标识 不填则获取weixin4j.properties#account中的id
*
* @param appId
* @return
*/
public JSSDKConfigurator appId(String appId) {
config.put("appId", appId);
return this;
}
/** /**
* 需要使用的JS接口列表 * 需要使用的JS接口列表
* *
@ -96,10 +84,10 @@ public class JSSDKConfigurator {
* @param url * @param url
* 当前网页的URL不包含#及其后面部分 * 当前网页的URL不包含#及其后面部分
* @return jssdk配置JSON字符串 * @return jssdk配置JSON字符串
* @see <a * @see <a href=
* href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang=zh_CN">公众号JSSDK</a> * "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang=zh_CN">公众号JSSDK</a>
* @see <a * @see <a href=
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BE%AE%E4%BF%A1JS-SDK%E6%8E%A5%E5%8F%A3">企业号JSSDK</a> * "http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BE%AE%E4%BF%A1JS-SDK%E6%8E%A5%E5%8F%A3">企业号JSSDK</a>
* @throws WeixinException * @throws WeixinException
*/ */
public String toJSONConfig(String url) throws WeixinException { public String toJSONConfig(String url) throws WeixinException {
@ -113,11 +101,8 @@ public class JSSDKConfigurator {
signMap.put("noncestr", noncestr); signMap.put("noncestr", noncestr);
signMap.put("jsapi_ticket", this.ticketTokenManager.getAccessToken()); signMap.put("jsapi_ticket", this.ticketTokenManager.getAccessToken());
signMap.put("url", url); signMap.put("url", url);
String sign = DigestUtil.SHA1(MapUtil.toJoinString(signMap, false, String sign = DigestUtil.SHA1(MapUtil.toJoinString(signMap, false, false));
false)); config.put("appId", ticketTokenManager.getWeixinId());
if (StringUtil.isBlank(config.getString("appId"))) {
config.put("appId", Weixin4jConfigUtil.getWeixinAccount().getId());
}
if (StringUtil.isBlank(config.getString("debug"))) { if (StringUtil.isBlank(config.getString("debug"))) {
config.put("debug", false); config.put("debug", false);
} }

View File

@ -33,13 +33,20 @@ public abstract class TokenCreator implements CacheCreator<Token> {
*/ */
@Override @Override
public String key() { public String key() {
return String.format("%s%s", CACHEKEY_PREFIX, key0()); return String.format("%s%s_%s", CACHEKEY_PREFIX, name(), uniqueid());
} }
/** /**
* 返回缓存KEY的名称:建议接口类型命名 mp_token_{appid} * 返回缓存类型命名如mp_token
* *
* @return * @return
*/ */
public abstract String key0(); public abstract String name();
/**
* 返回缓存唯一标识如appid
*
* @return
*/
public abstract String uniqueid();
} }

View File

@ -23,8 +23,7 @@ public class TokenManager extends CacheManager<Token> {
* @param cacheStorager * @param cacheStorager
* 负责token的存储 * 负责token的存储
*/ */
public TokenManager(TokenCreator tokenCreator, public TokenManager(TokenCreator tokenCreator, CacheStorager<Token> cacheStorager) {
CacheStorager<Token> cacheStorager) {
super(tokenCreator, cacheStorager); super(tokenCreator, cacheStorager);
} }
@ -37,4 +36,13 @@ public class TokenManager extends CacheManager<Token> {
public String getAccessToken() throws WeixinException { public String getAccessToken() throws WeixinException {
return super.getCache().getAccessToken(); return super.getCache().getAccessToken();
} }
/**
* 返回唯一标识ID
*
* @return
*/
public String getWeixinId() {
return ((TokenCreator) cacheCreator).uniqueid();
}
} }

View File

@ -281,7 +281,7 @@ public class WeixinProxy {
* @return * @return
*/ */
public TokenManager getTicketManager(TicketType ticketType) { public TokenManager getTicketManager(TicketType ticketType) {
return new TokenManager(new WeixinTicketCreator(weixinAccount.getId(), ticketType, this.tokenManager), return new TokenManager(new WeixinTicketCreator(ticketType, this.tokenManager),
this.cacheStorager); this.cacheStorager);
} }

View File

@ -34,8 +34,13 @@ public class WeixinComponentPreCodeCreator extends TokenCreator {
} }
@Override @Override
public String key0() { public String name() {
return String.format("mp_component_precode_%s", componentId); return "mp_component_precode";
}
@Override
public String uniqueid() {
return componentId;
} }
@Override @Override
@ -46,4 +51,5 @@ public class WeixinComponentPreCodeCreator extends TokenCreator {
JSONObject result = response.getAsJson(); JSONObject result = response.getAsJson();
return new Token(result.getString("pre_auth_code"), result.getLongValue("expires_in") * 1000l); return new Token(result.getString("pre_auth_code"), result.getLongValue("expires_in") * 1000l);
} }
} }

View File

@ -29,8 +29,13 @@ public class WeixinComponentTokenCreator extends TokenCreator {
} }
@Override @Override
public String key0() { public String name() {
return String.format("mp_component_token_%s", ticketManager.getThirdId()); return "mp_component_token";
}
@Override
public String uniqueid() {
return ticketManager.getThirdId();
} }
@Override @Override
@ -39,10 +44,8 @@ public class WeixinComponentTokenCreator extends TokenCreator {
obj.put("component_appid", ticketManager.getThirdId()); obj.put("component_appid", ticketManager.getThirdId());
obj.put("component_appsecret", ticketManager.getThirdSecret()); obj.put("component_appsecret", ticketManager.getThirdSecret());
obj.put("component_verify_ticket", ticketManager.getAccessTicket()); obj.put("component_verify_ticket", ticketManager.getAccessTicket());
WeixinResponse response = weixinExecutor.post( WeixinResponse response = weixinExecutor.post(URLConsts.COMPONENT_TOKEN_URL, obj.toJSONString());
URLConsts.COMPONENT_TOKEN_URL, obj.toJSONString());
obj = response.getAsJson(); obj = response.getAsJson();
return new Token(obj.getString("component_access_token"), return new Token(obj.getString("component_access_token"), obj.getLongValue("expires_in") * 1000l);
obj.getLongValue("expires_in") * 1000l);
} }
} }

View File

@ -29,16 +29,20 @@ public class WeixinTokenComponentCreator extends TokenCreator {
* @param componentTokenManager * @param componentTokenManager
* 第三方套件凭证token * 第三方套件凭证token
*/ */
public WeixinTokenComponentCreator(PerTicketManager perTicketManager, public WeixinTokenComponentCreator(PerTicketManager perTicketManager, TokenManager componentTokenManager) {
TokenManager componentTokenManager) {
this.perTicketManager = perTicketManager; this.perTicketManager = perTicketManager;
this.componentTokenManager = componentTokenManager; this.componentTokenManager = componentTokenManager;
} }
@Override @Override
public String key0() { public String name() {
return String.format("mp_token_component_%s_%s", return String.format("mp_token_component_%s_%s", perTicketManager.getThirdId(),
perTicketManager.getThirdId(), perTicketManager.getAuthAppId()); perTicketManager.getAuthAppId());
}
@Override
public String uniqueid() {
throw new UnsupportedOperationException();
} }
@Override @Override
@ -48,10 +52,10 @@ public class WeixinTokenComponentCreator extends TokenCreator {
obj.put("authorizer_appid", perTicketManager.getAuthAppId()); obj.put("authorizer_appid", perTicketManager.getAuthAppId());
obj.put("authorizer_refresh_token", perTicketManager.getAccessTicket()); obj.put("authorizer_refresh_token", perTicketManager.getAccessTicket());
WeixinResponse response = weixinExecutor.post( WeixinResponse response = weixinExecutor.post(
String.format(URLConsts.TOKEN_COMPONENT_URL, String.format(URLConsts.TOKEN_COMPONENT_URL, componentTokenManager.getAccessToken()),
componentTokenManager.getAccessToken()), obj.toJSONString()); obj.toJSONString());
obj = response.getAsJson(); obj = response.getAsJson();
return new Token(obj.getString("access_token"), return new Token(obj.getString("access_token"), obj.getLongValue("expires_in") * 1000l);
obj.getLongValue("expires_in") * 1000l);
} }
} }

View File

@ -15,8 +15,8 @@ import com.foxinmy.weixin4j.util.NameValue;
* @author jinyu(foxinmy@gmail.com) * @author jinyu(foxinmy@gmail.com)
* @date 2014年9月29日 * @date 2014年9月29日
* @since JDK 1.6 * @since JDK 1.6
* @see <a * @see <a href=
* href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN">模板消息</a> * "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN">模板消息</a>
*/ */
public class TemplateMessage implements Serializable { public class TemplateMessage implements Serializable {
@ -46,6 +46,20 @@ public class TemplateMessage implements Serializable {
*/ */
@JSONField(serialize = false) @JSONField(serialize = false)
private NameValue tail; private NameValue tail;
/**
* 跳小程序所需数据不需跳小程序可不用传该数据
*/
private String miniprogram;
/**
* 所需跳转到的小程序appid该小程序appid必须与发模板消息的公众号是绑定关联关系
*/
private String appid;
/**
* 所需跳转到小程序的具体页面路径支持带参数,示例index?foo=bar
*/
private String pagepath;
/** /**
* 数据项 * 数据项
*/ */
@ -57,8 +71,7 @@ public class TemplateMessage implements Serializable {
private final static String DEFAULT_COLOR = "#173177"; private final static String DEFAULT_COLOR = "#173177";
@JSONCreator @JSONCreator
public TemplateMessage(@JSONField(name = "toUser") String toUser, public TemplateMessage(@JSONField(name = "toUser") String toUser, @JSONField(name = "templateId") String templateId,
@JSONField(name = "templateId") String templateId,
@JSONField(name = "url") String url) { @JSONField(name = "url") String url) {
this.toUser = toUser; this.toUser = toUser;
this.templateId = templateId; this.templateId = templateId;
@ -180,10 +193,33 @@ public class TemplateMessage implements Serializable {
this.content = items; this.content = items;
} }
public String getMiniprogram() {
return miniprogram;
}
public void setMiniprogram(String miniprogram) {
this.miniprogram = miniprogram;
}
public String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getPagepath() {
return pagepath;
}
public void setPagepath(String pagepath) {
this.pagepath = pagepath;
}
@Override @Override
public String toString() { public String toString() {
return "TemplateMessage [toUser=" + toUser + ", templateId=" return "TemplateMessage [toUser=" + toUser + ", templateId=" + templateId + ", url=" + url + ", head="
+ templateId + ", url=" + url + ", head=" + getHead() + getHead() + ", tail=" + getTail() + ", content=" + content + "]";
+ ", tail=" + getTail() + ", content=" + content + "]";
} }
} }

View File

@ -22,39 +22,37 @@ import com.foxinmy.weixin4j.type.TicketType;
*/ */
public class WeixinTicketCreator extends TokenCreator { public class WeixinTicketCreator extends TokenCreator {
private final String appid;
private final TicketType ticketType; private final TicketType ticketType;
private final TokenManager weixinTokenManager; private final TokenManager weixinTokenManager;
/** /**
* jssdk * jssdk
* *
* @param appid
* 公众号的appid
* @param ticketType * @param ticketType
* 票据类型 * 票据类型
* @param weixinTokenManager * @param weixinTokenManager
* <font color="red">公众平台的access_token</font> * <font color="red">公众平台的access_token</font>
*/ */
public WeixinTicketCreator(String appid, TicketType ticketType, public WeixinTicketCreator(TicketType ticketType, TokenManager weixinTokenManager) {
TokenManager weixinTokenManager) {
this.appid = appid;
this.ticketType = ticketType; this.ticketType = ticketType;
this.weixinTokenManager = weixinTokenManager; this.weixinTokenManager = weixinTokenManager;
} }
@Override @Override
public String key0() { public String name() {
return String.format("mp_ticket_%s_%s", ticketType.name(), appid); return String.format("mp_ticket_%s", ticketType.name());
}
@Override
public String uniqueid() {
return weixinTokenManager.getWeixinId();
} }
@Override @Override
public Token create() throws WeixinException { public Token create() throws WeixinException {
WeixinResponse response = weixinExecutor.get(String.format( WeixinResponse response = weixinExecutor
URLConsts.JS_TICKET_URL, weixinTokenManager.getAccessToken(), .get(String.format(URLConsts.JS_TICKET_URL, weixinTokenManager.getAccessToken(), ticketType.name()));
ticketType.name()));
JSONObject result = response.getAsJson(); JSONObject result = response.getAsJson();
return new Token(result.getString("ticket"), return new Token(result.getString("ticket"), result.getLongValue("expires_in") * 1000l);
result.getLongValue("expires_in") * 1000l);
} }
} }

View File

@ -14,8 +14,8 @@ import com.foxinmy.weixin4j.token.TokenCreator;
* @author jinyu(foxinmy@gmail.com) * @author jinyu(foxinmy@gmail.com)
* @date 2015年1月10日 * @date 2015年1月10日
* @since JDK 1.6 * @since JDK 1.6
* @see <a * @see <a href=
* href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183&token=&lang=zh_CN">微信公众平台获取token说明</a> * "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183&token=&lang=zh_CN">微信公众平台获取token说明</a>
* @see com.foxinmy.weixin4j.model.Token * @see com.foxinmy.weixin4j.model.Token
*/ */
public class WeixinTokenCreator extends TokenCreator { public class WeixinTokenCreator extends TokenCreator {
@ -36,17 +36,20 @@ public class WeixinTokenCreator extends TokenCreator {
} }
@Override @Override
public String key0() { public String name() {
return String.format("mp_token_%s", appid); return "mp_token";
}
@Override
public String uniqueid() {
return appid;
} }
@Override @Override
public Token create() throws WeixinException { public Token create() throws WeixinException {
String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, appid, String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, appid, secret);
secret);
WeixinResponse response = weixinExecutor.get(tokenUrl); WeixinResponse response = weixinExecutor.get(tokenUrl);
JSONObject result = response.getAsJson(); JSONObject result = response.getAsJson();
return new Token(result.getString("access_token"), return new Token(result.getString("access_token"), result.getLongValue("expires_in") * 1000l);
result.getLongValue("expires_in") * 1000l);
} }
} }

View File

@ -151,10 +151,8 @@ public class WeixinProxy {
* @param cacheStorager * @param cacheStorager
* token管理 * token管理
*/ */
public WeixinProxy(WeixinAccount weixinAccount, public WeixinProxy(WeixinAccount weixinAccount, CacheStorager<Token> cacheStorager) {
CacheStorager<Token> cacheStorager) { this(weixinAccount, new WeixinTokenCreator(weixinAccount.getId(), weixinAccount.getSecret()), cacheStorager);
this(weixinAccount, new WeixinTokenCreator(weixinAccount.getId(),
weixinAccount.getSecret()), cacheStorager);
} }
/** /**
@ -169,13 +167,9 @@ public class WeixinProxy {
* @see com.foxinmy.weixin4j.qy.api.SuiteApi * @see com.foxinmy.weixin4j.qy.api.SuiteApi
* @see WeixinSuiteProxy#getWeixinProxy(String, String) * @see WeixinSuiteProxy#getWeixinProxy(String, String)
*/ */
public WeixinProxy(PerTicketManager perTicketManager, public WeixinProxy(PerTicketManager perTicketManager, TokenManager suiteTokenManager) {
TokenManager suiteTokenManager) { this(new WeixinAccount(perTicketManager.getThirdId(), perTicketManager.getThirdSecret()),
this( new WeixinTokenSuiteCreator(perTicketManager, suiteTokenManager), perTicketManager.getCacheStorager());
new WeixinAccount(perTicketManager.getThirdId(),
perTicketManager.getThirdSecret()),
new WeixinTokenSuiteCreator(perTicketManager, suiteTokenManager),
perTicketManager.getCacheStorager());
} }
/** /**
@ -186,18 +180,15 @@ public class WeixinProxy {
* @param tokenManager * @param tokenManager
* token管理 * token管理
*/ */
private WeixinProxy(WeixinAccount weixinAccount, TokenCreator tokenCreator, private WeixinProxy(WeixinAccount weixinAccount, TokenCreator tokenCreator, CacheStorager<Token> cacheStorager) {
CacheStorager<Token> cacheStorager) {
if (weixinAccount == null) { if (weixinAccount == null) {
throw new IllegalArgumentException( throw new IllegalArgumentException("weixinAccount must not be empty");
"weixinAccount must not be empty");
} }
if (tokenCreator == null) { if (tokenCreator == null) {
throw new IllegalArgumentException("tokenCreator must not be empty"); throw new IllegalArgumentException("tokenCreator must not be empty");
} }
if (cacheStorager == null) { if (cacheStorager == null) {
throw new IllegalArgumentException( throw new IllegalArgumentException("cacheStorager must not be empty");
"cacheStorager must not be empty");
} }
this.tokenManager = new TokenManager(tokenCreator, cacheStorager); this.tokenManager = new TokenManager(tokenCreator, cacheStorager);
this.weixinAccount = weixinAccount; this.weixinAccount = weixinAccount;
@ -251,8 +242,7 @@ public class WeixinProxy {
* @return * @return
*/ */
public TokenManager getTicketManager(TicketType ticketType) { public TokenManager getTicketManager(TicketType ticketType) {
return new TokenManager(new WeixinTicketCreator(weixinAccount.getId(), return new TokenManager(new WeixinTicketCreator(ticketType, this.tokenManager), cacheStorager);
ticketType, this.tokenManager), cacheStorager);
} }
/** /**
@ -266,7 +256,8 @@ public class WeixinProxy {
* @param message * @param message
* 消息对象 * 消息对象
* @return 如果对应用或收件人部门标签任何一个无权限则本次发送失败如果收件人部门或标签不存在发送仍然执行但返回无效的部分 * @return 如果对应用或收件人部门标签任何一个无权限则本次发送失败如果收件人部门或标签不存在发送仍然执行但返回无效的部分
* </br> { "errcode": 0, "errmsg": "ok", "invaliduser": "UserID1", * </br>
* { "errcode": 0, "errmsg": "ok", "invaliduser": "UserID1",
* "invalidparty":"PartyID1", "invalidtag":"TagID1" } * "invalidparty":"PartyID1", "invalidtag":"TagID1" }
* @throws WeixinException * @throws WeixinException
* @see com.foxinmy.weixin4j.qy.api.NotifyApi * @see com.foxinmy.weixin4j.qy.api.NotifyApi
@ -285,8 +276,7 @@ public class WeixinProxy {
* @see com.foxinmy.weixin4j.tuple.MpNews * @see com.foxinmy.weixin4j.tuple.MpNews
* @see com.foxinmy.weixin4j.qy.model.IdParameter * @see com.foxinmy.weixin4j.qy.model.IdParameter
*/ */
public IdParameter sendNotifyMessage(NotifyMessage message) public IdParameter sendNotifyMessage(NotifyMessage message) throws WeixinException {
throws WeixinException {
return notifyApi.sendNotifyMessage(message); return notifyApi.sendNotifyMessage(message);
} }
@ -308,8 +298,7 @@ public class WeixinProxy {
* @see com.foxinmy.weixin4j.qy.message.CustomeMessage * @see com.foxinmy.weixin4j.qy.message.CustomeMessage
* @throws WeixinException * @throws WeixinException
*/ */
public ApiResult sendCustomeMessage(CustomeMessage message) public ApiResult sendCustomeMessage(CustomeMessage message) throws WeixinException {
throws WeixinException {
return notifyApi.sendCustomeMessage(message); return notifyApi.sendCustomeMessage(message);
} }
@ -345,8 +334,7 @@ public class WeixinProxy {
* 创建自定义菜单</a> * 创建自定义菜单</a>
* @see com.foxinmy.weixin4j.model.Button * @see com.foxinmy.weixin4j.model.Button
*/ */
public ApiResult createMenu(int agentid, List<Button> buttons) public ApiResult createMenu(int agentid, List<Button> buttons) throws WeixinException {
throws WeixinException {
return menuApi.createMenu(agentid, buttons); return menuApi.createMenu(agentid, buttons);
} }
@ -398,8 +386,7 @@ public class WeixinProxy {
* @see com.foxinmy.weixin4j.qy.api.MediaApi * @see com.foxinmy.weixin4j.qy.api.MediaApi
* @throws WeixinException * @throws WeixinException
*/ */
public String uploadImage(InputStream is, String fileName) public String uploadImage(InputStream is, String fileName) throws WeixinException {
throws WeixinException {
return mediaApi.uploadImage(is, fileName); return mediaApi.uploadImage(is, fileName);
} }
@ -427,8 +414,7 @@ public class WeixinProxy {
* 上传永久素材文件说明</a> * 上传永久素材文件说明</a>
* @throws WeixinException * @throws WeixinException
*/ */
public MediaUploadResult uploadMedia(int agentid, InputStream is, public MediaUploadResult uploadMedia(int agentid, InputStream is, String fileName) throws WeixinException {
String fileName) throws WeixinException {
return mediaApi.uploadMedia(agentid, is, fileName); return mediaApi.uploadMedia(agentid, is, fileName);
} }
@ -450,8 +436,7 @@ public class WeixinProxy {
* 获取永久媒体说明</a> * 获取永久媒体说明</a>
* @throws WeixinException * @throws WeixinException
*/ */
public MediaDownloadResult downloadMedia(int agentid, String mediaId) public MediaDownloadResult downloadMedia(int agentid, String mediaId) throws WeixinException {
throws WeixinException {
return mediaApi.downloadMedia(agentid, mediaId); return mediaApi.downloadMedia(agentid, mediaId);
} }
@ -474,8 +459,7 @@ public class WeixinProxy {
* 上传永久媒体素材</a> * 上传永久媒体素材</a>
* @see com.foxinmy.weixin4j.tuple.MpArticle * @see com.foxinmy.weixin4j.tuple.MpArticle
*/ */
public String uploadMaterialArticle(int agentid, List<MpArticle> articles) public String uploadMaterialArticle(int agentid, List<MpArticle> articles) throws WeixinException {
throws WeixinException {
return mediaApi.uploadMaterialArticle(agentid, articles); return mediaApi.uploadMaterialArticle(agentid, articles);
} }
@ -493,8 +477,7 @@ public class WeixinProxy {
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E5%88%A0%E9%99%A4%E6%B0%B8%E4%B9%85%E7%B4%A0%E6%9D%90"> * "http://qydev.weixin.qq.com/wiki/index.php?title=%E5%88%A0%E9%99%A4%E6%B0%B8%E4%B9%85%E7%B4%A0%E6%9D%90">
* 删除永久媒体素材</a> * 删除永久媒体素材</a>
*/ */
public ApiResult deleteMaterialMedia(int agentid, String mediaId) public ApiResult deleteMaterialMedia(int agentid, String mediaId) throws WeixinException {
throws WeixinException {
return mediaApi.deleteMaterialMedia(agentid, mediaId); return mediaApi.deleteMaterialMedia(agentid, mediaId);
} }
@ -511,8 +494,7 @@ public class WeixinProxy {
* @see com.foxinmy.weixin4j.qy.api.MediaApi * @see com.foxinmy.weixin4j.qy.api.MediaApi
* @see com.foxinmy.weixin4j.tuple.MpArticle * @see com.foxinmy.weixin4j.tuple.MpArticle
*/ */
public List<MpArticle> downloadArticle(int agentid, String mediaId) public List<MpArticle> downloadArticle(int agentid, String mediaId) throws WeixinException {
throws WeixinException {
return mediaApi.downloadArticle(agentid, mediaId); return mediaApi.downloadArticle(agentid, mediaId);
} }
@ -533,8 +515,7 @@ public class WeixinProxy {
* 修改永久媒体素材</a> * 修改永久媒体素材</a>
* @see com.foxinmy.weixin4j.tuple.MpArticle * @see com.foxinmy.weixin4j.tuple.MpArticle
*/ */
public String updateMaterialArticle(int agentid, String mediaId, public String updateMaterialArticle(int agentid, String mediaId, List<MpArticle> articles) throws WeixinException {
List<MpArticle> articles) throws WeixinException {
return mediaApi.updateMaterialArticle(agentid, mediaId, articles); return mediaApi.updateMaterialArticle(agentid, mediaId, articles);
} }
@ -576,8 +557,7 @@ public class WeixinProxy {
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E7%B4%A0%E6%9D%90%E5%88%97%E8%A1%A8"> * "http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E7%B4%A0%E6%9D%90%E5%88%97%E8%A1%A8">
* 获取素材列表</a> * 获取素材列表</a>
*/ */
public MediaRecord listMaterialMedia(int agentid, MediaType mediaType, public MediaRecord listMaterialMedia(int agentid, MediaType mediaType, Pageable pageable) throws WeixinException {
Pageable pageable) throws WeixinException {
return mediaApi.listMaterialMedia(agentid, mediaType, pageable); return mediaApi.listMaterialMedia(agentid, mediaType, pageable);
} }
@ -593,8 +573,7 @@ public class WeixinProxy {
* @see {@link #listMaterialMedia(int,MediaType, Pageable)} * @see {@link #listMaterialMedia(int,MediaType, Pageable)}
* @throws WeixinException * @throws WeixinException
*/ */
public List<MediaItem> listAllMaterialMedia(int agentid, MediaType mediaType) public List<MediaItem> listAllMaterialMedia(int agentid, MediaType mediaType) throws WeixinException {
throws WeixinException {
return mediaApi.listAllMaterialMedia(agentid, mediaType); return mediaApi.listAllMaterialMedia(agentid, mediaType);
} }
@ -679,8 +658,7 @@ public class WeixinProxy {
* @return 上传后的mediaId * @return 上传后的mediaId
* @throws WeixinException * @throws WeixinException
*/ */
public String batchUploadParties(List<Party> parties) public String batchUploadParties(List<Party> parties) throws WeixinException {
throws WeixinException {
return mediaApi.batchUploadParties(parties); return mediaApi.batchUploadParties(parties);
} }
@ -716,8 +694,7 @@ public class WeixinProxy {
* @return 处理结果 * @return 处理结果
* @throws WeixinException * @throws WeixinException
*/ */
public ApiResult createUser(User user, InputStream avatar) public ApiResult createUser(User user, InputStream avatar) throws WeixinException {
throws WeixinException {
return userApi.createUser(user, avatar); return userApi.createUser(user, avatar);
} }
@ -753,8 +730,7 @@ public class WeixinProxy {
* @return 处理结果 * @return 处理结果
* @throws WeixinException * @throws WeixinException
*/ */
public ApiResult updateUser(User user, InputStream avatar) public ApiResult updateUser(User user, InputStream avatar) throws WeixinException {
throws WeixinException {
return userApi.updateUser(user, avatar); return userApi.updateUser(user, avatar);
} }
@ -849,8 +825,8 @@ public class WeixinProxy {
* @return 成员列表 * @return 成员列表
* @throws WeixinException * @throws WeixinException
*/ */
public List<User> listUser(int partyId, boolean fetchChild, public List<User> listUser(int partyId, boolean fetchChild, UserStatus userStatus, boolean findDetail)
UserStatus userStatus, boolean findDetail) throws WeixinException { throws WeixinException {
return userApi.listUser(partyId, fetchChild, userStatus, findDetail); return userApi.listUser(partyId, fetchChild, userStatus, findDetail);
} }
@ -911,8 +887,7 @@ public class WeixinProxy {
* @return 处理结果 * @return 处理结果
* @throws WeixinException * @throws WeixinException
*/ */
public ApiResult batchDeleteUser(List<String> userIds) public ApiResult batchDeleteUser(List<String> userIds) throws WeixinException {
throws WeixinException {
return userApi.batchDeleteUser(userIds); return userApi.batchDeleteUser(userIds);
} }
@ -930,8 +905,7 @@ public class WeixinProxy {
* 邀请成员关注说明</a> * 邀请成员关注说明</a>
* @throws WeixinException * @throws WeixinException
*/ */
public InviteType inviteUser(String userId, String tips) public InviteType inviteUser(String userId, String tips) throws WeixinException {
throws WeixinException {
return userApi.inviteUser(userId, tips); return userApi.inviteUser(userId, tips);
} }
@ -955,8 +929,9 @@ public class WeixinProxy {
* 创建标签(创建的标签属于管理组;默认为未加锁状态) * 创建标签(创建的标签属于管理组;默认为未加锁状态)
* *
* @param tag * @param tag
* 标签对象</br> 标签名称长度为1~64个字节标签名不可与其他标签重名</br> 标签id整型 * 标签对象</br>
* 指定此参数时新增的标签会生成对应的标签id不指定时则以目前最大的id自增 * 标签名称长度为1~64个字节标签名不可与其他标签重名</br>
* 标签id整型 指定此参数时新增的标签会生成对应的标签id不指定时则以目前最大的id自增
* @see <a href= * @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E5.88.9B.E5.BB.BA.E6.A0.87.E7.AD.BE"> * "http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E5.88.9B.E5.BB.BA.E6.A0.87.E7.AD.BE">
* 创建标签说明</a> * 创建标签说明</a>
@ -1026,8 +1001,8 @@ public class WeixinProxy {
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E8.8E.B7.E5.8F.96.E6.A0.87.E7.AD.BE.E6.88.90.E5.91.98"> * "http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E8.8E.B7.E5.8F.96.E6.A0.87.E7.AD.BE.E6.88.90.E5.91.98">
* 获取标签成员说明</a> * 获取标签成员说明</a>
* @see com.foxinmy.weixin4j.qy.api.TagApi * @see com.foxinmy.weixin4j.qy.api.TagApi
* @return 成员列表<font color="red">Contacts#getUsers</font>和部门列表 <font * @return 成员列表<font color="red">Contacts#getUsers</font>和部门列表
* color="red">Contacts#getPartyIds</font> * <font color="red">Contacts#getPartyIds</font>
* @throws WeixinException * @throws WeixinException
*/ */
public Contacts getTagUsers(int tagId) throws WeixinException { public Contacts getTagUsers(int tagId) throws WeixinException {
@ -1051,8 +1026,7 @@ public class WeixinProxy {
* @return 非法的userIds和partyIds * @return 非法的userIds和partyIds
* @throws WeixinException * @throws WeixinException
*/ */
public IdParameter addTagUsers(int tagId, List<String> userIds, public IdParameter addTagUsers(int tagId, List<String> userIds, List<Integer> partyIds) throws WeixinException {
List<Integer> partyIds) throws WeixinException {
return tagApi.addTagUsers(tagId, userIds, partyIds); return tagApi.addTagUsers(tagId, userIds, partyIds);
} }
@ -1073,8 +1047,7 @@ public class WeixinProxy {
* @return 非法的userIds和partyIds * @return 非法的userIds和partyIds
* @throws WeixinException * @throws WeixinException
*/ */
public IdParameter deleteTagUsers(int tagId, List<String> userIds, public IdParameter deleteTagUsers(int tagId, List<String> userIds, List<Integer> partyIds) throws WeixinException {
List<Integer> partyIds) throws WeixinException {
return tagApi.deleteTagUsers(tagId, userIds, partyIds); return tagApi.deleteTagUsers(tagId, userIds, partyIds);
} }
@ -1159,8 +1132,7 @@ public class WeixinProxy {
* 邀请成员关注</a> * 邀请成员关注</a>
* @throws WeixinException * @throws WeixinException
*/ */
public String batchInviteUser(IdParameter parameter, Callback callback, public String batchInviteUser(IdParameter parameter, Callback callback, String tips) throws WeixinException {
String tips) throws WeixinException {
return batchApi.inviteUser(parameter, callback, tips); return batchApi.inviteUser(parameter, callback, tips);
} }
@ -1168,7 +1140,8 @@ public class WeixinProxy {
* 批量更新成员,本接口以userid为主键增量更新企业号通讯录成员 * 批量更新成员,本接口以userid为主键增量更新企业号通讯录成员
* <p> * <p>
* 1.模板中的部门需填写部门ID多个部门用分号分隔部门ID必须为数字</br> * 1.模板中的部门需填写部门ID多个部门用分号分隔部门ID必须为数字</br>
* 2.文件中存在通讯录中也存在的成员更新成员在文件中指定的字段值 </br> 3.文件中存在通讯录中不存在的成员执行添加操作</br> * 2.文件中存在通讯录中也存在的成员更新成员在文件中指定的字段值 </br>
* 3.文件中存在通讯录中不存在的成员执行添加操作</br>
* 4.通讯录中存在文件中不存在的成员保持不变</br> * 4.通讯录中存在文件中不存在的成员保持不变</br>
* </p> * </p>
* *
@ -1184,15 +1157,15 @@ public class WeixinProxy {
* 批量更新成员</a> * 批量更新成员</a>
* @throws WeixinException * @throws WeixinException
*/ */
public String batchSyncUser(String mediaId, Callback callback) public String batchSyncUser(String mediaId, Callback callback) throws WeixinException {
throws WeixinException {
return batchApi.syncUser(mediaId, callback); return batchApi.syncUser(mediaId, callback);
} }
/** /**
* 批量覆盖成员,本接口以userid为主键全量覆盖企业号通讯录成员任务完成后企业号通讯录成员与提交的文件完全保持一致 * 批量覆盖成员,本接口以userid为主键全量覆盖企业号通讯录成员任务完成后企业号通讯录成员与提交的文件完全保持一致
* <p> * <p>
* 1.模板中的部门需填写部门ID多个部门用分号分隔部门ID必须为数字</br> 2.文件中存在通讯录中也存在的成员完全以文件为准</br> * 1.模板中的部门需填写部门ID多个部门用分号分隔部门ID必须为数字</br>
* 2.文件中存在通讯录中也存在的成员完全以文件为准</br>
* 3.文件中存在通讯录中不存在的成员执行添加操作</br> * 3.文件中存在通讯录中不存在的成员执行添加操作</br>
* 4.通讯录中存在文件中不存在的成员执行删除操作出于安全考虑如果需要删除的成员多于50人 * 4.通讯录中存在文件中不存在的成员执行删除操作出于安全考虑如果需要删除的成员多于50人
* 且多于现有人数的20%以上系统将中止导入并返回相应的错误码 * 且多于现有人数的20%以上系统将中止导入并返回相应的错误码
@ -1210,8 +1183,7 @@ public class WeixinProxy {
* 批量覆盖成员</a> * 批量覆盖成员</a>
* @throws WeixinException * @throws WeixinException
*/ */
public String batchReplaceUser(String mediaId, Callback callback) public String batchReplaceUser(String mediaId, Callback callback) throws WeixinException {
throws WeixinException {
return batchApi.replaceUser(mediaId, callback); return batchApi.replaceUser(mediaId, callback);
} }
@ -1237,7 +1209,8 @@ public class WeixinProxy {
/** /**
* 批量覆盖部门,本接口以partyid为键全量覆盖企业号通讯录组织架构任务完成后企业号通讯录组织架构与提交的文件完全保持一致 * 批量覆盖部门,本接口以partyid为键全量覆盖企业号通讯录组织架构任务完成后企业号通讯录组织架构与提交的文件完全保持一致
* <p> * <p>
* 1.文件中存在通讯录中也存在的部门执行修改操作</br> 2.文件中存在通讯录中不存在的部门执行添加操作</br> * 1.文件中存在通讯录中也存在的部门执行修改操作</br>
* 2.文件中存在通讯录中不存在的部门执行添加操作</br>
* 3.文件中不存在通讯录中存在的部门当部门为空时执行删除操作</br> * 3.文件中不存在通讯录中存在的部门当部门为空时执行删除操作</br>
* 4.CSV文件中部门名称部门ID父部门ID为必填字段部门ID必须为数字排序为可选字段置空或填0不修改排序 * 4.CSV文件中部门名称部门ID父部门ID为必填字段部门ID必须为数字排序为可选字段置空或填0不修改排序
* </p> * </p>
@ -1254,8 +1227,7 @@ public class WeixinProxy {
* 批量覆盖部门</a> * 批量覆盖部门</a>
* @throws WeixinException * @throws WeixinException
*/ */
public String batchReplaceParty(String mediaId, Callback callback) public String batchReplaceParty(String mediaId, Callback callback) throws WeixinException {
throws WeixinException {
return batchApi.replaceParty(mediaId, callback); return batchApi.replaceParty(mediaId, callback);
} }
@ -1291,8 +1263,7 @@ public class WeixinProxy {
* "http://qydev.weixin.qq.com/wiki/index.php?title=Userid%E4%B8%8Eopenid%E4%BA%92%E6%8D%A2%E6%8E%A5%E5%8F%A3"> * "http://qydev.weixin.qq.com/wiki/index.php?title=Userid%E4%B8%8Eopenid%E4%BA%92%E6%8D%A2%E6%8E%A5%E5%8F%A3">
* userid转换成openid</a> * userid转换成openid</a>
*/ */
public String[] userid2openid(String userid, int agentid) public String[] userid2openid(String userid, int agentid) throws WeixinException {
throws WeixinException {
return userApi.userid2openid(userid, agentid); return userApi.userid2openid(userid, agentid);
} }
@ -1366,8 +1337,7 @@ public class WeixinProxy {
* 修改会话信息</a> * 修改会话信息</a>
* @throws WeixinException * @throws WeixinException
*/ */
public ApiResult updateChat(ChatInfo chatInfo, String operator, public ApiResult updateChat(ChatInfo chatInfo, String operator, List<String> addUsers, List<String> deleteUsers)
List<String> addUsers, List<String> deleteUsers)
throws WeixinException { throws WeixinException {
return chatApi.updateChat(chatInfo, operator, addUsers, deleteUsers); return chatApi.updateChat(chatInfo, operator, addUsers, deleteUsers);
} }
@ -1386,8 +1356,7 @@ public class WeixinProxy {
* 退出会话</a> * 退出会话</a>
* @throws WeixinException * @throws WeixinException
*/ */
public ApiResult quitChat(String chatId, String operator) public ApiResult quitChat(String chatId, String operator) throws WeixinException {
throws WeixinException {
return chatApi.quitChat(chatId, operator); return chatApi.quitChat(chatId, operator);
} }
@ -1407,8 +1376,7 @@ public class WeixinProxy {
* 清除会话未读状态</a> * 清除会话未读状态</a>
* @throws WeixinException * @throws WeixinException
*/ */
public ApiResult clearChatNotify(String targetId, String owner, public ApiResult clearChatNotify(String targetId, String owner, ChatType chatType) throws WeixinException {
ChatType chatType) throws WeixinException {
return chatApi.clearChatNotify(targetId, owner, chatType); return chatApi.clearChatNotify(targetId, owner, chatType);
} }
@ -1426,8 +1394,7 @@ public class WeixinProxy {
* @return 列表中不存在的成员剩余合法成员会继续执行 * @return 列表中不存在的成员剩余合法成员会继续执行
* @throws WeixinException * @throws WeixinException
*/ */
public List<String> setChatMute(List<ChatMute> chatMutes) public List<String> setChatMute(List<ChatMute> chatMutes) throws WeixinException {
throws WeixinException {
return chatApi.setChatMute(chatMutes); return chatApi.setChatMute(chatMutes);
} }
@ -1444,8 +1411,7 @@ public class WeixinProxy {
* 发送消息</a> * 发送消息</a>
* @throws WeixinException * @throws WeixinException
*/ */
public ApiResult sendChatMessage(ChatMessage message) public ApiResult sendChatMessage(ChatMessage message) throws WeixinException {
throws WeixinException {
return chatApi.sendChatMessage(message); return chatApi.sendChatMessage(message);
} }

View File

@ -98,7 +98,7 @@ public class NotifyApi extends QyApi {
String.format(message_send_uri, token.getAccessToken()), String.format(message_send_uri, token.getAccessToken()),
obj.toJSONString()); obj.toJSONString());
obj = response.getAsJson(); obj = response.getAsJson();
IdParameter idParameter = new IdParameter(); IdParameter idParameter = IdParameter.get();
if (obj.containsKey("invaliduser")) { if (obj.containsKey("invaliduser")) {
idParameter.setUserIds(Arrays.asList(obj.getString("invaliduser") idParameter.setUserIds(Arrays.asList(obj.getString("invaliduser")
.split(IdParameter.SEPARATORS))); .split(IdParameter.SEPARATORS)));

View File

@ -200,7 +200,7 @@ public class TagApi extends QyApi {
WeixinResponse response = weixinExecutor.post( WeixinResponse response = weixinExecutor.post(
String.format(uri, token.getAccessToken()), obj.toJSONString()); String.format(uri, token.getAccessToken()), obj.toJSONString());
obj = response.getAsJson(); obj = response.getAsJson();
IdParameter idParameter = new IdParameter(); IdParameter idParameter = IdParameter.get();
if (obj.containsKey("invalidlist")) { if (obj.containsKey("invalidlist")) {
idParameter.setUserIds(Arrays.asList(obj.getString("invalidlist") idParameter.setUserIds(Arrays.asList(obj.getString("invalidlist")
.split(IdParameter.SEPARATORS))); .split(IdParameter.SEPARATORS)));

View File

@ -48,7 +48,7 @@ public class NotifyMessage implements Serializable {
private IdParameter target; private IdParameter target;
public NotifyMessage(int agentid, NotifyTuple tuple) { public NotifyMessage(int agentid, NotifyTuple tuple) {
this(agentid, tuple, new IdParameter(), false); this(agentid, tuple, IdParameter.get(), false);
} }
public NotifyMessage(int agentId, NotifyTuple tuple, IdParameter target, boolean isSafe) { public NotifyMessage(int agentId, NotifyTuple tuple, IdParameter target, boolean isSafe) {

View File

@ -35,12 +35,16 @@ public class IdParameter implements Serializable {
@JSONField(name = "tag") @JSONField(name = "tag")
private List<Integer> tagIds; private List<Integer> tagIds;
public IdParameter() { protected IdParameter() {
this.userIds = new ArrayList<String>(); this.userIds = new ArrayList<String>();
this.partyIds = new ArrayList<Integer>(); this.partyIds = new ArrayList<Integer>();
this.tagIds = new ArrayList<Integer>(); this.tagIds = new ArrayList<Integer>();
} }
public static IdParameter get(){
return new IdParameter();
}
/** /**
* 增加成员ID列表最多支持1000个 * 增加成员ID列表最多支持1000个
* *

View File

@ -32,25 +32,27 @@ public class WeixinSuitePreCodeCreator extends TokenCreator {
* @param suiteId * @param suiteId
* 应用套件ID * 应用套件ID
*/ */
public WeixinSuitePreCodeCreator(TokenManager suiteTokenManager, public WeixinSuitePreCodeCreator(TokenManager suiteTokenManager, String suiteId) {
String suiteId) {
this.suiteTokenManager = suiteTokenManager; this.suiteTokenManager = suiteTokenManager;
this.suiteId = suiteId; this.suiteId = suiteId;
} }
@Override @Override
public String key0() { public String name() {
return String.format("qy_suite_precode_%s", suiteId); return "qy_suite_precode";
}
@Override
public String uniqueid() {
return suiteId;
} }
@Override @Override
public Token create() throws WeixinException { public Token create() throws WeixinException {
WeixinResponse response = weixinExecutor.post( WeixinResponse response = weixinExecutor.post(
String.format(URLConsts.SUITE_PRE_CODE_URL, String.format(URLConsts.SUITE_PRE_CODE_URL, suiteTokenManager.getAccessToken()),
suiteTokenManager.getAccessToken()),
String.format("{\"suite_id\":\"%s\"}", suiteId)); String.format("{\"suite_id\":\"%s\"}", suiteId));
JSONObject result = response.getAsJson(); JSONObject result = response.getAsJson();
return new Token(result.getString("pre_auth_code"), return new Token(result.getString("pre_auth_code"), result.getLongValue("expires_in") * 1000l);
result.getLongValue("expires_in") * 1000l);
} }
} }

View File

@ -15,8 +15,8 @@ import com.foxinmy.weixin4j.token.TokenCreator;
* @author jinyu(foxinmy@gmail.com) * @author jinyu(foxinmy@gmail.com)
* @date 2015年6月17日 * @date 2015年6月17日
* @since JDK 1.6 * @since JDK 1.6
* @see <a * @see <a href=
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E5.BA.94.E7.94.A8.E5.A5.97.E4.BB.B6.E4.BB.A4.E7.89.8C">获取应用套件凭证</a> * "http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E5.BA.94.E7.94.A8.E5.A5.97.E4.BB.B6.E4.BB.A4.E7.89.8C">获取应用套件凭证</a>
* @see com.foxinmy.weixin4j.model.Token * @see com.foxinmy.weixin4j.model.Token
*/ */
public class WeixinSuiteTokenCreator extends TokenCreator { public class WeixinSuiteTokenCreator extends TokenCreator {
@ -33,8 +33,13 @@ public class WeixinSuiteTokenCreator extends TokenCreator {
} }
@Override @Override
public String key0() { public String name() {
return String.format("qy_suite_token_%s", ticketManager.getThirdId()); return "qy_suite_token";
}
@Override
public String uniqueid() {
return ticketManager.getThirdId();
} }
@Override @Override
@ -43,10 +48,8 @@ public class WeixinSuiteTokenCreator extends TokenCreator {
obj.put("suite_id", ticketManager.getThirdId()); obj.put("suite_id", ticketManager.getThirdId());
obj.put("suite_secret", ticketManager.getThirdSecret()); obj.put("suite_secret", ticketManager.getThirdSecret());
obj.put("suite_ticket", ticketManager.getAccessTicket()); obj.put("suite_ticket", ticketManager.getAccessTicket());
WeixinResponse response = weixinExecutor.post( WeixinResponse response = weixinExecutor.post(URLConsts.SUITE_TOKEN_URL, obj.toJSONString());
URLConsts.SUITE_TOKEN_URL, obj.toJSONString());
obj = response.getAsJson(); obj = response.getAsJson();
return new Token(obj.getString("suite_access_token"), return new Token(obj.getString("suite_access_token"), obj.getLongValue("expires_in") * 1000l);
obj.getLongValue("expires_in") * 1000l);
} }
} }

View File

@ -33,16 +33,19 @@ public class WeixinTokenSuiteCreator extends TokenCreator {
* @param suiteTokenManager * @param suiteTokenManager
* 第三方套件凭证token * 第三方套件凭证token
*/ */
public WeixinTokenSuiteCreator(PerTicketManager perTicketManager, public WeixinTokenSuiteCreator(PerTicketManager perTicketManager, TokenManager suiteTokenManager) {
TokenManager suiteTokenManager) {
this.perTicketManager = perTicketManager; this.perTicketManager = perTicketManager;
this.suiteTokenManager = suiteTokenManager; this.suiteTokenManager = suiteTokenManager;
} }
@Override @Override
public String key0() { public String name() {
return String.format("qy_token_suite_%s_%s", return String.format("qy_token_suite_%s_%s", perTicketManager.getThirdId(), perTicketManager.getAuthAppId());
perTicketManager.getThirdId(), perTicketManager.getAuthAppId()); }
@Override
public String uniqueid() {
throw new UnsupportedOperationException();
} }
@Override @Override
@ -51,11 +54,9 @@ public class WeixinTokenSuiteCreator extends TokenCreator {
obj.put("suite_id", perTicketManager.getThirdId()); obj.put("suite_id", perTicketManager.getThirdId());
obj.put("auth_corpid", perTicketManager.getAuthAppId()); obj.put("auth_corpid", perTicketManager.getAuthAppId());
obj.put("permanent_code", perTicketManager.getAccessTicket()); obj.put("permanent_code", perTicketManager.getAccessTicket());
WeixinResponse response = weixinExecutor.post( WeixinResponse response = weixinExecutor
String.format(URLConsts.TOKEN_SUITE_URL, .post(String.format(URLConsts.TOKEN_SUITE_URL, suiteTokenManager.getAccessToken()), obj.toJSONString());
suiteTokenManager.getAccessToken()), obj.toJSONString());
obj = response.getAsJson(); obj = response.getAsJson();
return new Token(obj.getString("access_token"), return new Token(obj.getString("access_token"), obj.getLongValue("expires_in") * 1000l);
obj.getLongValue("expires_in") * 1000l);
} }
} }

View File

@ -37,8 +37,13 @@ public class WeixinProviderTokenCreator extends TokenCreator {
} }
@Override @Override
public String key0() { public String name() {
return String.format("qy_provider_token_%s", corpid); return "qy_provider_token";
}
@Override
public String uniqueid() {
return corpid;
} }
@Override @Override
@ -46,10 +51,8 @@ public class WeixinProviderTokenCreator extends TokenCreator {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("corpid", corpid); obj.put("corpid", corpid);
obj.put("provider_secret", providersecret); obj.put("provider_secret", providersecret);
WeixinResponse response = weixinExecutor.post( WeixinResponse response = weixinExecutor.post(URLConsts.PROVIDER_TOKEN_URL, obj.toJSONString());
URLConsts.PROVIDER_TOKEN_URL, obj.toJSONString());
obj = response.getAsJson(); obj = response.getAsJson();
return new Token(obj.getString("provider_access_token"), return new Token(obj.getString("provider_access_token"), obj.getLongValue("expires_in") * 1000l);
obj.getLongValue("expires_in") * 1000l);
} }
} }

View File

@ -22,45 +22,42 @@ import com.foxinmy.weixin4j.type.TicketType;
*/ */
public class WeixinTicketCreator extends TokenCreator { public class WeixinTicketCreator extends TokenCreator {
private final String corpid;
private final TicketType ticketType; private final TicketType ticketType;
private final TokenManager weixinTokenManager; private final TokenManager weixinTokenManager;
/** /**
* @param corpid
* 企业号ID
* @param ticketType * @param ticketType
* 票据类型 * 票据类型
* @param weixinTokenManager * @param weixinTokenManager
* <font color="red">企业号的access_token</font> * <font color="red">企业号的access_token</font>
*/ */
public WeixinTicketCreator(String corpid, TicketType ticketType, public WeixinTicketCreator(TicketType ticketType, TokenManager weixinTokenManager) {
TokenManager weixinTokenManager) {
this.corpid = corpid;
this.ticketType = ticketType; this.ticketType = ticketType;
this.weixinTokenManager = weixinTokenManager; this.weixinTokenManager = weixinTokenManager;
} }
@Override @Override
public String key0() { public String name() {
return String.format("qy_ticket_%s_%s", ticketType.name(), corpid); return String.format("qy_ticket_%s", ticketType.name());
}
@Override
public String uniqueid() {
return weixinTokenManager.getWeixinId();
} }
@Override @Override
public Token create() throws WeixinException { public Token create() throws WeixinException {
WeixinResponse response = null; WeixinResponse response = null;
if (ticketType == TicketType.jsapi) { if (ticketType == TicketType.jsapi) {
response = weixinExecutor.get(String.format( response = weixinExecutor
URLConsts.JS_TICKET_URL, weixinTokenManager.getCache() .get(String.format(URLConsts.JS_TICKET_URL, weixinTokenManager.getCache().getAccessToken()));
.getAccessToken()));
} else { } else {
response = weixinExecutor.get(String.format( response = weixinExecutor.get(String.format(URLConsts.SUITE_TICKET_URL,
URLConsts.SUITE_TICKET_URL, weixinTokenManager.getCache() weixinTokenManager.getCache().getAccessToken(), ticketType.name()));
.getAccessToken(), ticketType.name()));
} }
JSONObject result = response.getAsJson(); JSONObject result = response.getAsJson();
return new Token(result.getString("ticket"), return new Token(result.getString("ticket"), result.getLong("expires_in") * 1000l).pushExtra("group_id",
result.getLong("expires_in") * 1000l).pushExtra("group_id",
result.getString("group_id")); result.getString("group_id"));
} }
} }

View File

@ -37,17 +37,20 @@ public class WeixinTokenCreator extends TokenCreator {
} }
@Override @Override
public String key0() { public String name() {
return String.format("qy_token_%s", corpid); return "qy_token";
}
@Override
public String uniqueid() {
return corpid;
} }
@Override @Override
public Token create() throws WeixinException { public Token create() throws WeixinException {
String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, corpid, String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, corpid, corpsecret);
corpsecret);
WeixinResponse response = weixinExecutor.get(tokenUrl); WeixinResponse response = weixinExecutor.get(tokenUrl);
JSONObject result = response.getAsJson(); JSONObject result = response.getAsJson();
return new Token(result.getString("access_token"), return new Token(result.getString("access_token"), result.getLongValue("expires_in") * 1000l);
result.getLongValue("expires_in") * 1000l);
} }
} }