This commit is contained in:
root
2025-07-12 20:46:30 +08:00
parent fe9597eb36
commit 7ee8bcab54
6 changed files with 733 additions and 440 deletions

View File

@@ -0,0 +1,201 @@
# Template.rs 更新总结
## 🎯 更新目标
参考 `python_cli.rs` 的实现模式,统一使用 `execute_python_cli_command` 来处理模板相关的命令。
## 🔄 主要变更
### 1. **统一响应结构**
新增 `TemplateResponse` 结构体,与 `python_cli.rs` 中的 `PythonCliResponse` 保持一致:
```rust
#[derive(Debug, Serialize, Deserialize)]
pub struct TemplateResponse {
pub success: bool,
pub message: String,
pub data: Option<serde_json::Value>,
pub output: Option<String>,
pub error: Option<String>,
}
```
### 2. **统一命令执行函数**
添加 `execute_python_cli_command` 函数,参考 `python_cli.rs` 的实现:
```rust
async fn execute_python_cli_command(
app: AppHandle,
command_args: Vec<String>,
) -> Result<TemplateResponse, String>
```
### 3. **新增请求结构体**
为支持更丰富的参数,新增多个请求结构体:
- `TemplateListRequest` - 模板列表查询参数
- `TemplateGetRequest` - 单个模板获取参数
- `TemplateDeleteRequest` - 模板删除参数
- `TemplateSearchRequest` - 模板搜索参数
- 更新 `BatchImportRequest` - 添加用户ID等参数
### 4. **新版本命令函数**
为每个原有命令添加新版本使用CLI模式
| 原函数 | 新函数 | 说明 |
|--------|--------|------|
| `batch_import_templates` | `batch_import_templates_cli` | 批量导入模板 |
| `get_templates` | `get_templates_cli` | 获取模板列表 |
| `get_template` | `get_template_cli` | 获取单个模板 |
| `delete_template` | `delete_template_cli` | 删除模板 |
### 5. **新增功能命令**
添加新的CLI命令函数
- `search_templates_cli` - 搜索模板
- `get_template_stats_cli` - 获取模板统计
- `get_popular_tags_cli` - 获取热门标签
- `get_templates_by_tag_cli` - 按标签获取模板
## 📋 命令对比
### **批量导入模板**
#### 原版本
```rust
#[tauri::command]
pub async fn batch_import_templates(
app: tauri::AppHandle,
request: BatchImportRequest
) -> Result<String, String>
```
#### 新版本
```rust
#[tauri::command]
pub async fn batch_import_templates_cli(
app: AppHandle,
request: BatchImportRequest,
) -> Result<TemplateResponse, String>
```
### **模板列表**
#### 原版本
```rust
#[tauri::command]
pub async fn get_templates(app: tauri::AppHandle) -> Result<String, String>
```
#### 新版本
```rust
#[tauri::command]
pub async fn get_templates_cli(
app: AppHandle,
request: TemplateListRequest,
) -> Result<TemplateResponse, String>
```
## 🚀 使用示例
### **前端调用新版本API**
```typescript
// 批量导入模板
const importResult = await invoke('batch_import_templates_cli', {
request: {
source_folder: '/path/to/templates',
user_id: 'user123',
verbose: true,
json_output: true
}
});
// 获取模板列表
const templatesResult = await invoke('get_templates_cli', {
request: {
user_id: 'user123',
include_cloud: true,
limit: 50,
verbose: false,
json_output: true
}
});
// 搜索模板
const searchResult = await invoke('search_templates_cli', {
request: {
query: '商业宣传',
user_id: 'user123',
include_cloud: true,
limit: 20,
json_output: true
}
});
```
### **响应格式**
```typescript
interface TemplateResponse {
success: boolean;
message: string;
data?: any;
output?: string;
error?: string;
}
```
## 🔧 CLI命令映射
新版本函数直接调用 Python CLI 命令:
| Rust函数 | Python CLI命令 |
|----------|----------------|
| `batch_import_templates_cli` | `python -m python_core.cli template batch-import` |
| `get_templates_cli` | `python -m python_core.cli template list` |
| `search_templates_cli` | `python -m python_core.cli template search` |
| `get_template_cli` | `python -m python_core.cli template get` |
| `delete_template_cli` | `python -m python_core.cli template delete` |
## 📈 优势
### **1. 统一性**
-`python_cli.rs` 保持一致的架构
- 统一的响应格式和错误处理
- 一致的参数传递方式
### **2. 功能丰富**
- 支持用户ID参数
- 支持详细的查询选项
- 新增搜索和统计功能
### **3. 向后兼容**
- 保留原有函数,确保现有代码不受影响
- 渐进式迁移到新版本
### **4. 可维护性**
- 统一的命令执行逻辑
- 清晰的参数结构
- 完善的类型定义
## 🔮 后续计划
1. **逐步迁移** - 前端代码逐步迁移到新版本API
2. **移除旧版本** - 确认迁移完成后移除旧版本函数
3. **功能扩展** - 基于CLI模式添加更多模板管理功能
4. **性能优化** - 优化命令执行和响应处理
## 📝 注意事项
1. **函数命名** - 新版本函数添加 `_cli` 后缀以区分
2. **参数结构** - 使用结构体传递参数,支持可选字段
3. **响应格式** - 统一使用 `TemplateResponse` 结构
4. **错误处理** - 保持与 `python_cli.rs` 一致的错误处理方式
Template.rs 已成功更新为统一的CLI模式提供了更丰富的功能和更好的一致性🎉