diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkApprovalHelper.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkApprovalHelper.java new file mode 100644 index 0000000..198d260 --- /dev/null +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkApprovalHelper.java @@ -0,0 +1,42 @@ +package com.ruoyi.flyingbook.LarkHelper; + +import com.lark.oapi.service.approval.v4.model.GetInstanceReq; +import com.lark.oapi.service.approval.v4.model.GetInstanceRespBody; +import com.ruoyi.flyingbook.domain.lark.LarkApprovalRequest; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + + + +/** + * @author yuxiangyong + * @create 2023-03-12 18:10 + */ +@Slf4j +@Component +public class LarkApprovalHelper extends LarkHelper{ + + static String instanceId = "3487F263-9116-42A3-99EC-A737845E66AD"; + + public static void main(String[] args) throws Exception { + LarkApprovalHelper bk = new LarkApprovalHelper(); + GetInstanceRespBody approvalInfoDetail = bk.getApprovalInfoDetail(new LarkApprovalRequest(appId, secret, instanceId)); + System.out.println(approvalInfoDetail); + } + + + /** + * 获取行详情 + * + * @return + */ + public GetInstanceRespBody getApprovalInfoDetail(LarkApprovalRequest request) { + try { + return buildClient(request).approval().instance().get(GetInstanceReq.newBuilder() + .instanceId(request.getInstanceId()).build()).getData(); + } catch (Exception e) { + + } + return null; + } +} diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkHelper.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkHelper.java new file mode 100644 index 0000000..67cc74d --- /dev/null +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkHelper.java @@ -0,0 +1,37 @@ +package com.ruoyi.flyingbook.LarkHelper; + +import com.lark.oapi.Client; +import com.lark.oapi.core.cache.ICache; +import com.lark.oapi.core.enums.AppType; +import com.lark.oapi.service.bitable.v1.model.*; +import com.ruoyi.flyingbook.domain.lark.LarkRequest; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author yuxiangyong + * @create 2023-03-12 18:10 + */ +@Slf4j +public class LarkHelper { + + static String appId = "cli_a482a8572cbc9013"; + static String secret = "lZNXbCLlOslWbwBIVc4qvgxOdnfA8Mos"; + + + /** + * cli_a482a8572cbc9013 + * lZNXbCLlOslWbwBIVc4qvgxOdnfA8Mos + * @param request + * @return + */ + protected Client buildClient(LarkRequest request){ + return Client.newBuilder(request.getAppId(), request.getAppSecret()) + .marketplaceApp() + .appType(AppType.SELF_BUILT) + .build(); + } +} diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkTableHelper.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkTableHelper.java new file mode 100644 index 0000000..ca14c40 --- /dev/null +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkTableHelper.java @@ -0,0 +1,110 @@ +package com.ruoyi.flyingbook.LarkHelper; + +import com.lark.oapi.service.bitable.v1.model.*; +import com.ruoyi.flyingbook.domain.lark.LarkTableRequest; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; + + +/** + * @author yuxiangyong + * @create 2023-03-12 18:10 + */ +@Slf4j +@Component +public class LarkTableHelper extends LarkHelper{ + + static String appToken = "Gyu1b8VAFaYiEysfEUjcCKomnVc"; + static String tableId = "tbloMVR0ACW2uO33"; + static String record = "rec8umOjIh"; + static Map map = new HashMap<>(); + + + public static void main(String[] args) throws Exception { + LarkTableHelper bk = new LarkTableHelper(); + map.put("b", "aaa"); + LarkTableRequest add = new LarkTableRequest(appId, secret, appToken, tableId,map); + CreateAppTableRecordRespBody createAppTableRecordRespBody = bk.addTableRecord(add); + String recordId = createAppTableRecordRespBody.getRecord().getRecordId(); +// map.put("b", "EEE"); +// LarkTableRequest update = new LarkTableRequest(appId, secret, appToken,tableId, record,map); +// bk.updateTableRecord(update); + LarkTableRequest delete = new LarkTableRequest(appId, secret, appToken,tableId, recordId); + bk.deleteTableRecord(delete); + } + + + /** + * 获取行详情 + * + * @return + */ + public GetAppTableRecordResp getTableRecordDetail(LarkTableRequest request) { + try { + return buildClient(request).bitable().appTableRecord().get( + GetAppTableRecordReq + .newBuilder() + .appToken(request.getAppToken()) + .tableId(request.getAppTable()) + .recordId(request.getRecord()). + build()); + } catch (Exception e) { + + } + return null; + } + + public CreateAppTableRecordRespBody addTableRecord(LarkTableRequest request) { + try { + return buildClient(request).bitable().appTableRecord() + .create(CreateAppTableRecordReq.newBuilder() + .appToken(request.getAppToken()) + .tableId(request.getAppTable()) + .appTableRecord( + AppTableRecord.newBuilder() + .fields(request.getBody()) + .build()) + .build()).getData(); + } catch (Exception e) { + + } + return null; + } + + public DeleteAppTableRecordRespBody deleteTableRecord(LarkTableRequest request) { + try { + return buildClient(request).bitable().appTableRecord().delete( + DeleteAppTableRecordReq.newBuilder() + .appToken(request.getAppToken()) + .tableId(request.getAppTable()) + .recordId(request.getRecord()) + .build() + ).getData(); + } catch (Exception e) { + + } + return null; + } + + public UpdateAppTableRecordRespBody updateTableRecord(LarkTableRequest request) { + try { + return buildClient(request).bitable().appTableRecord().update( + UpdateAppTableRecordReq.newBuilder() + .appToken(request.getAppToken()) + .tableId(request.getAppTable()) + .recordId(request.getRecord()) + .appTableRecord( + AppTableRecord.newBuilder() + .fields(request.getBody()) + .build() + ).build() + ).getData(); + } catch (Exception e) { + + } + return null; + } +} diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkTokenHelper.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkTokenHelper.java deleted file mode 100644 index 619dd2f..0000000 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkTokenHelper.java +++ /dev/null @@ -1,140 +0,0 @@ -package com.ruoyi.flyingbook.LarkHelper; - -import cn.hutool.core.util.ObjectUtil; -import com.alibaba.fastjson.JSONObject; -import lombok.extern.slf4j.Slf4j; -import okhttp3.*; -import org.apache.commons.collections4.CollectionUtils; -import org.springframework.stereotype.Component; - -import java.io.IOException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.TimeUnit; - -/** - * @author yuxiangyong - * @create 2023-03-12 18:10 - */ -@Slf4j -@Component -public class LarkTokenHelper { - - private static OkHttpClient client = new OkHttpClient().newBuilder() - .callTimeout(10, TimeUnit.SECONDS) - .connectTimeout(10, TimeUnit.SECONDS) - .readTimeout(10, TimeUnit.SECONDS) - .build(); - - private JSONObject execute(Request request) { - Response execute = null; - JSONObject jsonObject = null; - try { - execute = client.newCall(request).execute(); - if (execute == null || execute.body() == null){ - throw new RuntimeException("飞书返回结果为空"); - } - jsonObject = JSONObject.parseObject(execute.body().string()); - } catch (IOException e) { - String errorMessage = String.format("飞书接口调用失败:{}",e.getMessage()); - throw new RuntimeException(errorMessage); - } - - if ("0".equals(jsonObject.getString("code"))){ - return jsonObject; - }else { - String errorMessage = String.format("%s##%s", jsonObject.getString("code"), jsonObject.getString("msg")); - throw new RuntimeException(errorMessage); - } - } - - public String getToken(String appId, String srcret,String tokenType) { - Map map = new HashMap<>(); - map.put("app_id", appId); - map.put("app_secret", srcret); - Request.Builder builder = new Request.Builder(); - builder.url("https://open.feishu.cn/open-apis/auth/v3/app_access_token/internal"); - RequestBody requestBody = RequestBody.create(MediaType.get("application/json"),JSONObject.toJSONString(map)); - - builder.post(requestBody); - return execute(builder.build()).getString(tokenType); - } - - /** - * @param url - * @param token - * @return - */ - public JSONObject getLark(String url, Map body, String token, List safeCodeList) { - Response execute = null; - Request request = new Request.Builder() - .url(url) - .addHeader("Authorization",String.format("Bearer %s",token)) - .build(); - JSONObject jsonObject = null; - try { - execute = client.newCall(request).execute(); - if (execute == null || execute.body() == null){ - throw new RuntimeException("飞书返回结果为空"); - } - jsonObject = JSONObject.parseObject(execute.body().string()); - } catch (IOException e) { - String errorMessage = String.format("飞书接口调用失败:{}",e.getMessage()); - throw new RuntimeException(errorMessage); - } - String code = jsonObject.getString("code"); - if ("0".equals(code) || (CollectionUtils.isNotEmpty(safeCodeList) && safeCodeList.contains(code))){ - return jsonObject; - }else { - String errorMessage = String.format("%s##%s", code, jsonObject.getString("msg")); - throw new RuntimeException(errorMessage); - } - } - - public JSONObject postLark(String url,Map> body, String token) { - Request request = new Request.Builder() - .method("POST", RequestBody - .create(MediaType.get("application/json"),JSONObject.toJSONString(body))) - .url(url) - .addHeader("Authorization",String.format("Bearer %s",token)) - .build(); - return execute(request); - } - - public JSONObject putLark(String url,Map> body, String token) { - Request request = new Request.Builder() - .put(RequestBody - .create( MediaType.get("application/json"),JSONObject.toJSONString(body))) - .url(url) - .addHeader("Authorization",String.format("Bearer %s",token)) - .build(); - return execute(request); - } - - public JSONObject deleteLark(String url,Map> body, String token) { - Request.Builder builder = new Request.Builder() - .url(url) - .addHeader("Authorization", String.format("Bearer %s", token)); - if (body == null || body.isEmpty()){ - builder.delete(); - }else { - builder.delete(RequestBody - .create(MediaType.get("application/json"),JSONObject.toJSONString(body))); - } - return execute(builder.build()); - } - - public static void main(String[] args) { - LarkTokenHelper helper = new LarkTokenHelper(); - String token = helper.getToken( "cli_a482a8572cbc9013", "lZNXbCLlOslWbwBIVc4qvgxOdnfA8Mos","tenant_access_token"); - System.out.println(token); - JSONObject jsonObject = JSONObject.parseObject(token); - Map map = new HashMap<>(); - JSONObject b = new JSONObject(); - map.put("fields",b); - b.put("多行文本","345"); -// helper.putLark("https://open.feishu.cn/open-apis/bitable/v1/apps/Gyu1b8VAFaYiEysfEUjcCKomnVc/tables/tbloMVR0ACW2uO33/records/rece1nuKGU",map,jsonObject.getString("tenant_access_token")); - - } -} diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/controller/EventController.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/controller/EventController.java index d68aade..43fc996 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/controller/EventController.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/controller/EventController.java @@ -1,7 +1,7 @@ package com.ruoyi.flyingbook.controller; import com.alibaba.fastjson.JSONObject; -import com.ruoyi.flyingbook.domain.LarkRequest; +import com.ruoyi.flyingbook.domain.RequestVo; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.utils.DecryptUtil; import com.ruoyi.flyingbook.factory.LarkCallBackFactory; @@ -33,7 +33,7 @@ public class EventController extends BaseController { result = DecryptUtil.decrypt(jsonObject.getString("encrypt")); log.info("/event/test1/approval encrypt:{}", JSONObject.toJSONString(result)); } - LarkRequest larkRequest = new LarkRequest(); + RequestVo larkRequest = new RequestVo(); larkRequest.setMessage(result); larkFactory.execute(larkRequest); return result; diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/LarkRequest.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/RequestVo.java similarity index 78% rename from ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/LarkRequest.java rename to ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/RequestVo.java index 47350bb..96700eb 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/LarkRequest.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/RequestVo.java @@ -1,10 +1,12 @@ package com.ruoyi.flyingbook.domain; import com.alibaba.fastjson.JSONObject; +import com.lark.oapi.service.approval.v4.model.GetInstanceRespBody; +import com.lark.oapi.service.bitable.v1.model.GetAppTableRecordResp; import com.ruoyi.common.enums.EventOperateType; +import com.ruoyi.flyingbook.domain.lark.LarkApprovalRequest; import lombok.Data; -import java.util.List; import java.util.Map; /** @@ -12,13 +14,14 @@ import java.util.Map; * @create 2023-03-12 16:00 */ @Data -public class LarkRequest { +public class RequestVo { /** * 通用信息,不可修改不可删除 */ private String token; private String appId; + private String appSecret; private String fromAppToken; private String toAppToken; private String errorCode; @@ -29,11 +32,14 @@ public class LarkRequest { * ApprovalCallback 用参数 */ private Long eventId; + private String instance; + private GetInstanceRespBody instanceRespBody; private String message; /** * MultidimensionalTableOperate 用参数 */ + private GetAppTableRecordResp respBody; private JSONObject record; private Long companyRelationId; private Long tableRelationId; diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/lark/LarkApprovalRequest.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/lark/LarkApprovalRequest.java new file mode 100644 index 0000000..7c2e363 --- /dev/null +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/lark/LarkApprovalRequest.java @@ -0,0 +1,21 @@ +package com.ruoyi.flyingbook.domain.lark; + +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author yuxiangyong + * @create 2023-03-18 1:05 + */ +@Data +@NoArgsConstructor +public class LarkApprovalRequest extends LarkRequest{ + + + private String instanceId; + + public LarkApprovalRequest(String appId, String appSecret, String instanceId) { + super(appId, appSecret); + this.instanceId = instanceId; + } +} diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/lark/LarkRequest.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/lark/LarkRequest.java new file mode 100644 index 0000000..f3016c7 --- /dev/null +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/lark/LarkRequest.java @@ -0,0 +1,30 @@ +package com.ruoyi.flyingbook.domain.lark; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * @author yuxiangyong + * @create 2023-03-18 0:12 + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class LarkRequest { + + private String appId; + + private String appSecret; + /** + * 可以安全放过的code + */ + private List safeCode; + + public LarkRequest(String appId, String appSecret) { + this.appId = appId; + this.appSecret = appSecret; + } +} diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/lark/LarkTableRequest.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/lark/LarkTableRequest.java new file mode 100644 index 0000000..4201594 --- /dev/null +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/lark/LarkTableRequest.java @@ -0,0 +1,44 @@ +package com.ruoyi.flyingbook.domain.lark; + +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Map; + +/** + * @author yuxiangyong + * @create 2023-03-18 0:12 + */ +@Data +@NoArgsConstructor +public class LarkTableRequest extends LarkRequest{ + + private String appToken; + private String appTable; + private String record; + private Map body; + + + + public LarkTableRequest(String appId,String appSecret,String appToken, String appTable, Map body) { + super(appId,appSecret); + this.appToken = appToken; + this.appTable = appTable; + this.body = body; + } + + public LarkTableRequest(String appId,String appSecret,String appToken, String appTable, String record, Map body) { + super(appId,appSecret); + this.appToken = appToken; + this.appTable = appTable; + this.record = record; + this.body = body; + } + + public LarkTableRequest(String appId,String appSecret,String appToken, String appTable, String record) { + super(appId,appSecret); + this.appToken = appToken; + this.appTable = appTable; + this.record = record; + } +} diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/factory/LarkCallBackFactory.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/factory/LarkCallBackFactory.java index c1ed4a7..11d1b82 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/factory/LarkCallBackFactory.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/factory/LarkCallBackFactory.java @@ -2,7 +2,7 @@ package com.ruoyi.flyingbook.factory; import com.alibaba.fastjson.JSONObject; import com.ruoyi.common.enums.CallBackTypeEnum; -import com.ruoyi.flyingbook.domain.LarkRequest; +import com.ruoyi.flyingbook.domain.RequestVo; import com.ruoyi.flyingbook.strategy.callback.ApprovalCallback; import com.ruoyi.flyingbook.strategy.callback.MultidimensionalTableCallback; import org.apache.commons.lang3.StringUtils; @@ -21,7 +21,7 @@ public class LarkCallBackFactory { @Autowired private ApprovalCallback approvalCallback; - public void execute(LarkRequest larkRequest) { + public void execute(RequestVo larkRequest) { String message = larkRequest.getMessage(); if (StringUtils.isBlank(message)) { return; diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/factory/LarkOperateFactory.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/factory/LarkOperateFactory.java index fd44f0c..95fdb5e 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/factory/LarkOperateFactory.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/factory/LarkOperateFactory.java @@ -2,7 +2,7 @@ package com.ruoyi.flyingbook.factory; import com.ruoyi.common.enums.EventTypeEnum; import com.ruoyi.flyingbook.domain.Event; -import com.ruoyi.flyingbook.domain.LarkRequest; +import com.ruoyi.flyingbook.domain.RequestVo; import com.ruoyi.flyingbook.strategy.operate.ApprovalCallbackOperate; import com.ruoyi.flyingbook.strategy.operate.MultidimensionalTableOperate; import org.springframework.beans.factory.annotation.Autowired; @@ -25,7 +25,7 @@ public class LarkOperateFactory { if (eventTypeEnum == null) { return; } - LarkRequest request = new LarkRequest(); + RequestVo request = new RequestVo(); request.setEvent(event); switch (eventTypeEnum) { case ROW_CHANGE: diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/LarkOperateAbstract.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/LarkOperateAbstract.java index 9bd706c..7b1cfe6 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/LarkOperateAbstract.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/LarkOperateAbstract.java @@ -1,8 +1,6 @@ package com.ruoyi.flyingbook.strategy; -import com.ruoyi.common.enums.EventOperateType; -import com.ruoyi.flyingbook.domain.EventLog; -import com.ruoyi.flyingbook.domain.LarkRequest; +import com.ruoyi.flyingbook.domain.RequestVo; import lombok.extern.slf4j.Slf4j; /** @@ -12,7 +10,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public abstract class LarkOperateAbstract{ - public String execute(LarkRequest request){ + public String execute(RequestVo request){ try { //参数校验逻辑 if (!check(request)){ @@ -46,30 +44,30 @@ public abstract class LarkOperateAbstract{ * 由子类实现自己的校验逻辑,并输出对应的日志 * 默认校验通过 */ - protected Boolean check(LarkRequest request){ + protected Boolean check(RequestVo request){ return Boolean.TRUE; } /** * 由子类实现自己的预处理逻辑 */ - protected abstract void preOperate(LarkRequest request); + protected abstract void preOperate(RequestVo request); /** * 实际业务处理阶段 * @param request 请求参数 */ - protected abstract void businessProcessing(LarkRequest request); + protected abstract void businessProcessing(RequestVo request); /** * 业务处理完成后的附加操作 */ - protected void endHandle(LarkRequest request){}; + protected void endHandle(RequestVo request){}; /** * 具体的返回内容 */ - protected abstract String buildResult(LarkRequest request); + protected abstract String buildResult(RequestVo request); protected abstract String getName(); diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/callback/ApprovalCallback.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/callback/ApprovalCallback.java index 0d8677e..43928dc 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/callback/ApprovalCallback.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/callback/ApprovalCallback.java @@ -7,7 +7,7 @@ import com.ruoyi.common.enums.EventOperateType; import com.ruoyi.common.enums.FlagStatus; import com.ruoyi.flyingbook.domain.Event; import com.ruoyi.flyingbook.domain.EventLog; -import com.ruoyi.flyingbook.domain.LarkRequest; +import com.ruoyi.flyingbook.domain.RequestVo; import com.ruoyi.flyingbook.service.IEventLogService; import com.ruoyi.flyingbook.service.IEventService; import lombok.extern.slf4j.Slf4j; @@ -36,12 +36,12 @@ public class ApprovalCallback extends CallbackAbstract { private RedisTemplate redisTemplate; @Override - protected Boolean check(LarkRequest request) { + protected Boolean check(RequestVo request) { return Boolean.TRUE; } @Override - protected void preOperate(LarkRequest request) { + protected void preOperate(RequestVo request) { String message = request.getMessage(); JSONObject jsonObject = JSONObject.parseObject(message); JSONObject event = jsonObject.getJSONObject("event"); @@ -50,7 +50,7 @@ public class ApprovalCallback extends CallbackAbstract { } @Override - protected void businessProcessing(LarkRequest request) { + protected void businessProcessing(RequestVo request) { Event event = queryEvent(request.getAppId(),request.getFromRecordId()); //如果已经存在处理中的就不需要操作数据库了 if (event != null && EventOperateStatus.PENDING.getCode().equals(event.getOperateStatus())){ @@ -66,7 +66,7 @@ public class ApprovalCallback extends CallbackAbstract { } @Override - protected void endHandle(LarkRequest request) { + protected void endHandle(RequestVo request) { if (request.getEventId() == null){ return; } @@ -80,7 +80,7 @@ public class ApprovalCallback extends CallbackAbstract { } @Override - protected String buildResult(LarkRequest request) { + protected String buildResult(RequestVo request) { return ""; } @@ -102,7 +102,7 @@ public class ApprovalCallback extends CallbackAbstract { return null; } - private Event buildDto(LarkRequest request,Event event){ + private Event buildDto(RequestVo request, Event event){ event = event == null ? new Event() :event; event.setFlag(FlagStatus.OK.getCode()); event.setRecordId(request.getFromRecordId()); diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/callback/MultidimensionalTableCallback.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/callback/MultidimensionalTableCallback.java index 9fd1517..ab59de0 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/callback/MultidimensionalTableCallback.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/callback/MultidimensionalTableCallback.java @@ -7,7 +7,7 @@ import com.ruoyi.flyingbook.domain.Event; import com.ruoyi.flyingbook.domain.EventLog; import com.ruoyi.flyingbook.service.IEventLogService; import com.ruoyi.flyingbook.service.IEventService; -import com.ruoyi.flyingbook.domain.LarkRequest; +import com.ruoyi.flyingbook.domain.RequestVo; import com.ruoyi.common.constant.RedisConstants; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; @@ -35,12 +35,12 @@ public class MultidimensionalTableCallback extends CallbackAbstract { private RedisTemplate redisTemplate; @Override - protected Boolean check(LarkRequest request) { + protected Boolean check(RequestVo request) { return Boolean.TRUE; } @Override - protected void preOperate(LarkRequest request) { + protected void preOperate(RequestVo request) { if (StringUtils.isBlank(request.getMessage())) { return; } @@ -73,7 +73,7 @@ public class MultidimensionalTableCallback extends CallbackAbstract { } @Override - protected void businessProcessing(LarkRequest request) { + protected void businessProcessing(RequestVo request) { String cacheKey = String.format("%s_%s_%s_%s", RedisConstants.MULTIPLE_TABLE_RECORD, request.getFromAppToken(),request.getFromTableId(), request.getFromRecordId()); String eventStr = (String)redisTemplate.opsForValue().get(cacheKey); Event event = null; @@ -97,7 +97,7 @@ public class MultidimensionalTableCallback extends CallbackAbstract { } @Override - protected void endHandle(LarkRequest request) { + protected void endHandle(RequestVo request) { if (request.getEventId() == null){ return; } @@ -111,7 +111,7 @@ public class MultidimensionalTableCallback extends CallbackAbstract { } @Override - protected String buildResult(LarkRequest request) { + protected String buildResult(RequestVo request) { return ""; } @@ -134,7 +134,7 @@ public class MultidimensionalTableCallback extends CallbackAbstract { return null; } - private Event buildDto(LarkRequest request,Event event){ + private Event buildDto(RequestVo request, Event event){ event = event == null ? new Event() :event; event.setFlag(FlagStatus.OK.getCode()); event.setAppToken(request.getFromAppToken()); diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/operate/ApprovalCallbackOperate.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/operate/ApprovalCallbackOperate.java index d7fda10..f858b9f 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/operate/ApprovalCallbackOperate.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/operate/ApprovalCallbackOperate.java @@ -1,15 +1,19 @@ package com.ruoyi.flyingbook.strategy.operate; import com.alibaba.fastjson.JSONObject; +import com.lark.oapi.service.approval.v4.model.GetInstanceRespBody; +import com.lark.oapi.service.bitable.v1.model.CreateAppTableRecordRespBody; import com.ruoyi.common.enums.*; -import com.ruoyi.flyingbook.LarkHelper.LarkTokenHelper; +import com.ruoyi.flyingbook.LarkHelper.LarkApprovalHelper; +import com.ruoyi.flyingbook.LarkHelper.LarkTableHelper; import com.ruoyi.flyingbook.domain.*; +import com.ruoyi.flyingbook.domain.lark.LarkApprovalRequest; +import com.ruoyi.flyingbook.domain.lark.LarkTableRequest; import com.ruoyi.flyingbook.service.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import java.util.ArrayList; @@ -29,14 +33,14 @@ import java.util.Map; public class ApprovalCallbackOperate extends LarkAbstract { @Autowired - protected LarkTokenHelper larkTokenHelper; + protected LarkTableHelper larkTableHelper; + @Autowired + protected LarkApprovalHelper larkApprovalHelper; @Autowired protected ILarkCompanyRelationService larkCompanyRelationService; @Autowired protected IEventService iEventService; @Autowired - private RedisTemplate redisTemplate; - @Autowired protected ILarkTableRelationService iLarkTableRelationService; @Autowired protected ILarkTableRowRelationService iLarkTableRowRelationService; @@ -44,17 +48,9 @@ public class ApprovalCallbackOperate extends LarkAbstract { private IEventService eventService; @Autowired private IEventLogService eventLogService; - /** - * 获取明细行 - */ - private static final String DETAIL = "DETAIL"; - private String getDetail = "https://open.feishu.cn/open-apis/approval/v4/instances/:instance_id"; - //新增明细行 - private static final String CREATE_OPERATE = "CREATE_OPERATE"; - private String addLine = "https://open.feishu.cn/open-apis/bitable/v1/apps/:app_token/tables/:table_id/records"; - private void fillTableRelation(LarkRequest request) { + private void fillTableRelation(RequestVo request) { LarkTableRelation tableRelation = new LarkTableRelation(); tableRelation.setLarkCompanyRelationId(request.getCompanyRelationId()); @@ -95,7 +91,7 @@ public class ApprovalCallbackOperate extends LarkAbstract { } @Override - protected void preOperate(LarkRequest request) { + protected void preOperate(RequestVo request) { Event event = request.getEvent(); if (event == null) { throw new RuntimeException("当前事件为空"); @@ -110,62 +106,43 @@ public class ApprovalCallbackOperate extends LarkAbstract { request.setCompanyRelationId(relation.getId()); //关联关系表填充 this.fillTableRelation(request); - String token = larkTokenHelper.getToken(relation.getAppId(), relation.getSecret(), "tenant_access_token"); - request.setToken(token); + request.setAppId(relation.getAppId()); + request.setAppSecret(relation.getSecret()); } @Override - protected void businessProcessing(LarkRequest request) { - request.setOperateType(DETAIL); - JSONObject message = larkTokenHelper.getLark(getUrl(request), null, request.getToken(), null); - request.setRecord(message); + protected void businessProcessing(RequestVo request) { + LarkApprovalRequest detailRequest = new LarkApprovalRequest(request.getAppId(), request.getAppSecret(), request.getInstance()); + GetInstanceRespBody approvalInfoDetail = larkApprovalHelper.getApprovalInfoDetail(detailRequest); + request.setInstanceRespBody(approvalInfoDetail); for (Map.Entry entry : request.getTableRelationMap().entrySet()) { String[] arr = entry.getKey().split("_"); request.setToAppToken(arr[0]); request.setToTableId(arr[1]); request.setTableRelationId(entry.getValue()); - Map> body = getBody(request); - String url = getUrl(request); - if (StringUtils.isBlank(url)) { - return; - } - send(request.getOperateType(), url, body, request); + send( request); } } @Override - protected String getUrl(LarkRequest request) { - Event event = request.getEvent(); - if (CREATE_OPERATE.equals(request.getOperateType())) { - return addLine.replace(":app_token", request.getToAppToken()).replace(":table_id", request.getToTableId()); - } else if (DETAIL.equals(request.getOperateType())) { - return getDetail.replace(":instance_id", event.getRecordId()); - } - return null; - } - - @Override - protected Map> getBody(LarkRequest request) { - Map> result = new HashMap<>(); + protected Map getBody(RequestVo request) { + Map result = new HashMap<>(); JSONObject record = request.getRecord(); String responseCode = record.getString("code"); if ("0".equals(responseCode)) { result = parseMap(request); - request.setOperateType(CREATE_OPERATE); } return result; } - private Map> parseMap(LarkRequest request) { - Map result = new HashMap<>(); + private Map parseMap(RequestVo request) { + Map result = new HashMap<>(); JSONObject record = request.getRecord(); Map colRelation = request.getTableColRelationMap().getOrDefault(request.getTableRelationId(), new HashMap<>()); for (Map.Entry entry : colRelation.entrySet()) { result.put(entry.getValue(), recursiveValue(record,entry.getKey())); } - Map> map = new HashMap<>(); - map.put("fields", result); - return map; + return result; } private Object recursiveValue(JSONObject record,String key){ @@ -180,16 +157,15 @@ public class ApprovalCallbackOperate extends LarkAbstract { return record.get(split[split.length-1]); } - protected void send(String type, String url, Map> body, LarkRequest request) { + protected void send(RequestVo request) { + Map body = getBody(request); request.setOperateInfo(String.valueOf(body)); - String token = request.getToken(); - if (CREATE_OPERATE.equals(type)) { - JSONObject result = larkTokenHelper.postLark(url, body, token); - } + LarkTableRequest add = new LarkTableRequest(request.getAppId(), request.getAppSecret(), request.getToAppToken(), request.getToTableId(), body); + CreateAppTableRecordRespBody resp = larkTableHelper.addTableRecord(add); } @Override - protected void endHandle(LarkRequest request) { + protected void endHandle(RequestVo request) { Event event = request.getEvent(); EventLog eventLog = null; if (StringUtils.isBlank(request.getErrorMessage())) { @@ -208,7 +184,7 @@ public class ApprovalCallbackOperate extends LarkAbstract { } @Override - protected String buildResult(LarkRequest request) { + protected String buildResult(RequestVo request) { return null; } diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/operate/LarkAbstract.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/operate/LarkAbstract.java index 1c46e52..ede45f5 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/operate/LarkAbstract.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/operate/LarkAbstract.java @@ -1,9 +1,7 @@ package com.ruoyi.flyingbook.strategy.operate; -import com.alibaba.fastjson.JSONObject; -import com.ruoyi.flyingbook.domain.LarkRequest; +import com.ruoyi.flyingbook.domain.RequestVo; import com.ruoyi.flyingbook.strategy.LarkOperateAbstract; -import org.apache.commons.lang3.StringUtils; import java.util.Map; @@ -14,15 +12,11 @@ import java.util.Map; public abstract class LarkAbstract extends LarkOperateAbstract { - /** - * 获取URL地址 - */ - protected abstract String getUrl(LarkRequest request); + /** * 获取body体 */ - protected abstract Map> getBody(LarkRequest request); - + protected abstract Map getBody(RequestVo request); diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/operate/MultidimensionalTableOperate.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/operate/MultidimensionalTableOperate.java index 6bddc63..4db8613 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/operate/MultidimensionalTableOperate.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/operate/MultidimensionalTableOperate.java @@ -1,10 +1,12 @@ package com.ruoyi.flyingbook.strategy.operate; -import com.alibaba.fastjson.JSONObject; +import com.lark.oapi.service.bitable.v1.model.CreateAppTableRecordRespBody; +import com.lark.oapi.service.bitable.v1.model.GetAppTableRecordResp; import com.ruoyi.common.constant.RedisConstants; import com.ruoyi.common.enums.*; -import com.ruoyi.flyingbook.LarkHelper.LarkTokenHelper; +import com.ruoyi.flyingbook.LarkHelper.LarkTableHelper; import com.ruoyi.flyingbook.domain.*; +import com.ruoyi.flyingbook.domain.lark.LarkTableRequest; import com.ruoyi.flyingbook.service.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; @@ -13,10 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** @@ -30,7 +29,7 @@ import java.util.Map; public class MultidimensionalTableOperate extends LarkAbstract { @Autowired - protected LarkTokenHelper larkTokenHelper; + protected LarkTableHelper larkTokenHelper; @Autowired protected ILarkCompanyRelationService larkCompanyRelationService; @Autowired @@ -46,22 +45,20 @@ public class MultidimensionalTableOperate extends LarkAbstract { @Autowired private IEventLogService eventLogService; /** - * 获取明细行 + * 新增明细行 */ - private static final String DETAIL = "DETAIL"; - private String getDetail = "https://open.feishu.cn/open-apis/bitable/v1/apps/:app_token/tables/:table_id/records/:record_id"; - //新增明细行 private static final String CREATE_OPERATE = "CREATE_OPERATE"; - private String addLine = "https://open.feishu.cn/open-apis/bitable/v1/apps/:app_token/tables/:table_id/records"; - //修改明细行 + /** + * 修改明细行 + */ private static final String UPDATE_OPERATE = "UPDATE_OPERATE"; - private String updateLine = "https://open.feishu.cn/open-apis/bitable/v1/apps/:app_token/tables/:table_id/records/:record_id"; - //删除明细行 + /** + * 删除明细行 + */ private static final String DELETE_OPERATE = "DELETE_OPERATE"; - private String deleteLine = "https://open.feishu.cn/open-apis/bitable/v1/apps/:app_token/tables/:table_id/records/:record_id"; - private void fillTableRelation(LarkRequest request) { + private void fillTableRelation(RequestVo request) { LarkTableRelation tableRelation = new LarkTableRelation(); tableRelation.setLarkCompanyRelationId(request.getCompanyRelationId()); tableRelation.setFromTableId(request.getEvent().getTableId()); @@ -104,7 +101,7 @@ public class MultidimensionalTableOperate extends LarkAbstract { } @Override - protected void preOperate(LarkRequest request) { + protected void preOperate(RequestVo request) { Event event = request.getEvent(); if (event == null) { throw new RuntimeException("当前事件为空"); @@ -115,62 +112,36 @@ public class MultidimensionalTableOperate extends LarkAbstract { log.error("larkCompanyRelationService.getByCompanyName:{}", errorMessage); throw new RuntimeException(errorMessage); } + request.setAppId(relation.getAppId()); + request.setAppSecret(relation.getSecret()); //用于后续追加row时候做关联关系 request.setCompanyRelationId(relation.getId()); //关联关系表填充 this.fillTableRelation(request); - String token = larkTokenHelper.getToken(relation.getAppId(), relation.getSecret(), "tenant_access_token"); - request.setToken(token); } @Override - protected void businessProcessing(LarkRequest request) { - request.setOperateType(DETAIL); - List safeCodeList = new ArrayList<>(); - safeCodeList.add("1254043"); - JSONObject message = larkTokenHelper.getLark(getUrl(request), null, request.getToken(),safeCodeList); - request.setRecord(message); + protected void businessProcessing(RequestVo request) { + Event event = request.getEvent(); + LarkTableRequest detailRequest = new LarkTableRequest(request.getAppId(), request.getAppSecret(), event.getAppToken(), event.getTableId(), event.getRecordId()); + detailRequest.setSafeCode(Arrays.asList(1254043)); + GetAppTableRecordResp recordDetail = larkTokenHelper.getTableRecordDetail(detailRequest); + request.setRespBody(recordDetail); for (Map.Entry entry : request.getTableRelationMap().entrySet()) { String[] arr = entry.getKey().split("_"); request.setToAppToken(arr[0]); request.setToTableId(arr[1]); request.setTableRelationId(entry.getValue()); - Map> body = getBody(request); - String url = getUrl(request); - if (StringUtils.isBlank(url)) { - return; - } - send(request.getOperateType(), url, body, request); + send(request); } } - @Override - protected String getUrl(LarkRequest request) { - Event event = request.getEvent(); - Map rowRelation = request.getTableRowRelationMap().get(request.getTableRelationId()); - String cacheKey = String.format("%s_%s_%s_%s", RedisConstants.MULTIPLE_TABLE_RECORD, event.getAppToken(), event.getTableId(), event.getRecordId()); - redisTemplate.delete(cacheKey); - if (UPDATE_OPERATE.equals(request.getOperateType())) { - if (rowRelation.containsKey(event.getRecordId())) { - return updateLine.replace(":app_token", request.getToAppToken()).replace(":table_id", request.getToTableId()).replace(":record_id", rowRelation.get(event.getRecordId())); - } - } else if (CREATE_OPERATE.equals(request.getOperateType())) { - return addLine.replace(":app_token", request.getToAppToken()).replace(":table_id", request.getToTableId()); - } else if (DELETE_OPERATE.equals(request.getOperateType())) { - if (rowRelation.containsKey(event.getRecordId())) { - return deleteLine.replace(":app_token", request.getToAppToken()).replace(":table_id", request.getToTableId()).replace(":record_id", rowRelation.get(event.getRecordId())); - } - } else if (DETAIL.equals(request.getOperateType())) { - return getDetail.replace(":app_token", event.getAppToken()).replace(":table_id", event.getTableId()).replace(":record_id", event.getRecordId()); - } - return null; - } @Override - protected Map> getBody(LarkRequest request) { - Map> result = new HashMap<>(); - JSONObject record = request.getRecord(); - String responseCode = record.getString("code"); + protected Map getBody(RequestVo request) { + Map result = new HashMap<>(); + GetAppTableRecordResp respBody = request.getRespBody(); + Integer responseCode = respBody.getCode(); Map rowMap = request.getTableRowRelationMap().getOrDefault(request.getTableRelationId(), new HashMap<>()); if ("1254043".equals(responseCode)) { request.setOperateType(DELETE_OPERATE); @@ -185,50 +156,53 @@ public class MultidimensionalTableOperate extends LarkAbstract { return result; } - private Map> parseMap(LarkRequest request) { - Map result = new HashMap<>(); - JSONObject record = request.getRecord(); + /** + * 对两个表的列进行映射 + */ + private Map parseMap(RequestVo request) { + Map result = new HashMap<>(); Map colRelation = request.getTableColRelationMap().getOrDefault(request.getTableRelationId(), new HashMap<>()); - String fields = record.getJSONObject("data").getJSONObject("record").getString("fields"); - Map midMap = JSONObject.parseObject(fields, Map.class); - for (Map.Entry entry : midMap.entrySet()) { + Map midMap = request.getRespBody().getData().getRecord().getFields(); + for (Map.Entry entry : midMap.entrySet()) { String key = String.valueOf(entry.getKey()); if (!colRelation.containsKey(key)) { continue; } result.put(colRelation.get(key), entry.getValue()); } - Map> map = new HashMap<>(); - map.put("fields", result); - return map; + return result; } - protected void send(String type, String url, Map> body, LarkRequest request) { + private void send(RequestVo request) { + String type = request.getOperateType(); + Map body = getBody(request); + Event event = request.getEvent(); + String cacheKey = String.format("%s_%s_%s_%s", RedisConstants.MULTIPLE_TABLE_RECORD, event.getAppToken(), event.getTableId(), event.getRecordId()); + redisTemplate.delete(cacheKey); request.setOperateInfo(String.valueOf(body)); - String token = request.getToken(); if (CREATE_OPERATE.equals(type)) { - JSONObject result = larkTokenHelper.postLark(url, body, token); - JSONObject data = result.getJSONObject("data"); - if (data != null) { - JSONObject record = data.getJSONObject("record"); - if (record != null && StringUtils.isNotBlank(record.getString("record_id"))) { - String recordId = record.getString("record_id"); - LarkTableRowRelation rowRelation = new LarkTableRowRelation(request.getCompanyRelationId(), request.getEvent().getRecordId(), recordId, TableDetailRelationTypeEnum.ROW.getCode(), TableDetailRelationTypeEnum.ROW.getCode()); - iLarkTableRowRelationService.insertLarkTableRowRelation(rowRelation); - } + LarkTableRequest add = new LarkTableRequest(request.getAppId(), request.getAppSecret(), request.getToAppToken(), request.getToTableId(), body); + CreateAppTableRecordRespBody addResp = larkTokenHelper.addTableRecord(add); + if (addResp != null) { + String recordId = addResp.getRecord().getRecordId(); + LarkTableRowRelation rowRelation = new LarkTableRowRelation(request.getCompanyRelationId(), event.getRecordId(), recordId, TableDetailRelationTypeEnum.ROW.getCode(), TableDetailRelationTypeEnum.ROW.getCode()); + iLarkTableRowRelationService.insertLarkTableRowRelation(rowRelation); } request.setEventOperateType(EventOperateType.CREATE); } else if (UPDATE_OPERATE.equals(type)) { request.setEventOperateType(EventOperateType.UPDATE); - larkTokenHelper.putLark(url, body, token); + LarkTableRequest update = new LarkTableRequest(request.getAppId(), request.getAppSecret(), request.getToAppToken(), request.getToTableId(),request.getToRecordId(), body); + larkTokenHelper.updateTableRecord(update); } else if (DELETE_OPERATE.equals(type)) { request.setEventOperateType(EventOperateType.DELETE); - larkTokenHelper.deleteLark(url, body, token); + LarkTableRequest delete = new LarkTableRequest(request.getAppId(), request.getAppSecret(), request.getToAppToken(), request.getToTableId(),request.getToRecordId()); + larkTokenHelper.deleteTableRecord(delete); + } } @Override - protected void endHandle(LarkRequest request) { + protected void endHandle(RequestVo request) { Event event = request.getEvent(); EventLog eventLog = null; if (StringUtils.isBlank(request.getErrorMessage())) { @@ -247,7 +221,7 @@ public class MultidimensionalTableOperate extends LarkAbstract { } @Override - protected String buildResult(LarkRequest request) { + protected String buildResult(RequestVo request) { return null; }