AuthenticationNetworkManager.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using SC.XR.Unity;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using UnityEngine;
  7. using UnityEngine.Networking;
  8. public class AuthenticationNetworkManager : SingletonMono<AuthenticationNetworkManager>
  9. {
  10. public void Post<T>(string url, Dictionary<string, string> header, T data, Action<string> successCallback, Action<string> failCallback, Func<string, string> encryptMethod = null)
  11. {
  12. if (AuthenticationManager.Instance.testMode && url.Contains(NetworkApiConstant.SDK_AUTHENTICATION))
  13. {
  14. AuthenticationRequestData authenticationRequestData = data as AuthenticationRequestData;
  15. switch (authenticationRequestData.channelUserId)
  16. {
  17. case "200":
  18. successCallback?.Invoke(JsonUtility.ToJson(new AuthenticationResponseData()
  19. {
  20. code = 200,
  21. message = "请求成功",
  22. data = new AuthenticationData()
  23. {
  24. distributorUserId = "testdistributorUserId",
  25. channelUserId = "200",
  26. token = "testToken",
  27. data = new ChannelUserInfoData()
  28. {
  29. nickName = "testNickName",
  30. avatar = "testAvatar",
  31. extend = "testExtend"
  32. }
  33. }
  34. }));
  35. break;
  36. case "201":
  37. successCallback?.Invoke(JsonUtility.ToJson(new ResponseDataBase()
  38. {
  39. code = 5101,
  40. message = "参数错误"
  41. }));
  42. break;
  43. case "202":
  44. successCallback?.Invoke(JsonUtility.ToJson(new ResponseDataBase()
  45. {
  46. code = 5152,
  47. message = "数据库出错"
  48. }));
  49. break;
  50. case "203":
  51. successCallback?.Invoke(JsonUtility.ToJson(new ResponseDataBase()
  52. {
  53. code = 5504,
  54. message = "应用不存在"
  55. }));
  56. break;
  57. case "204":
  58. successCallback?.Invoke(JsonUtility.ToJson(new ResponseDataBase()
  59. {
  60. code = 5603,
  61. message = "鉴权失败"
  62. }));
  63. break;
  64. case "205":
  65. successCallback?.Invoke(JsonUtility.ToJson(new ResponseDataBase()
  66. {
  67. code = 5501,
  68. message = "应用未购买"
  69. }));
  70. break;
  71. case "206":
  72. successCallback?.Invoke(JsonUtility.ToJson(new ResponseDataBase()
  73. {
  74. code = 5602,
  75. message = "账号在过多的设备上玩游戏"
  76. }));
  77. break;
  78. case "207":
  79. successCallback?.Invoke(JsonUtility.ToJson(new ResponseDataBase()
  80. {
  81. code = 5601,
  82. message = "解密失败"
  83. }));
  84. break;
  85. default:
  86. successCallback?.Invoke(JsonUtility.ToJson(new ResponseDataBase()
  87. {
  88. code = 0,
  89. message = "Known"
  90. }));
  91. break;
  92. }
  93. return;
  94. }
  95. string jsonData = JsonUtility.ToJson(data);
  96. Debug.Log("EncryptBefore:" + jsonData);
  97. string encryptData = encryptMethod != null ? encryptMethod?.Invoke(jsonData) : jsonData;
  98. StartCoroutine(PostCoroutine(url, header, encryptData, successCallback, failCallback));
  99. }
  100. public void Get<T>(string url, Dictionary<string, string> header, Dictionary<string, string> data, Action<string> successCallback, Action<string> failCallback, Func<string, string> encryptMethod = null)
  101. {
  102. string jsonData = JsonUtility.ToJson(data);
  103. string encryptData = encryptMethod != null ? encryptMethod?.Invoke(jsonData) : jsonData;
  104. StartCoroutine(GetCoroutine(url, header, encryptData, successCallback, failCallback));
  105. }
  106. private IEnumerator PostCoroutine(string url, Dictionary<string, string> header, string encryptData, Action<string> successCallback = null, Action<string> failCallback = null)
  107. {
  108. using (UnityWebRequest web = UnityWebRequest.Post(url, ""))
  109. {
  110. foreach (var key in header.Keys)
  111. {
  112. web.SetRequestHeader(key, header[key]);
  113. }
  114. //Debug.LogError("RequestData:" + encryptData);
  115. //把客户端数据传往服务器:给服务器上传我们的数据;
  116. web.uploadHandler = new UploadHandlerRaw(Encoding.Default.GetBytes(encryptData != null ? encryptData : "null"));
  117. web.timeout = 5;
  118. //服务器数据---》客户端 获取服务器返回的数据
  119. web.downloadHandler = new DownloadHandlerBuffer();
  120. //发送一个请求:SendWebRequest异步,你发完后,不用等底层完整的发出去,你就可以做别的;防止卡主游戏线程
  121. yield return web.SendWebRequest();
  122. if (web.isNetworkError || web.isHttpError)
  123. {
  124. Debug.LogWarning("responseData :" + url + web.error);
  125. failCallback?.Invoke(url + " " + web.error);
  126. yield break;
  127. }
  128. else
  129. {
  130. try
  131. {
  132. Debug.Log("responseData :" + web.downloadHandler.text);
  133. successCallback?.Invoke(web.downloadHandler.text);
  134. }
  135. catch (Exception e)
  136. {
  137. Debug.Log("Exception message :" + e.Message);
  138. failCallback?.Invoke(e.Message);
  139. }
  140. }
  141. }
  142. }
  143. private IEnumerator GetCoroutine(string url, Dictionary<string, string> header, string encryptData, Action<string> successCallback = null, Action<string> failCallback = null)
  144. {
  145. using (UnityWebRequest web = UnityWebRequest.Get(url))
  146. {
  147. foreach (var key in header.Keys)
  148. {
  149. web.SetRequestHeader(key, header[key]);
  150. }
  151. //把客户端数据传往服务器:给服务器上传我们的数据;
  152. web.uploadHandler = new UploadHandlerRaw(Encoding.Default.GetBytes(encryptData != null ? encryptData : "null"));
  153. web.timeout = 5;
  154. //服务器数据---》客户端 获取服务器返回的数据
  155. web.downloadHandler = new DownloadHandlerBuffer();
  156. //发送一个请求:SendWebRequest异步,你发完后,不用等底层完整的发出去,你就可以做别的;防止卡主游戏线程
  157. yield return web.SendWebRequest();
  158. if (web.isNetworkError || web.isHttpError)
  159. {
  160. Debug.LogWarning("RequestData :" + url + web.error);
  161. failCallback?.Invoke(url + " " + web.error);
  162. yield break;
  163. }
  164. else
  165. {
  166. try
  167. {
  168. Debug.Log("RequestData :" + web.downloadHandler.text);
  169. successCallback?.Invoke(web.downloadHandler.text);
  170. }
  171. catch (Exception e)
  172. {
  173. Debug.Log("Exception message :" + e.Message);
  174. failCallback?.Invoke(e.Message);
  175. }
  176. }
  177. }
  178. }
  179. }