update something
This commit is contained in:
parent
e8422b1076
commit
dbdc5eee72
@ -56,6 +56,17 @@ public class WeixinSuiteProxy {
|
||||
this.suiteApi = new SuiteApi(suiteId, suiteSecret, tokenStorager);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param suiteTicketHolder
|
||||
* 套件ticket存取
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public WeixinSuiteProxy(SuiteTicketHolder suiteTicketHolder)
|
||||
throws WeixinException {
|
||||
this.suiteApi = new SuiteApi(suiteTicketHolder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用套件token
|
||||
*
|
||||
@ -100,7 +111,7 @@ public class WeixinSuiteProxy {
|
||||
* @return 企业号token
|
||||
*/
|
||||
public TokenHolder crateTokenHolder(String authCorpid) {
|
||||
return suiteApi.crateTokenHolder(authCorpid);
|
||||
return suiteApi.createTokenHolder(authCorpid);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -47,10 +47,6 @@ public class SuiteApi extends QyApi {
|
||||
* 应用套件pre_code
|
||||
*/
|
||||
private final TokenHolder suitePreCodeHolder;
|
||||
/**
|
||||
* 应用套件ID
|
||||
*/
|
||||
private final String suiteId;
|
||||
|
||||
public SuiteApi() throws WeixinException {
|
||||
this(DEFAULT_WEIXIN_ACCOUNT.getSuiteId(), DEFAULT_WEIXIN_ACCOUNT
|
||||
@ -67,22 +63,32 @@ public class SuiteApi extends QyApi {
|
||||
* 应用ID
|
||||
* @param suiteSecret
|
||||
* 应用secret
|
||||
* @param ticketStorager
|
||||
* 应用ticket存储器(用于读取)
|
||||
* @param tokenStorager
|
||||
* 应用token存储器
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public SuiteApi(String suiteId, String suiteSecret,
|
||||
TokenStorager tokenStorager) throws WeixinException {
|
||||
this.suiteTicketHolder = new SuiteTicketHolder(tokenStorager);
|
||||
this(new SuiteTicketHolder(suiteId, suiteSecret, tokenStorager));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param suiteTicketHolder
|
||||
* 套件ticket存取
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public SuiteApi(SuiteTicketHolder suiteTicketHolder) throws WeixinException {
|
||||
this.suiteTicketHolder = suiteTicketHolder;
|
||||
this.suiteTokenHolder = new TokenHolder(new WeixinSuiteTokenCreator(
|
||||
suiteId, suiteSecret, suiteTicketHolder), tokenStorager);
|
||||
suiteTicketHolder), suiteTicketHolder.getTokenStorager());
|
||||
this.suitePreCodeHolder = new TokenHolder(
|
||||
new WeixinSuitePreCodeCreator(suiteTokenHolder, suiteId),
|
||||
tokenStorager);
|
||||
this.suitePerCodeHolder = new SuitePerCodeHolder(tokenStorager);
|
||||
this.suiteId = suiteId;
|
||||
new WeixinSuitePreCodeCreator(suiteTokenHolder,
|
||||
suiteTicketHolder.getSuiteId()),
|
||||
suiteTicketHolder.getTokenStorager());
|
||||
this.suitePerCodeHolder = new SuitePerCodeHolder(
|
||||
suiteTicketHolder.getSuiteId(),
|
||||
suiteTicketHolder.getTokenStorager());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -128,8 +134,8 @@ public class SuiteApi extends QyApi {
|
||||
* 授权方corpid
|
||||
* @return 企业号token
|
||||
*/
|
||||
public TokenHolder crateTokenHolder(String authCorpid) {
|
||||
return new TokenHolder(new WeixinTokenSuiteCreator(suiteId, authCorpid,
|
||||
public TokenHolder createTokenHolder(String authCorpid) {
|
||||
return new TokenHolder(new WeixinTokenSuiteCreator(authCorpid,
|
||||
suitePerCodeHolder), suiteTicketHolder.getTokenStorager());
|
||||
}
|
||||
|
||||
@ -147,7 +153,7 @@ public class SuiteApi extends QyApi {
|
||||
public JsonResult setSuiteSession(int... appids) throws WeixinException {
|
||||
String suite_set_session_uri = getRequestUri("suite_set_session_uri");
|
||||
JSONObject para = new JSONObject();
|
||||
para.put("pre_auth_code", suiteTicketHolder.getTicket(suiteId));
|
||||
para.put("pre_auth_code", suiteTicketHolder.getTicket());
|
||||
para.put("session_info", appids);
|
||||
WeixinResponse response = weixinClient
|
||||
.post(String.format(suite_set_session_uri,
|
||||
@ -171,7 +177,7 @@ public class SuiteApi extends QyApi {
|
||||
throws WeixinException {
|
||||
String suite_get_permanent_uri = getRequestUri("suite_get_permanent_uri");
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("suite_id", suiteId);
|
||||
obj.put("suite_id", suiteTicketHolder.getSuiteId());
|
||||
obj.put("auth_code", authCode);
|
||||
WeixinResponse response = weixinClient.post(
|
||||
String.format(suite_get_permanent_uri,
|
||||
@ -181,7 +187,7 @@ public class SuiteApi extends QyApi {
|
||||
obj.put("user_info", obj.remove("auth_user_info"));
|
||||
OUserInfo oInfo = JSON.toJavaObject(obj, OUserInfo.class);
|
||||
// 缓存微信企业号access_token
|
||||
TokenCreator tokenCreator = new WeixinTokenSuiteCreator(suiteId, null,
|
||||
TokenCreator tokenCreator = new WeixinTokenSuiteCreator(null,
|
||||
suitePerCodeHolder);
|
||||
Token token = new Token(obj.getString("access_token"));
|
||||
token.setExpiresIn(obj.getIntValue("expires_in"));
|
||||
@ -189,8 +195,8 @@ public class SuiteApi extends QyApi {
|
||||
suiteTicketHolder.getTokenStorager().caching(
|
||||
tokenCreator.getCacheKey(), token);
|
||||
// 缓存微信企业号永久授权码
|
||||
suitePerCodeHolder.cachingPermanentCode(suiteId,
|
||||
obj.getString("permanent_code"));
|
||||
suitePerCodeHolder
|
||||
.cachingPermanentCode(obj.getString("permanent_code"));
|
||||
return oInfo;
|
||||
}
|
||||
|
||||
@ -208,9 +214,9 @@ public class SuiteApi extends QyApi {
|
||||
public OUserInfo getOAuthInfo(String authCorpid) throws WeixinException {
|
||||
String suite_get_authinfo_uri = getRequestUri("suite_get_authinfo_uri");
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("suite_id", suiteId);
|
||||
obj.put("suite_id", suiteTicketHolder.getSuiteId());
|
||||
obj.put("auth_corpid", authCorpid);
|
||||
obj.put("permanent_code", suitePerCodeHolder.getPermanentCode(suiteId));
|
||||
obj.put("permanent_code", suitePerCodeHolder.getPermanentCode());
|
||||
WeixinResponse response = weixinClient.post(
|
||||
String.format(suite_get_authinfo_uri,
|
||||
suiteTokenHolder.getAccessToken()), obj.toJSONString());
|
||||
@ -235,9 +241,9 @@ public class SuiteApi extends QyApi {
|
||||
throws WeixinException {
|
||||
String suite_get_agent_uri = getRequestUri("suite_get_agent_uri");
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("suite_id", suiteId);
|
||||
obj.put("suite_id", suiteTicketHolder.getSuiteId());
|
||||
obj.put("auth_corpid", authCorpid);
|
||||
obj.put("permanent_code", suitePerCodeHolder.getPermanentCode(suiteId));
|
||||
obj.put("permanent_code", suitePerCodeHolder.getPermanentCode());
|
||||
obj.put("agentid", agentid);
|
||||
WeixinResponse response = weixinClient.post(
|
||||
String.format(suite_get_agent_uri,
|
||||
@ -272,9 +278,9 @@ public class SuiteApi extends QyApi {
|
||||
throws WeixinException {
|
||||
String suite_set_agent_uri = getRequestUri("suite_set_agent_uri");
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("suite_id", suiteId);
|
||||
obj.put("suite_id", suiteTicketHolder.getSuiteId());
|
||||
obj.put("auth_corpid", authCorpid);
|
||||
obj.put("permanent_code", suitePerCodeHolder.getPermanentCode(suiteId));
|
||||
obj.put("permanent_code", suitePerCodeHolder.getPermanentCode());
|
||||
obj.put("agent", agentSet);
|
||||
WeixinResponse response = weixinClient.post(
|
||||
String.format(suite_set_agent_uri,
|
||||
|
||||
@ -36,8 +36,8 @@ public class Party implements Serializable {
|
||||
|
||||
}
|
||||
|
||||
public Party(String name) {
|
||||
this(0, name, 1, 1);
|
||||
public Party(int id, String name) {
|
||||
this(id, name, 0, 0);
|
||||
}
|
||||
|
||||
public Party(int id, String name, int parentid, int order) {
|
||||
|
||||
@ -33,7 +33,8 @@ public class User implements Serializable {
|
||||
/**
|
||||
* 非必须 成员所属部门id列表。注意,每个部门的直属员工上限为1000个
|
||||
*/
|
||||
private List<Integer> department;
|
||||
@JSONField(name = "department")
|
||||
private List<Integer> partys;
|
||||
/**
|
||||
* 非必须 职位信息。长度为0~64个字符
|
||||
*/
|
||||
@ -123,16 +124,16 @@ public class User implements Serializable {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<Integer> getDepartment() {
|
||||
return department;
|
||||
public List<Integer> getPartys() {
|
||||
return partys;
|
||||
}
|
||||
|
||||
public void setDepartment(List<Integer> department) {
|
||||
this.department = department;
|
||||
public void setPartys(List<Integer> partys) {
|
||||
this.partys = partys;
|
||||
}
|
||||
|
||||
public void setDepartment(Integer... department) {
|
||||
this.department = Arrays.asList(department);
|
||||
public void setPartys(Integer... partys) {
|
||||
this.partys = Arrays.asList(partys);
|
||||
}
|
||||
|
||||
public String getPosition() {
|
||||
@ -252,8 +253,8 @@ public class User implements Serializable {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User [userid=" + userid + ", name=" + name + ", department="
|
||||
+ department + ", position=" + position + ", mobile=" + mobile
|
||||
return "User [userid=" + userid + ", name=" + name + ", partys="
|
||||
+ partys + ", position=" + position + ", mobile=" + mobile
|
||||
+ ", gender=" + getFormatGender() + ", tel=" + tel + ", email="
|
||||
+ email + ", weixinid=" + weixinid + ", avatar=" + avatar
|
||||
+ ", status=" + getFormatStatus() + ", enable="
|
||||
|
||||
@ -15,44 +15,47 @@ import com.foxinmy.weixin4j.token.TokenStorager;
|
||||
*/
|
||||
public class SuitePerCodeHolder {
|
||||
|
||||
public final TokenStorager tokenStorager;
|
||||
private final String suiteId;
|
||||
private final TokenStorager tokenStorager;
|
||||
|
||||
public SuitePerCodeHolder(TokenStorager tokenStorager) {
|
||||
public SuitePerCodeHolder(String suiteId, TokenStorager tokenStorager) {
|
||||
this.suiteId = suiteId;
|
||||
this.tokenStorager = tokenStorager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存永久授权码
|
||||
*
|
||||
* @param suiteId
|
||||
* @param permanentCode
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public void cachingPermanentCode(String suiteId, String permanentCode)
|
||||
public void cachingPermanentCode(String permanentCode)
|
||||
throws WeixinException {
|
||||
Token token = new Token(permanentCode);
|
||||
token.setExpiresIn(-1);
|
||||
tokenStorager.caching(getCacheKey(suiteId), token);
|
||||
tokenStorager.caching(getCacheKey(), token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取永久授权码的key
|
||||
*
|
||||
* @param suiteId
|
||||
* @return
|
||||
*/
|
||||
private String getCacheKey(String suiteId) {
|
||||
private String getCacheKey() {
|
||||
return String.format("qy_suite_percode_%s", suiteId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找永久二维码
|
||||
*
|
||||
* @param suiteId
|
||||
* @return
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String getPermanentCode(String suiteId) throws WeixinException {
|
||||
return tokenStorager.lookup(getCacheKey(suiteId)).getAccessToken();
|
||||
public String getPermanentCode() throws WeixinException {
|
||||
return tokenStorager.lookup(getCacheKey()).getAccessToken();
|
||||
}
|
||||
|
||||
public String getSuiteId() {
|
||||
return this.suiteId;
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,9 +15,14 @@ import com.foxinmy.weixin4j.token.TokenStorager;
|
||||
*/
|
||||
public class SuiteTicketHolder {
|
||||
|
||||
private final String suiteId;
|
||||
private final String suiteSecret;
|
||||
private final TokenStorager tokenStorager;
|
||||
|
||||
public SuiteTicketHolder(TokenStorager tokenStorager) {
|
||||
public SuiteTicketHolder(String suiteId, String suiteSecret,
|
||||
TokenStorager tokenStorager) {
|
||||
this.suiteId = suiteId;
|
||||
this.suiteSecret = suiteSecret;
|
||||
this.tokenStorager = tokenStorager;
|
||||
}
|
||||
|
||||
@ -28,8 +33,8 @@ public class SuiteTicketHolder {
|
||||
* @return
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String getTicket(String suiteId) throws WeixinException {
|
||||
return tokenStorager.lookup(getCacheKey(suiteId)).getAccessToken();
|
||||
public String getTicket() throws WeixinException {
|
||||
return tokenStorager.lookup(getCacheKey()).getAccessToken();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -38,7 +43,7 @@ public class SuiteTicketHolder {
|
||||
* @param suiteId
|
||||
* @return
|
||||
*/
|
||||
private String getCacheKey(String suiteId) {
|
||||
private String getCacheKey() {
|
||||
return String.format("qy_suite_ticket_%s", suiteId);
|
||||
}
|
||||
|
||||
@ -52,7 +57,15 @@ public class SuiteTicketHolder {
|
||||
throws WeixinException {
|
||||
Token token = new Token(suiteTicket.getSuiteTicket());
|
||||
token.setExpiresIn(-1);
|
||||
tokenStorager.caching(getCacheKey(suiteTicket.getSuiteId()), token);
|
||||
tokenStorager.caching(getCacheKey(), token);
|
||||
}
|
||||
|
||||
public String getSuiteId() {
|
||||
return this.suiteId;
|
||||
}
|
||||
|
||||
public String getSuiteSecret() {
|
||||
return this.suiteSecret;
|
||||
}
|
||||
|
||||
public TokenStorager getTokenStorager() {
|
||||
|
||||
@ -22,38 +22,29 @@ import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
public class WeixinSuiteTokenCreator implements TokenCreator {
|
||||
|
||||
private final WeixinHttpClient httpClient;
|
||||
private final String suiteId;
|
||||
private final String suiteSecret;
|
||||
private final SuiteTicketHolder ticketHolder;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param suiteId
|
||||
* 套件ID
|
||||
* @param suiteSecret
|
||||
* 套件secret
|
||||
* @param stringStorager
|
||||
* 套件ticket存取器
|
||||
*/
|
||||
public WeixinSuiteTokenCreator(String suiteId, String suiteSecret,
|
||||
SuiteTicketHolder ticketHolder) {
|
||||
this.suiteId = suiteId;
|
||||
this.suiteSecret = suiteSecret;
|
||||
public WeixinSuiteTokenCreator(SuiteTicketHolder ticketHolder) {
|
||||
this.ticketHolder = ticketHolder;
|
||||
this.httpClient = new WeixinHttpClient();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey() {
|
||||
return String.format("qy_suite_token_%s", suiteId);
|
||||
return String.format("qy_suite_token_%s", ticketHolder.getSuiteId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("suite_id", suiteId);
|
||||
obj.put("suite_secret", suiteSecret);
|
||||
obj.put("suite_ticket", ticketHolder.getTicket(suiteId));
|
||||
obj.put("suite_id", ticketHolder.getSuiteId());
|
||||
obj.put("suite_secret", ticketHolder.getSuiteSecret());
|
||||
obj.put("suite_ticket", ticketHolder.getTicket());
|
||||
WeixinResponse response = httpClient.post(URLConsts.SUITE_TOKEN_URL,
|
||||
obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
|
||||
@ -22,22 +22,18 @@ import com.foxinmy.weixin4j.token.TokenCreator;
|
||||
public class WeixinTokenSuiteCreator implements TokenCreator {
|
||||
|
||||
private final WeixinHttpClient httpClient;
|
||||
private final String suiteId;
|
||||
private final String authCorpid;
|
||||
private final SuitePerCodeHolder perCodeHolder;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param suiteId
|
||||
* 应用套件ID
|
||||
* @param suiteSecret
|
||||
* 授权方corpid
|
||||
* @param perCodeHolder
|
||||
* 永久授权码
|
||||
*/
|
||||
public WeixinTokenSuiteCreator(String suiteId, String authCorpid,
|
||||
public WeixinTokenSuiteCreator(String authCorpid,
|
||||
SuitePerCodeHolder perCodeHolder) {
|
||||
this.suiteId = suiteId;
|
||||
this.authCorpid = authCorpid;
|
||||
this.perCodeHolder = perCodeHolder;
|
||||
this.httpClient = new WeixinHttpClient();
|
||||
@ -45,15 +41,15 @@ public class WeixinTokenSuiteCreator implements TokenCreator {
|
||||
|
||||
@Override
|
||||
public String getCacheKey() {
|
||||
return String.format("qy_token_suite_%s", suiteId);
|
||||
return String.format("qy_token_suite_%s", perCodeHolder.getSuiteId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("suite_id", suiteId);
|
||||
obj.put("suite_id", perCodeHolder.getSuiteId());
|
||||
obj.put("auth_corpid", authCorpid);
|
||||
obj.put("permanent_code", perCodeHolder.getPermanentCode(suiteId));
|
||||
obj.put("permanent_code", perCodeHolder.getPermanentCode());
|
||||
WeixinResponse response = httpClient.post(URLConsts.TOKEN_SUITE_URL,
|
||||
obj.toJSONString());
|
||||
obj = response.getAsJson();
|
||||
|
||||
@ -30,15 +30,14 @@ public class PartyTest extends TokenTest {
|
||||
|
||||
@Test
|
||||
public void create() throws WeixinException {
|
||||
Party Party = new Party("苦逼组");
|
||||
Party Party = new Party(1, "苦逼组");
|
||||
int id = partyApi.createParty(Party);
|
||||
Assert.assertTrue(id > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() throws WeixinException {
|
||||
Party Party = new Party("苦逼组111");
|
||||
Party.setId(2);
|
||||
Party Party = new Party(2, "苦逼组111");
|
||||
JsonResult result = partyApi.updateParty(Party);
|
||||
Assert.assertEquals("updated", result.getDesc());
|
||||
}
|
||||
|
||||
@ -37,7 +37,7 @@ public class UserTest extends TokenTest {
|
||||
public void create() throws WeixinException {
|
||||
User user = new User("u001", "jack");
|
||||
user.setMobile("13500000000");
|
||||
user.setDepartment(1);
|
||||
user.setPartys(1);
|
||||
user.pushExattr("爱好", "code");
|
||||
JsonResult result = userApi.createUser(user);
|
||||
Assert.assertEquals("created", result.getDesc());
|
||||
@ -47,7 +47,7 @@ public class UserTest extends TokenTest {
|
||||
public void batchUpload() throws WeixinException {
|
||||
User user = new User("u001", "jack");
|
||||
user.setMobile("13500000000");
|
||||
user.setDepartment(1);
|
||||
user.setPartys(1);
|
||||
String mediaId = mediaApi.batchUploadUsers(Arrays.asList(user));
|
||||
System.err.println(mediaId);
|
||||
}
|
||||
@ -56,7 +56,7 @@ public class UserTest extends TokenTest {
|
||||
public void update() throws WeixinException {
|
||||
User user = new User("u001", "ted");
|
||||
user.setMobile("13500000000");
|
||||
user.setDepartment(1);
|
||||
user.setPartys(1);
|
||||
user.pushExattr("爱好", "code");
|
||||
JsonResult result = userApi.updateUser(user);
|
||||
Assert.assertEquals("updated", result.getDesc());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user