diff --git a/express_tool/ali_cloud_client.go b/ali_cloud_tool/ali_cloud_express_client.go similarity index 62% rename from express_tool/ali_cloud_client.go rename to ali_cloud_tool/ali_cloud_express_client.go index 7e0fafa..df7672c 100644 --- a/express_tool/ali_cloud_client.go +++ b/ali_cloud_tool/ali_cloud_express_client.go @@ -1,4 +1,4 @@ -package express_tool +package ali_cloud_tool import ( "context" @@ -9,7 +9,6 @@ import ( "log" "net/http" "net/url" - "strings" "time" ) @@ -141,77 +140,6 @@ func (a *AliCloudExpressClient) numberKey(prefix, number string) string { return fmt.Sprintf("%s:number:%s", prefix, number) } -// 调用实名认证API -func (s *AliCloudExpressClient) CallRealNameAuthAPI(ctx context.Context, name, idCard string) (*RealNameAuthData, error) { - // 验证输入 - if strings.TrimSpace(name) == "" || strings.TrimSpace(idCard) == "" { - return nil, errors.New("姓名和身份证号不能为空") - } - - // 设置参数 - params := url.Values{} - params.Add("realName", name) - params.Add("cardNo", idCard) - - // 构建完整URL - fullURL, err := url.JoinPath(s.Host) - if err != nil { - return nil, fmt.Errorf("构建实名认证URL失败: %w, 姓名:%s, 身份证号:%s", err, name, idCard) - } - - // 创建HTTP客户端 - client := &http.Client{ - Timeout: 2 * time.Second, - } - - // 创建POST请求 - req, err := http.NewRequestWithContext(ctx, http.MethodPost, fullURL, strings.NewReader(params.Encode())) - if err != nil { - return nil, fmt.Errorf("创建实名认证请求失败: %w, 姓名:%s, 身份证号:%s", err, name, idCard) - } - - // 设置请求头 - req.Header.Set("Authorization", "APPCODE "+s.AppCode) - req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") - - // 发送请求 - resp, err := client.Do(req) - if err != nil { - return nil, fmt.Errorf("发送实名认证请求失败: %w, 姓名:%s, 身份证号:%s", err, name, idCard) - } - defer func() { - if closeErr := resp.Body.Close(); closeErr != nil { - log.Printf("关闭实名认证响应体失败: %v, 姓名:%s, 身份证号:%s", closeErr, name, idCard) - } - }() - - // 读取响应体 - body, err := io.ReadAll(resp.Body) - if err != nil { - return nil, fmt.Errorf("读取实名认证响应失败: %w, 姓名:%s, 身份证号:%s", err, name, idCard) - } - - // 解析JSON响应 - var result RealNameAuthData - if err = json.Unmarshal(body, &result); err != nil { - return nil, fmt.Errorf("解析实名认证响应失败: %w, 响应内容:%s, 姓名:%s, 身份证号:%s", err, string(body), name, idCard) - } - - // 检查API返回的错误码 - if result.ErrorCode != 0 { - return &result, fmt.Errorf("实名认证接口返回错误, 错误码:%d, 原因:%s, 姓名:%s, 身份证号:%s", - result.ErrorCode, result.Reason, name, idCard) - } - - // 检查认证结果 - if !result.Result.Isok { - return &result, fmt.Errorf("实名认证不通过, 原因:%s, 姓名:%s, 身份证号:%s", - result.Reason, name, idCard) - } - - return &result, nil -} - type ExpressRes struct { Code int `json:"code"` Desc string `json:"desc"` @@ -231,22 +159,3 @@ type List struct { Time string `json:"time"` Status string `json:"status"` } - -type RealNameAuthData struct { - ErrorCode int `json:"error_code"` - Reason string `json:"reason"` - Result struct { - Realname string `json:"realname"` - Idcard string `json:"idcard"` - Isok bool `json:"isok"` - IdCardInfor struct { - Province string `json:"province"` - City string `json:"city"` - District string `json:"district"` - Area string `json:"area"` - Sex string `json:"sex"` - Birthday string `json:"birthday"` - } `json:"IdCardInfor"` - } `json:"result"` - Sn string `json:"sn"` -} diff --git a/express_tool/ali_cloud_client_test.go b/ali_cloud_tool/ali_cloud_express_client_test.go similarity index 89% rename from express_tool/ali_cloud_client_test.go rename to ali_cloud_tool/ali_cloud_express_client_test.go index 01bf2f7..040e822 100644 --- a/express_tool/ali_cloud_client_test.go +++ b/ali_cloud_tool/ali_cloud_express_client_test.go @@ -1,4 +1,4 @@ -package express_tool +package ali_cloud_tool import ( "context" @@ -128,16 +128,3 @@ func (r *RedisCache) Get(ctx context.Context, number string) (*ExpressRes, error return &info, nil } - -func TestAliCloudExpressClient_CallRealNameAuthAPI(t *testing.T) { - s := &AliCloudExpressClient{ - AppCode: "", - Host: "", - } - got, err := s.CallRealNameAuthAPI(context.Background(), "", "") - if err != nil { - t.Errorf("CallRealNameAuthAPI() error = %v", err) - return - } - fmt.Println("=====", got) -} diff --git a/ali_cloud_tool/ali_cloud_realname_client.go b/ali_cloud_tool/ali_cloud_realname_client.go new file mode 100644 index 0000000..065fb06 --- /dev/null +++ b/ali_cloud_tool/ali_cloud_realname_client.go @@ -0,0 +1,116 @@ +package ali_cloud_tool + +import ( + "context" + "encoding/json" + "fmt" + "github.com/pkg/errors" + "io" + "log" + "net/http" + "net/url" + "strings" + "time" +) + +type AliCloudRealNameClient struct { + AppCode string + Host string +} + +func NewAliCloudRealNameClient(host, appCode string) *AliCloudRealNameClient { + return &AliCloudRealNameClient{ + AppCode: appCode, + Host: host, + } +} + +// 调用实名认证API +func (s *AliCloudRealNameClient) CallRealNameAuthAPI(ctx context.Context, name, idCard string) (*RealNameAuthData, error) { + // 验证输入 + if strings.TrimSpace(name) == "" || strings.TrimSpace(idCard) == "" { + return nil, errors.New("姓名和身份证号不能为空") + } + + // 设置参数 + params := url.Values{} + params.Add("realName", name) + params.Add("cardNo", idCard) + + // 构建完整URL + fullURL, err := url.JoinPath(s.Host) + if err != nil { + return nil, fmt.Errorf("构建实名认证URL失败: %w, 姓名:%s, 身份证号:%s", err, name, idCard) + } + + // 创建HTTP客户端 + client := &http.Client{ + Timeout: 2 * time.Second, + } + + // 创建POST请求 + req, err := http.NewRequestWithContext(ctx, http.MethodPost, fullURL, strings.NewReader(params.Encode())) + if err != nil { + return nil, fmt.Errorf("创建实名认证请求失败: %w, 姓名:%s, 身份证号:%s", err, name, idCard) + } + + // 设置请求头 + req.Header.Set("Authorization", "APPCODE "+s.AppCode) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") + + // 发送请求 + resp, err := client.Do(req) + if err != nil { + return nil, fmt.Errorf("发送实名认证请求失败: %w, 姓名:%s, 身份证号:%s", err, name, idCard) + } + defer func() { + if closeErr := resp.Body.Close(); closeErr != nil { + log.Printf("关闭实名认证响应体失败: %v, 姓名:%s, 身份证号:%s", closeErr, name, idCard) + } + }() + + // 读取响应体 + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("读取实名认证响应失败: %w, 姓名:%s, 身份证号:%s", err, name, idCard) + } + + // 解析JSON响应 + var result RealNameAuthData + if err = json.Unmarshal(body, &result); err != nil { + return nil, fmt.Errorf("解析实名认证响应失败: %w, 响应内容:%s, 姓名:%s, 身份证号:%s", err, string(body), name, idCard) + } + + // 检查API返回的错误码 + if result.ErrorCode != 0 { + return &result, fmt.Errorf("实名认证接口返回错误, 错误码:%d, 原因:%s, 姓名:%s, 身份证号:%s", + result.ErrorCode, result.Reason, name, idCard) + } + + // 检查认证结果 + if !result.Result.Isok { + return &result, fmt.Errorf("实名认证不通过, 原因:%s, 姓名:%s, 身份证号:%s", + result.Reason, name, idCard) + } + + return &result, nil +} + +type RealNameAuthData struct { + ErrorCode int `json:"error_code"` + Reason string `json:"reason"` + Result struct { + Realname string `json:"realname"` + Idcard string `json:"idcard"` + Isok bool `json:"isok"` + IdCardInfor struct { + Province string `json:"province"` + City string `json:"city"` + District string `json:"district"` + Area string `json:"area"` + Sex string `json:"sex"` + Birthday string `json:"birthday"` + } `json:"IdCardInfor"` + } `json:"result"` + Sn string `json:"sn"` +} diff --git a/ali_cloud_tool/ali_cloud_realname_client_test.go b/ali_cloud_tool/ali_cloud_realname_client_test.go new file mode 100644 index 0000000..21d7f13 --- /dev/null +++ b/ali_cloud_tool/ali_cloud_realname_client_test.go @@ -0,0 +1,20 @@ +package ali_cloud_tool + +import ( + "context" + "fmt" + "testing" +) + +func TestAliCloudExpressClient_CallRealNameAuthAPI(t *testing.T) { + s := &AliCloudRealNameClient{ + AppCode: "", + Host: "", + } + got, err := s.CallRealNameAuthAPI(context.Background(), "", "") + if err != nil { + t.Errorf("CallRealNameAuthAPI() error = %v", err) + return + } + fmt.Println("=====", got) +} diff --git a/express_tool/express_cache.go b/ali_cloud_tool/express_cache.go similarity index 91% rename from express_tool/express_cache.go rename to ali_cloud_tool/express_cache.go index 50a0354..5e3645c 100644 --- a/express_tool/express_cache.go +++ b/ali_cloud_tool/express_cache.go @@ -1,4 +1,4 @@ -package express_tool +package ali_cloud_tool import ( "context"