Skip to content

Commit 5b69d91

Browse files
committed
binarywang#520 企业微信网页授权增加使用user_ticket获取成员详情的接口
1 parent 61e3163 commit 5b69d91

File tree

9 files changed

+137
-44
lines changed

9 files changed

+137
-44
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.chanjar.weixin.cp.api;
22

33
import me.chanjar.weixin.common.exception.WxErrorException;
4+
import me.chanjar.weixin.cp.bean.WxCpUserDetail;
45

56
/**
67
* <pre>
@@ -64,4 +65,19 @@ public interface WxCpOAuth2Service {
6465
*/
6566
String[] getUserInfo(Integer agentId, String code) throws WxErrorException;
6667

68+
/**
69+
* <pre>
70+
* 使用user_ticket获取成员详情.
71+
*
72+
* 文档地址:https://work.weixin.qq.com/api/doc#10028/%E4%BD%BF%E7%94%A8user_ticket%E8%8E%B7%E5%8F%96%E6%88%90%E5%91%98%E8%AF%A6%E6%83%85
73+
* 请求方式:POST(HTTPS)
74+
* 请求地址:https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail?access_token=ACCESS_TOKEN
75+
*
76+
* 权限说明:
77+
* 需要有对应应用的使用权限,且成员必须在授权应用的可见范围内。
78+
* </pre>
79+
*
80+
* @param userTicket 成员票据
81+
*/
82+
WxCpUserDetail getUserDetail(String userTicket) throws WxErrorException;
6783
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.chanjar.weixin.cp.api.impl;
22

3+
import com.google.gson.GsonBuilder;
34
import com.google.gson.JsonElement;
45
import com.google.gson.JsonObject;
56
import com.google.gson.JsonParser;
@@ -8,6 +9,7 @@
89
import me.chanjar.weixin.common.util.json.GsonHelper;
910
import me.chanjar.weixin.cp.api.WxCpOAuth2Service;
1011
import me.chanjar.weixin.cp.api.WxCpService;
12+
import me.chanjar.weixin.cp.bean.WxCpUserDetail;
1113

1214
/**
1315
* <pre>
@@ -52,9 +54,8 @@ public String[] getUserInfo(String code) throws WxErrorException {
5254

5355
@Override
5456
public String[] getUserInfo(Integer agentId, String code) throws WxErrorException {
55-
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?"
56-
+ "code=" + code
57-
+ "&agentid=" + agentId;
57+
String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?code=%s&agentid=%d",
58+
code, agentId);
5859
String responseText = this.mainService.get(url, null);
5960
JsonElement je = new JsonParser().parse(responseText);
6061
JsonObject jo = je.getAsJsonObject();
@@ -63,4 +64,12 @@ public String[] getUserInfo(Integer agentId, String code) throws WxErrorExceptio
6364
GsonHelper.getString(jo, "OpenId")};
6465
}
6566

67+
@Override
68+
public WxCpUserDetail getUserDetail(String userTicket) throws WxErrorException {
69+
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail";
70+
JsonObject param = new JsonObject();
71+
param.addProperty("user_ticket", userTicket);
72+
String responseText = this.mainService.post(url, param.toString());
73+
return new GsonBuilder().create().fromJson(responseText, WxCpUserDetail.class);
74+
}
6675
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package me.chanjar.weixin.cp.bean;
2+
3+
/**
4+
* <pre>
5+
* 性别枚举
6+
* Created by BinaryWang on 2018/4/22.
7+
* </pre>
8+
*
9+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
10+
*/
11+
public enum Gender {
12+
/**
13+
* 男
14+
*/
15+
MALE("男", "1"),
16+
/**
17+
* 女
18+
*/
19+
FEMALE("女", "2");
20+
21+
private String genderName;
22+
private String code;
23+
24+
Gender(String genderName, String code) {
25+
this.genderName = genderName;
26+
this.code = code;
27+
}
28+
29+
public String getGenderName() {
30+
return this.genderName;
31+
}
32+
33+
public String getCode() {
34+
return this.code;
35+
}
36+
37+
public static Gender fromCode(String code) {
38+
if ("1".equals(code)) {
39+
return Gender.MALE;
40+
}
41+
if ("2".equals(code)) {
42+
return Gender.FEMALE;
43+
}
44+
45+
return null;
46+
}
47+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpUser.java

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,6 @@
1515
*/
1616
@Data
1717
public class WxCpUser implements Serializable {
18-
public enum Gender {
19-
/**
20-
* 男
21-
*/
22-
MALE("男", "1"),
23-
/**
24-
* 女
25-
*/
26-
FEMALE("女", "2");
27-
28-
private String genderName;
29-
private String code;
30-
31-
Gender(String genderName, String code) {
32-
this.genderName = genderName;
33-
this.code = code;
34-
}
35-
36-
public String getGenderName() {
37-
return this.genderName;
38-
}
39-
40-
public String getCode() {
41-
return this.code;
42-
}
43-
44-
public static Gender fromCode(String code) {
45-
if ("1".equals(code)) {
46-
return Gender.MALE;
47-
}
48-
if ("2".equals(code)) {
49-
return Gender.FEMALE;
50-
}
51-
52-
return null;
53-
}
54-
}
55-
5618
private static final long serialVersionUID = -5696099236344075582L;
5719
private String userId;
5820
private String name;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package me.chanjar.weixin.cp.bean;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
6+
/**
7+
* <pre>
8+
* 使用user_ticket获取成员详情接口返回类.
9+
* Created by BinaryWang on 2018/4/22.
10+
* </pre>
11+
*
12+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
13+
*/
14+
@Data
15+
public class WxCpUserDetail {
16+
@SerializedName("userid")
17+
private String userId;
18+
private String name;
19+
private String mobile;
20+
private String gender;
21+
private String email;
22+
@SerializedName("qr_code")
23+
private String qrCode;
24+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpUserGsonAdapter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import com.google.gson.*;
1212
import me.chanjar.weixin.common.util.json.GsonHelper;
13+
import me.chanjar.weixin.cp.bean.Gender;
1314
import me.chanjar.weixin.cp.bean.WxCpUser;
1415

1516
import java.lang.reflect.Type;
@@ -39,7 +40,7 @@ public WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationC
3940
user.setName(GsonHelper.getString(o, "name"));
4041
user.setPosition(GsonHelper.getString(o, "position"));
4142
user.setMobile(GsonHelper.getString(o, "mobile"));
42-
user.setGender(WxCpUser.Gender.fromCode(GsonHelper.getString(o, "gender")));
43+
user.setGender(Gender.fromCode(GsonHelper.getString(o, "gender")));
4344
user.setEmail(GsonHelper.getString(o, "email"));
4445
user.setAvatar(GsonHelper.getString(o, "avatar"));
4546
user.setStatus(GsonHelper.getInteger(o, "status"));

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void testList(Integer id) throws Exception {
5656
}
5757
}
5858

59-
@Test(dependsOnMethods = {"testListAll", "testCreate"})
59+
@Test(dependsOnMethods = {"testList", "testCreate"})
6060
public void testUpdate() throws Exception {
6161
System.out.println("=================更新部门");
6262
this.depart.setName("子部门改名" + System.currentTimeMillis());
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package me.chanjar.weixin.cp.api.impl;
2+
3+
import com.google.inject.Inject;
4+
import me.chanjar.weixin.common.exception.WxErrorException;
5+
import me.chanjar.weixin.cp.api.ApiTestModule;
6+
import me.chanjar.weixin.cp.api.WxCpService;
7+
import me.chanjar.weixin.cp.bean.WxCpUserDetail;
8+
import org.testng.annotations.Guice;
9+
import org.testng.annotations.Test;
10+
11+
/**
12+
* <pre>
13+
* Created by BinaryWang on 2018/4/22.
14+
* </pre>
15+
*
16+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
17+
*/
18+
@Guice(modules = ApiTestModule.class)
19+
public class WxCpOAuth2ServiceImplTest {
20+
@Inject
21+
private WxCpService wxService;
22+
23+
@Test
24+
public void testGetUserDetail() throws WxErrorException {
25+
WxCpUserDetail userDetail = this.wxService.getOauth2Service().getUserDetail("b");
26+
System.out.println(userDetail);
27+
}
28+
29+
@Test
30+
public void testGetUserInfo() throws WxErrorException {
31+
this.wxService.getOauth2Service().getUserInfo("abc");
32+
}
33+
}

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImplTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.inject.Inject;
44
import me.chanjar.weixin.cp.api.ApiTestModule;
55
import me.chanjar.weixin.cp.api.WxCpService;
6+
import me.chanjar.weixin.cp.bean.Gender;
67
import me.chanjar.weixin.cp.bean.WxCpUser;
78
import org.apache.commons.lang3.builder.ToStringBuilder;
89
import org.apache.commons.lang3.builder.ToStringStyle;
@@ -40,7 +41,7 @@ public void testCreate() throws Exception {
4041
user.setName("Some Woman");
4142
user.setDepartIds(new Integer[]{2});
4243
user.setEmail("none@none.com");
43-
user.setGender(WxCpUser.Gender.FEMALE);
44+
user.setGender(Gender.FEMALE);
4445
user.setMobile("13560084979");
4546
user.setPosition("woman");
4647
user.setTelephone("3300393");

0 commit comments

Comments
 (0)