yangys
2024-04-04 ed4a5236bab800094be4a8378f5098eebe3de6ac
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.qianwen.smartman.modules.notify.api;
 
import java.util.Map;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
import com.qianwen.core.notify.DefaultNotifyType;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.smartman.modules.notify.entity.NotifyConfigEntity;
import com.qianwen.smartman.modules.notify.service.INotifyConfigService;
import com.taobao.api.ApiException;
 
@Component
/* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/notify/api/DingTalkApi.class */
public class DingTalkApi {
    @Autowired
    @Lazy
    private INotifyConfigService notifyConfigService;
 
 
    public String getToken() {
        DingTalkConfig dingTalkDTO = getDingTalkConfig();
        try {
            DefaultDingTalkClient defaultDingTalkClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
            OapiGettokenRequest req = new OapiGettokenRequest();
            req.setHttpMethod("GET");
            req.setAppkey(dingTalkDTO.getAppkey());
            req.setAppsecret(dingTalkDTO.getAppsecret());
            OapiGettokenResponse rsp = defaultDingTalkClient.execute(req);
            if (Func.equals(rsp.getErrorCode(), "0")) {
                return rsp.getAccessToken();
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    public String getUserIdByMobile(String mobile) {
        try {
            DefaultDingTalkClient defaultDingTalkClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
            OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
            req.setMobile(mobile);
            OapiV2UserGetbymobileResponse rsp = defaultDingTalkClient.execute(req, getToken());
            OapiV2UserGetbymobileResponse.UserGetByMobileResponse result = rsp.getResult();
            return Func.equals(rsp.getErrorCode(), "0") ? result.getUserid() : "";
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
 
    private DingTalkConfig getDingTalkConfig() {
        NotifyConfigEntity config = this.notifyConfigService.getOne(Wrappers.<NotifyConfigEntity>lambdaQuery()
                .eq(NotifyConfigEntity::getType, DefaultNotifyType.dingTalk.name())
                .eq(NotifyConfigEntity::getStatus, 1));
        /*
        NotifyConfigEntity config = (NotifyConfigEntity) this.notifyConfigService.getOne((Wrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getType();
        }, DefaultNotifyType.dingTalk.name())).eq((v0) -> {
            return v0.getStatus();
        }, 1));*/
        Map<String, Object> configuration = config.getConfiguration();
        Object appKey = configuration.get("appKey");
        Object appSecret = configuration.get("appSecret");
        DingTalkConfig dingTalkConfig = new DingTalkConfig();
        dingTalkConfig.setAppkey(String.valueOf(appKey));
        dingTalkConfig.setAppsecret(String.valueOf(appSecret));
        return dingTalkConfig;
    }
 
    public String getCompanyName() {
        DefaultDingTalkClient defaultDingTalkClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
        OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
        req.setDeptId(1L);
        req.setLanguage("zh_CN");
        try {
            OapiV2DepartmentGetResponse rsp = defaultDingTalkClient.execute(req, getToken());
            if (Func.equals(rsp.getErrorCode(), "0")) {
                OapiV2DepartmentGetResponse.DeptGetResponse result = rsp.getResult();
                return result.getName();
            }
            return "";
        } catch (ApiException e) {
            e.printStackTrace();
            return "";
        }
    }
}