123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Word模板替换</title>
- <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
- <style>
- /* 全局样式 */
- body {
- font-family: Arial, sans-serif;
- max-width: 800px;
- margin: 0 auto;
- padding: 20px;
- background-color: #f5f5f5;
- }
- .container {
- background-color: white;
- padding: 20px;
- border-radius: 8px;
- box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
- }
- h1 {
- color: #333;
- text-align: center;
- }
- /* 表单样式 */
- .form {
- margin-top: 20px;
- }
- .form-group {
- margin-bottom: 15px;
- }
- label {
- display: block;
- margin-bottom: 5px;
- font-weight: bold;
- }
- .text-input {
- width: 100%;
- padding: 8px 10px;
- border: 1px solid #ddd;
- border-radius: 4px;
- box-sizing: border-box;
- }
- textarea.text-input {
- resize: vertical;
- min-height: 60px;
- font-family: Arial, sans-serif;
- }
- /* 按钮样式 */
- .btn {
- background-color: #4caf50;
- color: white;
- padding: 12px 20px;
- border: none;
- border-radius: 4px;
- cursor: pointer;
- font-size: 16px;
- display: block;
- width: 100%;
- margin-top: 20px;
- }
- .btn:hover {
- background-color: #45a049;
- }
- .btn:disabled {
- background-color: #cccccc;
- cursor: not-allowed;
- }
- /* 状态消息样式 */
- .status {
- margin-top: 20px;
- padding: 15px;
- border-radius: 4px;
- display: none;
- }
- .success {
- background-color: #dff0d8;
- color: #3c763d;
- border: 1px solid #d6e9c6;
- }
- .error {
- background-color: #f2dede;
- color: #a94442;
- border: 1px solid #ebccd1;
- }
- /* 下载链接样式 */
- .download-link {
- display: inline-block;
- margin-top: 15px;
- padding: 8px 15px;
- background-color: #337ab7;
- color: white;
- text-decoration: none;
- border-radius: 4px;
- }
- .download-link:hover {
- background-color: #286090;
- }
- /* 字段操作样式 */
- .field-actions {
- margin-top: 20px;
- text-align: center;
- }
- .add-field-btn {
- background-color: #5bc0de;
- color: white;
- padding: 8px 15px;
- border: none;
- border-radius: 4px;
- cursor: pointer;
- margin-right: 10px;
- }
- .add-field-btn:hover {
- background-color: #46b8da;
- }
- .field-row {
- display: flex;
- margin-bottom: 10px;
- align-items: flex-start;
- }
- .field-key {
- flex: 1;
- margin-right: 10px;
- }
- .field-value {
- flex: 2;
- margin-right: 10px;
- }
- .field-label {
- width: 100px;
- text-align: left;
- color: #666;
- font-size: 14px;
- }
- .remove-field-btn {
- background-color: #d9534f;
- color: white;
- border: none;
- border-radius: 4px;
- cursor: pointer;
- padding: 5px 10px;
- }
- .remove-field-btn:hover {
- background-color: #c9302c;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <h1>Word模板替换系统</h1>
- <div class="form">
- <h3>输入模板变量</h3>
- <p>系统将使用固定模板,根据下方输入的字段自动替换模板中的变量。</p>
- <p>
- 请填写以下字段,系统将自动替换模板中对应的变量(如{name}、{address}等)。
- </p>
- <div id="fieldsContainer">
- <!-- 动态字段将在这里添加 -->
- <div class="field-row">
- <input
- type="text"
- class="text-input field-key"
- placeholder="字段名"
- value="person1"
- readonly
- />
- <input
- type="text"
- class="text-input field-value"
- placeholder="甲方"
- value="杭州遁地科技"
- />
- <span class="field-label">甲方</span>
- <button
- class="remove-field-btn"
- onclick="removeField(this)"
- style="visibility: hidden"
- >
- 删除
- </button>
- </div>
- <div class="field-row">
- <input
- type="text"
- class="text-input field-key"
- placeholder="字段名"
- value="person2"
- readonly
- />
- <input
- type="text"
- class="text-input field-value"
- placeholder="乙方"
- value="杭州飞天科技"
- />
- <span class="field-label">乙方</span>
- <button
- class="remove-field-btn"
- onclick="removeField(this)"
- style="visibility: hidden"
- >
- 删除
- </button>
- </div>
- <div class="field-row">
- <input
- type="text"
- class="text-input field-key"
- placeholder="字段名"
- value="name"
- readonly
- />
- <input
- type="text"
- class="text-input field-value"
- placeholder="货物名称"
- />
- <span class="field-label">货物名称</span>
- <button
- class="remove-field-btn"
- onclick="removeField(this)"
- style="visibility: hidden"
- >
- 删除
- </button>
- </div>
- <div class="field-row">
- <input
- type="text"
- class="text-input field-key"
- placeholder="字段名"
- value="address"
- readonly
- />
- <input
- type="text"
- class="text-input field-value"
- placeholder="请输入原产地"
- />
- <span class="field-label">原产地</span>
- <button
- class="remove-field-btn"
- onclick="removeField(this)"
- style="visibility: hidden"
- >
- 删除
- </button>
- </div>
- <div class="field-row">
- <input
- type="text"
- class="text-input field-key"
- placeholder="字段名"
- value="address1"
- readonly
- />
- <input
- type="text"
- class="text-input field-value"
- placeholder="请输入目的地口岸"
- />
- <span class="field-label">目的地口岸</span>
- <button
- class="remove-field-btn"
- onclick="removeField(this)"
- style="visibility: hidden"
- >
- 删除
- </button>
- </div>
- <div class="field-row">
- <input
- type="text"
- class="text-input field-key"
- placeholder="字段名"
- value="addType"
- readonly
- />
- <input
- type="text"
- class="text-input field-value"
- placeholder="请输入交货方式"
- />
- <span class="field-label">交货方式</span>
- <button
- class="remove-field-btn"
- onclick="removeField(this)"
- style="visibility: hidden"
- >
- 删除
- </button>
- </div>
- <div class="field-row" style="height: 300px">
- <input
- type="text"
- class="text-input field-key"
- placeholder="字段名"
- value="productGuide"
- readonly
- />
- <textarea
- class="text-input field-value"
- placeholder="请输入产品规格"
- rows="15"
- >
- 我是一瓶番茄酱+沙拉酱
- 我是一瓶番茄酱+沙拉酱
- 我是一瓶番茄酱+沙拉酱
- 我是一瓶番茄酱+沙拉酱
- 我是一瓶番茄酱+沙拉酱
- 我是一瓶番茄酱+沙拉酱
- 我是一瓶番茄酱+沙拉酱
- 我是一瓶番茄酱+沙拉酱
- 我是一瓶番茄酱+沙拉酱
- 我是一瓶番茄酱+沙拉酱
- 我是一瓶番茄酱+沙拉酱
- 我是一瓶番茄酱+沙拉酱
- 我是一瓶番茄酱+沙拉酱
- 我是一瓶番茄酱+沙拉酱
- 我是一瓶番茄酱+沙拉酱
- </textarea
- >
- <span class="field-label">产品规格</span>
- <button
- class="remove-field-btn"
- onclick="removeField(this)"
- style="visibility: hidden"
- >
- 删除
- </button>
- </div>
- </div>
- <div class="field-actions">
- <!-- 移除添加字段按钮 -->
- </div>
- <button id="generateBtn" class="btn">生成文档</button>
- </div>
- <!-- 状态消息区域 -->
- <div id="statusMessage" class="status"></div>
- <!-- 下载区域 -->
- <div id="downloadSection" style="display: none; margin-top: 20px">
- <p>文档已成功生成,点击下面的链接下载:</p>
- <a id="downloadLink" href="#" class="download-link" download
- >下载文档</a
- >
- </div>
- </div>
- <script>
- // DOM元素引用
- const generateBtn = document.getElementById("generateBtn");
- const statusMessage = document.getElementById("statusMessage");
- const downloadSection = document.getElementById("downloadSection");
- const downloadLink = document.getElementById("downloadLink");
- const fieldsContainer = document.getElementById("fieldsContainer");
-
- // 获取当前页面的主机名和端口,用于API请求
- const apiBaseUrl = `${window.location.protocol}//${window.location.hostname}:5000`;
-
- // 收集所有字段
- function collectFields() {
- const data = {};
- const fieldRows = fieldsContainer.querySelectorAll(".field-row");
- fieldRows.forEach((row) => {
- const key = row.querySelector(".field-key").value.trim();
- const value = row.querySelector(".field-value").value.trim();
- if (key) {
- data[key] = value;
- }
- });
- return data;
- }
- // 生成文档
- generateBtn.addEventListener("click", function (e) {
- e.preventDefault();
- // 收集字段数据
- const data = collectFields();
- // 更新UI状态为处理中
- generateBtn.disabled = true;
- generateBtn.textContent = "处理中...";
- statusMessage.className = "status";
- statusMessage.style.display = "none";
- // 发送请求到Flask后端
- axios
- .post(`${apiBaseUrl}/process_file`, data, {
- headers: {
- "Content-Type": "application/json",
- },
- })
- .then((response) => {
- // 处理成功响应
- statusMessage.className = "status success";
- statusMessage.style.display = "block";
- statusMessage.textContent = `成功: ${response.data.message}`;
- console.log("成功:", response.data);
- // 更新下载链接
- downloadLink.href = `${apiBaseUrl}/download/${response.data.output_filename}`;
- // 显示下载部分
- downloadSection.style.display = "block";
- })
- .catch((error) => {
- // 处理错误响应
- statusMessage.className = "status error";
- statusMessage.style.display = "block";
- if (error.response) {
- statusMessage.textContent = `错误: ${
- error.response.data.error || "处理失败"
- }`;
- } else {
- statusMessage.textContent = "错误: 服务器连接失败";
- }
- console.error("处理错误:", error);
- })
- .finally(() => {
- // 恢复按钮状态
- generateBtn.disabled = false;
- generateBtn.textContent = "生成文档";
- });
- });
- </script>
- </body>
- </html>
|