!1293 【功能新增】AI:讯飞、文多多 PPT API 对接

Merge pull request !1293 from 小新/feature/ai
This commit is contained in:
芋道源码
2025-03-17 13:52:02 +00:00
committed by Gitee
4 changed files with 1761 additions and 0 deletions

View File

@@ -0,0 +1,389 @@
package cn.iocoder.yudao.framework.ai.core.model.wenduoduo.api;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* 文多多 API
* <p>
* <p>
* * 对接文多多:<a href="https://docmee.cn/open-platform/api">PPT 生成 API</a>
*
* @author xiaoxin
*/
@Slf4j
public class WddPptApi {
public static final String BASE_URL = "https://docmee.cn";
private final WebClient webClient;
private final Predicate<HttpStatusCode> STATUS_PREDICATE = status -> !status.is2xxSuccessful();
private final Function<Object, Function<ClientResponse, Mono<? extends Throwable>>> EXCEPTION_FUNCTION =
reqParam -> response -> response.bodyToMono(String.class).handle((responseBody, sink) -> {
HttpRequest request = response.request();
log.error("[wdd-api] 调用失败!请求方式:[{}],请求地址:[{}],请求参数:[{}],响应数据: [{}]",
request.getMethod(), request.getURI(), reqParam, responseBody);
sink.error(new IllegalStateException("[wdd-api] 调用失败!"));
});
public WddPptApi(String baseUrl) {
this.webClient = WebClient.builder()
.baseUrl(baseUrl)
.defaultHeaders((headers) -> headers.setContentType(MediaType.APPLICATION_JSON))
.build();
}
/**
* 创建 token
*
* @param request 请求信息
* @return token
*/
public String createApiToken(CreateTokenRequest request) {
return this.webClient.post()
.uri("/api/user/createApiToken")
.header("Api-Key", request.apiKey)
.body(Mono.just(request), CreateTokenRequest.class)
.retrieve()
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
.bodyToMono(ApiResponse.class)
.<String>handle((response, sink) -> {
if (response.code != 0) {
sink.error(new IllegalStateException("创建 token 异常," + response.message));
return;
}
sink.next(response.data.get("token").toString());
})
.block();
}
/**
* 创建任务
*
* @param type 类型
* 1.智能生成(主题、要求)
* 2.上传文件生成
* 3.上传思维导图生成
* 4.通过word精准转ppt
* 5.通过网页链接生成
* 6.粘贴文本内容生成
* 7.Markdown大纲生成
* @param content 内容
* type=1 用户输入主题或要求不超过1000字符
* type=2、4 不传
* type=3 幕布等分享链接
* type=5 网页链接地址http/https
* type=6 粘贴文本内容不超过20000字符
* type=7 大纲内容markdown
* @param files 文件列表
* 文件列表文件数不超过5个总大小不超过50M
* type=1 上传参考文件(非必传,支持多个)
* type=2 上传文件(支持多个)
* type=3 上传思维导图xmind/mm/md仅支持一个
* type=4 上传word文件仅支持一个
* type=5、6、7 不传
* <p>
* 支持格式doc/docx/pdf/ppt/pptx/txt/md/xls/xlsx/csv/html/epub/mobi/xmind/mm
* @return 任务ID
*/
public ApiResponse createTask(String token, Integer type, String content, List<MultipartFile> files) {
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
formData.add("type", type);
if (content != null) {
formData.add("content", content);
}
if (files != null) {
for (MultipartFile file : files) {
formData.add("file", file.getResource());
}
}
return this.webClient.post()
.uri("/api/ppt/v2/createTask")
.header("token", token)
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(formData))
.retrieve()
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(formData))
.bodyToMono(ApiResponse.class)
.block();
}
/**
* 获取生成选项
*
* @param lang 语种
* @return 生成选项
*/
public Map<String, Object> getOptions(String lang) {
String uri = "/api/ppt/v2/options";
if (lang != null) {
uri += "?lang=" + lang;
}
return this.webClient.get()
.uri(uri)
.retrieve()
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(lang))
.bodyToMono(new ParameterizedTypeReference<ApiResponse>() {
})
.<Map<String, Object>>handle((response, sink) -> {
if (response.code != 0) {
sink.error(new IllegalStateException("获取生成选项异常," + response.message));
return;
}
sink.next(response.data);
})
.block();
}
/**
* 分页查询PPT模板
*
* @param token 令牌
* @param request 请求体
* @return 模板列表
*/
public PagePptTemplateInfo getTemplatePage(String token, TemplateQueryRequest request) {
return this.webClient.post()
.uri("/api/ppt/templates")
.header("token", token)
.bodyValue(request)
.retrieve()
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
.bodyToMono(new ParameterizedTypeReference<PagePptTemplateInfo>() {
})
.block();
}
/**
* 生成大纲内容
*
* @return 大纲内容流
*/
public Flux<Map<String, Object>> createOutline(String token, CreateOutlineRequest request) {
return this.webClient.post()
.uri("/api/ppt/v2/generateContent")
.header("token", token)
.body(Mono.just(request), CreateOutlineRequest.class)
.retrieve()
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
.bodyToFlux(new ParameterizedTypeReference<>() {
});
}
/**
* 修改大纲内容
*
* @param request 请求体
* @return 大纲内容流
*/
public Flux<Map<String, Object>> updateOutline(String token, UpdateOutlineRequest request) {
return this.webClient.post()
.uri("/api/ppt/v2/updateContent")
.header("token", token)
.body(Mono.just(request), UpdateOutlineRequest.class)
.retrieve()
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
.bodyToFlux(new ParameterizedTypeReference<>() {
});
}
/**
* 生成PPT
*
* @return PPT信息
*/
public PptInfo create(String token, CreatePptRequest request) {
return this.webClient.post()
.uri("/api/ppt/v2/generatePptx")
.header("token", token)
.body(Mono.just(request), CreatePptRequest.class)
.retrieve()
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
.bodyToMono(ApiResponse.class)
.<PptInfo>handle((response, sink) -> {
if (response.code != 0) {
sink.error(new IllegalStateException("生成 PPT 异常," + response.message));
return;
}
sink.next(Objects.requireNonNull(JsonUtils.parseObject(JsonUtils.toJsonString(response.data.get("pptInfo")), PptInfo.class)));
})
.block();
}
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record CreateTokenRequest(
String apiKey,
String uid,
Integer limit
) {
public CreateTokenRequest(String apiKey) {
this(apiKey, null, null);
}
}
/**
* API 通用响应
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record ApiResponse(
Integer code,
String message,
Map<String, Object> data
) {
}
/**
* 创建任务
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record CreateTaskRequest(
Integer type,
String content,
List<MultipartFile> files
) {
}
/**
* 生成大纲内容请求
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record CreateOutlineRequest(
String id,
String length,
String scene,
String audience,
String lang,
String prompt
) {
}
/**
* 修改大纲内容请求
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record UpdateOutlineRequest(
String id,
String markdown,
String question
) {
}
/**
* 生成PPT请求
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record CreatePptRequest(
String id,
String templateId,
String markdown
) {
}
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record PptInfo(
String id,
String name,
String subject,
String coverUrl,
String fileUrl,
String templateId,
String pptxProperty,
String userId,
String userName,
int companyId,
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime updateTime,
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime createTime,
String createUser,
String updateUser
) {
}
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record TemplateQueryRequest(
int page,
int size,
Filter filters
) {
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record Filter(
int type,
String category,
String style,
String themeColor
) {
}
}
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record PagePptTemplateInfo(
List<PptTemplateInfo> data,
String total
) {
}
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record PptTemplateInfo(
String id,
int type,
Integer subType,
String layout,
String category,
String style,
String themeColor,
String lang,
boolean animation,
String subject,
String coverUrl,
String fileUrl,
List<String> pageCoverUrls,
String pptxProperty,
int sort,
int num,
Integer imgNum,
int isDeleted,
String userId,
int companyId,
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime updateTime,
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime createTime,
String createUser,
String updateUser
) {
}
}

View File

@@ -0,0 +1,766 @@
package cn.iocoder.yudao.framework.ai.core.model.xunfei.api;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* 讯飞智能PPT生成 API
* <p>
* 对接讯飞:<a href="https://www.xfyun.cn/doc/spark/PPTv2.html">智能 PPT 生成 API</a>
*
* @author xiaoxin
*/
@Slf4j
public class XunfeiPptApi {
public static final String BASE_URL = "https://zwapi.xfyun.cn/api/ppt/v2";
private final WebClient webClient;
private final String appId;
private final String apiSecret;
private final Predicate<HttpStatusCode> STATUS_PREDICATE = status -> !status.is2xxSuccessful();
private final Function<Object, Function<ClientResponse, Mono<? extends Throwable>>> EXCEPTION_FUNCTION =
reqParam -> response -> response.bodyToMono(String.class).handle((responseBody, sink) -> {
log.error("[xunfei-ppt-api] 调用失败!请求参数:[{}],响应数据: [{}]", reqParam, responseBody);
sink.error(new IllegalStateException("[xunfei-ppt-api] 调用失败!"));
});
public XunfeiPptApi(String baseUrl, String appId, String apiSecret) {
this.webClient = WebClient.builder()
.baseUrl(baseUrl)
.build();
this.appId = appId;
this.apiSecret = apiSecret;
}
/**
* 获取签名
*
* @return 签名信息
*/
private SignatureInfo getSignature() {
long timestamp = System.currentTimeMillis() / 1000;
String ts = String.valueOf(timestamp);
String signature = generateSignature(appId, apiSecret, timestamp);
return new SignatureInfo(appId, ts, signature);
}
/**
* 生成签名
*
* @param appId 应用ID
* @param apiSecret 应用密钥
* @param timestamp 时间戳(秒)
* @return 签名
*/
private String generateSignature(String appId, String apiSecret, long timestamp) {
try {
String auth = md5(appId + timestamp);
return hmacSHA1Encrypt(auth, apiSecret);
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
log.error("[xunfei-ppt-api] 生成签名失败", e);
throw new IllegalStateException("[xunfei-ppt-api] 生成签名失败");
}
}
/**
* HMAC SHA1 加密
*/
private String hmacSHA1Encrypt(String encryptText, String encryptKey)
throws NoSuchAlgorithmException, InvalidKeyException {
SecretKeySpec keySpec = new SecretKeySpec(
encryptKey.getBytes(StandardCharsets.UTF_8), "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(keySpec);
byte[] result = mac.doFinal(encryptText.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(result);
}
/**
* MD5 哈希
*/
private String md5(String text) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] digest = md.digest(text.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
/**
* 获取PPT模板列表
*
* @param style 风格,如"商务"
* @param pageSize 每页数量
* @return 模板列表
*/
public TemplatePageResponse getTemplatePage(String style, Integer pageSize) {
SignatureInfo signInfo = getSignature();
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("style", style);
requestBody.put("pageSize", pageSize != null ? pageSize.toString() : "10");
return this.webClient.post()
.uri("/template/list")
.header("appId", signInfo.appId)
.header("timestamp", signInfo.timestamp)
.header("signature", signInfo.signature)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(requestBody)
.retrieve()
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(requestBody))
.bodyToMono(TemplatePageResponse.class)
.block();
}
/**
* 创建大纲(通过文本)
*
* @param query 查询文本
* @return 大纲创建响应
*/
public CreateResponse createOutline(String query) {
SignatureInfo signInfo = getSignature();
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
formData.add("query", query);
return this.webClient.post()
.uri("/createOutline")
.header("appId", signInfo.appId)
.header("timestamp", signInfo.timestamp)
.header("signature", signInfo.signature)
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(formData))
.retrieve()
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(formData))
.bodyToMono(CreateResponse.class)
.block();
}
/**
* 直接创建PPT简化版 - 通过文本)
*
* @param query 查询文本
* @return 创建响应
*/
public CreateResponse create(String query) {
CreatePptRequest request = CreatePptRequest.builder()
.query(query)
.build();
return create(request);
}
/**
* 直接创建PPT简化版 - 通过文件)
*
* @param file 文件
* @param fileName 文件名
* @return 创建响应
*/
public CreateResponse create(MultipartFile file, String fileName) {
CreatePptRequest request = CreatePptRequest.builder()
.file(file)
.fileName(fileName)
.build();
return create(request);
}
/**
* 直接创建PPT完整版
*
* @param request 请求参数
* @return 创建响应
*/
public CreateResponse create(CreatePptRequest request) {
SignatureInfo signInfo = getSignature();
MultiValueMap<String, Object> formData = buildCreateFormData(request);
return this.webClient.post()
.uri("/create")
.header("appId", signInfo.appId)
.header("timestamp", signInfo.timestamp)
.header("signature", signInfo.signature)
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(formData))
.retrieve()
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(formData))
.bodyToMono(CreateResponse.class)
.block();
}
/**
* 通过大纲创建PPT简化版
*
* @param outline 大纲内容
* @param query 查询文本
* @return 创建响应
*/
public CreateResponse createPptByOutline(OutlineData outline, String query) {
CreatePptByOutlineRequest request = CreatePptByOutlineRequest.builder()
.outline(outline)
.query(query)
.build();
return createPptByOutline(request);
}
/**
* 通过大纲创建PPT完整版
*
* @param request 请求参数
* @return 创建响应
*/
public CreateResponse createPptByOutline(CreatePptByOutlineRequest request) {
SignatureInfo signInfo = getSignature();
return this.webClient.post()
.uri("/createPptByOutline")
.header("appId", signInfo.appId)
.header("timestamp", signInfo.timestamp)
.header("signature", signInfo.signature)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(request)
.retrieve()
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
.bodyToMono(CreateResponse.class)
.block();
}
/**
* 检查PPT生成进度
*
* @param sid 任务ID
* @return 进度响应
*/
public ProgressResponse checkProgress(String sid) {
SignatureInfo signInfo = getSignature();
return this.webClient.get()
.uri(uriBuilder -> uriBuilder
.path("/progress")
.queryParam("sid", sid)
.build())
.header("appId", signInfo.appId)
.header("timestamp", signInfo.timestamp)
.header("signature", signInfo.signature)
.retrieve()
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(sid))
.bodyToMono(ProgressResponse.class)
.block();
}
/**
* 签名信息
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
private record SignatureInfo(
String appId,
String timestamp,
String signature
) {
}
/**
* 模板列表响应
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record TemplatePageResponse(
boolean flag,
int code,
String desc,
Integer count,
TemplatePageData data
) {
}
/**
* 模板列表数据
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record TemplatePageData(
String total,
List<TemplateInfo> records,
Integer pageNum
) {
}
/**
* 模板信息
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record TemplateInfo(
String templateIndexId,
Integer pageCount,
String type,
String color,
String industry,
String style,
String detailImage
) {
}
/**
* 创建响应
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record CreateResponse(
boolean flag,
int code,
String desc,
Integer count,
CreateResponseData data
) {
}
/**
* 创建响应数据
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record CreateResponseData(
String sid,
String coverImgSrc,
String title,
String subTitle,
OutlineData outline
) {
}
/**
* 大纲数据结构
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record OutlineData(
String title,
String subTitle,
List<Chapter> chapters
) {
/**
* 章节结构
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record Chapter(
String chapterTitle,
List<ChapterContent> chapterContents
) {
/**
* 章节内容
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record ChapterContent(
String chapterTitle
) {
}
}
/**
* 将大纲对象转换为JSON字符串
*
* @return 大纲JSON字符串
*/
public String toJsonString() {
return JsonUtils.toJsonString(this);
}
}
/**
* 进度响应
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record ProgressResponse(
int code,
String desc,
ProgressResponseData data
) {
}
/**
* 进度响应数据
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record ProgressResponseData(
int process,
String pptId,
String pptUrl,
String pptStatus, // PPT构建状态building构建中done已完成build_failed生成失败
String aiImageStatus, // ai配图状态building构建中done已完成
String cardNoteStatus, // 演讲备注状态building构建中done已完成
String errMsg, // 生成PPT的失败信息
Integer totalPages, // 生成PPT的总页数
Integer donePages // 生成PPT的完成页数
) {
/**
* 是否全部完成
*
* @return 是否全部完成
*/
public boolean isAllDone() {
return "done".equals(pptStatus)
&& ("done".equals(aiImageStatus) || aiImageStatus == null)
&& ("done".equals(cardNoteStatus) || cardNoteStatus == null);
}
/**
* 是否失败
*
* @return 是否失败
*/
public boolean isFailed() {
return "build_failed".equals(pptStatus);
}
/**
* 获取进度百分比
*
* @return 进度百分比
*/
public int getProgressPercent() {
if (totalPages == null || totalPages == 0 || donePages == null) {
return process; // 兼容旧版返回
}
return (int) (donePages * 100.0 / totalPages);
}
}
/**
* 通过大纲创建PPT请求参数
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record CreatePptByOutlineRequest(
String query, // 用户生成PPT要求最多8000字
String outlineSid, // 已生成大纲后响应返回的请求大纲唯一id
OutlineData outline, // 大纲内容
String templateId, // 模板ID
String businessId, // 业务ID非必传
String author, // PPT作者名
Boolean isCardNote, // 是否生成PPT演讲备注
Boolean search, // 是否联网搜索
String language, // 语种
String fileUrl, // 文件地址
String fileName, // 文件名(带文件名后缀)
Boolean isFigure, // 是否自动配图
String aiImage // ai配图类型normal、advanced
) {
/**
* 创建构建器
*
* @return 构建器
*/
public static Builder builder() {
return new Builder();
}
/**
* 构建器类
*/
public static class Builder {
private String query;
private String outlineSid;
private OutlineData outline;
private String templateId;
private String businessId;
private String author;
private Boolean isCardNote;
private Boolean search;
private String language;
private String fileUrl;
private String fileName;
private Boolean isFigure;
private String aiImage;
public Builder query(String query) {
this.query = query;
return this;
}
public Builder outlineSid(String outlineSid) {
this.outlineSid = outlineSid;
return this;
}
public Builder outline(OutlineData outline) {
this.outline = outline;
return this;
}
public Builder templateId(String templateId) {
this.templateId = templateId;
return this;
}
public Builder businessId(String businessId) {
this.businessId = businessId;
return this;
}
public Builder author(String author) {
this.author = author;
return this;
}
public Builder isCardNote(Boolean isCardNote) {
this.isCardNote = isCardNote;
return this;
}
public Builder search(Boolean search) {
this.search = search;
return this;
}
public Builder language(String language) {
this.language = language;
return this;
}
public Builder fileUrl(String fileUrl) {
this.fileUrl = fileUrl;
return this;
}
public Builder fileName(String fileName) {
this.fileName = fileName;
return this;
}
public Builder isFigure(Boolean isFigure) {
this.isFigure = isFigure;
return this;
}
public Builder aiImage(String aiImage) {
this.aiImage = aiImage;
return this;
}
public CreatePptByOutlineRequest build() {
return new CreatePptByOutlineRequest(
query, outlineSid, outline, templateId, businessId, author,
isCardNote, search, language, fileUrl, fileName, isFigure, aiImage
);
}
}
}
/**
* 构建创建PPT的表单数据
*
* @param request 请求参数
* @return 表单数据
*/
private MultiValueMap<String, Object> buildCreateFormData(CreatePptRequest request) {
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
// 添加请求参数
if (request.query() != null) {
formData.add("query", request.query());
}
if (request.file() != null) {
try {
formData.add("file", new ByteArrayResource(request.file().getBytes()) {
@Override
public String getFilename() {
return request.file().getOriginalFilename();
}
});
} catch (IOException e) {
log.error("[xunfei-ppt-api] 文件处理失败", e);
throw new IllegalStateException("[xunfei-ppt-api] 文件处理失败", e);
}
}
if (request.fileUrl() != null) {
formData.add("fileUrl", request.fileUrl());
}
if (request.fileName() != null) {
formData.add("fileName", request.fileName());
}
if (request.templateId() != null) {
formData.add("templateId", request.templateId());
}
if (request.businessId() != null) {
formData.add("businessId", request.businessId());
}
if (request.author() != null) {
formData.add("author", request.author());
}
if (request.isCardNote() != null) {
formData.add("isCardNote", request.isCardNote().toString());
}
if (request.search() != null) {
formData.add("search", request.search().toString());
}
if (request.language() != null) {
formData.add("language", request.language());
}
if (request.isFigure() != null) {
formData.add("isFigure", request.isFigure().toString());
}
if (request.aiImage() != null) {
formData.add("aiImage", request.aiImage());
}
return formData;
}
/**
* 直接生成PPT请求参数
*/
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public record CreatePptRequest(
String query, // 用户生成PPT要求最多8000字
MultipartFile file, // 上传文件
String fileUrl, // 文件地址
String fileName, // 文件名(带文件名后缀)
String templateId, // 模板ID
String businessId, // 业务ID非必传
String author, // PPT作者名
Boolean isCardNote, // 是否生成PPT演讲备注
Boolean search, // 是否联网搜索
String language, // 语种
Boolean isFigure, // 是否自动配图
String aiImage // ai配图类型normal、advanced
) {
/**
* 创建构建器
*
* @return 构建器
*/
public static Builder builder() {
return new Builder();
}
/**
* 构建器类
*/
public static class Builder {
private String query;
private MultipartFile file;
private String fileUrl;
private String fileName;
private String templateId;
private String businessId;
private String author;
private Boolean isCardNote;
private Boolean search;
private String language;
private Boolean isFigure;
private String aiImage;
public Builder query(String query) {
this.query = query;
return this;
}
public Builder file(MultipartFile file) {
this.file = file;
return this;
}
public Builder fileUrl(String fileUrl) {
this.fileUrl = fileUrl;
return this;
}
public Builder fileName(String fileName) {
this.fileName = fileName;
return this;
}
public Builder templateId(String templateId) {
this.templateId = templateId;
return this;
}
public Builder businessId(String businessId) {
this.businessId = businessId;
return this;
}
public Builder author(String author) {
this.author = author;
return this;
}
public Builder isCardNote(Boolean isCardNote) {
this.isCardNote = isCardNote;
return this;
}
public Builder search(Boolean search) {
this.search = search;
return this;
}
public Builder language(String language) {
this.language = language;
return this;
}
public Builder isFigure(Boolean isFigure) {
this.isFigure = isFigure;
return this;
}
public Builder aiImage(String aiImage) {
this.aiImage = aiImage;
return this;
}
public CreatePptRequest build() {
// 验证参数
if (query == null && file == null && fileUrl == null) {
throw new IllegalArgumentException("query、file、fileUrl必填其一");
}
if ((file != null || fileUrl != null) && fileName == null) {
throw new IllegalArgumentException("如果传file或者fileUrlfileName必填");
}
return new CreatePptRequest(
query, file, fileUrl, fileName, templateId, businessId, author,
isCardNote, search, language, isFigure, aiImage
);
}
}
}
}