44 lines
925 B
Go
44 lines
925 B
Go
|
|
package qcxg
|
||
|
|
|
||
|
|
import (
|
||
|
|
_ "embed"
|
||
|
|
"encoding/json"
|
||
|
|
"strings"
|
||
|
|
"unicode/utf8"
|
||
|
|
)
|
||
|
|
|
||
|
|
//go:embed data/car_plate.json
|
||
|
|
var carPlateJSON []byte
|
||
|
|
|
||
|
|
var carPlateCityMap map[string]string
|
||
|
|
|
||
|
|
func init() {
|
||
|
|
carPlateCityMap = make(map[string]string)
|
||
|
|
_ = json.Unmarshal(carPlateJSON, &carPlateCityMap)
|
||
|
|
}
|
||
|
|
|
||
|
|
// lookupCityByPlateNo 根据车牌号前缀(省简称+字母)查城市,未匹配返回 "-"
|
||
|
|
func lookupCityByPlateNo(plateNo string) string {
|
||
|
|
prefix := extractPlatePrefix(plateNo)
|
||
|
|
if prefix == "" {
|
||
|
|
return "-"
|
||
|
|
}
|
||
|
|
if city, ok := carPlateCityMap[prefix]; ok && city != "" {
|
||
|
|
return city
|
||
|
|
}
|
||
|
|
return "-"
|
||
|
|
}
|
||
|
|
|
||
|
|
// extractPlatePrefix 取车牌前两个字符作为字典键,如 "鲁A12345" -> "鲁A"
|
||
|
|
func extractPlatePrefix(plateNo string) string {
|
||
|
|
plateNo = strings.TrimSpace(plateNo)
|
||
|
|
if plateNo == "" {
|
||
|
|
return ""
|
||
|
|
}
|
||
|
|
if utf8.RuneCountInString(plateNo) < 2 {
|
||
|
|
return ""
|
||
|
|
}
|
||
|
|
runes := []rune(plateNo)
|
||
|
|
return string(runes[:2])
|
||
|
|
}
|