257 lines
7.2 KiB
Go
257 lines
7.2 KiB
Go
package esign
|
||
|
||
import (
|
||
"fmt"
|
||
"log"
|
||
)
|
||
|
||
// Example 展示如何使用重构后的e签宝SDK
|
||
func Example() {
|
||
// 1. 创建配置
|
||
config, err := NewConfig(
|
||
"your_app_id",
|
||
"your_app_secret",
|
||
"https://smlopenapi.esign.cn",
|
||
"your_template_id",
|
||
&EsignContractConfig{
|
||
Name: "测试合同",
|
||
ExpireDays: 30,
|
||
RetryCount: 3,
|
||
},
|
||
&EsignAuthConfig{
|
||
OrgAuthModes: []string{"ORG"},
|
||
DefaultAuthMode: "ORG",
|
||
PsnAuthModes: []string{"PSN"},
|
||
WillingnessAuthModes: []string{"WILLINGNESS"},
|
||
RedirectUrl: "https://console.tianyuanapi.com/certification/callback",
|
||
},
|
||
&EsignSignConfig{
|
||
AutoFinish: true,
|
||
SignFieldStyle: 1,
|
||
ClientType: "ALL",
|
||
RedirectUrl: "https://console.tianyuanapi.com/certification/callback",
|
||
},
|
||
)
|
||
if err != nil {
|
||
log.Fatal("配置创建失败:", err)
|
||
}
|
||
|
||
// 2. 创建客户端
|
||
client := NewClient(config)
|
||
|
||
// 示例1: 简单合同签署流程
|
||
fmt.Println("=== 示例1: 简单合同签署流程 ===")
|
||
contractReq := &ContractSigningRequest{
|
||
CompanyName: "测试公司",
|
||
UnifiedSocialCode: "123456789012345678",
|
||
LegalPersonName: "张三",
|
||
LegalPersonID: "123456789012345678",
|
||
LegalPersonPhone: "13800138000",
|
||
}
|
||
|
||
result, err := client.GenerateContractSigning(contractReq)
|
||
if err != nil {
|
||
log.Printf("合同签署失败: %v", err)
|
||
} else {
|
||
fmt.Printf("合同签署成功: %+v\n", result)
|
||
}
|
||
|
||
// 示例2: 企业认证流程
|
||
fmt.Println("\n=== 示例2: 企业认证流程 ===")
|
||
authReq := &EnterpriseAuthRequest{
|
||
CompanyName: "测试公司",
|
||
UnifiedSocialCode: "123456789012345678",
|
||
LegalPersonName: "张三",
|
||
LegalPersonID: "123456789012345678",
|
||
TransactorName: "李四",
|
||
TransactorMobile: "13800138001",
|
||
TransactorID: "123456789012345679",
|
||
}
|
||
|
||
authResult, err := client.GenerateEnterpriseAuth(authReq)
|
||
if err != nil {
|
||
log.Printf("企业认证失败: %v", err)
|
||
} else {
|
||
fmt.Printf("企业认证成功: %+v\n", authResult)
|
||
}
|
||
|
||
// 示例3: 分步操作
|
||
fmt.Println("\n=== 示例3: 分步操作 ===")
|
||
|
||
// 3.1 填写模板
|
||
templateData := map[string]string{
|
||
"JFQY": "甲方公司",
|
||
"JFFR": "甲方法人",
|
||
"YFQY": "乙方公司",
|
||
"YFFR": "乙方法人",
|
||
"QDRQ": "2024年01月01日",
|
||
}
|
||
|
||
fileID, err := client.FillTemplate(templateData)
|
||
if err != nil {
|
||
log.Printf("模板填写失败: %v", err)
|
||
return
|
||
}
|
||
fmt.Printf("模板填写成功,文件ID: %s\n", fileID)
|
||
|
||
// 3.2 创建签署流程
|
||
signFlowReq := &CreateSignFlowRequest{
|
||
FileID: fileID.FileID,
|
||
SignerAccount: "123456789012345678",
|
||
SignerName: "乙方公司",
|
||
TransactorPhone: "13800138000",
|
||
TransactorName: "乙方法人",
|
||
TransactorIDCardNum: "123456789012345678",
|
||
}
|
||
|
||
signFlowID, err := client.CreateSignFlow(signFlowReq)
|
||
if err != nil {
|
||
log.Printf("创建签署流程失败: %v", err)
|
||
return
|
||
}
|
||
fmt.Printf("签署流程创建成功,流程ID: %s\n", signFlowID)
|
||
|
||
// 3.3 获取签署链接
|
||
signURL, shortURL, err := client.GetSignURL(signFlowID, "13800138000", "乙方公司")
|
||
if err != nil {
|
||
log.Printf("获取签署链接失败: %v", err)
|
||
return
|
||
}
|
||
fmt.Printf("签署链接: %s\n", signURL)
|
||
fmt.Printf("短链接: %s\n", shortURL)
|
||
|
||
// 3.4 查询签署状态
|
||
status, err := client.GetSignFlowStatus(signFlowID)
|
||
if err != nil {
|
||
log.Printf("查询签署状态失败: %v", err)
|
||
return
|
||
}
|
||
fmt.Printf("签署状态: %s\n", status)
|
||
|
||
// 3.5 检查是否完成
|
||
completed, err := client.IsSignFlowCompleted(signFlowID)
|
||
if err != nil {
|
||
log.Printf("检查签署状态失败: %v", err)
|
||
return
|
||
}
|
||
fmt.Printf("签署是否完成: %t\n", completed)
|
||
}
|
||
|
||
// ExampleBasicUsage 基础用法示例
|
||
func ExampleBasicUsage() {
|
||
// 最简单的用法 - 一行代码完成合同签署
|
||
config, _ := NewConfig("app_id", "app_secret", "server_url", "template_id", &EsignContractConfig{
|
||
Name: "测试合同",
|
||
ExpireDays: 30,
|
||
RetryCount: 3,
|
||
}, &EsignAuthConfig{
|
||
OrgAuthModes: []string{"ORG"},
|
||
DefaultAuthMode: "ORG",
|
||
PsnAuthModes: []string{"PSN"},
|
||
WillingnessAuthModes: []string{"WILLINGNESS"},
|
||
RedirectUrl: "https://console.tianyuanapi.com/certification/callback",
|
||
}, &EsignSignConfig{
|
||
AutoFinish: true,
|
||
SignFieldStyle: 1,
|
||
ClientType: "ALL",
|
||
RedirectUrl: "https://console.tianyuanapi.com/certification/callback",
|
||
})
|
||
client := NewClient(config)
|
||
|
||
// 快速合同签署
|
||
result, err := client.GenerateContractSigning(&ContractSigningRequest{
|
||
CompanyName: "我的公司",
|
||
UnifiedSocialCode: "123456789012345678",
|
||
LegalPersonName: "张三",
|
||
LegalPersonID: "123456789012345678",
|
||
LegalPersonPhone: "13800138000",
|
||
})
|
||
|
||
if err != nil {
|
||
log.Fatal("签署失败:", err)
|
||
}
|
||
|
||
fmt.Printf("请访问以下链接进行签署: %s\n", result.SignURL)
|
||
}
|
||
|
||
// ExampleWithCustomData 自定义数据示例
|
||
func ExampleWithCustomData() {
|
||
config, _ := NewConfig("app_id", "app_secret", "server_url", "template_id", &EsignContractConfig{
|
||
Name: "测试合同",
|
||
ExpireDays: 30,
|
||
RetryCount: 3,
|
||
}, &EsignAuthConfig{
|
||
OrgAuthModes: []string{"ORG"},
|
||
DefaultAuthMode: "ORG",
|
||
PsnAuthModes: []string{"PSN"},
|
||
WillingnessAuthModes: []string{"WILLINGNESS"},
|
||
RedirectUrl: "https://console.tianyuanapi.com/certification/callback",
|
||
}, &EsignSignConfig{
|
||
AutoFinish: true,
|
||
SignFieldStyle: 1,
|
||
ClientType: "ALL",
|
||
RedirectUrl: "https://console.tianyuanapi.com/certification/callback",
|
||
})
|
||
client := NewClient(config)
|
||
|
||
// 使用自定义模板数据
|
||
customData := map[string]string{
|
||
"custom_field_1": "自定义值1",
|
||
"custom_field_2": "自定义值2",
|
||
"contract_date": "2024年01月01日",
|
||
}
|
||
|
||
result, err := client.GenerateContractSigning(&ContractSigningRequest{
|
||
CompanyName: "我的公司",
|
||
UnifiedSocialCode: "123456789012345678",
|
||
LegalPersonName: "张三",
|
||
LegalPersonID: "123456789012345678",
|
||
LegalPersonPhone: "13800138000",
|
||
CustomData: customData,
|
||
})
|
||
|
||
if err != nil {
|
||
log.Fatal("签署失败:", err)
|
||
}
|
||
|
||
fmt.Printf("自定义合同签署链接: %s\n", result.SignURL)
|
||
}
|
||
|
||
// ExampleEnterpriseAuth 企业认证示例
|
||
func ExampleEnterpriseAuth() {
|
||
config, _ := NewConfig("app_id", "app_secret", "server_url", "template_id", &EsignContractConfig{
|
||
Name: "测试合同",
|
||
ExpireDays: 30,
|
||
RetryCount: 3,
|
||
}, &EsignAuthConfig{
|
||
OrgAuthModes: []string{"ORG"},
|
||
DefaultAuthMode: "ORG",
|
||
PsnAuthModes: []string{"PSN"},
|
||
WillingnessAuthModes: []string{"WILLINGNESS"},
|
||
RedirectUrl: "https://console.tianyuanapi.com/certification/callback",
|
||
}, &EsignSignConfig{
|
||
AutoFinish: true,
|
||
SignFieldStyle: 1,
|
||
ClientType: "ALL",
|
||
RedirectUrl: "https://console.tianyuanapi.com/certification/callback",
|
||
})
|
||
client := NewClient(config)
|
||
|
||
// 企业认证
|
||
authResult, err := client.GenerateEnterpriseAuth(&EnterpriseAuthRequest{
|
||
CompanyName: "我的公司",
|
||
UnifiedSocialCode: "123456789012345678",
|
||
LegalPersonName: "张三",
|
||
LegalPersonID: "123456789012345678",
|
||
TransactorName: "李四",
|
||
TransactorMobile: "13800138001",
|
||
TransactorID: "123456789012345679",
|
||
})
|
||
|
||
if err != nil {
|
||
log.Fatal("企业认证失败:", err)
|
||
}
|
||
|
||
fmt.Printf("请访问以下链接进行企业认证: %s\n", authResult.AuthURL)
|
||
}
|