123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- 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<AuthenticationNetworkManager>
- {
- public void Post<T>(string url, Dictionary<string, string> header, T data, Action<string> successCallback, Action<string> failCallback, Func<string, string> 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<T>(string url, Dictionary<string, string> header, Dictionary<string, string> data, Action<string> successCallback, Action<string> failCallback, Func<string, string> 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<string, string> header, string encryptData, Action<string> successCallback = null, Action<string> 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<string, string> header, string encryptData, Action<string> successCallback = null, Action<string> 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);
- }
- }
- }
- }
- }
|