270 lines
		
	
	
		
			8.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
		
		
			
		
	
	
			270 lines
		
	
	
		
			8.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
|  | package esign | |||
|  | 
 | |||
|  | import ( | |||
|  | 	"fmt" | |||
|  | ) | |||
|  | 
 | |||
|  | // Client e签宝客户端 | |||
|  | // 提供统一的e签宝服务接口,整合所有功能模块 | |||
|  | type Client struct { | |||
|  | 	config     *Config          // 配置信息 | |||
|  | 	httpClient *HTTPClient      // HTTP客户端 | |||
|  | 	template   *TemplateService // 模板服务 | |||
|  | 	signFlow   *SignFlowService // 签署流程服务 | |||
|  | 	orgAuth    *OrgAuthService  // 机构认证服务 | |||
|  | 	fileOps    *FileOpsService  // 文件操作服务 | |||
|  | } | |||
|  | 
 | |||
|  | // NewClient 创建e签宝客户端 | |||
|  | // 使用配置信息初始化客户端及所有服务模块 | |||
|  | // | |||
|  | // 参数: | |||
|  | //   - config: e签宝配置信息 | |||
|  | // | |||
|  | // 返回: 客户端实例 | |||
|  | func NewClient(config *Config) *Client { | |||
|  | 	httpClient := NewHTTPClient(config) | |||
|  | 
 | |||
|  | 	client := &Client{ | |||
|  | 		config:     config, | |||
|  | 		httpClient: httpClient, | |||
|  | 	} | |||
|  | 
 | |||
|  | 	// 初始化各个服务模块 | |||
|  | 	client.template = NewTemplateService(httpClient, config) | |||
|  | 	client.signFlow = NewSignFlowService(httpClient, config) | |||
|  | 	client.orgAuth = NewOrgAuthService(httpClient, config) | |||
|  | 	client.fileOps = NewFileOpsService(httpClient, config) | |||
|  | 
 | |||
|  | 	return client | |||
|  | } | |||
|  | 
 | |||
|  | // GetConfig 获取当前配置 | |||
|  | func (c *Client) GetConfig() *Config { | |||
|  | 	return c.config | |||
|  | } | |||
|  | 
 | |||
|  | // UpdateConfig 更新配置 | |||
|  | func (c *Client) UpdateConfig(config *Config) { | |||
|  | 	c.config = config | |||
|  | 	c.httpClient.UpdateConfig(config) | |||
|  | 
 | |||
|  | 	// 更新各服务模块的配置 | |||
|  | 	c.template.UpdateConfig(config) | |||
|  | 	c.signFlow.UpdateConfig(config) | |||
|  | 	c.orgAuth.UpdateConfig(config) | |||
|  | 	c.fileOps.UpdateConfig(config) | |||
|  | } | |||
|  | 
 | |||
|  | // ==================== 模板操作 ==================== | |||
|  | 
 | |||
|  | // FillTemplate 填写模板 | |||
|  | // 使用自定义数据填写模板生成文件 | |||
|  | func (c *Client) FillTemplate(components map[string]string) (*FillTemplate, error) { | |||
|  | 	return c.template.FillWithCustomData(components) | |||
|  | } | |||
|  | 
 | |||
|  | // FillTemplateWithDefaults 使用默认数据填写模板 | |||
|  | func (c *Client) FillTemplateWithDefaults(partyA, legalRepA, partyB, legalRepB string) (*FillTemplate, error) { | |||
|  | 	return c.template.FillWithDefaults(partyA, legalRepA, partyB, legalRepB) | |||
|  | } | |||
|  | 
 | |||
|  | // ==================== 签署流程 ==================== | |||
|  | 
 | |||
|  | // CreateSignFlow 创建签署流程 | |||
|  | func (c *Client) CreateSignFlow(req *CreateSignFlowRequest) (string, error) { | |||
|  | 	return c.signFlow.Create(req) | |||
|  | } | |||
|  | 
 | |||
|  | // GetSignURL 获取签署链接 | |||
|  | func (c *Client) GetSignURL(signFlowID, psnAccount, orgName string) (string, string, error) { | |||
|  | 	return c.signFlow.GetSignURL(signFlowID, psnAccount, orgName) | |||
|  | } | |||
|  | 
 | |||
|  | // QuerySignFlowDetail 查询签署流程详情 | |||
|  | func (c *Client) QuerySignFlowDetail(signFlowID string) (*QuerySignFlowDetailResponse, error) { | |||
|  | 	return c.fileOps.QuerySignFlowDetail(signFlowID) | |||
|  | } | |||
|  | 
 | |||
|  | // IsSignFlowCompleted 检查签署流程是否完成 | |||
|  | func (c *Client) IsSignFlowCompleted(signFlowID string) (bool, error) { | |||
|  | 	result, err := c.QuerySignFlowDetail(signFlowID) | |||
|  | 	if err != nil { | |||
|  | 		return false, err | |||
|  | 	} | |||
|  | 	// 状态码2表示已完成 | |||
|  | 	return result.Data.SignFlowStatus == 2, nil | |||
|  | } | |||
|  | 
 | |||
|  | // ==================== 机构认证 ==================== | |||
|  | 
 | |||
|  | // GetOrgAuthURL 获取机构认证链接 | |||
|  | func (c *Client) GetOrgAuthURL(req *OrgAuthRequest) (string, string, string, error) { | |||
|  | 	return c.orgAuth.GetAuthURL(req) | |||
|  | } | |||
|  | 
 | |||
|  | // ValidateOrgAuthInfo 验证机构认证信息 | |||
|  | func (c *Client) ValidateOrgAuthInfo(req *OrgAuthRequest) error { | |||
|  | 	return c.orgAuth.ValidateAuthInfo(req) | |||
|  | } | |||
|  | 
 | |||
|  | // ==================== 文件操作 ==================== | |||
|  | 
 | |||
|  | // DownloadSignedFile 下载已签署文件 | |||
|  | func (c *Client) DownloadSignedFile(signFlowID string) (*DownloadSignedFileResponse, error) { | |||
|  | 	return c.fileOps.DownloadSignedFile(signFlowID) | |||
|  | } | |||
|  | 
 | |||
|  | // GetSignFlowStatus 获取签署流程状态 | |||
|  | func (c *Client) GetSignFlowStatus(signFlowID string) (string, error) { | |||
|  | 	detail, err := c.QuerySignFlowDetail(signFlowID) | |||
|  | 	if err != nil { | |||
|  | 		return "", err | |||
|  | 	} | |||
|  | 	return GetSignFlowStatusText(detail.Data.SignFlowStatus), nil | |||
|  | } | |||
|  | 
 | |||
|  | // ==================== 业务集成接口 ==================== | |||
|  | 
 | |||
|  | // ContractSigningRequest 合同签署请求 | |||
|  | type ContractSigningRequest struct { | |||
|  | 	// 企业信息 | |||
|  | 	CompanyName       string `json:"companyName"`       // 企业名称 | |||
|  | 	UnifiedSocialCode string `json:"unifiedSocialCode"` // 统一社会信用代码 | |||
|  | 	LegalPersonName   string `json:"legalPersonName"`   // 法人姓名 | |||
|  | 	LegalPersonID     string `json:"legalPersonId"`     // 法人身份证号 | |||
|  | 	LegalPersonPhone  string `json:"legalPersonPhone"`  // 法人手机号 | |||
|  | 
 | |||
|  | 	// 经办人信息(可选,如果与法人不同) | |||
|  | 	TransactorName  string `json:"transactorName,omitempty"`  // 经办人姓名 | |||
|  | 	TransactorPhone string `json:"transactorPhone,omitempty"` // 经办人手机号 | |||
|  | 	TransactorID    string `json:"transactorId,omitempty"`    // 经办人身份证号 | |||
|  | 
 | |||
|  | 	// 模板数据(可选) | |||
|  | 	CustomData map[string]string `json:"customData,omitempty"` // 自定义模板数据 | |||
|  | } | |||
|  | 
 | |||
|  | // ContractSigningResult 合同签署结果 | |||
|  | type ContractSigningResult struct { | |||
|  | 	FileID     string `json:"fileId"`     // 文件ID | |||
|  | 	SignFlowID string `json:"signFlowId"` // 签署流程ID | |||
|  | 	SignURL    string `json:"signUrl"`    // 签署链接 | |||
|  | 	ShortURL   string `json:"shortUrl"`   // 短链接 | |||
|  | } | |||
|  | 
 | |||
|  | // GenerateContractSigning 生成合同签署 | |||
|  | // 一站式合同签署服务:填写模板 -> 创建签署流程 -> 获取签署链接 | |||
|  | func (c *Client) GenerateContractSigning(req *ContractSigningRequest) (*ContractSigningResult, error) { | |||
|  | 	// 1. 准备模板数据 | |||
|  | 	var err error | |||
|  | 	var fillTemplate *FillTemplate | |||
|  | 	if len(req.CustomData) > 0 { | |||
|  | 		// 使用自定义数据 | |||
|  | 		fillTemplate, err = c.FillTemplate(req.CustomData) | |||
|  | 	} else { | |||
|  | 		// 使用默认数据 | |||
|  | 		fillTemplate, err = c.FillTemplateWithDefaults( | |||
|  | 			"海南省学宇思网络科技有限公司", | |||
|  | 			"刘福思", | |||
|  | 			req.CompanyName, | |||
|  | 			req.LegalPersonName, | |||
|  | 		) | |||
|  | 	} | |||
|  | 	if err != nil { | |||
|  | 		return nil, fmt.Errorf("填写模板失败: %w", err) | |||
|  | 	} | |||
|  | 
 | |||
|  | 	// 2. 确定签署人信息 | |||
|  | 	signerName := req.LegalPersonName | |||
|  | 	transactorName := req.LegalPersonName | |||
|  | 	transactorPhone := req.LegalPersonPhone | |||
|  | 	transactorID := req.LegalPersonID | |||
|  | 
 | |||
|  | 	if req.TransactorName != "" { | |||
|  | 		signerName = req.TransactorName | |||
|  | 		transactorName = req.TransactorName | |||
|  | 		transactorPhone = req.TransactorPhone | |||
|  | 		transactorID = req.TransactorID | |||
|  | 	} | |||
|  | 
 | |||
|  | 	// 3. 创建签署流程 | |||
|  | 	signFlowReq := &CreateSignFlowRequest{ | |||
|  | 		FileID:              fillTemplate.FileID, | |||
|  | 		SignerAccount:       req.UnifiedSocialCode, | |||
|  | 		SignerName:          signerName, | |||
|  | 		TransactorPhone:     transactorPhone, | |||
|  | 		TransactorName:      transactorName, | |||
|  | 		TransactorIDCardNum: transactorID, | |||
|  | 	} | |||
|  | 
 | |||
|  | 	signFlowID, err := c.CreateSignFlow(signFlowReq) | |||
|  | 	if err != nil { | |||
|  | 		return nil, fmt.Errorf("创建签署流程失败: %w", err) | |||
|  | 	} | |||
|  | 
 | |||
|  | 	// 4. 获取签署链接 | |||
|  | 	signURL, shortURL, err := c.GetSignURL(signFlowID, transactorPhone, signerName) | |||
|  | 	if err != nil { | |||
|  | 		return nil, fmt.Errorf("获取签署链接失败: %w", err) | |||
|  | 	} | |||
|  | 
 | |||
|  | 	return &ContractSigningResult{ | |||
|  | 		FileID:     fillTemplate.FileID, | |||
|  | 		SignFlowID: signFlowID, | |||
|  | 		SignURL:    signURL, | |||
|  | 		ShortURL:   shortURL, | |||
|  | 	}, nil | |||
|  | } | |||
|  | 
 | |||
|  | // EnterpriseAuthRequest 企业认证请求 | |||
|  | type EnterpriseAuthRequest struct { | |||
|  | 	// 企业信息 | |||
|  | 	CompanyName       string `json:"companyName"`       // 企业名称 | |||
|  | 	UnifiedSocialCode string `json:"unifiedSocialCode"` // 统一社会信用代码 | |||
|  | 	LegalPersonName   string `json:"legalPersonName"`   // 法人姓名 | |||
|  | 	LegalPersonID     string `json:"legalPersonId"`     // 法人身份证号 | |||
|  | 
 | |||
|  | 	// 经办人信息 | |||
|  | 	TransactorName   string `json:"transactorName"`   // 经办人姓名 | |||
|  | 	TransactorMobile string `json:"transactorMobile"` // 经办人手机号 | |||
|  | 	TransactorID     string `json:"transactorId"`     // 经办人身份证号 | |||
|  | } | |||
|  | 
 | |||
|  | // EnterpriseAuthResult 企业认证结果 | |||
|  | type EnterpriseAuthResult struct { | |||
|  | 	AuthFlowID   string `json:"authFlowId"`   // 认证流程ID | |||
|  | 	AuthURL      string `json:"authUrl"`      // 认证链接 | |||
|  | 	AuthShortURL string `json:"authShortUrl"` // 短链接 | |||
|  | } | |||
|  | 
 | |||
|  | // GenerateEnterpriseAuth 生成企业认证 | |||
|  | // 一站式企业认证服务 | |||
|  | func (c *Client) GenerateEnterpriseAuth(req *EnterpriseAuthRequest) (*EnterpriseAuthResult, error) { | |||
|  | 	authReq := &OrgAuthRequest{ | |||
|  | 		OrgName:             req.CompanyName, | |||
|  | 		OrgIDCardNum:        req.UnifiedSocialCode, | |||
|  | 		LegalRepName:        req.LegalPersonName, | |||
|  | 		LegalRepIDCardNum:   req.LegalPersonID, | |||
|  | 		TransactorName:      req.TransactorName, | |||
|  | 		TransactorIDCardNum: req.TransactorID, | |||
|  | 		TransactorMobile:    req.TransactorMobile, | |||
|  | 	} | |||
|  | 
 | |||
|  | 	// 验证信息 | |||
|  | 	if err := c.ValidateOrgAuthInfo(authReq); err != nil { | |||
|  | 		return nil, fmt.Errorf("认证信息验证失败: %w", err) | |||
|  | 	} | |||
|  | 
 | |||
|  | 	// 获取认证链接 | |||
|  | 	authFlowID, authURL, shortURL, err := c.GetOrgAuthURL(authReq) | |||
|  | 	if err != nil { | |||
|  | 		return nil, fmt.Errorf("获取认证链接失败: %w", err) | |||
|  | 	} | |||
|  | 
 | |||
|  | 	return &EnterpriseAuthResult{ | |||
|  | 		AuthFlowID:   authFlowID, | |||
|  | 		AuthURL:      authURL, | |||
|  | 		AuthShortURL: shortURL, | |||
|  | 	}, nil | |||
|  | } |