using SC.XR.Unity; using System; using System.Collections; using System.Collections.Generic; using System.Text; using UnityEngine; using UnityEngine.Networking; public class AuthenticationNetworkManager : SingletonMono { public void Post(string url, Dictionary header, T data, Action successCallback, Action failCallback, Func encryptMethod = null) { if (AuthenticationManager.Instance.testMode && url.Contains(NetworkApiConstant.SDK_AUTHENTICATION)) { AuthenticationRequestData authenticationRequestData = data as AuthenticationRequestData; switch (authenticationRequestData.channelUserId) { case "200": successCallback?.Invoke(JsonUtility.ToJson(new AuthenticationResponseData() { code = 200, message = "请求成功", data = new AuthenticationData() { distributorUserId = "testdistributorUserId", channelUserId = "200", token = "testToken", data = new ChannelUserInfoData() { nickName = "testNickName", avatar = "testAvatar", extend = "testExtend" } } })); break; case "201": successCallback?.Invoke(JsonUtility.ToJson(new ResponseDataBase() { code = 5101, message = "参数错误" })); break; case "202": successCallback?.Invoke(JsonUtility.ToJson(new ResponseDataBase() { code = 5152, message = "数据库出错" })); break; case "203": successCallback?.Invoke(JsonUtility.ToJson(new ResponseDataBase() { code = 5504, message = "应用不存在" })); break; case "204": successCallback?.Invoke(JsonUtility.ToJson(new ResponseDataBase() { code = 5603, message = "鉴权失败" })); break; case "205": successCallback?.Invoke(JsonUtility.ToJson(new ResponseDataBase() { code = 5501, message = "应用未购买" })); break; case "206": successCallback?.Invoke(JsonUtility.ToJson(new ResponseDataBase() { code = 5602, message = "账号在过多的设备上玩游戏" })); break; case "207": successCallback?.Invoke(JsonUtility.ToJson(new ResponseDataBase() { code = 5601, message = "解密失败" })); break; default: successCallback?.Invoke(JsonUtility.ToJson(new ResponseDataBase() { code = 0, message = "Known" })); break; } return; } string jsonData = JsonUtility.ToJson(data); Debug.Log("EncryptBefore:" + jsonData); string encryptData = encryptMethod != null ? encryptMethod?.Invoke(jsonData) : jsonData; StartCoroutine(PostCoroutine(url, header, encryptData, successCallback, failCallback)); } public void Get(string url, Dictionary header, Dictionary data, Action successCallback, Action failCallback, Func encryptMethod = null) { string jsonData = JsonUtility.ToJson(data); string encryptData = encryptMethod != null ? encryptMethod?.Invoke(jsonData) : jsonData; StartCoroutine(GetCoroutine(url, header, encryptData, successCallback, failCallback)); } private IEnumerator PostCoroutine(string url, Dictionary header, string encryptData, Action successCallback = null, Action failCallback = null) { using (UnityWebRequest web = UnityWebRequest.Post(url, "")) { foreach (var key in header.Keys) { web.SetRequestHeader(key, header[key]); } //Debug.LogError("RequestData:" + encryptData); //把客户端数据传往服务器:给服务器上传我们的数据; web.uploadHandler = new UploadHandlerRaw(Encoding.Default.GetBytes(encryptData != null ? encryptData : "null")); web.timeout = 5; //服务器数据---》客户端 获取服务器返回的数据 web.downloadHandler = new DownloadHandlerBuffer(); //发送一个请求:SendWebRequest异步,你发完后,不用等底层完整的发出去,你就可以做别的;防止卡主游戏线程 yield return web.SendWebRequest(); if (web.isNetworkError || web.isHttpError) { Debug.LogWarning("responseData :" + url + web.error); failCallback?.Invoke(url + " " + web.error); yield break; } else { try { Debug.Log("responseData :" + web.downloadHandler.text); successCallback?.Invoke(web.downloadHandler.text); } catch (Exception e) { Debug.Log("Exception message :" + e.Message); failCallback?.Invoke(e.Message); } } } } private IEnumerator GetCoroutine(string url, Dictionary header, string encryptData, Action successCallback = null, Action failCallback = null) { using (UnityWebRequest web = UnityWebRequest.Get(url)) { foreach (var key in header.Keys) { web.SetRequestHeader(key, header[key]); } //把客户端数据传往服务器:给服务器上传我们的数据; web.uploadHandler = new UploadHandlerRaw(Encoding.Default.GetBytes(encryptData != null ? encryptData : "null")); web.timeout = 5; //服务器数据---》客户端 获取服务器返回的数据 web.downloadHandler = new DownloadHandlerBuffer(); //发送一个请求:SendWebRequest异步,你发完后,不用等底层完整的发出去,你就可以做别的;防止卡主游戏线程 yield return web.SendWebRequest(); if (web.isNetworkError || web.isHttpError) { Debug.LogWarning("RequestData :" + url + web.error); failCallback?.Invoke(url + " " + web.error); yield break; } else { try { Debug.Log("RequestData :" + web.downloadHandler.text); successCallback?.Invoke(web.downloadHandler.text); } catch (Exception e) { Debug.Log("Exception message :" + e.Message); failCallback?.Invoke(e.Message); } } } } }