开发企业微信
前言:
开发企业微信需要看企业微信官方文档:企业微信官方文档,里面有各种API
然后需要一个开发工具包,我用的github的,github文档,里面有微信的各种开发工具包,按需要添加对应的依赖即可,用开发工具包可以省去获取token和请求http请求简化开发提高开发效率
第一步导入企业微信依赖
<!-- 企业微信 -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-cp</artifactId>
<version>4.4.0</version>
</dependency>
第二步把必要的参数放到配置文件
wx:
cp:
configs:
corpId: ww913379742b252258
corpsecret: 5EC-y8yF6-03wUmznSPu4E6nZ-U7bGnwhEtYQQkric8
agentId: 123
redirectUrl: https://10.13.156.7
chatId: ww913379742b2gfhhjfgh
第三步创建配置文件
把需要的接口注入到spring容器中交给spring来管理,我只用到下面两个,如需要其他参考企业微信接口文档配置
@Configuration
@Getter
public class WxConfig {
@Value("${wx.cp.configs.corpsecret}")
private String secret;
@Value("${wx.cp.configs.corpId}")
private String corpId;
@Value("${wx.cp.configs.agentId}")
private Integer agentId;
@Value("${wx.cp.configs.redirectUrl}")
private String redirectUrl;
@Value("${wx.cp.configs.chatId}")
private String chatId;
private static String APP_AGENT_PREFIX = "wx";
@Resource
private RedisService redisService;
@Bean
public WxCpService getWxCpService() {
WxCpService wxCpService = new WxCpServiceImpl();
WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
config.setAgentId(agentId);
config.setCorpSecret(secret);
config.setCorpId(corpId);
resetTokenAndJsApi(wxCpService, config);
return wxCpService;
}
@Bean
public WxCpExternalContactService getwxCpExternalContactService() {
WxCpService wxCpService = new WxCpServiceImpl();
WxCpExternalContactService wxCpExternalContactService = new WxCpExternalContactServiceImpl(wxCpService);
WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
config.setAgentId(agentId);
config.setCorpSecret(secret);
config.setCorpId(corpId);
resetTokenAndJsApi(wxCpService, config);
return wxCpExternalContactService;
}
public void resetTokenAndJsApi(WxCpService wxCpService, WxCpDefaultConfigImpl wxCpDefaultConfig) {
wxCpService.setWxCpConfigStorage(wxCpDefaultConfig);
String wxAccessToken = APP_AGENT_PREFIX + this.agentId;
String json = redisService.getCacheObject(wxAccessToken);
if (!StringUtils.isEmpty(json)) {
wxCpDefaultConfig = JSON.parseObject(json, WxCpDefaultConfigImpl.class);
}
if (wxCpDefaultConfig.isAccessTokenExpired()) {
try {
String accessToken = null;
accessToken = wxCpService.getAccessToken(false);
wxCpDefaultConfig.setAccessToken(accessToken);
} catch (WxErrorException e) {
e.printStackTrace();
}
}
if(wxCpDefaultConfig.isJsapiTicketExpired()){
String jsApi = null;
try {
jsApi = wxCpService.getJsapiTicket();
wxCpDefaultConfig.setJsapiTicket(jsApi);
} catch (WxErrorException e) {
e.printStackTrace();
}
}
redisService.setCacheObject(wxAccessToken, JSON.toJSONString(wxCpDefaultConfig));
}
}
第四步实现功能
需要哪个接口参考企业微信官方文档和企业微信接口文档都可以完成,链接在上面,如果找不到对应的接口文档,就可以用get,post请求获取,需要多传一个url
/**
* 获取客户群列表(同步客户群)
*/
@Override
public boolean synchCustomCrowd() {
// 获取当前用户
UmsAdmin currentMember = umsAdminService.getCurrentMember();
String username = currentMember.getUsername();
try {
String post = wxCpService.post("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/list", "{\"owner_filter\": {" +
"\"userid_list\": [\"填写用户的企业id\"]\n" +
"},\"limit\" : 10}");
CustomCrowd customCrowd = JSON.parseObject(post, CustomCrowd.class);
List<GroupChatList> groupChatList = customCrowd.getGroup_chat_list();
List<WxCrowdEntity> wxCrowdEntities = new ArrayList<>();
for (GroupChatList chatList : groupChatList) {
WxCrowdEntity wxCrowdEntity = new WxCrowdEntity();
wxCrowdEntity.setChatId(chatList.getChat_id());
List<WxCrowdEntity> customCrowd1 = getCustomCrowd(wxCrowdEntity);
// 判断该群是否已经同步,如果同步不在同步
if (customCrowd1.size() == 0) {
// 获取群聊详细信息
WxCpUserExternalGroupChatInfo.GroupChat crowd = wxCpExternalContactService.getGroupChat(chatList.getChat_id(), 0).getGroupChat();
wxCrowdEntity.setName(crowd.getName());
wxCrowdEntity.setOwner(crowd.getOwner());
wxCrowdEntities.add(wxCrowdEntity);
}
}
// 群聊信息存入数据库
if (wxCrowdEntities.size() != 0)
wxCrowdService.saveBatch(wxCrowdEntities);
} catch (WxErrorException e) {
e.printStackTrace();
}
return true;
}