修复 WxCpApprovalWorkflowDemo.java 的 Java 8 兼容性和编译错误 #3789
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR #3687 引入的演示代码使用了 Java 10+ 的
var关键字,且调用了不存在的方法导致编译失败。修复内容
Java 8 兼容性
import me.chanjar.weixin.cp.bean.oa.WxCpOaApprovalTemplateResultvar templateResult→WxCpOaApprovalTemplateResult templateResult方法调用修正
detail.getSpStatus().getCode()→detail.getSpStatus()(枚举类型无 getCode)detail.getApplyer()→detail.getApplier()detail.getSpRecord()→detail.getSpRecords()(数组字段)record.getSpStatus()→record.getStatus()approvalInfo.getCount()→approvalInfo.getSpNoList().size()Original prompt
Issue
The test/demo class weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpApprovalWorkflowDemo.java currently uses Java local variable type inference (var) in templateManagement(), which causes compilation failures in projects/builds using Java 8. The demo file is also missing an explicit import for WxCpOaApprovalTemplateResult.
Goal
Create a small change to make the demo Java 8 compatible by replacing the use of 'var' with an explicit type and adding the required import. Keep all other code unchanged.
Files to change
Change details
var templateResult = wxCpService.getOaService().getTemplateDetail(templateId);
with:
WxCpOaApprovalTemplateResult templateResult = wxCpService.getOaService().getTemplateDetail(templateId);
Provide the updated file contents (full file) below. This is a minimal, non-behavioral change intended only to fix compilation on Java8.
Updated file contents:
package me.chanjar.weixin.cp.demo;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.oa.WxCpApprovalDetailResult;
import me.chanjar.weixin.cp.bean.oa.WxCpApprovalInfo;
import me.chanjar.weixin.cp.bean.oa.WxCpOaApplyEventRequest;
import me.chanjar.weixin.cp.bean.oa.applydata.ApplyDataContent;
import me.chanjar.weixin.cp.bean.oa.applydata.ContentValue;
import me.chanjar.weixin.cp.bean.oa.WxCpOaApprovalTemplateResult;
import java.util.Arrays;
import java.util.Date;
/**
企业微信流程审批功能演示代码
WeChat Enterprise Workflow Approval Demo
演示如何使用WxJava SDK中已实现的完整审批流程功能
Demonstrates how to use the comprehensive approval workflow features already implemented in WxJava SDK
文档参考 Documentation Reference: https://work.weixin.qq.com/api/doc/90000/90135/91853
*/
public class WxCpApprovalWorkflowDemo {
private WxCpService wxCpService;
public WxCpApprovalWorkflowDemo(WxCpService wxCpService) {
this.wxCpService = wxCpService;
}
public String submitApprovalApplication() throws Exception {
WxCpOaApplyEventRequest request = new WxCpOaApplyEventRequest()
.setCreatorUserId("creator_user_id")
.setTemplateId("3Tka1eD6v6JfzhDMqPd3aMkFdxqtJMc2ZRioUBGCNS")
.setUseTemplateApprover(0)
.setApprovers(Arrays.asList(
new WxCpOaApplyEventRequest.Approver()
.setAttr(2)
.setUserIds(new String[]{"approver1", "approver2"})
))
.setNotifiers(new String[]{"notifier1", "notifier2"})
.setNotifyType(1)
.setApplyData(new WxCpOaApplyEventRequest.ApplyData()
.setContents(Arrays.asList(
new ApplyDataContent()
.setControl("Text")
.setId("Text-1234567890")
.setValue(new ContentValue().setText("这是一个审批申请的文本内容")),
new ApplyDataContent()
.setControl("Number")
.setId("Number-1234567890")
.setValue(new ContentValue().setNewNumber("1000")),
new ApplyDataContent()
.setControl("Money")
.setId("Money-1234567890")
.setValue(new ContentValue().setNewMoney("10000"))
))
);
}
public void getApprovalDetails(String spNo) throws Exception {
WxCpApprovalDetailResult result = wxCpService.getOaService().getApprovalDetail(spNo);
WxCpApprovalDetailResult.WxCpApprovalDetail detail = result.getInfo();
System.out.println("审批单号: " + detail.getSpNo());
System.out.println("审批名称: " + detail.getSpName());
System.out.println("审批状态: " + detail.getSpStatus().getCode());
System.out.println("申请人: " + detail.getApplyer().getUserId());
System.out.println("申请时间: " + detail.getApplyTime());
if (detail.getSpRecord() != null) {
detail.getSpRecord().forEach(record -> {
System.out.println("审批节点状态: " + record.getSpStatus());
System.out.println("审批人: " + record.getDetails());
});
}
}
public void batchGetApprovalInfo() throws Exception {
Date startTime = new Date(System.currentTimeMillis() - 7 * 24 * 60 * 60 * 1000L);
Date endTime = new Date();
WxCpApprovalInfo approvalInfo = wxCpService.getOaService().getApprovalInfo(startTime, endTime, "0", 100, null);
System.out.println("获取到的审批单数量: " + approvalInfo.getCount());
if (approvalInfo.getSpNoList() != null) {
approvalInfo.getSpNoList().forEach(spNo -> System.out.println("审批单号: " + spNo));
...
This pull request was created as a result of the following prompt from Copilot chat.
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.