using AppUI.Bluetooth; using Newtonsoft.Json; using System; using System.Collections; using UnityEngine; using UnityEngine.Networking; /* Socket组件-用户 */ public class UserComp : JCUnityLib.Singleton { public void getUserInfo(System.Action cb) { UserPlayer.ins.call("userComp.getUserInfo", null, cb); } public void saveUserInfo(UserInfo userInfo) { UserPlayer.ins.call("userComp.saveUserInfo", userInfo); } private string _lastSaveMac; public void saveMac() { string mac = LoginMgr.myUserInfo.mac; if (string.IsNullOrEmpty(mac)) return; _lastSaveMac = mac; // int type = AimHandler.ins.DeviceType; int type = SmartBowDeviceHub.ins.Aim.DeviceType; if (type > 0) { UserPlayer.ins.call("userComp.saveMac2", mac, type); } else { Action cb = (Newtonsoft.Json.Linq.JToken o) => { string gyrStr = o.Value("gyr"); string magStr = o.Value("mag"); // AimHandler.ins.InitGyr(gyrStr); // AimHandler.ins.InitMag(magStr); SmartBowDeviceHub.ins.Aim.InitGyr(gyrStr); SmartBowDeviceHub.ins.Aim.InitMag(magStr); }; UserPlayer.ins.call("userComp.saveMac", new object[]{mac}, cb); } if (CommonConfig.StandaloneMode) { //WWWForm form = new WWWForm(); //form.AddField("mac", mac); //JCUnityLib.CoroutineStarter.Start(Post(CommonConfig.SmartBowSdkURL + "/SmartBowSDK/getCalibrateRecord", form, (res) => //{ // if (res.code == 0) onResumeCalibrateRecord(res.data as string); //})); //onResumeCalibrateRecord(PlayerPrefs.GetString(GetCalibrateDataStorageLocalKey(mac), "")); string key = GetCalibrateDataStorageLocalKey(mac); if (PlayerPrefs.HasKey(key)) { // 存储值存在,获取并处理 string calibrateData = PlayerPrefs.GetString(key, ""); if(calibrateData.Length >0) onResumeCalibrateRecord(calibrateData); } } } public void saveCalibrateRecord(string record) { string mac = SmartBowDeviceHub.ins.Ble.curMac; if (string.IsNullOrEmpty(mac)) { SideTipView.ShowTip("没有Mac无法上传九轴数据", UnityEngine.Color.yellow); throw new Exception("没有Mac无法上传九轴数据"); } SaveCalibrateRecordForSdk(mac, record); } /// SDK Host 模式保存:WebSocket 上传 + 本地缓存(与旧 Axis9Handler 一致)。 public void SaveCalibrateRecordForSdk(string mac, string record) { if (string.IsNullOrEmpty(mac)) { Debug.LogWarning("[UserComp] SaveCalibrateRecordForSdk: mac 为空"); return; } if (string.IsNullOrEmpty(record)) return; int type = SmartBowDeviceHub.ins.Aim.DeviceType; if (type <= 0) return; Axis9CalibrateRecord.CacheCalibrateRecord(mac, record); Axis9CalibrateRecord.SetCalibrateOkRecord(mac, true); if (UserPlayer.ins != null && UserPlayer.ins.isValid) UserPlayer.ins.call("userComp.saveCalibrateRecord2", type, record, mac); if (CommonConfig.StandaloneMode) PlayerPrefs.SetString(GetCalibrateDataStorageLocalKey(mac), record ?? ""); } /// SDK Host 模式读取:内存缓存 → PlayerPrefs → 游戏业务服 HTTP。 public void LoadCalibrateRecord(string mac, Action onComplete) { if (onComplete == null) return; if (string.IsNullOrEmpty(mac)) { onComplete(""); return; } if (Axis9CalibrateRecord.TryGetCachedRecord(mac, out string cached)) { onComplete(cached); return; } string key = GetCalibrateDataStorageLocalKey(mac); if (PlayerPrefs.HasKey(key)) { string local = PlayerPrefs.GetString(key, ""); if (!string.IsNullOrEmpty(local)) { onComplete(local); return; } } JCUnityLib.CoroutineStarter.Start(LoadCalibrateRecordHttp(mac, onComplete)); } static IEnumerator LoadCalibrateRecordHttp(string mac, Action onComplete) { WWWForm form = new WWWForm(); form.AddField("mac", mac); string url = CommonConfig.gateServerURL + "/SmartBowSDK/getCalibrateRecord"; using (UnityWebRequest request = UnityWebRequest.Post(url, form)) { yield return request.SendWebRequest(); if (request.result != UnityWebRequest.Result.Success) { onComplete(""); yield break; } RequestResult res = JsonConvert.DeserializeObject(request.downloadHandler.text); string record = res != null && res.code == 0 ? res.data as string : null; if (string.IsNullOrEmpty(record)) { onComplete(""); yield break; } Axis9CalibrateRecord.CacheCalibrateRecord(mac, record); onComplete(record); } } private string GetCalibrateDataStorageLocalKey(string mac) { string key = "CalibrateDataStorage"; string version = "1.0.0"; return key + "_" + mac + "_" + version; } public void deleteAccount(Action callback) { if (UserPlayer.ins.isValid && UserPlayer.ins.loginAuthed) { UserPlayer.ins.call("userComp.deleteAccount", null, callback); } else { callback?.Invoke(false); } } #region 被服务端调用的函数 public void onResumeCalibrateRecord(string record) { Axis9CalibrateRecord.CacheCalibrateRecord(_lastSaveMac, record); // AimHandler.ins.ResumeCalibrateRecord(record); SmartBowDeviceHub.ins.Aim.ResumeCalibrateRecord(record); } #endregion //public static IEnumerator Post(string url, WWWForm form, Action callback) //{ // using (UnityWebRequest request = UnityWebRequest.Post(url, form)) // { // yield return request.SendWebRequest(); // if (request.result == UnityWebRequest.Result.Success) // callback?.Invoke(JsonConvert.DeserializeObject(request.downloadHandler.text)); // else // callback?.Invoke(new RequestResult()); // } //} }