206 lines
6.2 KiB
Go
206 lines
6.2 KiB
Go
package esign
|
||
|
||
import (
|
||
"fmt"
|
||
)
|
||
|
||
// OrgAuthService 机构认证服务
|
||
// 处理机构认证和授权相关操作
|
||
type OrgAuthService struct {
|
||
httpClient *HTTPClient
|
||
config *Config
|
||
}
|
||
|
||
// NewOrgAuthService 创建机构认证服务
|
||
func NewOrgAuthService(httpClient *HTTPClient, config *Config) *OrgAuthService {
|
||
return &OrgAuthService{
|
||
httpClient: httpClient,
|
||
config: config,
|
||
}
|
||
}
|
||
|
||
// UpdateConfig 更新配置
|
||
func (s *OrgAuthService) UpdateConfig(config *Config) {
|
||
s.config = config
|
||
}
|
||
|
||
// OrgAuthRequest 机构认证请求
|
||
type OrgAuthRequest struct {
|
||
OrgName string `json:"orgName"` // 机构名称
|
||
OrgIDCardNum string `json:"orgIdCardNum"` // 机构证件号
|
||
LegalRepName string `json:"legalRepName"` // 法定代表人姓名
|
||
LegalRepIDCardNum string `json:"legalRepIdCardNum"` // 法定代表人身份证号
|
||
TransactorName string `json:"transactorName"` // 经办人姓名
|
||
TransactorIDCardNum string `json:"transactorIdCardNum"` // 经办人身份证号
|
||
TransactorMobile string `json:"transactorMobile"` // 经办人手机号
|
||
}
|
||
|
||
// GetAuthURL 获取机构认证&授权页面链接
|
||
// 为机构用户获取认证和授权页面链接,用于机构身份认证
|
||
func (s *OrgAuthService) GetAuthURL(req *OrgAuthRequest) (string, string, string, error) {
|
||
// 构建请求数据
|
||
requestData := GetOrgAuthUrlRequest{
|
||
OrgAuthConfig: &OrgAuthConfig{
|
||
OrgName: req.OrgName,
|
||
OrgInfo: &OrgAuthInfo{
|
||
OrgIDCardNum: req.OrgIDCardNum,
|
||
OrgIDCardType: OrgCardTypeUSCC,
|
||
LegalRepName: req.LegalRepName,
|
||
LegalRepIDCardNum: req.LegalRepIDCardNum,
|
||
LegalRepIDCardType: IDCardTypeChina,
|
||
},
|
||
TransactorAuthPageConfig: &TransactorAuthPageConfig{
|
||
PsnAvailableAuthModes: []string{AuthModeMobile3},
|
||
PsnDefaultAuthMode: AuthModeMobile3,
|
||
PsnEditableFields: []string{},
|
||
},
|
||
TransactorInfo: &TransactorAuthInfo{
|
||
PsnAccount: req.TransactorMobile,
|
||
PsnInfo: &PsnAuthInfo{
|
||
PsnName: req.TransactorName,
|
||
PsnIDCardNum: req.TransactorIDCardNum,
|
||
PsnIDCardType: IDCardTypeChina,
|
||
PsnMobile: req.TransactorMobile,
|
||
PsnIdentityVerify: true,
|
||
},
|
||
},
|
||
},
|
||
ClientType: ClientTypeAll,
|
||
}
|
||
|
||
// 序列化请求数据
|
||
jsonData, err := MarshalRequest(requestData)
|
||
if err != nil {
|
||
return "", "", "", err
|
||
}
|
||
|
||
fmt.Printf("获取机构认证&授权页面链接请求数据: %s\n", string(jsonData))
|
||
|
||
// 发送API请求
|
||
responseBody, err := s.httpClient.Request("POST", "/v3/org-auth-url", jsonData)
|
||
if err != nil {
|
||
return "", "", "", fmt.Errorf("获取机构认证&授权页面链接失败: %v", err)
|
||
}
|
||
|
||
// 解析响应
|
||
var response GetOrgAuthUrlResponse
|
||
if err := UnmarshalResponse(responseBody, &response); err != nil {
|
||
return "", "", "", err
|
||
}
|
||
|
||
if err := CheckResponseCode(response.Code, response.Message); err != nil {
|
||
return "", "", "", err
|
||
}
|
||
|
||
fmt.Printf("机构认证&授权页面链接获取成功!\n")
|
||
fmt.Printf("认证流程ID: %s\n", response.Data.AuthFlowId)
|
||
fmt.Printf("完整链接: %s\n", response.Data.AuthUrl)
|
||
fmt.Printf("短链接: %s\n", response.Data.AuthShortUrl)
|
||
|
||
return response.Data.AuthFlowId, response.Data.AuthUrl, response.Data.AuthShortUrl, nil
|
||
}
|
||
|
||
// CreateAuthConfig 创建机构认证配置
|
||
// 构建机构认证所需的配置信息
|
||
func (s *OrgAuthService) CreateAuthConfig(req *OrgAuthRequest) *OrgAuthConfig {
|
||
return &OrgAuthConfig{
|
||
OrgName: req.OrgName,
|
||
OrgInfo: &OrgAuthInfo{
|
||
OrgIDCardNum: req.OrgIDCardNum,
|
||
OrgIDCardType: OrgCardTypeUSCC,
|
||
LegalRepName: req.LegalRepName,
|
||
LegalRepIDCardNum: req.LegalRepIDCardNum,
|
||
LegalRepIDCardType: IDCardTypeChina,
|
||
},
|
||
TransactorAuthPageConfig: &TransactorAuthPageConfig{
|
||
PsnAvailableAuthModes: []string{AuthModeMobile3},
|
||
PsnDefaultAuthMode: AuthModeMobile3,
|
||
PsnEditableFields: []string{},
|
||
},
|
||
TransactorInfo: &TransactorAuthInfo{
|
||
PsnAccount: req.TransactorMobile,
|
||
PsnInfo: &PsnAuthInfo{
|
||
PsnName: req.TransactorName,
|
||
PsnIDCardNum: req.TransactorIDCardNum,
|
||
PsnIDCardType: IDCardTypeChina,
|
||
PsnMobile: req.TransactorMobile,
|
||
PsnIdentityVerify: true,
|
||
},
|
||
},
|
||
}
|
||
}
|
||
|
||
// ValidateAuthInfo 验证机构认证信息
|
||
// 检查机构认证信息的完整性和格式
|
||
func (s *OrgAuthService) ValidateAuthInfo(req *OrgAuthRequest) error {
|
||
if req.OrgName == "" {
|
||
return fmt.Errorf("机构名称不能为空")
|
||
}
|
||
if req.OrgIDCardNum == "" {
|
||
return fmt.Errorf("机构证件号不能为空")
|
||
}
|
||
if req.LegalRepName == "" {
|
||
return fmt.Errorf("法定代表人姓名不能为空")
|
||
}
|
||
if req.LegalRepIDCardNum == "" {
|
||
return fmt.Errorf("法定代表人身份证号不能为空")
|
||
}
|
||
if req.TransactorName == "" {
|
||
return fmt.Errorf("经办人姓名不能为空")
|
||
}
|
||
if req.TransactorIDCardNum == "" {
|
||
return fmt.Errorf("经办人身份证号不能为空")
|
||
}
|
||
if req.TransactorMobile == "" {
|
||
return fmt.Errorf("经办人手机号不能为空")
|
||
}
|
||
|
||
// 验证统一社会信用代码格式(18位)
|
||
if len(req.OrgIDCardNum) != 18 {
|
||
return fmt.Errorf("机构证件号(统一社会信用代码)必须是18位")
|
||
}
|
||
|
||
// 验证身份证号格式(18位)
|
||
if len(req.LegalRepIDCardNum) != 18 {
|
||
return fmt.Errorf("法定代表人身份证号必须是18位")
|
||
}
|
||
if len(req.TransactorIDCardNum) != 18 {
|
||
return fmt.Errorf("经办人身份证号必须是18位")
|
||
}
|
||
|
||
// 验证手机号格式(11位)
|
||
if len(req.TransactorMobile) != 11 {
|
||
return fmt.Errorf("经办人手机号必须是11位")
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// QueryOrgIdentity 查询机构认证信息
|
||
// 查询机构的实名认证状态和信息
|
||
func (s *OrgAuthService) QueryOrgIdentity(req *QueryOrgIdentityRequest) (*QueryOrgIdentityResponse, error) {
|
||
// 序列化请求数据
|
||
jsonData, err := MarshalRequest(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 发送API请求
|
||
responseBody, err := s.httpClient.Request("POST", "/v3/organizations/identity", jsonData)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("查询机构认证信息失败: %v", err)
|
||
}
|
||
|
||
// 解析响应
|
||
var response QueryOrgIdentityResponse
|
||
if err := UnmarshalResponse(responseBody, &response); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
if err := CheckResponseCode(int(response.Code), response.Message); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &response, nil
|
||
}
|