2024-11-21 12:14:34 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2025-04-26 13:09:34 +08:00
|
|
|
"context"
|
2024-11-21 12:14:34 +08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2025-04-27 12:17:18 +08:00
|
|
|
"tyc-server/app/main/api/internal/config"
|
2025-04-09 15:58:06 +08:00
|
|
|
"tyc-server/pkg/lzkit/crypto"
|
|
|
|
|
|
|
|
"github.com/tidwall/gjson"
|
2024-11-21 12:14:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type VerificationService struct {
|
2025-04-26 13:09:34 +08:00
|
|
|
c config.Config
|
|
|
|
westDexService *WestDexService
|
|
|
|
apiRequestService *ApiRequestService
|
2024-11-21 12:14:34 +08:00
|
|
|
}
|
|
|
|
|
2025-04-26 13:09:34 +08:00
|
|
|
func NewVerificationService(c config.Config, westDexService *WestDexService, apiRequestService *ApiRequestService) *VerificationService {
|
2024-11-21 12:14:34 +08:00
|
|
|
return &VerificationService{
|
2025-04-26 13:09:34 +08:00
|
|
|
c: c,
|
|
|
|
westDexService: westDexService,
|
|
|
|
apiRequestService: apiRequestService,
|
2024-11-21 12:14:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-25 00:25:48 +08:00
|
|
|
// 二要素
|
2024-11-21 12:14:34 +08:00
|
|
|
type TwoFactorVerificationRequest struct {
|
|
|
|
Name string
|
|
|
|
IDCard string
|
|
|
|
}
|
|
|
|
type TwoFactorVerificationResp struct {
|
|
|
|
Msg string `json:"msg"`
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Code int `json:"code"`
|
|
|
|
Data *TwoFactorVerificationData `json:"data"` //
|
|
|
|
}
|
|
|
|
type TwoFactorVerificationData struct {
|
|
|
|
Birthday string `json:"birthday"`
|
|
|
|
Result int `json:"result"`
|
|
|
|
Address string `json:"address"`
|
|
|
|
OrderNo string `json:"orderNo"`
|
|
|
|
Sex string `json:"sex"`
|
|
|
|
Desc string `json:"desc"`
|
|
|
|
}
|
|
|
|
|
2024-11-25 00:25:48 +08:00
|
|
|
// 三要素
|
|
|
|
type ThreeFactorVerificationRequest struct {
|
|
|
|
Name string
|
|
|
|
IDCard string
|
|
|
|
Mobile string
|
|
|
|
}
|
|
|
|
|
2024-11-21 12:14:34 +08:00
|
|
|
// VerificationResult 定义校验结果结构体
|
|
|
|
type VerificationResult struct {
|
|
|
|
Passed bool
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidationError 定义校验错误类型
|
|
|
|
type ValidationError struct {
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ValidationError) Error() string {
|
|
|
|
return e.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *VerificationService) TwoFactorVerification(request TwoFactorVerificationRequest) (*VerificationResult, error) {
|
|
|
|
appCode := r.c.Ali.Code
|
|
|
|
requestUrl := "https://kzidcardv1.market.alicloudapi.com/api-mall/api/id_card/check"
|
|
|
|
|
|
|
|
// 构造查询参数
|
|
|
|
data := url.Values{}
|
|
|
|
data.Add("name", request.Name)
|
|
|
|
data.Add("idcard", request.IDCard)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, requestUrl, strings.NewReader(data.Encode()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("创建请求失败: %+v", err)
|
|
|
|
}
|
|
|
|
req.Header.Set("Authorization", "APPCODE "+appCode)
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("请求失败: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("请求失败, 状态码: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("响应体读取失败:%v", err)
|
|
|
|
}
|
|
|
|
var twoFactorVerificationResp TwoFactorVerificationResp
|
|
|
|
err = json.Unmarshal(respBody, &twoFactorVerificationResp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("二要素解析错误: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !twoFactorVerificationResp.Success {
|
|
|
|
return &VerificationResult{
|
|
|
|
Passed: false,
|
|
|
|
Err: &ValidationError{Message: "请输入有效的身份证号码"},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if twoFactorVerificationResp.Code != 200 {
|
|
|
|
return &VerificationResult{
|
|
|
|
Passed: false,
|
|
|
|
Err: &ValidationError{Message: twoFactorVerificationResp.Msg},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if twoFactorVerificationResp.Data.Result == 1 {
|
|
|
|
return &VerificationResult{
|
|
|
|
Passed: false,
|
|
|
|
Err: &ValidationError{Message: "姓名与身份证不一致"},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &VerificationResult{Passed: true, Err: nil}, nil
|
|
|
|
}
|
2025-04-26 13:09:34 +08:00
|
|
|
func (r *VerificationService) TwoFactorVerificationWest(ctx context.Context, request TwoFactorVerificationRequest) (*VerificationResult, error) {
|
|
|
|
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"name": request.Name,
|
|
|
|
"id_card": request.IDCard,
|
|
|
|
}
|
|
|
|
marshal, err := json.Marshal(params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("二要素参数创建错误: %v", err)
|
|
|
|
}
|
|
|
|
resp, err := r.apiRequestService.ProcessLayoutIdcardRequest(ctx, marshal)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("请求失败: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
respStr := string(resp.Data)
|
|
|
|
if respStr != "0" {
|
|
|
|
return &VerificationResult{
|
|
|
|
Passed: false,
|
|
|
|
Err: &ValidationError{Message: "姓名与身份证不一致"},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &VerificationResult{Passed: true, Err: nil}, nil
|
|
|
|
}
|
2024-11-25 00:25:48 +08:00
|
|
|
func (r *VerificationService) ThreeFactorVerification(request ThreeFactorVerificationRequest) (*VerificationResult, error) {
|
2024-11-25 01:05:25 +08:00
|
|
|
westName, err := crypto.WestDexEncrypt(request.Name, r.c.WestConfig.Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
westIDCard, err := crypto.WestDexEncrypt(request.IDCard, r.c.WestConfig.Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
westPhone, err := crypto.WestDexEncrypt(request.Mobile, r.c.WestConfig.Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-11-25 00:25:48 +08:00
|
|
|
threeElementsReq := map[string]interface{}{
|
2024-11-25 01:05:25 +08:00
|
|
|
"data": map[string]interface{}{
|
|
|
|
"name": westName,
|
|
|
|
"idNo": westIDCard,
|
|
|
|
"phone": westPhone,
|
|
|
|
},
|
2024-11-25 00:25:48 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|