Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
0377692779 | ||
![]() |
2f4e52cac5 | ||
![]() |
754a8910f2 | ||
![]() |
587fd91338 | ||
![]() |
0c1bce5d8c |
@@ -1,4 +1,4 @@
|
||||
package express_tool
|
||||
package ali_cloud_tool
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -18,10 +18,10 @@ type AliCloudExpressClient struct {
|
||||
cache ICacheAdapter
|
||||
}
|
||||
|
||||
func NewAliCloudExpressClient(host, appCode string) *AliCloudExpressClient {
|
||||
func NewAliCloudExpressClient(appCode string) *AliCloudExpressClient {
|
||||
return &AliCloudExpressClient{
|
||||
AppCode: appCode,
|
||||
Host: host,
|
||||
Host: "https://qryexpress.market.alicloudapi.com/lundear/expressTracking",
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package express_tool
|
||||
package ali_cloud_tool
|
||||
|
||||
import (
|
||||
"context"
|
116
ali_cloud_tool/ali_cloud_realname_client.go
Normal file
116
ali_cloud_tool/ali_cloud_realname_client.go
Normal file
@@ -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(appCode string) *AliCloudRealNameClient {
|
||||
return &AliCloudRealNameClient{
|
||||
AppCode: appCode,
|
||||
Host: "https://zidv2.market.alicloudapi.com/idcheck/Post",
|
||||
}
|
||||
}
|
||||
|
||||
// 调用实名认证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"`
|
||||
}
|
20
ali_cloud_tool/ali_cloud_realname_client_test.go
Normal file
20
ali_cloud_tool/ali_cloud_realname_client_test.go
Normal file
@@ -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)
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package express_tool
|
||||
package ali_cloud_tool
|
||||
|
||||
import (
|
||||
"context"
|
2
go.mod
2
go.mod
@@ -27,7 +27,7 @@ require (
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/tjfoc/gmsm v1.4.1 // indirect
|
||||
golang.org/x/net v0.26.0 // indirect
|
||||
golang.org/x/net v0.41.0 // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@@ -171,6 +171,10 @@ golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
||||
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
|
||||
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
|
||||
golang.org/x/net v0.37.1-0.20250305215238-2914f4677317 h1:wneCP+2d9NUmndnyTmY7VwUNYiP26xiN/AtdcojQ1lI=
|
||||
golang.org/x/net v0.37.1-0.20250305215238-2914f4677317/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
|
||||
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
|
||||
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
|
@@ -114,7 +114,7 @@ func (h *HuaChenIpClient) GetIpInfoFormCache(ctx context.Context, ip string, opt
|
||||
return nil, errors.Wrapf(err, "无法将IP信息转换为JSON,ip:%s", ip)
|
||||
}
|
||||
|
||||
if len(opt) == 0 {
|
||||
if len(opt) != 0 {
|
||||
err = h.cache.Set(ctx, h.ipKey(ip), string(infoJson), opt[0])
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "缓存ip:%s信息失败,", ip)
|
||||
|
Reference in New Issue
Block a user