zhangcl пре 6 месеци
родитељ
комит
6b30c5042b

+ 9 - 0
yudao-module-system/yudao-module-system-biz/pom.xml

@@ -114,6 +114,15 @@
             <groupId>com.xingyuv</groupId>
             <artifactId>spring-boot-starter-captcha-plus</artifactId> <!-- 验证码,一般用于登录使用 -->
         </dependency>
+
+        <!-- 钉钉接口调用 -->
+        <dependency>
+            <groupId>com.dingtalk</groupId>
+            <artifactId>dingtalk-api-sdk</artifactId>
+            <version>1.0.0-SNAPSHOT</version>
+            <scope>system</scope>
+            <systemPath>${project.basedir}/src/main/resources/lib/taobao-sdk-java-auto_1479188381469-20210402.jar</systemPath>
+        </dependency>
     </dependencies>
 
 </project>

+ 123 - 0
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/util/DingtalkUtil.java

@@ -0,0 +1,123 @@
+package cn.iocoder.yudao.module.system.util;
+
+import com.dingtalk.api.DefaultDingTalkClient;
+import com.dingtalk.api.DingTalkClient;
+import com.dingtalk.api.request.OapiGettokenRequest;
+import com.dingtalk.api.request.OapiV2UserGetRequest;
+import com.dingtalk.api.request.OapiV2UserGetuserinfoRequest;
+import com.dingtalk.api.response.OapiGettokenResponse;
+import com.dingtalk.api.response.OapiV2UserGetResponse;
+import com.dingtalk.api.response.OapiV2UserGetuserinfoResponse;
+import com.taobao.api.ApiException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+/**
+ * @author suiyy
+ * @date 20230602
+ * @desc 钉钉API接口工具类
+ */
+@Service
+public class DingtalkUtil {
+    private static final Logger logger = LoggerFactory.getLogger(DingtalkUtil.class);
+
+    /**
+     * 钉钉企业id
+     */
+    private static String CORPID;
+    @Value("${dingtalk.CORPID}")
+    public void setCORPID(String CORPID) {
+        DingtalkUtil.CORPID = CORPID;
+    }
+
+    /**
+     * 微应用appKey
+     */
+    @Value("${justauth.type.DINGTALK.client-id}")
+    private String APP_KEY;
+
+    /**
+     * 微应用appSecret
+     */
+    @Value("${justauth.type.DINGTALK.client-secret}")
+    private String APP_SECRET;
+
+    /**
+     * 获取access_token
+     */
+    @Value("${dingtalk.GET_ACCESS_TOKEN_URL}")
+    private String GET_ACCESS_TOKEN_URL;
+
+    /**
+     * 根据钉钉用户id获取用户信息
+     */
+    @Value("${dingtalk.URL_GET_USERINFO_BYUSERID}")
+    private String URL_GET_USERINFO_BYUSERID;
+
+    /**
+     * 通过免登授权码获取用户信息
+     */
+    @Value("${dingtalk.URL_GET_USERINFO_BYAUTHCODE}")
+    private String URL_GET_USERINFO_BYAUTHCODE;
+
+    /**
+     * 获取钉钉 accessToken
+     * @return
+     * @throws ApiException
+     */
+    public String getAccessToken() throws ApiException {
+        DefaultDingTalkClient client = new DefaultDingTalkClient(GET_ACCESS_TOKEN_URL);
+        OapiGettokenRequest request = new OapiGettokenRequest();
+        request.setAppkey(APP_KEY);
+        request.setAppsecret(APP_SECRET);
+        request.setHttpMethod("GET");
+        OapiGettokenResponse response = client.execute(request);
+        return response.getAccessToken();
+    }
+
+    /**
+     * 根据免登授权码获取用户id
+     *
+     * @param authCode 免登授权码
+     * @return
+     */
+    public String getUserIdByAuthCode(String authCode) throws Exception {
+        // 1. 获取access_token
+        String accessToken = getAccessToken();
+        // 2. 获取用户信息
+        DingTalkClient client = new DefaultDingTalkClient(URL_GET_USERINFO_BYAUTHCODE);
+        OapiV2UserGetuserinfoRequest req = new OapiV2UserGetuserinfoRequest();
+        req.setCode(authCode);
+        OapiV2UserGetuserinfoResponse rsp = client.execute(req, accessToken);
+        if (rsp.getErrcode() != 0L) {
+            throw new Exception(rsp.getBody());
+        }
+        // 3. 返回用户id
+        return rsp.getResult().getUserid();
+    }
+
+    /**
+     * 根据用户id获取用户信息(用户名 工号 所属部门列表)
+     *
+     * @param userId 用户id
+     * @return
+     */
+    public OapiV2UserGetResponse.UserGetResponse getUserDetail(String userId) throws Exception {
+        // 1. 获取access_token
+        String accessToken = getAccessToken();
+        // 2. 获取用户详情
+        DingTalkClient client = new DefaultDingTalkClient(URL_GET_USERINFO_BYUSERID);
+        OapiV2UserGetRequest req = new OapiV2UserGetRequest();
+        req.setUserid(userId);
+        req.setLanguage("zh_CN");
+        OapiV2UserGetResponse rsp = client.execute(req, accessToken);
+        if (rsp.getErrcode() != 0L) {
+            throw new Exception(rsp.getBody());
+        }
+        // 返回用户名称rsp.getResult().getName()
+        return rsp.getResult();
+    }
+
+}

BIN
yudao-module-system/yudao-module-system-biz/src/main/resources/lib/taobao-sdk-java-auto_1479188381469-20210402.jar


+ 7 - 0
yudao-server/src/main/resources/application-dev.yaml

@@ -141,6 +141,13 @@ logging:
   file:
     name: ${user.home}/logs/${spring.application.name}.log # 日志文件名,全路径
 
+--- #################### 钉微应用相关配置 ####################
+dingtalk:
+  GET_ACCESS_TOKEN_URL: https://oapi.dingtalk.com/gettoken  # 获取access_token
+  URL_GET_USERINFO_BYCODE: https://oapi.dingtalk.com/sns/getuserinfo_bycode # 通过二维码扫码获取UNIONID
+  URL_GET_USERINFO_BYUNIONID: https://oapi.dingtalk.com/topapi/user/getbyunionid # 通过UNIONID获取用户信息
+  URL_GET_USERINFO_BYUSERID: https://oapi.dingtalk.com/topapi/v2/user/get # 根据用户id获取用户详情 url
+
 --- #################### 微信公众号相关配置 ####################
 wx: # 参见 https://github.com/Wechat-Group/WxJava/blob/develop/spring-boot-starters/wx-java-mp-spring-boot-starter/README.md 文档
   mp:

+ 12 - 4
yudao-server/src/main/resources/application-local.yaml

@@ -46,7 +46,7 @@ spring:
       primary: master
       datasource:
         master:
-          url: jdbc:mysql://127.0.0.1:3306/ruoyi-vue-pro?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true&rewriteBatchedStatements=true # MySQL Connector/J 8.X 连接的示例
+          url: jdbc:mysql://172.21.0.70:3306/ruoyi-vue-pro?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true&rewriteBatchedStatements=true # MySQL Connector/J 8.X 连接的示例
           #          url: jdbc:mysql://127.0.0.1:3306/ruoyi-vue-pro?useSSL=true&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true # MySQL Connector/J 5.X 连接的示例
           #          url: jdbc:postgresql://127.0.0.1:5432/ruoyi-vue-pro # PostgreSQL 连接的示例
           #          url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例
@@ -64,7 +64,7 @@ spring:
           #          password: Yudao@2024 # OpenGauss 连接的示例
         slave: # 模拟从库,可根据自己需要修改
           lazy: true # 开启懒加载,保证启动速度
-          url: jdbc:mysql://127.0.0.1:3306/ruoyi-vue-pro?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true&nullCatalogMeansCurrent=true
+          url: jdbc:mysql://172.21.0.70:3306/ruoyi-vue-pro?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true&nullCatalogMeansCurrent=true
           username: root
           password: 123456
 
@@ -180,6 +180,13 @@ logging:
 
 debug: false
 
+--- #################### 钉微应用相关配置 ####################
+dingtalk:
+  GET_ACCESS_TOKEN_URL: https://oapi.dingtalk.com/gettoken  # 获取access_token
+  URL_GET_USERINFO_BYCODE: https://oapi.dingtalk.com/sns/getuserinfo_bycode # 通过二维码扫码获取UNIONID
+  URL_GET_USERINFO_BYUNIONID: https://oapi.dingtalk.com/topapi/user/getbyunionid # 通过UNIONID获取用户信息
+  URL_GET_USERINFO_BYUSERID: https://oapi.dingtalk.com/topapi/v2/user/get # 根据用户id获取用户详情 url
+
 --- #################### 微信公众号、小程序相关配置 ####################
 wx:
   mp: # 公众号配置(必填),参见 https://github.com/Wechat-Group/WxJava/blob/develop/spring-boot-starters/wx-java-mp-spring-boot-starter/README.md 文档
@@ -233,9 +240,10 @@ justauth:
   enabled: true
   type:
     DINGTALK: # 钉钉
-      client-id: dingvrnreaje3yqvzhxg
-      client-secret: i8E6iZyDvZj51JIb0tYsYfVQYOks9Cq1lgryEjFRqC79P3iJcrxEwT6Qk2QvLrLI
+      client-id: dingik345qmyhtysvs2x
+      client-secret: MIF55t94hRhI2EPaymoUF8-LRhWO6RxTxevYdwE9imdRUEZzxdgpNexqoTn2AIMr
       ignore-check-redirect-uri: true
+      redirect-uri: http://1.94.244.160:81
     WECHAT_ENTERPRISE: # 企业微信
       client-id: wwd411c69a39ad2e54
       client-secret: 1wTb7hYxnpT2TUbIeHGXGo7T0odav1ic10mLdyyATOw