fix: resolve deployment errors and add missing files
Fix CSS issue and create missing documentation files Add new data platform and mobile optimization features Co-authored-by: null <4804959+fnvtk@users.noreply.github.com>
This commit is contained in:
@@ -18,6 +18,28 @@ import {
|
||||
WidthType,
|
||||
BorderStyle,
|
||||
} from "docx"
|
||||
import { Buffer } from "buffer"
|
||||
|
||||
interface Screenshot {
|
||||
id: string
|
||||
name: string
|
||||
url: string
|
||||
dataUrl?: string
|
||||
timestamp: Date
|
||||
status: string
|
||||
}
|
||||
|
||||
interface DocumentSettings {
|
||||
title: string
|
||||
author: string
|
||||
description: string
|
||||
includeTimestamp: boolean
|
||||
includePageUrls: boolean
|
||||
imageFormat: string
|
||||
imageQuality: number
|
||||
pageSize: string
|
||||
orientation: string
|
||||
}
|
||||
|
||||
interface DocumentSection {
|
||||
title: string
|
||||
@@ -34,34 +56,52 @@ interface DocumentData {
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成Word文档
|
||||
* @param data 文档数据
|
||||
* @returns 文档的blob URL
|
||||
* 将base64字符串转换为Buffer
|
||||
* @param base64 base64字符串
|
||||
* @returns Buffer
|
||||
*/
|
||||
export async function generateDocx(data: DocumentData): Promise<string> {
|
||||
function base64ToBuffer(base64: string): Buffer {
|
||||
try {
|
||||
// 移除data URL前缀
|
||||
const base64Data = base64.includes("base64,") ? base64.split("base64,")[1] : base64
|
||||
|
||||
// 转换为Buffer
|
||||
return Buffer.from(base64Data, "base64")
|
||||
} catch (error) {
|
||||
console.error("base64转换为Buffer时出错:", error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成Word文档
|
||||
* @param screenshots 截图数据
|
||||
* @param settings 文档设置
|
||||
* @returns 文档的ArrayBuffer
|
||||
*/
|
||||
export async function generateDocx(screenshots: Screenshot[], settings: DocumentSettings): Promise<ArrayBuffer> {
|
||||
console.log("开始生成Word文档...")
|
||||
|
||||
try {
|
||||
// 验证输入数据
|
||||
if (!data) {
|
||||
throw new Error("文档数据不能为空")
|
||||
if (!screenshots || !Array.isArray(screenshots)) {
|
||||
throw new Error("截图数据不能为空或不是数组")
|
||||
}
|
||||
|
||||
if (!Array.isArray(data.sections)) {
|
||||
console.warn("sections不是数组,使用空数组代替")
|
||||
data.sections = []
|
||||
if (!settings) {
|
||||
throw new Error("文档设置不能为空")
|
||||
}
|
||||
|
||||
console.log(`文档标题: ${data.title}`)
|
||||
console.log(`作者: ${data.author}`)
|
||||
console.log(`日期: ${data.date}`)
|
||||
console.log(`部分数量: ${data.sections.length}`)
|
||||
console.log(`文档标题: ${settings.title}`)
|
||||
console.log(`作者: ${settings.author}`)
|
||||
console.log(`描述: ${settings.description}`)
|
||||
console.log(`部分数量: ${screenshots.length}`)
|
||||
|
||||
// 创建文档
|
||||
const doc = new Document({
|
||||
title: data.title,
|
||||
description: "由文档生成工具自动生成",
|
||||
creator: data.author,
|
||||
title: settings.title,
|
||||
description: settings.description,
|
||||
creator: settings.author,
|
||||
styles: {
|
||||
paragraphStyles: [
|
||||
{
|
||||
@@ -126,7 +166,6 @@ export async function generateDocx(data: DocumentData): Promise<string> {
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
// 文档部分
|
||||
@@ -138,44 +177,44 @@ export async function generateDocx(data: DocumentData): Promise<string> {
|
||||
children: [
|
||||
new Paragraph({
|
||||
text: "",
|
||||
spacing: {
|
||||
spacing: {\
|
||||
before: 3000, // 大约页面1/3处
|
||||
},
|
||||
}),
|
||||
new Paragraph({
|
||||
text: data.title,
|
||||
},\
|
||||
}),\
|
||||
new Paragraph({\
|
||||
text: settings.title,\
|
||||
heading: HeadingLevel.TITLE,
|
||||
alignment: AlignmentType.CENTER,
|
||||
spacing: {
|
||||
spacing: {\
|
||||
after: 400,
|
||||
},
|
||||
}),
|
||||
new Paragraph({
|
||||
text: "",
|
||||
spacing: {
|
||||
new Paragraph({\
|
||||
text: "",\
|
||||
spacing: {\
|
||||
before: 800,
|
||||
},
|
||||
}),
|
||||
new Paragraph({
|
||||
alignment: AlignmentType.CENTER,
|
||||
alignment: AlignmentType.CENTER,\
|
||||
children: [
|
||||
new TextRun({
|
||||
text: `作者: ${data.author}`,
|
||||
text: \`作者: ${settings.author}`,\
|
||||
size: 24,\
|
||||
}),
|
||||
],
|
||||
}),
|
||||
new Paragraph({
|
||||
alignment: AlignmentType.CENTER,\
|
||||
children: [
|
||||
new TextRun({
|
||||
text: `生成日期: ${new Date().toLocaleString()}`,\
|
||||
size: 24,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
new Paragraph({
|
||||
alignment: AlignmentType.CENTER,
|
||||
children: [
|
||||
new TextRun({
|
||||
text: `生成日期: ${data.date}`,
|
||||
size: 24,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
new Paragraph({
|
||||
text: "",
|
||||
text: "",\
|
||||
break: PageBreak.AFTER,
|
||||
}),
|
||||
],
|
||||
@@ -184,20 +223,20 @@ export async function generateDocx(data: DocumentData): Promise<string> {
|
||||
// 目录
|
||||
sections.push({
|
||||
properties: {},
|
||||
children: [
|
||||
children: [\
|
||||
new Paragraph({
|
||||
text: "目录",
|
||||
heading: HeadingLevel.HEADING_1,
|
||||
heading: HeadingLevel.HEADING_1,\
|
||||
alignment: AlignmentType.CENTER,
|
||||
}),
|
||||
new TableOfContents("目录", {
|
||||
hyperlink: true,
|
||||
headingStyleRange: "1-3",
|
||||
}),
|
||||
headingStyleRange: "1-3",\
|
||||
}),\
|
||||
new Paragraph({
|
||||
text: "",
|
||||
break: PageBreak.AFTER,
|
||||
}),
|
||||
}),\
|
||||
],
|
||||
})
|
||||
|
||||
@@ -208,7 +247,7 @@ export async function generateDocx(data: DocumentData): Promise<string> {
|
||||
}
|
||||
|
||||
// 添加简介
|
||||
contentSection.children.push(
|
||||
contentSection.children.push(\
|
||||
new Paragraph({
|
||||
text: "1. 简介",
|
||||
heading: HeadingLevel.HEADING_1,
|
||||
@@ -218,7 +257,7 @@ export async function generateDocx(data: DocumentData): Promise<string> {
|
||||
}),
|
||||
new Paragraph({
|
||||
text: "文档目的是帮助用户了解系统功能和使用方法,为系统管理员和最终用户提供参考。",
|
||||
}),
|
||||
}),\
|
||||
new Paragraph({
|
||||
text: "",
|
||||
}),
|
||||
@@ -236,53 +275,56 @@ export async function generateDocx(data: DocumentData): Promise<string> {
|
||||
console.log("开始处理文档部分...")
|
||||
|
||||
// 使用for循环而不是forEach,以便更好地处理错误
|
||||
for (let i = 0; i < data.sections.length; i++) {
|
||||
for (let i = 0; i < screenshots.length; i++) {
|
||||
try {
|
||||
const section = data.sections[i]
|
||||
console.log(`处理部分 ${i + 1}/${data.sections.length}: ${section.title}`)
|
||||
|
||||
// 验证部分数据
|
||||
if (!section.title) {
|
||||
console.warn(`部分 ${i + 1} 缺少标题,使用默认标题`)
|
||||
section.title = `页面 ${i + 1}`
|
||||
}
|
||||
|
||||
if (!section.description) {
|
||||
console.warn(`部分 ${i + 1} 缺少描述,使用默认描述`)
|
||||
section.description = `这是 ${section.title} 页面的描述。`
|
||||
}
|
||||
const screenshot = screenshots[i]
|
||||
console.log(\`处理部分 ${i + 1}/${screenshots.length}: ${screenshot.name}`)
|
||||
|
||||
// 添加标题
|
||||
contentSection.children.push(
|
||||
new Paragraph({
|
||||
text: `2.${i + 1} ${section.title}`,
|
||||
text: `2.${i + 1} ${screenshot.name}`,
|
||||
heading: HeadingLevel.HEADING_2,
|
||||
}),
|
||||
)
|
||||
|
||||
// 添加描述
|
||||
const descriptionParagraphs = section.description.split("\n")
|
||||
for (const paragraph of descriptionParagraphs) {
|
||||
// 添加页面URL和截图时间
|
||||
if (settings.includePageUrls) {
|
||||
contentSection.children.push(
|
||||
new Paragraph({
|
||||
text: paragraph,
|
||||
text: `页面URL: ${screenshot.url}`,
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
if (settings.includeTimestamp) {
|
||||
contentSection.children.push(
|
||||
new Paragraph({
|
||||
text: `截图时间: ${screenshot.timestamp.toLocaleString()}`,
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
// 添加状态
|
||||
contentSection.children.push(
|
||||
new Paragraph({
|
||||
text: `状态: ${screenshot.status}`,
|
||||
}),
|
||||
)
|
||||
|
||||
// 添加截图
|
||||
try {
|
||||
if (section.screenshot && section.screenshot.startsWith("data:image/")) {
|
||||
console.log(`处理截图: ${section.title}`)
|
||||
if (screenshot.dataUrl && screenshot.dataUrl.startsWith("data:image/")) {
|
||||
console.log(`处理截图: ${screenshot.name}`)
|
||||
|
||||
// 从base64数据URL中提取图像数据
|
||||
const base64Data = section.screenshot.split(",")[1]
|
||||
const base64Data = screenshot.dataUrl.split(",")[1]
|
||||
if (!base64Data) {
|
||||
throw new Error("无效的base64数据")
|
||||
}
|
||||
|
||||
// 将base64转换为二进制数据
|
||||
const imageBuffer = Buffer.from(base64Data, "base64")
|
||||
const imageBuffer = base64ToBuffer(screenshot.dataUrl)
|
||||
|
||||
// 添加图像
|
||||
contentSection.children.push(
|
||||
@@ -299,7 +341,7 @@ export async function generateDocx(data: DocumentData): Promise<string> {
|
||||
alignment: AlignmentType.CENTER,
|
||||
}),
|
||||
new Paragraph({
|
||||
text: `图 ${i + 1}: ${section.title} 页面截图`,
|
||||
text: `图 ${i + 1}: ${screenshot.name} 页面截图`,
|
||||
style: "Caption",
|
||||
}),
|
||||
new Paragraph({
|
||||
@@ -385,7 +427,7 @@ export async function generateDocx(data: DocumentData): Promise<string> {
|
||||
size: 70,
|
||||
type: WidthType.PERCENTAGE,
|
||||
},
|
||||
children: [new Paragraph(data.title)],
|
||||
children: [new Paragraph(settings.title)],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
@@ -395,7 +437,7 @@ export async function generateDocx(data: DocumentData): Promise<string> {
|
||||
children: [new Paragraph("作者")],
|
||||
}),
|
||||
new TableCell({
|
||||
children: [new Paragraph(data.author)],
|
||||
children: [new Paragraph(settings.author)],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
@@ -405,7 +447,7 @@ export async function generateDocx(data: DocumentData): Promise<string> {
|
||||
children: [new Paragraph("生成日期")],
|
||||
}),
|
||||
new TableCell({
|
||||
children: [new Paragraph(data.date)],
|
||||
children: [new Paragraph(new Date().toLocaleString())],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
@@ -415,7 +457,7 @@ export async function generateDocx(data: DocumentData): Promise<string> {
|
||||
children: [new Paragraph("页面数量")],
|
||||
}),
|
||||
new TableCell({
|
||||
children: [new Paragraph(String(data.sections.length))],
|
||||
children: [new Paragraph(String(screenshots.length))],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
@@ -425,44 +467,19 @@ export async function generateDocx(data: DocumentData): Promise<string> {
|
||||
contentSection.children.push(infoTable)
|
||||
|
||||
// 添加内容部分到文档
|
||||
sections.push(contentSection)
|
||||
|
||||
// 设置文档部分
|
||||
doc.addSection({
|
||||
children: [...sections[0].children, ...sections[1].children, ...contentSection.children],
|
||||
children: sections[0].children.concat(sections[1].children, contentSection.children),
|
||||
})
|
||||
|
||||
console.log("文档生成完成,准备导出...")
|
||||
|
||||
// 生成blob
|
||||
const buffer = await doc.save()
|
||||
const blob = new Blob([buffer], { type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document" })
|
||||
console.log("文档生成完成")
|
||||
|
||||
// 创建URL
|
||||
const url = URL.createObjectURL(blob)
|
||||
console.log("文档URL已创建:", url)
|
||||
|
||||
return url
|
||||
return buffer
|
||||
} catch (error) {
|
||||
console.error("生成Word文档时出错:", error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将base64字符串转换为Buffer
|
||||
* @param base64 base64字符串
|
||||
* @returns Buffer
|
||||
*/
|
||||
function base64ToBuffer(base64: string): Buffer {
|
||||
try {
|
||||
// 移除data URL前缀
|
||||
const base64Data = base64.includes("base64,") ? base64.split("base64,")[1] : base64
|
||||
|
||||
// 转换为Buffer
|
||||
return Buffer.from(base64Data, "base64")
|
||||
} catch (error) {
|
||||
console.error("base64转换为Buffer时出错:", error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user