This commit is contained in:
2026-07-05 01:42:03 +08:00
parent 743902fff5
commit 59becf9e16
3 changed files with 69 additions and 31 deletions

View File

@@ -4,16 +4,7 @@ import "strings"
const defaultPlateType = "02"
// plateTypeToPlateColor 将车牌类型编码转换为 ETC 人车关系核验所需的车牌颜色
func plateTypeToPlateColor(plateType string) string {
code := normalizePlateType(plateType)
if color, ok := plateTypePlateColorMap[code]; ok {
return color
}
return "0"
}
// normalizePlateType 归一化车牌类型空值默认小型汽车02
// normalizePlateType 归一化车牌类型空值默认小型汽车02供行驶证 V2 使用
func normalizePlateType(plateType string) string {
code := strings.TrimSpace(plateType)
if code == "" {
@@ -22,11 +13,25 @@ func normalizePlateType(plateType string) string {
return code
}
// etcPlateColorParam 将车牌类型转为 ETC 接口的 plateColor。
// 第二个返回值表示是否需要传 plateColor不传时由数据宝按默认蓝牌处理。
func etcPlateColorParam(plateType string) (color string, include bool) {
code := strings.TrimSpace(plateType)
if code == "" || code == "02" {
return "", false
}
color, ok := plateTypePlateColorMap[code]
if !ok || color == "0" {
return "", false
}
return color, true
}
// plateTypePlateColorMap 车牌类型 -> ETC 车牌颜色
// 0蓝 1黄 2黑 3白 4渐变绿 5黄绿双拼 6蓝白渐变 7临时 11绿 12红
var plateTypePlateColorMap = map[string]string{
"01": "1", // 大型汽车 -> 黄色
"02": "0", // 小型汽车 -> 蓝色
"02": "0", // 小型汽车 -> 蓝色(默认不传 plateColor
"03": "2", // 使馆汽车 -> 黑色
"04": "2", // 领馆汽车 -> 黑色
"05": "2", // 境外汽车 -> 黑色
@@ -47,7 +52,30 @@ var plateTypePlateColorMap = map[string]string{
"22": "7", // 临时行驶车 -> 临时牌照
"23": "3", // 警用汽车 -> 白色
"24": "3", // 警用摩托 -> 白色
"38": "0", // 预留 -> 蓝色
"38": "0", // 预留 -> 蓝色(等同默认,不传)
"51": "5", // 新能源大型车 -> 黄绿双拼色
"52": "11", // 新能源小型车 -> 绿色
}
func buildETCShujubaoParams(plateNo, name, plateType string) map[string]interface{} {
params := map[string]interface{}{
"key": "a2f32fc54b44ebc85b97a2aaff1734ec",
"carNo": plateNo,
"name": name,
}
if color, include := etcPlateColorParam(plateType); include {
params["plateColor"] = color
}
return params
}
func buildETCTianyuanParams(plateNo, name, plateType string) map[string]string {
params := map[string]string{
"plate_no": plateNo,
"name": name,
}
if color, include := etcPlateColorParam(plateType); include {
params["plate_color"] = color
}
return params
}

View File

@@ -2,24 +2,27 @@ package qcxg
import "testing"
func TestPlateTypeToPlateColor(t *testing.T) {
func TestEtcPlateColorParam(t *testing.T) {
tests := []struct {
plateType string
want string
wantColor string
wantInclude bool
}{
{"", "0"},
{"02", "0"},
{"01", "1"},
{"52", "11"},
{"51", "5"},
{"23", "3"},
{"22", "7"},
{"99", "0"},
{"", "", false},
{"02", "", false},
{"01", "1", true},
{"52", "11", true},
{"51", "5", true},
{"23", "3", true},
{"38", "", false},
{"99", "", false},
}
for _, tt := range tests {
if got := plateTypeToPlateColor(tt.plateType); got != tt.want {
t.Fatalf("plateTypeToPlateColor(%q) = %q, want %q", tt.plateType, got, tt.want)
color, include := etcPlateColorParam(tt.plateType)
if color != tt.wantColor || include != tt.wantInclude {
t.Fatalf("etcPlateColorParam(%q) = (%q, %v), want (%q, %v)",
tt.plateType, color, include, tt.wantColor, tt.wantInclude)
}
}
}
@@ -32,3 +35,15 @@ func TestNormalizePlateType(t *testing.T) {
t.Fatalf("normalizePlateType trim = %q, want 51", got)
}
}
func TestBuildETCShujubaoParams(t *testing.T) {
params := buildETCShujubaoParams("桂A12345", "张三", "01")
if params["plateColor"] != "1" {
t.Fatalf("plateColor = %v, want 1", params["plateColor"])
}
params = buildETCShujubaoParams("桂A12345", "张三", "")
if _, ok := params["plateColor"]; ok {
t.Fatalf("default should not include plateColor, got %v", params)
}
}

View File

@@ -23,12 +23,7 @@ func ProcessQCXG7K2NRequest(ctx context.Context, params []byte, deps *processors
}
plateType := normalizePlateType(paramsDto.PlateType)
etcParams := map[string]interface{}{
"key": "a2f32fc54b44ebc85b97a2aaff1734ec",
"carNo": paramsDto.PlateNo,
"name": paramsDto.Name,
"plateColor": plateTypeToPlateColor(paramsDto.PlateType),
}
etcParams := buildETCShujubaoParams(paramsDto.PlateNo, paramsDto.Name, paramsDto.PlateType)
data, err := deps.ShujubaoService.CallAPI(ctx, "/communication/personal/10093", etcParams)
if err == nil {