Files
2026-07-22 12:16:27 +08:00

188 lines
6.2 KiB
Go
Raw Permalink 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 fadada
import (
"fmt"
"strings"
)
// AuthConfig 认证相关配置
type AuthConfig struct {
RedirectURL string `json:"redirectUrl" yaml:"redirect_url"`
}
// SignConfig 签署相关配置
type SignConfig struct {
RedirectURL string `json:"redirectUrl" yaml:"redirect_url"`
}
// ContractConfig 合同相关配置
type ContractConfig struct {
Name string `json:"name" yaml:"name"`
ExpireDays int `json:"expireDays" yaml:"expire_days"`
RetryCount int `json:"retryCount" yaml:"retry_count"`
}
// CallbackConfig 回调配置
type CallbackConfig struct {
Enabled bool `json:"enabled" yaml:"enabled"`
}
// TemplateFields 合作协议模板控件 fieldId
type TemplateFields struct {
// AgreementNo 协议编号控件编码列表(法大大控件不可复用,多处填同一值)
AgreementNo []string `json:"agreementNo" yaml:"agreement_no"`
ContractDate string `json:"contractDate" yaml:"contract_date"`
// PartyAName 甲方企业名控件编码列表(多处填同一企业名)
PartyAName []string `json:"partyAName" yaml:"party_a_name"`
PartyAUSCC string `json:"partyAUscc" yaml:"party_a_uscc"`
PartyAAddress string `json:"partyAAddress" yaml:"party_a_address"`
PartyARep string `json:"partyARep" yaml:"party_a_rep"`
PartyASignDate string `json:"partyASignDate" yaml:"party_a_sign_date"`
PartyBSignDate string `json:"partyBSignDate" yaml:"party_b_sign_date"`
}
// Config 法大大服务配置
type Config struct {
AppID string `json:"appId" yaml:"app_id"`
AppSecret string `json:"appSecret" yaml:"app_secret"`
ServerURL string `json:"serverUrl" yaml:"server_url"`
OpenCorpID string `json:"openCorpId" yaml:"open_corp_id"`
// NoAuthSceneCode 免验证签场景码businessIdrequestVerifyFree=true 时必传。
NoAuthSceneCode string `json:"noAuthSceneCode" yaml:"no_auth_scene_code"`
TemplateID string `json:"templateId" yaml:"template_id"`
// PartyAActorID / PartyBActorID 签署模板中的参与方标识actorId须与模板角色名一致
PartyAActorID string `json:"partyAActorId" yaml:"party_a_actor_id"`
PartyBActorID string `json:"partyBActorId" yaml:"party_b_actor_id"`
// TemplateDocID 签署模板文档 docId填单必填为空则创建任务后从详情自动解析
TemplateDocID string `json:"templateDocId" yaml:"template_doc_id"`
TemplateFields *TemplateFields `json:"templateFields" yaml:"template_fields"`
Contract *ContractConfig `json:"contract" yaml:"contract"`
Auth *AuthConfig `json:"auth" yaml:"auth"`
Sign *SignConfig `json:"sign" yaml:"sign"`
Callback *CallbackConfig `json:"callback" yaml:"callback"`
}
// NewConfig 创建并校验法大大配置
func NewConfig(
appID, appSecret, serverURL, openCorpID, noAuthSceneCode, templateID string,
partyAActorID, partyBActorID, templateDocID string,
fields *TemplateFields,
contract *ContractConfig,
auth *AuthConfig,
sign *SignConfig,
callback *CallbackConfig,
) (*Config, error) {
if appID == "" {
return nil, fmt.Errorf("法大大应用ID不能为空")
}
if appSecret == "" {
return nil, fmt.Errorf("法大大应用密钥不能为空")
}
if serverURL == "" {
return nil, fmt.Errorf("法大大服务器URL不能为空")
}
return &Config{
AppID: appID,
AppSecret: appSecret,
ServerURL: serverURL,
OpenCorpID: openCorpID,
NoAuthSceneCode: noAuthSceneCode,
TemplateID: templateID,
PartyAActorID: partyAActorID,
PartyBActorID: partyBActorID,
TemplateDocID: templateDocID,
TemplateFields: fields,
Contract: contract,
Auth: auth,
Sign: sign,
Callback: callback,
}, nil
}
// Validate 验证配置完整性
func (c *Config) Validate() error {
if c.AppID == "" {
return fmt.Errorf("法大大应用ID不能为空")
}
if c.AppSecret == "" {
return fmt.Errorf("法大大应用密钥不能为空")
}
if c.ServerURL == "" {
return fmt.Errorf("法大大服务器URL不能为空")
}
return nil
}
// ValidateTemplateFields 校验模板填单所需配置
func (c *Config) ValidateTemplateFields() error {
if c.TemplateID == "" {
return fmt.Errorf("法大大模板ID不能为空")
}
if c.OpenCorpID == "" {
return fmt.Errorf("法大大 open_corp_id 不能为空")
}
if c.TemplateFields == nil {
return fmt.Errorf("法大大 template_fields 未配置")
}
f := c.TemplateFields
if len(f.AgreementNo) == 0 {
return fmt.Errorf("法大大 template_fields.agreement_no 不能为空")
}
for i, id := range f.AgreementNo {
if strings.TrimSpace(id) == "" {
return fmt.Errorf("法大大 template_fields.agreement_no[%d] 不能为空", i)
}
}
if len(f.PartyAName) == 0 {
return fmt.Errorf("法大大 template_fields.party_a_name 不能为空")
}
for i, id := range f.PartyAName {
if strings.TrimSpace(id) == "" {
return fmt.Errorf("法大大 template_fields.party_a_name[%d] 不能为空", i)
}
}
// party_a/b_sign_date 为签署控件,签署时自动写入,填单可不配
required := map[string]string{
"contract_date": f.ContractDate,
"party_a_uscc": f.PartyAUSCC,
"party_a_address": f.PartyAAddress,
"party_a_rep": f.PartyARep,
}
for name, id := range required {
if id == "" {
return fmt.Errorf("法大大 template_fields.%s 不能为空", name)
}
}
return nil
}
// ResolvePartyAActorID 甲方参与方标识(优先配置,默认与签署模板角色名一致)
func (c *Config) ResolvePartyAActorID() string {
if c != nil && c.PartyAActorID != "" {
return c.PartyAActorID
}
return ActorIDPartyA
}
// ResolvePartyBActorID 乙方参与方标识(优先配置,默认与签署模板角色名一致)
func (c *Config) ResolvePartyBActorID() string {
if c != nil && c.PartyBActorID != "" {
return c.PartyBActorID
}
return ActorIDPartyB
}
const (
// 接口成功码
successCode = "100000"
// 个人证件类型:身份证(文档 userIdentType
UserIdentTypeIDCard = "id_card"
// 企业认证方式:法人认证(文档 corpIdentMethod
CorpIdentMethodLegalRep = "legalRep"
// 经办人认证方式:手机号(文档 oprIdentMethod
OprIdentMethodMobile = "mobile"
)