实名认证接口
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
package express_tool
|
package ali_cloud_tool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -141,77 +140,6 @@ func (a *AliCloudExpressClient) numberKey(prefix, number string) string {
|
|||||||
return fmt.Sprintf("%s:number:%s", prefix, number)
|
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 {
|
type ExpressRes struct {
|
||||||
Code int `json:"code"`
|
Code int `json:"code"`
|
||||||
Desc string `json:"desc"`
|
Desc string `json:"desc"`
|
||||||
@@ -231,22 +159,3 @@ type List struct {
|
|||||||
Time string `json:"time"`
|
Time string `json:"time"`
|
||||||
Status string `json:"status"`
|
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"`
|
|
||||||
}
|
|
@@ -1,4 +1,4 @@
|
|||||||
package express_tool
|
package ali_cloud_tool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -128,16 +128,3 @@ func (r *RedisCache) Get(ctx context.Context, number string) (*ExpressRes, error
|
|||||||
|
|
||||||
return &info, nil
|
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)
|
|
||||||
}
|
|
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(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"`
|
||||||
|
}
|
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 (
|
import (
|
||||||
"context"
|
"context"
|
Reference in New Issue
Block a user