钉钉云第三方企业H5微应用开发(三)

先来看一下工程结构:

在包handler中的服务就是处理各个推送事件的类。

企业授权

我们开发的第三方微应用,只有企业授权并激活才可以使用

在应用的详情页面,点击「体验组织」tab可以添加授权组织。所选好体验组织,点击授权按钮,如上图所示。

后台事件处理如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package club.itwork.dingtalk.handler;

import club.itwork.dingtalk.constant.Constants;
import club.itwork.dingtalk.dto.EventMsg;
import club.itwork.dingtalk.service.CorpInfoService;
import club.itwork.dingtalk.service.DingTalkCorpService;
import club.itwork.dingtalk.thread.ThreadPoolManager;
import com.dingtalk.api.response.OapiServiceGetAuthInfoResponse;
import com.dingtalk.api.response.OapiServiceGetPermanentCodeResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
* 当企业开通授权第三方企业应用后,
* 钉钉服务器会向创建应用时填写的回调URL推送临时授权码。
*
* @author dgb8901
*/
@Slf4j
@Component(value = Constants.Event.TMP_AUTH_CODE)
public class TmpAuthCodeEventHandler implements EventHandler {

@Resource
private DingTalkCorpService dingTalkCorpService;
@Resource
private CorpInfoService corpInfoService;

/**
* 处理授权事件
* - 最好异步处理
*
* @param eventMsg
*/
@Override
public void handle(EventMsg eventMsg) {
log.info("处理授权事件参数:{}", eventMsg);

// 异步处理
ThreadPoolManager.getInstance().addTask(TmpAuthCodeEventHandler.class, () -> {
// 1. 拿到临时授权码 --> 临时授权码只能使用一次
String authCode = eventMsg.getAuthCode();
// 2. 换取永久授权码
OapiServiceGetPermanentCodeResponse response = dingTalkCorpService.getPermanentCode(authCode);
if (response != null) {
String permanentCode = response.getPermanentCode();
// 3. 激活应用
Boolean result = dingTalkCorpService.activateSuite(eventMsg.getAuthCorpId(), permanentCode);
log.info("激活应用结果:{}", result);
// 4. 获取企业授权信息
OapiServiceGetAuthInfoResponse authInfo = dingTalkCorpService.getCorpAuthInfo(eventMsg.getAuthCorpId());
// 5. 保存企业授权信息
corpInfoService.save(authInfo, permanentCode);
}
});
}
}

此步如果成功,那么企业授权并且已经激活,可以在钉钉的客户端(PC/手机)查看结果:

这样企业就成功授权了,接下来就要根据自己的相关业务去做相应的开发了。

开通接口权限

开发相关通讯录业务,还要开通相应的接口权限,如上图。

获取源码

关注微信公众号「特想学英语」,回复:「钉钉」

原文作者: dgb8901,yinxing

原文链接: https://www.itwork.club/2020/05/11/dingtalk-corp-3/

版权声明: 转载请注明出处

为您推荐

体验小程序「简易记账」

关注公众号「特想学英语」

迁移 Mac, 抹掉磁盘