82 lines
1.8 KiB
Go
82 lines
1.8 KiB
Go
|
|
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
|
|||
|
|
}
|
|||
|
|
}
|