update: 更新 DeviceID 的生成和缓存机制
							parent
							
								
									d427fba5d2
								
							
						
					
					
						commit
						457317ee59
					
				|  | @ -0,0 +1,46 @@ | |||
| 
 | ||||
| namespace Guru | ||||
| { | ||||
|     public class DeviceIDUtil | ||||
|     { | ||||
|         /// <summary> | ||||
|         /// UUID (V4)  | ||||
|         /// </summary> | ||||
|         /// <returns></returns> | ||||
|         public static string UUID => System.Guid.NewGuid().ToString(); | ||||
|          | ||||
| #if UNITY_IOS | ||||
|         /// <summary> | ||||
|         /// IOS 或者 IDFV | ||||
|         /// 当获取到非法的值时, 用 UUID 代替 | ||||
|         /// </summary> | ||||
|         /// <returns></returns> | ||||
|         public static string IDFV() | ||||
|         { | ||||
|             var idfv = UnityEngine.SystemInfo.deviceUniqueIdentifier; | ||||
|             if (string.IsNullOrEmpty(idfv) || idfv.Contains("0000-0000-0000")) | ||||
|             { | ||||
|                 idfv = UUID; | ||||
|             } | ||||
|             return idfv; | ||||
|         } | ||||
|          | ||||
| #endif | ||||
|          | ||||
| #if UNITY_ANDROID | ||||
|         /// <summary> | ||||
|         /// Android ID | ||||
|         /// 可通过Unity 本身的接口来获取 | ||||
|         /// </summary> | ||||
|         /// <returns></returns> | ||||
|         public static string AndroidID() | ||||
|         { | ||||
|             var aid = UnityEngine.SystemInfo.deviceUniqueIdentifier; | ||||
|             if (string.IsNullOrEmpty(aid)) aid = UUID; | ||||
|             return aid; | ||||
|         } | ||||
| #endif | ||||
| 
 | ||||
| 
 | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,3 @@ | |||
| fileFormatVersion: 2 | ||||
| guid: fc9bb1c0043f4fa58b3f0f8edd4f902f | ||||
| timeCreated: 1706670074 | ||||
|  | @ -33,13 +33,13 @@ namespace Guru | |||
|          | ||||
|         private static void GetIOSDeviceInfo() | ||||
|         { | ||||
| #if UNITY_IOS && !UNITY_EDITOR | ||||
| #if UNITY_IOS  | ||||
| 			string content = iOSDeviceInfo(); | ||||
| 			Debug.Log($"GetDeviceInfo:{content}"); | ||||
| 			if(!string.IsNullOrEmpty(content)) | ||||
| 			{ | ||||
| 				string[] infos = content.Split('$'); | ||||
| 				IPMConfig.IPM_DEVICE_ID = infos[0]; | ||||
| 				IPMConfig.SetDeviceId(infos[0]); | ||||
| 				IPMConfig.IPM_APP_VERSION = infos[1]; | ||||
| 				IPMConfig.IPM_TIMEZONE = infos[2]; | ||||
| 				IPMConfig.IPM_MODEL = infos[3]; | ||||
|  | @ -49,6 +49,8 @@ namespace Guru | |||
| 				IsGetDeviceInfoSuccess = true; | ||||
| 			} | ||||
| #endif | ||||
| 	         | ||||
| 	         | ||||
|         } | ||||
| 		 | ||||
|         public static void SetiOSBadge() | ||||
|  |  | |||
|  | @ -12,6 +12,7 @@ namespace Guru | |||
| 		public static string IPM_X_APP_ID => GuruSettings.Instance.IPMSetting.AppId; | ||||
| 		public static int TOKEN_VALID_TIME => GuruSettings.Instance.IPMSetting.TokenValidTime; | ||||
| 		public static readonly int FIREBASE_TOKEN_VALID_TIME = TimeUtil.HOUR_TO_SECOND; | ||||
| 		public static bool UsingUUID = false; | ||||
| 		 | ||||
| 
 | ||||
| 		public static readonly string Header_Param_APPID = "X-APP-ID"; | ||||
|  | @ -47,13 +48,50 @@ namespace Guru | |||
| 			{ | ||||
| 				if (string.IsNullOrEmpty(SavedDeviceId)) | ||||
| 				{ | ||||
| 					SavedDeviceId = SystemInfo.deviceUniqueIdentifier; // 目前使用 UNITY 接口来生成对应的 DeviceID | ||||
| 					if (UsingUUID) | ||||
| 					{ | ||||
| 						SavedDeviceId = GenerateDeviceIdV2(); | ||||
| 					} | ||||
| 					else | ||||
| 					{ | ||||
| 						SavedDeviceId = GenerateDeviceIdV1(); | ||||
| 					} | ||||
| 				} | ||||
| 				return SavedDeviceId; // 优先使用缓存的 DeviceID | ||||
| 			} | ||||
| 		} | ||||
| 		 | ||||
| 		private static bool IsValidDeviceId(string deviceId) | ||||
| 		{ | ||||
| 			return !(string.IsNullOrEmpty(deviceId) || deviceId.Contains("0000-0000-0000")); | ||||
| 		} | ||||
| 
 | ||||
| 		/// <summary> | ||||
| 		/// 缓存设备 ID | ||||
| 		/// 用生成设备ID (V1) | ||||
| 		/// 使用 Unity 的 SystemInfo.deviceUniqueIdentifier 作为设备 ID | ||||
| 		/// </summary> | ||||
| 		/// <returns></returns> | ||||
| 		private static string GenerateDeviceIdV1() | ||||
| 		{ | ||||
| 			var did = SystemInfo.deviceUniqueIdentifier; | ||||
| 			if (!IsValidDeviceId(did)) | ||||
| 			{ | ||||
| 				//空串, 或者IOS 生成了无效的 IDFV, 则使用 DeviceIDUtil 代替 | ||||
| 				did = DeviceIDUtil.UUID; | ||||
| 			} | ||||
| 			return did; | ||||
| 		} | ||||
| 		 | ||||
| 		/// <summary> | ||||
| 		/// 用生成设备ID (V2) | ||||
| 		/// 直接使用 DeviceIDUtil 作为设备的唯一标识 | ||||
| 		/// </summary> | ||||
| 		/// <returns></returns> | ||||
| 		private static string GenerateDeviceIdV2() => DeviceIDUtil.UUID; | ||||
| 
 | ||||
| 		 | ||||
| 		/// <summary> | ||||
| 		/// 缓存设备ID | ||||
| 		/// </summary> | ||||
| 		private static string SavedDeviceId | ||||
| 		{ | ||||
|  | @ -61,6 +99,20 @@ namespace Guru | |||
| 			set => PlayerPrefs.SetString(nameof(SavedDeviceId), value); | ||||
| 		} | ||||
| 		 | ||||
| 		/// <summary> | ||||
| 		/// 设置 DeviceID, 此处设计覆写原则 | ||||
| 		/// </summary> | ||||
| 		/// <param name="deviceId"></param> | ||||
| 		/// <param name="overwrite"></param> | ||||
| 		public static void SetDeviceId(string deviceId, bool overwrite  = false) | ||||
| 		{ | ||||
| 			// 不写入空字符串 | ||||
| 			if (string.IsNullOrEmpty(deviceId)) return; | ||||
| 			// 如果设备ID无效 或 需要覆盖写入 | ||||
| 			if (!IsValidDeviceId(SavedDeviceId) || overwrite) | ||||
| 				SavedDeviceId = deviceId; | ||||
| 		} | ||||
| 		 | ||||
| #if DEBUG | ||||
| 		public static readonly string IPM_URL = "https://dev.saas.castbox.fm/"; | ||||
| #else | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue