goProject/.svn/pristine/9f/9fe5996eb204228a5a9f75b93e8d663d005bbf12.svn-base
2025-01-06 16:21:36 +08:00

82 lines
1.8 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package deviceUtil
import (
"strings"
)
// 将MAC地址转化为标准格式
func ConvertMacToStandardFormat(mac string) string {
if mac == "" || mac == "00:00:00:00:00:00" || mac == "02:00:00:00:00:00" {
return ""
}
//如果mac的长度不为12或17则是不正确的格式
if len(mac) != 12 && len(mac) != 17 {
return ""
}
//转化为大写
mac = strings.ToUpper(mac)
//如果mac地址的长度为17已经有:),则直接返回
if len(mac) == 17 {
return mac
}
//如果没有分隔符,则添加分隔符
newMac := make([]rune, 0, 17)
for i, v := range []rune(mac) {
newMac = append(newMac, v)
if i < len(mac) - 1 && i % 2 == 1 {
newMac = append(newMac, ':')
}
}
return string(newMac)
}
func ConvertIdfaToStandardFormat(idfa string) string {
//如果是空或默认值则返回String.Empty
if idfa == "" || idfa == "00000000-0000-0000-0000-000000000000" {
return ""
}
//如果idfa的长度不为32或36则代表是Android的数据则可以直接返回
if len(idfa) != 32 && len(idfa) != 36 {
return idfa
}
//转化为大写
idfa = strings.ToUpper(idfa);
//如果idfa地址的长度为36已经有:),则直接返回
if len(idfa) == 36 {
return idfa
}
//如果没有分隔符,则添加分隔符
newIdfa := make([]rune, 0, 36)
for i, v := range []rune(idfa) {
newIdfa = append(newIdfa, v)
if i == 7 || i == 11 || i == 15 || i == 19 {
newIdfa = append(newIdfa, '-')
}
}
return string(newIdfa)
}
// 根据MAC和IDFA获取唯一标识
func GetIdentifier(mac, idfa string) string {
mac = ConvertMacToStandardFormat(mac)
idfa = ConvertIdfaToStandardFormat(idfa);
//如果idfa不为空则使用idfa否则使用mac
if idfa != "" {
return idfa
} else {
return mac
}
}