feat(user): 新增三要素

This commit is contained in:
2024-11-25 00:25:48 +08:00
parent 6f77085c72
commit f13dcda370
9 changed files with 178 additions and 35 deletions

View File

@@ -3,6 +3,7 @@ package service
import (
"encoding/json"
"fmt"
"github.com/tidwall/gjson"
"io"
"net/http"
"net/url"
@@ -11,15 +12,18 @@ import (
)
type VerificationService struct {
c config.Config
c config.Config
westDexService *WestDexService
}
func NewVerificationService(c config.Config) *VerificationService {
func NewVerificationService(c config.Config, westDexService *WestDexService) *VerificationService {
return &VerificationService{
c: c,
c: c,
westDexService: westDexService,
}
}
// 二要素
type TwoFactorVerificationRequest struct {
Name string
IDCard string
@@ -39,6 +43,13 @@ type TwoFactorVerificationData struct {
Desc string `json:"desc"`
}
// 三要素
type ThreeFactorVerificationRequest struct {
Name string
IDCard string
Mobile string
}
// VerificationResult 定义校验结果结构体
type VerificationResult struct {
Passed bool
@@ -114,3 +125,51 @@ func (r *VerificationService) TwoFactorVerification(request TwoFactorVerificatio
return &VerificationResult{Passed: true, Err: nil}, nil
}
func (r *VerificationService) ThreeFactorVerification(request ThreeFactorVerificationRequest) (*VerificationResult, error) {
threeElementsReq := map[string]interface{}{
"name": request.Name,
"idNo": request.IDCard,
"phone": request.Mobile,
}
resp, err := r.westDexService.CallAPI("G15BJ02", threeElementsReq)
if err != nil {
return nil, err
}
dataResult := gjson.GetBytes(resp, "data.code")
if !dataResult.Exists() {
return nil, fmt.Errorf("code 字段不存在")
}
code := dataResult.Int()
switch code {
case 1000:
case 1002:
return &VerificationResult{
Passed: false,
Err: &ValidationError{Message: "姓名、证件号、手机号信息不一致"},
}, nil
case 1003:
return &VerificationResult{
Passed: false,
Err: &ValidationError{Message: "姓名、证件号、手机号信息不一致"},
}, nil
case 1004:
return &VerificationResult{
Passed: false,
Err: &ValidationError{Message: "姓名不正确"},
}, nil
case 1005:
return &VerificationResult{
Passed: false,
Err: &ValidationError{Message: "证件号码不正确"},
}, nil
default:
dataResultMsg := gjson.GetBytes(resp, "data.msg")
if !dataResultMsg.Exists() {
return nil, fmt.Errorf("msg字段不存在")
}
return nil, fmt.Errorf("三要素核验错误状态响应: %s", dataResultMsg.String())
}
return &VerificationResult{Passed: true, Err: nil}, nil
}