This commit is contained in:
Mrx
2026-01-24 10:44:09 +08:00
parent b171288361
commit ea59a8ddff

View File

@@ -4,12 +4,25 @@ import (
"context"
"encoding/json"
"errors"
"strconv"
"strings"
"time"
"tyapi-server/internal/domains/api/dto"
"tyapi-server/internal/domains/api/services/processors"
"tyapi-server/internal/infrastructure/external/shumai"
)
// ivyz2a8bShumaiResp 数脉 /v4/id_card/check 返回格式
type ivyz2a8bShumaiResp struct {
Result float64 `json:"result"`
OrderNo string `json:"order_no"`
Desc string `json:"desc"`
Sex string `json:"sex"`
Birthday string `json:"birthday"` // yyyyMMdd
Address string `json:"address"`
}
// ProcessIVYZ2A8BRequest IVYZ2A8B API处理方法 - 身份二要素认证政务版 数脉内部替换
func ProcessIVYZ2A8BRequest(ctx context.Context, params []byte, deps *processors.ProcessorDependencies) ([]byte, error) {
var paramsDto dto.IVYZ2A8BReq
@@ -46,5 +59,98 @@ func ProcessIVYZ2A8BRequest(ctx context.Context, params []byte, deps *processors
}
}
return respBytes, nil
// 将数脉返回的新格式映射为原有 API 输出格式
oldFormat, err := mapIVYZ2A8BShumaiToOld(respBytes)
if err != nil {
return nil, errors.Join(processors.ErrSystem, err)
}
return json.Marshal(oldFormat)
}
func mapIVYZ2A8BShumaiToOld(respBytes []byte) (map[string]interface{}, error) {
var r ivyz2a8bShumaiResp
if err := json.Unmarshal(respBytes, &r); err != nil {
return nil, err
}
// final_auth_result: "0"=一致,"1"=不一致/无记录;按 result0-一致1-不一致2-无记录(预留)
finalAuth := "1"
switch int(r.Result) {
case 0:
finalAuth = "0"
}
return map[string]interface{}{
"final_auth_result": finalAuth,
"birthday": formatBirthdayOld(r.Birthday),
"address": r.Address,
"constellation": constellationFromBirthday(r.Birthday),
"gender": r.Sex,
"age": ageFromBirthday(r.Birthday),
}, nil
}
func formatBirthdayOld(s string) string {
s = strings.TrimSpace(s)
if len(s) != 8 {
return s
}
for _, c := range s {
if c < '0' || c > '9' {
return s
}
}
return s[0:4] + "年" + s[4:6] + "月" + s[6:8] + "日"
}
func constellationFromBirthday(s string) string {
if len(s) != 8 {
return ""
}
month, _ := strconv.Atoi(s[4:6])
day, _ := strconv.Atoi(s[6:8])
if month < 1 || month > 12 || day < 1 || day > 31 {
return ""
}
switch {
case (month == 12 && day >= 22) || (month == 1 && day <= 19):
return "魔羯座"
case (month == 1 && day >= 20) || (month == 2 && day <= 18):
return "水瓶座"
case (month == 2 && day >= 19) || (month == 3 && day <= 20):
return "双鱼座"
case (month == 3 && day >= 21) || (month == 4 && day <= 19):
return "白羊座"
case (month == 4 && day >= 20) || (month == 5 && day <= 20):
return "金牛座"
case (month == 5 && day >= 21) || (month == 6 && day <= 21):
return "双子座"
case (month == 6 && day >= 22) || (month == 7 && day <= 22):
return "巨蟹座"
case (month == 7 && day >= 23) || (month == 8 && day <= 22):
return "狮子座"
case (month == 8 && day >= 23) || (month == 9 && day <= 22):
return "处女座"
case (month == 9 && day >= 23) || (month == 10 && day <= 23):
return "天秤座"
case (month == 10 && day >= 24) || (month == 11 && day <= 22):
return "天蝎座"
case (month == 11 && day >= 23) || (month == 12 && day <= 21):
return "射手座"
default:
return "魔羯座"
}
}
func ageFromBirthday(s string) string {
if len(s) < 4 {
return ""
}
y, err := strconv.Atoi(s[0:4])
if err != nil || y <= 0 {
return ""
}
age := time.Now().Year() - y
if age < 0 {
age = 0
}
return strconv.Itoa(age)
}