耳机阻抗数据:过滤了阻抗=0,耳机型号乱码的数据

This commit is contained in:
eafonyang
2026-08-25 16:47:22 +08:00
parent 27ee791891
commit ceea657ccf
+39 -8
View File
@@ -56,6 +56,15 @@ func (h *ImpedanceHandler) ReportImpedance(c *gin.Context) {
return
}
// 品牌/型号含乱码或控制字符、超长属于无效数据,静默丢弃
if !isValidHeadphoneText(brand) || !isValidHeadphoneText(headphoneModel) {
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "操作成功",
})
return
}
impedance, err := strconv.Atoi(valueStr)
if err != nil {
c.JSON(http.StatusOK, gin.H{
@@ -65,18 +74,27 @@ func (h *ImpedanceHandler) ReportImpedance(c *gin.Context) {
return
}
// 阻抗值为 0 属于无效数据,静默丢弃
if impedance == 0 {
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "操作成功",
})
return
}
brandNorm := normalizeHeadphoneText(brand)
modelNorm := normalizeHeadphoneText(headphoneModel)
impedanceInfo := map[string]any{
"mac_addr": mac,
"device_model": deviceModel,
"impedance_ohm": impedance,
"headphone_brand": brand,
"headphone_model": headphoneModel,
"headphone_brand_norm": brandNorm,
"headphone_model_norm": modelNorm,
"ip_addr": clientIP,
"mac_addr": mac,
"device_model": deviceModel,
"impedance_ohm": impedance,
"headphone_brand": brand,
"headphone_model": headphoneModel,
"headphone_brand_norm": brandNorm,
"headphone_model_norm": modelNorm,
"ip_addr": clientIP,
}
jsonData, err := json.Marshal(impedanceInfo)
@@ -110,6 +128,19 @@ func normalizeHeadphoneText(s string) string {
return strings.ToLower(strings.TrimSpace(s))
}
// isValidHeadphoneText 校验耳机品牌/型号:拒绝含乱码字符(U+FFFD)、控制字符及超长(>100 字符)的输入
func isValidHeadphoneText(s string) bool {
if len(s) > 100 {
return false
}
for _, r := range s {
if r == '\ufffd' || r < 0x20 || r == 0x7f {
return false
}
}
return true
}
func impedanceRedisField(mac, brandNorm, modelNorm string) string {
return fmt.Sprintf("%s|%s|%s", mac, brandNorm, modelNorm)
}