2023年11月29日发(作者:)

企业微信发布⼯作通知

public interface WeiChatService {

/**

* 获取企业微信 access_token

* @return access_token

*/

String getAccessToken();

/**

* 获取所有部门列表

* @param accessToken

* @param id

* @return 部门列表

*/

List getDeptList(String accessToken, String id);

/**

* 获取部门成员列表

@Autowired

private UserService userService;

@SneakyThrows

@Override

public String getAccessToken() {

// redis中获取token信息

String accessToken = Value().get("WeiChat_AccessToken");

if (!y(accessToken)) {

return accessToken;

}

// 发送http请求企业微信服务器,获取AccessToken

String path = "/cgi-bin/gettoken";

String method = "GET";

Map headers = new HashMap();

Map querys = new HashMap();

// 从数据库查询corpidcorpsecret

if (!(_APP_CONFIG)) {

throw new RuntimeException("第三⽅接⼊配置获取失败,请重新配置!");

}

ExternalAccessConfig config = (Value().get(_APP_CONFIG),

);

if (y(AppId())) {

throw new RuntimeException("corpid获取失败,请重新配置!");

}

if (y(Secret())) {

throw new RuntimeException("corpid获取失败,请重新配置!");

}

("corpid", AppId().trim());

("corpsecret", Secret().trim());

host = getCurrentHost();

HttpResponse httpResponse = (host, path, method, headers, querys);

String response = ng(ity());

if (y(response)) {

throw new RuntimeException("企业微信AccessToken获取失败!");

}

JSONObject json = bject(response);

try {

// token信息保存到redis中,并设置过期时间

List list = null;

/**

* redis中为获取到accessToken,从企业微信服务器获取

*/

///cgi-bin/department/list?access_token=ACCESS_TOKEN&id=ID

String path = "/cgi-bin/department/list";

String method = "GET";

Map headers = new HashMap();

Map querys = new HashMap();

("access_token", accessToken);

if(!y(id)){

("id", id);//部门id。获取指定部门及其下的⼦部门(以及及⼦部门的⼦部门等等,递归)。 如果不填,默认获取全量组织架构

}

host = getCurrentHost();

HttpResponse httpResponse = (host, path, method, headers, querys);

String response = ng(ity());

if (y(response)) {

throw new RuntimeException("企业微信AccessToken获取失败!");

}

JSONObject json = bject(response);

try {

String errcode = ing("errcode");

String department = ing("department");

("errcode = {}, department = {}", errcode, department);

list = (rray(userlist), );

} catch (Exception e) {

String error = ("获取token失败 errcode: %s ,errmsg: %s", eger("errcode"),

ing("errmsg"));

(error);

throw new RuntimeException(e);

}

return list;

}

/**

* 获取成员详情

* @param accessToken

* @return 成员详情

*/

@SneakyThrows

public WeChatDeptUserInfoDto getDeptUserInfo(String accessToken, String userid) {

///cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=USERID

String path = "/cgi-bin/user/get";

}

ntid(AgentId());

ser(touser);

Map contentMap = new HashMap();

("content", content);

t(contentMap);

String jsonSrtData = Str(noticeDto);

host = getCurrentHost();

String response = (host + path, jsonSrtData, accessToken);

if (y(response)) {

throw new RuntimeException("企业微信发送通知失败!");

}