update: 添加 Guru Uid 关联生成 UUID 的逻辑

Signed-off-by: huyufei <yufei.hu@castbox.fm>
deeplink
胡宇飞 2024-05-22 17:54:27 +08:00
parent e2c3b5bc7c
commit 0125fa383e
6 changed files with 105 additions and 1 deletions

View File

@ -127,7 +127,11 @@ namespace Guru
public static readonly string IPM_EVENT_URL = IPM_URL + "push/api/v1/push/app/event"; public static readonly string IPM_EVENT_URL = IPM_URL + "push/api/v1/push/app/event";
public static string UUID
{
get => PlayerPrefs.GetString(nameof(UUID), "");
set => PlayerPrefs.SetString(nameof(UUID), value);
}
public static string IPM_UID public static string IPM_UID
{ {

View File

@ -32,10 +32,16 @@ namespace Guru
public string idfa; // 可选, ios广告id public string idfa; // 可选, ios广告id
public string adid; // 可选, adjust id public string adid; // 可选, adjust id
public string gpsAdid; // 可选, android广告id public string gpsAdid; // 可选, android广告id
public string userUuid; // 可选, 用户唯一ID由 uid 关联生成的 uuid
public DeviceData() public DeviceData()
{ {
DeviceUtil.GetDeviceInfo(); DeviceUtil.GetDeviceInfo();
if (string.IsNullOrEmpty(IPMConfig.UUID))
{
IPMConfig.UUID = IDHelper.GenUUID(IPMConfig.IPM_UID);
}
deviceId = IPMConfig.IPM_DEVICE_ID; deviceId = IPMConfig.IPM_DEVICE_ID;
uid = IPMConfig.IPM_UID; uid = IPMConfig.IPM_UID;
androidId = SystemInfo.deviceUniqueIdentifier; // Unity get AndroidID androidId = SystemInfo.deviceUniqueIdentifier; // Unity get AndroidID
@ -56,6 +62,7 @@ namespace Guru
idfa = IPMConfig.ADJUST_IDFA; idfa = IPMConfig.ADJUST_IDFA;
adid = IPMConfig.ADJUST_ADID; adid = IPMConfig.ADJUST_ADID;
gpsAdid = IPMConfig.ADJUST_GPSADID; gpsAdid = IPMConfig.ADJUST_GPSADID;
userUuid = IPMConfig.UUID;
} }
public override string ToString() public override string ToString()

View File

@ -55,7 +55,9 @@ namespace Guru
IPMConfig.IPM_NEWUSER = responseData.data.newUser; IPMConfig.IPM_NEWUSER = responseData.data.newUser;
IPMConfig.IPM_TOKEN_TIME = TimeUtil.GetCurrentTimeStampSecond(); IPMConfig.IPM_TOKEN_TIME = TimeUtil.GetCurrentTimeStampSecond();
IPMConfig.IPM_FIREBASE_TOKEN_TIME = TimeUtil.GetCurrentTimeStampSecond(); IPMConfig.IPM_FIREBASE_TOKEN_TIME = TimeUtil.GetCurrentTimeStampSecond();
IPMConfig.UUID = IDHelper.GenUUID(responseData.data.uid);
DeviceUtil.Save2AppGroup(); DeviceUtil.Save2AppGroup();
} }
} }
} }

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: da0e89af84894267b29675e22a97856d
timeCreated: 1716337392

View File

@ -0,0 +1,85 @@
namespace Guru
{
using System;
using UnityEngine;
using System.Linq;
/// <summary>
/// IDHelper 生成器
/// </summary>
public static class IDHelper
{
private const string UUID_PREFIX = "1b6eb1"; // 6 bytes
private const string UUID_NAMESCPACE = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
/// <summary>
/// 通过给定字符串生成固定的 UUID
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GenUUID(string key)
{
if (string.IsNullOrEmpty(key))
{
Debug.Log("<color=red>Invalid Key</color>");
return "";
}
var uid_code = key;
if(key.Contains('-')) uid_code = key.Split('-').Last();
if (uid_code.Length > 13)
{
return MakeUUIDV5(key);
}
return MakeGuruKeyUUID(uid_code);
}
public static string MakeUUIDV5(string key)
{
// 使用DNS命名空间的GUID
Guid namespaceId = Guid.Parse(UUID_NAMESCPACE);
byte[] nameBytes = System.Text.Encoding.UTF8.GetBytes(key);
// 计算MD5散列
byte[] hashBytes = System.Security.Cryptography.MD5.Create().ComputeHash(nameBytes);
// 将命名空间GUID的二进制值与散列值的前16个字节连接起来
byte[] guidBytes = new byte[16];
Array.Copy(namespaceId.ToByteArray(), guidBytes, 16);
Array.Copy(hashBytes, 0, guidBytes, 0, 16);
// 设置版本号和变体以生成V5 UUID
guidBytes[6] &= 0x0F; // 清除版本位
guidBytes[6] |= 0x50; // 设置版本和变体位
return new Guid(guidBytes).ToString("D");
}
public static string MakeGuruKeyUUID(string uidCode, string prefix = "")
{
// 使用MD5创建一个散列值
if(string.IsNullOrEmpty(prefix)) prefix = UUID_PREFIX; // 6 bytes
string haxString = ""; // should be less than 26 bytes
for(int i =0; i < uidCode.Length; i++){
int charCode = (int)(uidCode[i]);
// Debug.Log($"charCode: {charCode}");
if (charCode > 0xFF)
{
return "";
}
haxString += $"{charCode:X2}";
}
var padNum = 32 - prefix.Length;
if (haxString.Length > padNum)
{
return "";
}
// Debug.Log($"haxString: {haxString} len: {haxString.Length}");
string final = prefix + haxString.PadLeft(padNum, '0');
return new Guid(final).ToString("D");
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 88ede6c4c4614fd98f070b1275bc9282
timeCreated: 1716337403