|
@@ -1,19 +1,18 @@
|
|
|
package com.fs.feishu.service;
|
|
package com.fs.feishu.service;
|
|
|
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
import cn.hutool.json.JSONUtil;
|
|
|
-import com.fs.feishu.config.FeiShuConfig;
|
|
|
|
|
-import com.fs.feishu.util.SecureTokenUtil;
|
|
|
|
|
import com.fs.common.exception.CustomException;
|
|
import com.fs.common.exception.CustomException;
|
|
|
import com.fs.course.config.CourseConfig;
|
|
import com.fs.course.config.CourseConfig;
|
|
|
import com.fs.course.domain.FsUserCourseVideo;
|
|
import com.fs.course.domain.FsUserCourseVideo;
|
|
|
import com.fs.course.mapper.FsUserCourseVideoMapper;
|
|
import com.fs.course.mapper.FsUserCourseVideoMapper;
|
|
|
|
|
+import com.fs.feishu.domain.FsFeishuConfig;
|
|
|
|
|
+import com.fs.feishu.util.SecureTokenUtil;
|
|
|
import com.fs.system.service.ISysConfigService;
|
|
import com.fs.system.service.ISysConfigService;
|
|
|
import com.lark.oapi.Client;
|
|
import com.lark.oapi.Client;
|
|
|
import com.lark.oapi.service.docx.v1.model.*;
|
|
import com.lark.oapi.service.docx.v1.model.*;
|
|
|
import com.lark.oapi.service.drive.v2.model.PatchPermissionPublicReq;
|
|
import com.lark.oapi.service.drive.v2.model.PatchPermissionPublicReq;
|
|
|
import com.lark.oapi.service.drive.v2.model.PermissionPublic;
|
|
import com.lark.oapi.service.drive.v2.model.PermissionPublic;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
@Component
|
|
@Component
|
|
@@ -22,17 +21,21 @@ public class FeiShuService {
|
|
|
private final String COURSE_PATH = "/feishu/pages_course/videovip?token=%s";
|
|
private final String COURSE_PATH = "/feishu/pages_course/videovip?token=%s";
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
- private FeiShuConfig feishuConfig;
|
|
|
|
|
|
|
+ private IFsFeishuConfigService fsFeishuConfigService;
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private ISysConfigService configService;
|
|
private ISysConfigService configService;
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private FsUserCourseVideoMapper videoMapper;
|
|
private FsUserCourseVideoMapper videoMapper;
|
|
|
|
|
|
|
|
- private Client client;
|
|
|
|
|
-
|
|
|
|
|
- @PostConstruct
|
|
|
|
|
- public void init() {
|
|
|
|
|
- this.client = Client.newBuilder(feishuConfig.getAppId(), feishuConfig.getAppSecret()).logReqAtDebug(true).build();
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 随机取一条状态为「正常」的飞书配置并构建 Client
|
|
|
|
|
+ */
|
|
|
|
|
+ private Client getClient() {
|
|
|
|
|
+ FsFeishuConfig config = fsFeishuConfigService.getRandomNormalConfig();
|
|
|
|
|
+ if (config == null) {
|
|
|
|
|
+ throw new CustomException("无可用的飞书应用配置(需状态为正常)");
|
|
|
|
|
+ }
|
|
|
|
|
+ return Client.newBuilder(config.getAppId(), config.getAppSecret()).logReqAtDebug(true).build();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -42,27 +45,30 @@ public class FeiShuService {
|
|
|
if (companyUserId == null || videoId == null) {
|
|
if (companyUserId == null || videoId == null) {
|
|
|
throw new CustomException("用户ID和视频ID不能为空");
|
|
throw new CustomException("用户ID和视频ID不能为空");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
FsUserCourseVideo userCourseVideo = videoMapper.selectFsUserCourseVideoByVideoId(videoId);
|
|
FsUserCourseVideo userCourseVideo = videoMapper.selectFsUserCourseVideoByVideoId(videoId);
|
|
|
if (userCourseVideo == null) {
|
|
if (userCourseVideo == null) {
|
|
|
throw new CustomException("视频不存在: " + videoId);
|
|
throw new CustomException("视频不存在: " + videoId);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
|
|
+ Client client = getClient();
|
|
|
// 创建云文档
|
|
// 创建云文档
|
|
|
- String documentId = createDocument(userCourseVideo.getTitle());
|
|
|
|
|
|
|
+ String documentId = createDocument(client, userCourseVideo.getTitle());
|
|
|
|
|
|
|
|
// 拼接看课url
|
|
// 拼接看课url
|
|
|
String url = buildCourseLink(companyUserId, videoId, periodId);
|
|
String url = buildCourseLink(companyUserId, videoId, periodId);
|
|
|
|
|
|
|
|
// 创建iframe块
|
|
// 创建iframe块
|
|
|
- createIframeBlock(documentId, url);
|
|
|
|
|
|
|
+ createIframeBlock(client, documentId, url);
|
|
|
|
|
|
|
|
// 更新文档权限
|
|
// 更新文档权限
|
|
|
- changeDocumentPermissions(documentId);
|
|
|
|
|
|
|
+ changeDocumentPermissions(client, documentId);
|
|
|
|
|
|
|
|
// 返回飞书看课链接
|
|
// 返回飞书看课链接
|
|
|
return "https://www.feishu.cn/docx/" + documentId;
|
|
return "https://www.feishu.cn/docx/" + documentId;
|
|
|
|
|
+ } catch (CustomException e) {
|
|
|
|
|
+ throw e;
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
throw new CustomException("创建飞书课程链接失败: " + e.getMessage(), e);
|
|
throw new CustomException("创建飞书课程链接失败: " + e.getMessage(), e);
|
|
|
}
|
|
}
|
|
@@ -71,7 +77,7 @@ public class FeiShuService {
|
|
|
/**
|
|
/**
|
|
|
* 创建云文档
|
|
* 创建云文档
|
|
|
*/
|
|
*/
|
|
|
- private String createDocument(String title) throws Exception {
|
|
|
|
|
|
|
+ private String createDocument(Client client, String title) throws Exception {
|
|
|
CreateDocumentReq req = CreateDocumentReq.newBuilder()
|
|
CreateDocumentReq req = CreateDocumentReq.newBuilder()
|
|
|
.createDocumentReqBody(CreateDocumentReqBody.newBuilder().title(title).build())
|
|
.createDocumentReqBody(CreateDocumentReqBody.newBuilder().title(title).build())
|
|
|
.build();
|
|
.build();
|
|
@@ -98,7 +104,7 @@ public class FeiShuService {
|
|
|
/**
|
|
/**
|
|
|
* 创建iframe块
|
|
* 创建iframe块
|
|
|
*/
|
|
*/
|
|
|
- private void createIframeBlock(String documentId, String url) throws Exception {
|
|
|
|
|
|
|
+ private void createIframeBlock(Client client, String documentId, String url) throws Exception {
|
|
|
CreateDocumentBlockChildrenReq req = CreateDocumentBlockChildrenReq.newBuilder()
|
|
CreateDocumentBlockChildrenReq req = CreateDocumentBlockChildrenReq.newBuilder()
|
|
|
.documentId(documentId)
|
|
.documentId(documentId)
|
|
|
.blockId(documentId)
|
|
.blockId(documentId)
|
|
@@ -126,7 +132,7 @@ public class FeiShuService {
|
|
|
/**
|
|
/**
|
|
|
* 修改文档权限
|
|
* 修改文档权限
|
|
|
*/
|
|
*/
|
|
|
- private void changeDocumentPermissions(String documentId) throws Exception {
|
|
|
|
|
|
|
+ private void changeDocumentPermissions(Client client, String documentId) throws Exception {
|
|
|
PatchPermissionPublicReq req = PatchPermissionPublicReq.newBuilder()
|
|
PatchPermissionPublicReq req = PatchPermissionPublicReq.newBuilder()
|
|
|
.token(documentId)
|
|
.token(documentId)
|
|
|
.type("docx")
|
|
.type("docx")
|