实名认证接口
This commit is contained in:
parent
0237a95a43
commit
0c1bce5d8c
@ -9,6 +9,7 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -140,6 +141,77 @@ 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"`
|
||||
@ -159,3 +231,22 @@ 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"`
|
||||
}
|
||||
|
@ -128,3 +128,16 @@ 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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user