UserComp.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using AppUI.Bluetooth;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections;
  5. using UnityEngine;
  6. using UnityEngine.Networking;
  7. /* Socket组件-用户 */
  8. public class UserComp : JCUnityLib.Singleton<UserComp>
  9. {
  10. public void getUserInfo(System.Action<UserInfo> cb) {
  11. UserPlayer.ins.call("userComp.getUserInfo", null, cb);
  12. }
  13. public void saveUserInfo(UserInfo userInfo) {
  14. UserPlayer.ins.call("userComp.saveUserInfo", userInfo);
  15. }
  16. private string _lastSaveMac;
  17. public void saveMac()
  18. {
  19. string mac = LoginMgr.myUserInfo.mac;
  20. if (string.IsNullOrEmpty(mac)) return;
  21. _lastSaveMac = mac;
  22. // int type = AimHandler.ins.DeviceType;
  23. int type = SmartBowDeviceHub.ins.Aim.DeviceType;
  24. if (type > 0) {
  25. UserPlayer.ins.call("userComp.saveMac2", mac, type);
  26. } else {
  27. Action<Newtonsoft.Json.Linq.JToken> cb = (Newtonsoft.Json.Linq.JToken o) => {
  28. string gyrStr = o.Value<string>("gyr");
  29. string magStr = o.Value<string>("mag");
  30. // AimHandler.ins.InitGyr(gyrStr);
  31. // AimHandler.ins.InitMag(magStr);
  32. SmartBowDeviceHub.ins.Aim.InitGyr(gyrStr);
  33. SmartBowDeviceHub.ins.Aim.InitMag(magStr);
  34. };
  35. UserPlayer.ins.call("userComp.saveMac", new object[]{mac}, cb);
  36. }
  37. if (CommonConfig.StandaloneMode)
  38. {
  39. //WWWForm form = new WWWForm();
  40. //form.AddField("mac", mac);
  41. //JCUnityLib.CoroutineStarter.Start(Post(CommonConfig.SmartBowSdkURL + "/SmartBowSDK/getCalibrateRecord", form, (res) =>
  42. //{
  43. // if (res.code == 0) onResumeCalibrateRecord(res.data as string);
  44. //}));
  45. //onResumeCalibrateRecord(PlayerPrefs.GetString(GetCalibrateDataStorageLocalKey(mac), ""));
  46. string key = GetCalibrateDataStorageLocalKey(mac);
  47. if (PlayerPrefs.HasKey(key))
  48. {
  49. // 存储值存在,获取并处理
  50. string calibrateData = PlayerPrefs.GetString(key, "");
  51. if(calibrateData.Length >0)
  52. onResumeCalibrateRecord(calibrateData);
  53. }
  54. }
  55. }
  56. public void saveCalibrateRecord(string record) {
  57. string mac = SmartBowDeviceHub.ins.Ble.curMac;
  58. if (string.IsNullOrEmpty(mac)) {
  59. SideTipView.ShowTip("没有Mac无法上传九轴数据", UnityEngine.Color.yellow);
  60. throw new Exception("没有Mac无法上传九轴数据");
  61. }
  62. SaveCalibrateRecordForSdk(mac, record);
  63. }
  64. /// <summary>SDK Host 模式保存:WebSocket 上传 + 本地缓存(与旧 Axis9Handler 一致)。</summary>
  65. public void SaveCalibrateRecordForSdk(string mac, string record)
  66. {
  67. if (string.IsNullOrEmpty(mac))
  68. {
  69. Debug.LogWarning("[UserComp] SaveCalibrateRecordForSdk: mac 为空");
  70. return;
  71. }
  72. if (string.IsNullOrEmpty(record)) return;
  73. int type = SmartBowDeviceHub.ins.Aim.DeviceType;
  74. if (type <= 0) return;
  75. Axis9CalibrateRecord.CacheCalibrateRecord(mac, record);
  76. Axis9CalibrateRecord.SetCalibrateOkRecord(mac, true);
  77. if (UserPlayer.ins != null && UserPlayer.ins.isValid)
  78. UserPlayer.ins.call("userComp.saveCalibrateRecord2", type, record, mac);
  79. if (CommonConfig.StandaloneMode)
  80. PlayerPrefs.SetString(GetCalibrateDataStorageLocalKey(mac), record ?? "");
  81. }
  82. /// <summary>SDK Host 模式读取:内存缓存 → PlayerPrefs → 游戏业务服 HTTP。</summary>
  83. public void LoadCalibrateRecord(string mac, Action<string> onComplete)
  84. {
  85. if (onComplete == null) return;
  86. if (string.IsNullOrEmpty(mac))
  87. {
  88. onComplete("");
  89. return;
  90. }
  91. if (Axis9CalibrateRecord.TryGetCachedRecord(mac, out string cached))
  92. {
  93. onComplete(cached);
  94. return;
  95. }
  96. string key = GetCalibrateDataStorageLocalKey(mac);
  97. if (PlayerPrefs.HasKey(key))
  98. {
  99. string local = PlayerPrefs.GetString(key, "");
  100. if (!string.IsNullOrEmpty(local))
  101. {
  102. onComplete(local);
  103. return;
  104. }
  105. }
  106. JCUnityLib.CoroutineStarter.Start(LoadCalibrateRecordHttp(mac, onComplete));
  107. }
  108. static IEnumerator LoadCalibrateRecordHttp(string mac, Action<string> onComplete)
  109. {
  110. WWWForm form = new WWWForm();
  111. form.AddField("mac", mac);
  112. string url = CommonConfig.gateServerURL + "/SmartBowSDK/getCalibrateRecord";
  113. using (UnityWebRequest request = UnityWebRequest.Post(url, form))
  114. {
  115. yield return request.SendWebRequest();
  116. if (request.result != UnityWebRequest.Result.Success)
  117. {
  118. onComplete("");
  119. yield break;
  120. }
  121. RequestResult res = JsonConvert.DeserializeObject<RequestResult>(request.downloadHandler.text);
  122. string record = res != null && res.code == 0 ? res.data as string : null;
  123. if (string.IsNullOrEmpty(record))
  124. {
  125. onComplete("");
  126. yield break;
  127. }
  128. Axis9CalibrateRecord.CacheCalibrateRecord(mac, record);
  129. onComplete(record);
  130. }
  131. }
  132. private string GetCalibrateDataStorageLocalKey(string mac)
  133. {
  134. string key = "CalibrateDataStorage";
  135. string version = "1.0.0";
  136. return key + "_" + mac + "_" + version;
  137. }
  138. public void deleteAccount(Action<bool> callback)
  139. {
  140. if (UserPlayer.ins.isValid && UserPlayer.ins.loginAuthed)
  141. {
  142. UserPlayer.ins.call("userComp.deleteAccount", null, callback);
  143. }
  144. else
  145. {
  146. callback?.Invoke(false);
  147. }
  148. }
  149. #region 被服务端调用的函数
  150. public void onResumeCalibrateRecord(string record) {
  151. Axis9CalibrateRecord.CacheCalibrateRecord(_lastSaveMac, record);
  152. // AimHandler.ins.ResumeCalibrateRecord(record);
  153. SmartBowDeviceHub.ins.Aim.ResumeCalibrateRecord(record);
  154. }
  155. #endregion
  156. //public static IEnumerator Post(string url, WWWForm form, Action<RequestResult> callback)
  157. //{
  158. // using (UnityWebRequest request = UnityWebRequest.Post(url, form))
  159. // {
  160. // yield return request.SendWebRequest();
  161. // if (request.result == UnityWebRequest.Result.Success)
  162. // callback?.Invoke(JsonConvert.DeserializeObject<RequestResult>(request.downloadHandler.text));
  163. // else
  164. // callback?.Invoke(new RequestResult());
  165. // }
  166. //}
  167. }