Files
tyapi-server/internal/shared/esign/fileops_service.go
2025-07-28 01:46:39 +08:00

208 lines
5.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package esign
import (
"fmt"
)
// FileOpsService 文件操作服务
// 处理文件下载、流程查询等操作
type FileOpsService struct {
httpClient *HTTPClient
config *Config
}
// NewFileOpsService 创建文件操作服务
func NewFileOpsService(httpClient *HTTPClient, config *Config) *FileOpsService {
return &FileOpsService{
httpClient: httpClient,
config: config,
}
}
// UpdateConfig 更新配置
func (s *FileOpsService) UpdateConfig(config *Config) {
s.config = config
}
// DownloadSignedFile 下载已签署文件及附属材料
// 获取签署完成后的文件下载链接和证书下载链接
//
// 参数说明:
// - signFlowId: 签署流程ID
//
// 返回: 下载文件响应和错误信息
func (s *FileOpsService) DownloadSignedFile(signFlowId string) (*DownloadSignedFileResponse, error) {
fmt.Println("开始下载已签署文件及附属材料...")
// 按照最新e签宝文档接口路径应为 /v3/sign-flow/{signFlowId}/file-download-url
urlPath := fmt.Sprintf("/v3/sign-flow/%s/file-download-url", signFlowId)
responseBody, err := s.httpClient.Request("GET", urlPath, nil)
if err != nil {
return nil, fmt.Errorf("下载已签署文件失败: %v", err)
}
// 解析响应
var response DownloadSignedFileResponse
if err := UnmarshalResponse(responseBody, &response); err != nil {
return nil, err
}
if err := CheckResponseCode(response.Code, response.Message); err != nil {
return nil, err
}
fmt.Printf("已签署文件下载信息获取成功!\n")
fmt.Printf("文件数量: %d\n", len(response.Data.Files))
fmt.Printf("附属材料数量: %d\n", len(response.Data.Attachments))
if response.Data.CertificateDownloadUrl != "" {
fmt.Printf("证书下载链接: %s\n", response.Data.CertificateDownloadUrl)
}
return &response, nil
}
// QuerySignFlowDetail 查询签署流程详情
// 获取签署流程的详细状态和参与方信息
//
// 参数说明:
// - signFlowId: 签署流程ID
//
// 返回: 流程详情响应和错误信息
func (s *FileOpsService) QuerySignFlowDetail(signFlowId string) (*QuerySignFlowDetailResponse, error) {
fmt.Println("开始查询签署流程详情...")
// 发送API请求
urlPath := fmt.Sprintf("/v3/sign-flow/%s/detail", signFlowId)
responseBody, err := s.httpClient.Request("GET", urlPath, nil)
if err != nil {
return nil, fmt.Errorf("查询签署流程详情失败: %v", err)
}
// 解析响应
var response QuerySignFlowDetailResponse
if err := UnmarshalResponse(responseBody, &response); err != nil {
return nil, err
}
if err := CheckResponseCode(response.Code, response.Message); err != nil {
return nil, err
}
fmt.Printf("查询签署流程详情响应: %+v\n", response)
return &response, nil
}
// GetSignedFileDownloadUrls 获取已签署文件的下载链接
// 从下载响应中提取所有文件的下载链接
//
// 参数说明:
// - downloadResponse: 下载文件响应
//
// 返回: 文件下载链接映射
func GetSignedFileDownloadUrls(downloadResponse *DownloadSignedFileResponse) map[string]string {
urls := make(map[string]string)
// 添加已签署文件
for _, file := range downloadResponse.Data.Files {
urls[file.FileName] = file.DownloadUrl
}
// 添加附属材料
for _, attachment := range downloadResponse.Data.Attachments {
urls[attachment.FileName] = attachment.DownloadUrl
}
return urls
}
// GetSignFlowStatusText 获取签署流程状态文本
// 从流程详情中提取状态信息
//
// 参数说明:
// - status: 流程状态码
//
// 返回: 流程状态描述
func GetSignFlowStatusText(status int32) string {
switch status {
case 1:
return "草稿"
case 2:
return "签署中"
case 3:
return "已完成"
case 4:
return "已撤销"
case 5:
return "已过期"
case 6:
return "已拒绝"
default:
return fmt.Sprintf("未知状态(%d)", status)
}
}
// GetSignerStatus 获取签署人状态
// 从流程详情中提取指定签署人的状态
//
// 参数说明:
// - detailResponse: 流程详情响应
// - signerName: 签署人姓名
//
// 返回: 签署人状态描述
func GetSignerStatus(detailResponse *QuerySignFlowDetailResponse, signerName string) string {
for _, signer := range detailResponse.Data.Signers {
var name string
if signer.PsnSigner != nil {
name = signer.PsnSigner.PsnName
} else if signer.OrgSigner != nil {
name = signer.OrgSigner.OrgName
}
if name == signerName {
switch signer.SignStatus {
case 1:
return "待签署"
case 2:
return "已签署"
case 3:
return "已拒绝"
case 4:
return "已过期"
default:
return fmt.Sprintf("未知状态(%d)", signer.SignStatus)
}
}
}
return "未找到签署人"
}
// IsSignFlowCompleted 检查签署流程是否完成
// 根据状态码判断签署流程是否已完成
//
// 参数说明:
// - detailResponse: 流程详情响应
//
// 返回: 是否完成
func IsSignFlowCompleted(detailResponse *QuerySignFlowDetailResponse) bool {
// 状态码2表示已完成
return detailResponse.Data.SignFlowStatus == 2
}
// GetFileList 获取文件列表
// 从下载响应中获取所有文件信息
//
// 参数说明:
// - downloadResponse: 下载文件响应
//
// 返回: 文件信息列表
func GetFileList(downloadResponse *DownloadSignedFileResponse) []SignedFileInfo {
var files []SignedFileInfo
// 添加已签署文件
files = append(files, downloadResponse.Data.Files...)
// 添加附属材料
files = append(files, downloadResponse.Data.Attachments...)
return files
}