#520 企业微信网页授权增加使用user_ticket获取成员详情的接口

This commit is contained in:
Binary Wang 2018-04-22 22:35:47 +08:00
parent 61e3163f48
commit 5b69d91e65
9 changed files with 137 additions and 44 deletions

View File

@ -1,6 +1,7 @@
package me.chanjar.weixin.cp.api;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpUserDetail;
/**
* <pre>
@ -64,4 +65,19 @@ public interface WxCpOAuth2Service {
*/
String[] getUserInfo(Integer agentId, String code) throws WxErrorException;
/**
* <pre>
* 使用user_ticket获取成员详情.
*
* 文档地址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
* 请求方式POSTHTTPS
* 请求地址https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail?access_token=ACCESS_TOKEN
*
* 权限说明
* 需要有对应应用的使用权限且成员必须在授权应用的可见范围内
* </pre>
*
* @param userTicket 成员票据
*/
WxCpUserDetail getUserDetail(String userTicket) throws WxErrorException;
}

View File

@ -1,5 +1,6 @@
package me.chanjar.weixin.cp.api.impl;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
@ -8,6 +9,7 @@ import me.chanjar.weixin.common.util.http.URIUtil;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.cp.api.WxCpOAuth2Service;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpUserDetail;
/**
* <pre>
@ -52,9 +54,8 @@ public class WxCpOAuth2ServiceImpl implements WxCpOAuth2Service {
@Override
public String[] getUserInfo(Integer agentId, String code) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?"
+ "code=" + code
+ "&agentid=" + agentId;
String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?code=%s&agentid=%d",
code, agentId);
String responseText = this.mainService.get(url, null);
JsonElement je = new JsonParser().parse(responseText);
JsonObject jo = je.getAsJsonObject();
@ -63,4 +64,12 @@ public class WxCpOAuth2ServiceImpl implements WxCpOAuth2Service {
GsonHelper.getString(jo, "OpenId")};
}
@Override
public WxCpUserDetail getUserDetail(String userTicket) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail";
JsonObject param = new JsonObject();
param.addProperty("user_ticket", userTicket);
String responseText = this.mainService.post(url, param.toString());
return new GsonBuilder().create().fromJson(responseText, WxCpUserDetail.class);
}
}

View File

@ -0,0 +1,47 @@
package me.chanjar.weixin.cp.bean;
/**
* <pre>
* 性别枚举
* Created by BinaryWang on 2018/4/22.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public enum Gender {
/**
*
*/
MALE("", "1"),
/**
*
*/
FEMALE("", "2");
private String genderName;
private String code;
Gender(String genderName, String code) {
this.genderName = genderName;
this.code = code;
}
public String getGenderName() {
return this.genderName;
}
public String getCode() {
return this.code;
}
public static Gender fromCode(String code) {
if ("1".equals(code)) {
return Gender.MALE;
}
if ("2".equals(code)) {
return Gender.FEMALE;
}
return null;
}
}

View File

@ -15,44 +15,6 @@ import java.util.List;
*/
@Data
public class WxCpUser implements Serializable {
public enum Gender {
/**
*
*/
MALE("", "1"),
/**
*
*/
FEMALE("", "2");
private String genderName;
private String code;
Gender(String genderName, String code) {
this.genderName = genderName;
this.code = code;
}
public String getGenderName() {
return this.genderName;
}
public String getCode() {
return this.code;
}
public static Gender fromCode(String code) {
if ("1".equals(code)) {
return Gender.MALE;
}
if ("2".equals(code)) {
return Gender.FEMALE;
}
return null;
}
}
private static final long serialVersionUID = -5696099236344075582L;
private String userId;
private String name;

View File

@ -0,0 +1,24 @@
package me.chanjar.weixin.cp.bean;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
/**
* <pre>
* 使用user_ticket获取成员详情接口返回类.
* Created by BinaryWang on 2018/4/22.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Data
public class WxCpUserDetail {
@SerializedName("userid")
private String userId;
private String name;
private String mobile;
private String gender;
private String email;
@SerializedName("qr_code")
private String qrCode;
}

View File

@ -10,6 +10,7 @@ package me.chanjar.weixin.cp.util.json;
import com.google.gson.*;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.cp.bean.Gender;
import me.chanjar.weixin.cp.bean.WxCpUser;
import java.lang.reflect.Type;
@ -39,7 +40,7 @@ public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSeri
user.setName(GsonHelper.getString(o, "name"));
user.setPosition(GsonHelper.getString(o, "position"));
user.setMobile(GsonHelper.getString(o, "mobile"));
user.setGender(WxCpUser.Gender.fromCode(GsonHelper.getString(o, "gender")));
user.setGender(Gender.fromCode(GsonHelper.getString(o, "gender")));
user.setEmail(GsonHelper.getString(o, "email"));
user.setAvatar(GsonHelper.getString(o, "avatar"));
user.setStatus(GsonHelper.getInteger(o, "status"));

View File

@ -56,7 +56,7 @@ public class WxCpDepartmentServiceImplTest {
}
}
@Test(dependsOnMethods = {"testListAll", "testCreate"})
@Test(dependsOnMethods = {"testList", "testCreate"})
public void testUpdate() throws Exception {
System.out.println("=================更新部门");
this.depart.setName("子部门改名" + System.currentTimeMillis());

View File

@ -0,0 +1,33 @@
package me.chanjar.weixin.cp.api.impl;
import com.google.inject.Inject;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpUserDetail;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
/**
* <pre>
* Created by BinaryWang on 2018/4/22.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Guice(modules = ApiTestModule.class)
public class WxCpOAuth2ServiceImplTest {
@Inject
private WxCpService wxService;
@Test
public void testGetUserDetail() throws WxErrorException {
WxCpUserDetail userDetail = this.wxService.getOauth2Service().getUserDetail("b");
System.out.println(userDetail);
}
@Test
public void testGetUserInfo() throws WxErrorException {
this.wxService.getOauth2Service().getUserInfo("abc");
}
}

View File

@ -3,6 +3,7 @@ package me.chanjar.weixin.cp.api.impl;
import com.google.inject.Inject;
import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.Gender;
import me.chanjar.weixin.cp.bean.WxCpUser;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
@ -40,7 +41,7 @@ public class WxCpUserServiceImplTest {
user.setName("Some Woman");
user.setDepartIds(new Integer[]{2});
user.setEmail("none@none.com");
user.setGender(WxCpUser.Gender.FEMALE);
user.setGender(Gender.FEMALE);
user.setMobile("13560084979");
user.setPosition("woman");
user.setTelephone("3300393");