{"autoAddUser":true,"createDeptGroup":true,"deptId":581377085,"name":"运营部","parentId":1}
{"autoAddUser":true,"createDeptGroup":true,"deptId":581377086,"name":"设计部","parentId":1}
{"autoAddUser":true,"createDeptGroup":true,"deptId":581377087,"name":"产品部","parentId":1}
{"autoAddUser":true,"createDeptGroup":true,"deptId":581377088,"name":"人事部","parentId":1}
{"autoAddUser":true,"createDeptGroup":true,"deptId":581377089,"name":"行政部","parentId":1}
获取部门用户列表
@Test
public void getUserInfo() {
Long deptId = 1L;
final List<AbstrctUser> userList = userInfoService.selectUserListBaseOnDeptId(deptId);
for (AbstrctUser abstrctUser : userList) {
System.out.println(JSON.toJSONString(abstrctUser));
}
}
{"userId":"XXX","userName":"丁1"}
{"userId":"XXXX","userName":"张1"}
通过手机号获取用户信息
@Test
public void getUsrDetailOnPhoneTest() {
String phone = "phone";
final List<AbstrctUser> userList = userInfoService.selectUserBaseOnPhone(phone);
for (AbstrctUser abstrctUser : userList) {
System.out.println(JSON.toJSONString(abstrctUser));
}
}
发送信息
@Test
public void sendMsg() {
for (int i = 0; i < 1; i ) {
List<String> userIds = Arrays.asList(new String[]{"tet,ttttt"});
List<Long> deptIds = Arrays.asList(new Long[]{1l});
messageService.sendToDeptInUser(userIds,deptIds,false,new TextMessage("你们好,元旦放假了!!!,我正在测试消息发送多人" UUID.randomUUID()));
}
}
免登录前端
- 同样的钉钉中我们也需要获取当前登陆用户信息,所以还需要我们钉钉登陆授权。
- 点我了解登陆认证钉钉
- npm install dingtalk-jsapi --save进行安装。
dd.ready(function() {
// dd.ready参数为回调函数,在环境准备就绪时触发,jsapi的调用需要保证在该回调函数触发后调用,否则无效。
dd.runtime.permission.requestAuthCode({
corpId: "corpid",
onSuccess: function(result) {
/*{
code: 'hYLK98jkf0m' //string authCode
}*/
},
onFail : function(err) {}
});
});
- 安装完成后就可以通过上面代码获取授权码code,其中需要我们的参数corpId也是我们在企业配置账号中的信息之一。
- 前端获取到code之后,后端就可以根据code获取到当前登陆用户了。
- https://oapi.dingtalk.com/user/getuserinfo?access_token=%s&code=%s
- 同样我们钉钉发送消息也不仅仅局限于文本,https://oapi.dingtalk.com/media/upload 。
- 经过和微信的对比我们知道和微信支持的类型有很大的交集。所以在微信章节中我提到消息进行抽象化 , 现在我们可以共用一套实体类,出现两者不共同的我们在单独的创建特有的实体满足不同需求。
public MeterialResponse uploadPic(Meterial meterial, String FileName, InputStream inputStream) {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/media/upload");
OapiMediaUploadRequest req = new OapiMediaUploadRequest();
req.setType(meterial.getType());
// 要上传的媒体文件
FileItem item = new FileItem(fileName,inputStream);
req.setMedia(item);
OapiMediaUploadResponse rsp=null;
try {
rsp = client.execute(req, tokenService.accessAndGetDingDingToken());
} catch (ApiException e) {
e.printStackTrace();
}
MeterialResponse response = new MeterialResponse();
final Field[] declaredFields = rsp.getClass().getDeclaredFields();
JSONObject jsonObject = new JSONObject();
for (Field declaredField : declaredFields) {
final ApiField annotation = declaredField.getAnnotation(ApiField.class);
if (null == annotation) {
continue;
}
final String value = annotation.value();
declaredField.setAccessible(true);
try {
jsonObject.put(value, declaredField.get(rsp));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
BeanUtils.transBeanWithTranAnnotation(jsonObject, response);
return response;
}
发送消息
- 在上面我们已经实现了上传文件了。返回的media_id就是我们交互的载体。我们发送信息只需要将media_id发送过去就行了。
@Test
public void sendMultiTypeMessage() {
String fileName = "/macpower.png";
InputStream resourceAsStream = this.getClass().getResourceAsStream(fileName);
final MeterialResponse response = uploadService.uploadPic(new ImageMeterial(), fileName, resourceAsStream);
System.out.println(response);
Message message = new ImageMessage(response.getMediaId());
List<String> userIds = Arrays.asList(new String[]{"manager2239"});
List<Long> deptIds = Arrays.asList(new Long[]{1l});
messageService.sendToDeptInUser(userIds,deptIds,false,message);
}
- 这里的发送实际上知识Message的不同而已。