| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- using AppUI.Bluetooth;
- using Newtonsoft.Json;
- using System;
- using System.Collections;
- using UnityEngine;
- using UnityEngine.Networking;
- /* Socket组件-用户 */
- public class UserComp : JCUnityLib.Singleton<UserComp>
- {
- public void getUserInfo(System.Action<UserInfo> 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<Newtonsoft.Json.Linq.JToken> cb = (Newtonsoft.Json.Linq.JToken o) => {
- string gyrStr = o.Value<string>("gyr");
- string magStr = o.Value<string>("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);
- }
- /// <summary>SDK Host 模式保存:WebSocket 上传 + 本地缓存(与旧 Axis9Handler 一致)。</summary>
- 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 ?? "");
- }
- /// <summary>SDK Host 模式读取:内存缓存 → PlayerPrefs → 游戏业务服 HTTP。</summary>
- public void LoadCalibrateRecord(string mac, Action<string> 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<string> 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<RequestResult>(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<bool> 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<RequestResult> callback)
- //{
- // using (UnityWebRequest request = UnityWebRequest.Post(url, form))
- // {
- // yield return request.SendWebRequest();
- // if (request.result == UnityWebRequest.Result.Success)
- // callback?.Invoke(JsonConvert.DeserializeObject<RequestResult>(request.downloadHandler.text));
- // else
- // callback?.Invoke(new RequestResult());
- // }
- //}
- }
|