temp
This commit is contained in:
167
internal/shared/esign/template_service.go
Normal file
167
internal/shared/esign/template_service.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package esign
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TemplateService 模板服务
|
||||
// 处理模板填写和文件生成相关操作
|
||||
type TemplateService struct {
|
||||
httpClient *HTTPClient
|
||||
config *Config
|
||||
}
|
||||
|
||||
// NewTemplateService 创建模板服务
|
||||
func NewTemplateService(httpClient *HTTPClient, config *Config) *TemplateService {
|
||||
return &TemplateService{
|
||||
httpClient: httpClient,
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateConfig 更新配置
|
||||
func (s *TemplateService) UpdateConfig(config *Config) {
|
||||
s.config = config
|
||||
}
|
||||
|
||||
// Fill 填写模板生成文件
|
||||
// 根据模板ID和填写内容生成包含填写内容的文档
|
||||
//
|
||||
// 参数说明:
|
||||
// - components: 需要填写的组件列表,包含字段键名和值
|
||||
//
|
||||
// 返回: 生成的文件ID和错误信息
|
||||
func (s *TemplateService) Fill(components []Component) (*FillTemplate, error) {
|
||||
fmt.Println("开始填写模板生成文件...")
|
||||
|
||||
// 生成带时间戳的文件名
|
||||
fileName := generateFileName("天远数据API合作协议", "pdf")
|
||||
|
||||
// 构建请求数据
|
||||
requestData := FillTemplateRequest{
|
||||
DocTemplateID: s.config.TemplateID,
|
||||
FileName: fileName,
|
||||
Components: components,
|
||||
}
|
||||
|
||||
// 序列化请求数据
|
||||
jsonData, err := MarshalRequest(requestData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 发送API请求
|
||||
responseBody, err := s.httpClient.Request("POST", "/v3/files/create-by-doc-template", jsonData)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("填写模板失败: %v", err)
|
||||
}
|
||||
|
||||
// 解析响应
|
||||
var response FillTemplateResponse
|
||||
if err := UnmarshalResponse(responseBody, &response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 检查响应状态
|
||||
if err := CheckResponseCode(response.Code, response.Message); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Printf("模板填写成功,文件ID: %s\n", response.Data.FileID)
|
||||
return &FillTemplate{
|
||||
FileID: response.Data.FileID,
|
||||
FileDownloadUrl: response.Data.FileDownloadUrl,
|
||||
FileName: fileName,
|
||||
TemplateID: s.config.TemplateID,
|
||||
FillTime: time.Now(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// FillWithDefaults 使用默认数据填写模板
|
||||
// 使用预设的默认数据填写模板,适用于测试或标准流程
|
||||
//
|
||||
// 参数说明:
|
||||
// - partyA: 甲方企业名称
|
||||
// - legalRepA: 甲方法人姓名
|
||||
// - partyB: 乙方企业名称
|
||||
// - legalRepB: 乙方法人姓名
|
||||
//
|
||||
// 返回: 生成的文件ID和错误信息
|
||||
func (s *TemplateService) FillWithDefaults(partyA, legalRepA, partyB, legalRepB string) (*FillTemplate, error) {
|
||||
// 构建默认填写组件
|
||||
components := []Component{
|
||||
{
|
||||
ComponentKey: "JFQY",
|
||||
ComponentValue: partyA,
|
||||
},
|
||||
{
|
||||
ComponentKey: "JFFR",
|
||||
ComponentValue: legalRepA,
|
||||
},
|
||||
{
|
||||
ComponentKey: "YFQY",
|
||||
ComponentValue: partyB,
|
||||
},
|
||||
{
|
||||
ComponentKey: "YFFR",
|
||||
ComponentValue: legalRepB,
|
||||
},
|
||||
{
|
||||
ComponentKey: "QDRQ",
|
||||
ComponentValue: formatDateForTemplate(),
|
||||
},
|
||||
}
|
||||
|
||||
return s.Fill(components)
|
||||
}
|
||||
|
||||
// FillWithCustomData 使用自定义数据填写模板
|
||||
// 允许传入自定义的组件数据来填写模板
|
||||
//
|
||||
// 参数说明:
|
||||
// - customComponents: 自定义组件数据
|
||||
//
|
||||
// 返回: 生成的文件ID和错误信息
|
||||
func (s *TemplateService) FillWithCustomData(customComponents map[string]string) (*FillTemplate, error) {
|
||||
var components []Component
|
||||
|
||||
// 将map转换为Component切片
|
||||
for key, value := range customComponents {
|
||||
components = append(components, Component{
|
||||
ComponentKey: key,
|
||||
ComponentValue: value,
|
||||
})
|
||||
}
|
||||
|
||||
return s.Fill(components)
|
||||
}
|
||||
|
||||
// CreateDefaultComponents 创建默认模板数据
|
||||
// 返回用于测试的默认模板填写数据
|
||||
//
|
||||
// 返回: 默认组件数据
|
||||
func CreateDefaultComponents() []Component {
|
||||
return []Component{
|
||||
{
|
||||
ComponentKey: "JFQY",
|
||||
ComponentValue: "海南省学宇思网络科技有限公司",
|
||||
},
|
||||
{
|
||||
ComponentKey: "JFFR",
|
||||
ComponentValue: "刘福思",
|
||||
},
|
||||
{
|
||||
ComponentKey: "YFQY",
|
||||
ComponentValue: "测试企业",
|
||||
},
|
||||
{
|
||||
ComponentKey: "YFFR",
|
||||
ComponentValue: "测试法人",
|
||||
},
|
||||
{
|
||||
ComponentKey: "QDRQ",
|
||||
ComponentValue: time.Now().Format("2006年01月02日"),
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user