123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Security.Cryptography;
- using XRTool.Util;
- using LitJson;
- using System;
- using System.Text;
- using System.Linq;
- using ShadowStudio.UI;
- using UnityEngine.Networking;
- using Studio.Scripts.HttpMessage;
- using Studio.Scripts;
- public class NetWorkHeaders : MonoBehaviour
- {
- private List<Network> queue;
- private static bool init;
- public static NetWorkHeaders Instance;
- public static string MD5STR = "sTuDi0jH7FlCgd";
- //public string baseUrl = "http://192.168.60.240:8899/";
- void Awake()
- {
- if (!init)
- {
- init = true;
- Instance = this;
- queue = new List<Network>();
- DontDestroyOnLoad(this.gameObject);
- }
- else
- {
- Destroy(this.gameObject);
- return;
- }
- }
- //private void Update()
- //{
- // if (Input.GetKeyDown(KeyCode.W))
- // {
- // CommonMethod.ShowLoginAbnormalOutPop();
- // }
- //}
- #region CaoTingGolang
- /// <summary>
- /// 图形验证码
- /// </summary>
- public void CaptchaVerificationCode()
- {
- Studio.Scripts.HttpMessage.CaptchaCodeMessage captchaCodeMessage = new Studio.Scripts.HttpMessage.CaptchaCodeMessage();
- DoHttp<Studio.Scripts.HttpMessage.CapthcaCodeCallBackMessage>(captchaCodeMessage);
- }
- /// <summary>
- /// 手机验证码
- /// </summary>
- /// <param name="phoneNumb"></param>
- /// <param name="phoneSMSCodeType"></param>
- public void PhoneVerificationCode(string phoneNumb, PhoneSMSCodeType phoneSMSCodeType)
- {
- Studio.Scripts.HttpMessage.SMSCodeMessage smsCodeMessage = new Studio.Scripts.HttpMessage.SMSCodeMessage();
- smsCodeMessage.body.phone = phoneNumb;
- smsCodeMessage.body.type = (int)phoneSMSCodeType;
- DoHttp<Studio.Scripts.HttpMessage.SMSCodeCallBackMessage>(smsCodeMessage);
- }
- /// <summary>
- /// 用户sn登录
- /// </summary>
- public void SNLogin()
- {
- Studio.Scripts.HttpMessage.SnLoginMessage snLoginMessage = new Studio.Scripts.HttpMessage.SnLoginMessage();
- string sn = DeviceInfo.SN;
- if (string.IsNullOrEmpty(sn))
- {
- sn = "";
- }
- snLoginMessage.body.sn = sn;
- snLoginMessage.body.source = Application.platform.ToString();
- DoHttp<Studio.Scripts.HttpMessage.SnLoginCallBackMessage>(snLoginMessage);
- }
- /// <summary>
- /// 手机验证登录
- /// </summary>
- /// <param name="phone"></param>
- /// <param name="smsCode"></param>
- public void PhoneNumLogin(string phone, string smsCode)
- {
- Studio.Scripts.HttpMessage.PhoneLoginMessage phoneLoginMessage = new Studio.Scripts.HttpMessage.PhoneLoginMessage();
- phoneLoginMessage.body.phone = phone;
- phoneLoginMessage.body.smsCode = smsCode;
- phoneLoginMessage.body.deviceID = string.IsNullOrEmpty(DeviceInfo.SN) ? "Test" : DeviceInfo.SN;
- phoneLoginMessage.body.deviceType = string.IsNullOrEmpty(DeviceInfo.MODEL) ? "Test" : DeviceInfo.MODEL;
- phoneLoginMessage.body.device_os = string.IsNullOrEmpty(DeviceInfo.RELEASE_VERSION) ? "Test" : DeviceInfo.RELEASE_VERSION;
- phoneLoginMessage.body.source = Application.platform.ToString();
- phoneLoginMessage.body.codeType = 2;//目前只有2 登陆验证码
- DoHttp<Studio.Scripts.HttpMessage.PhoneLoginCallBackMessage>(phoneLoginMessage);
- }
- /// <summary>
- /// 用户账号登录
- /// </summary>
- /// <param name="account"></param>
- /// <param name="password"></param>
- /// <param name="captchaID"></param>
- /// <param name="captchaValue"></param>
- public void AccountLogin(string account, string password, string captchaID, string captchaValue, string diallingCode)
- {
- Studio.Scripts.HttpMessage.AccountLoginMessage accountLoginMessage = new Studio.Scripts.HttpMessage.AccountLoginMessage();
- accountLoginMessage.body.account = account;
- accountLoginMessage.body.password = password;
- accountLoginMessage.body.deviceID = string.IsNullOrEmpty(DeviceInfo.SN) ? "Test" : DeviceInfo.SN;
- accountLoginMessage.body.deviceType = string.IsNullOrEmpty(DeviceInfo.MODEL) ? "Test" : DeviceInfo.MODEL;
- accountLoginMessage.body.deviceOs = string.IsNullOrEmpty(DeviceInfo.RELEASE_VERSION) ? "Test" : DeviceInfo.RELEASE_VERSION;
- accountLoginMessage.body.source = Application.platform.ToString();
- accountLoginMessage.body.diallingCode = diallingCode;
- accountLoginMessage.body.captchaID = captchaID;
- accountLoginMessage.body.captchaValue = captchaValue;
- DoHttp<Studio.Scripts.HttpMessage.AccountLoginCallBackMessage>(accountLoginMessage);
- }
- /// <summary>
- /// 顶号登录
- /// </summary>
- /// <param name="cert"></param>
- public void BreakAccountLogin(string cert)
- {
- Studio.Scripts.HttpMessage.BreakLoginMessage breakLoginMessage = new Studio.Scripts.HttpMessage.BreakLoginMessage();
- breakLoginMessage.body.cert = cert;
- DoHttp<Studio.Scripts.HttpMessage.AccountLoginCallBackMessage>(breakLoginMessage);
- }
- /// <summary>
- /// 用户账号预登录
- /// </summary>
- /// <param name="account"></param>
- /// <param name="password"></param>
- public void AdvanceAccount(string account, string password)
- {
- Studio.Scripts.HttpMessage.AdvanceAccountMessage advanceAccountMessage = new Studio.Scripts.HttpMessage.AdvanceAccountMessage();
- advanceAccountMessage.body.account = account;
- advanceAccountMessage.body.password = password;
- advanceAccountMessage.body.deviceID = string.IsNullOrEmpty(DeviceInfo.SN) ? "Test" : DeviceInfo.SN;
- advanceAccountMessage.body.source = Application.platform.ToString();
- DoHttp<Studio.Scripts.HttpMessage.AdvanceAccountCallBackMessage>(advanceAccountMessage);
- }
- /// <summary>
- /// 获取头像列表
- /// </summary>
- /// <param name="token"></param>
- public void GetUserAvatar(string token)
- {
- GetAvatarListMessage getAvatarListMessage = new GetAvatarListMessage();
- getAvatarListMessage.token = token; //"262773181c66240b1529c3e2ebe076b1";
- //getAvatarListMessage.body.source = Application.platform.ToString();
- DoHttp<Studio.Scripts.HttpMessage.GetAvatarListCallBackMessage>(getAvatarListMessage);
- }
- /// <summary>
- /// 用户信息修改
- /// </summary>
- /// <param name="token"></param>
- /// <param name="avatar"></param>
- /// <param name="nickName"></param>
- public void ModifyRole(string token, string avatar, string nickName)
- {
- ModifyRoleMessage modifyRoleMessage = new ModifyRoleMessage();
- modifyRoleMessage.token = token;
- modifyRoleMessage.body.avatar = avatar;
- modifyRoleMessage.body.nickName = nickName;
- DoHttp<Studio.Scripts.HttpMessage.ModifyRoleCallBackMessage>(modifyRoleMessage);
- }
- /// <summary>
- /// 退出
- /// </summary>
- public void LogOut(string token)
- {
- Studio.Scripts.HttpMessage.LogOutMessage logOutMessage = new Studio.Scripts.HttpMessage.LogOutMessage();
- logOutMessage.body.source = Application.platform.ToString();
- logOutMessage.token = token; //"262773181c66240b1529c3e2ebe076b1";
- DoHttp<Studio.Scripts.HttpMessage.LogOutCallBackMessage>(logOutMessage);
- }
- /// <summary>
- /// 创建房间
- /// </summary>
- /// <param name="roomType">房间类型1 是个人房间 2是公共房间</param>
- /// <param name="roomName"></param>
- /// <param name="roomExplain"></param>
- /// <param name="roomPassword"></param>
- /// <param name="maxMember"></param>
- /// <param name="publicRoomType">公开房间类型 1 是只能浏览 2是 全功能</param>
- public void CreateRoom(string token, int roomType, string roomName = "", string roomExplain = "", string roomPassword = "", int maxMember = 1, int publicRoomType = 1)
- {
- Studio.Scripts.HttpMessage.CreateRoomMessage createRoomMessage = new Studio.Scripts.HttpMessage.CreateRoomMessage();
- createRoomMessage.body.type = roomType;
- createRoomMessage.body.roomName = roomName;
- createRoomMessage.body.roomExplain = roomExplain;
- createRoomMessage.body.roomPassword = roomPassword;
- createRoomMessage.body.roomType = publicRoomType;
- createRoomMessage.token = token;// "262773181c66240b1529c3e2ebe076b1";
- createRoomMessage.body.maxMember = maxMember;
- DoHttp<Studio.Scripts.HttpMessage.CreateRoomCallBackMessage>(createRoomMessage);
- }
- /// <summary>
- /// 删除房间
- /// </summary>
- /// <param name="roomID"></param>
- public void DeleteRoom(int roomID, string token)
- {
- Studio.Scripts.HttpMessage.DeleteRoomMessage deleteRoomMessage = new Studio.Scripts.HttpMessage.DeleteRoomMessage();
- deleteRoomMessage.body.roomID = roomID;
- deleteRoomMessage.token = token;// "262773181c66240b1529c3e2ebe076b1";
- DoHttp<Studio.Scripts.HttpMessage.DeleteRoomCallBackMessage>(deleteRoomMessage);
- }
- /// <summary>
- /// 加入房间
- /// </summary>
- /// <param name="roomID"></param>
- public void JoinedRoom(int roomID)
- {
- Studio.Scripts.HttpMessage.JoinedRoomMessage joinedRoomMessage = new Studio.Scripts.HttpMessage.JoinedRoomMessage();
- joinedRoomMessage.body.roomID = roomID;
- joinedRoomMessage.token = "262773181c66240b1529c3e2ebe076b1";
- DoHttp<Studio.Scripts.HttpMessage.JoinedRoomCallBackMessage>(joinedRoomMessage);
- }
- /// <summary>
- /// 房间列表
- /// </summary>
- public void GetRoomList()
- {
- Studio.Scripts.HttpMessage.RoomListMessage roomListMessage = new Studio.Scripts.HttpMessage.RoomListMessage();
- roomListMessage.token = "262773181c66240b1529c3e2ebe076b1";
- DoHttp<Studio.Scripts.HttpMessage.RoomListCallBackMessage>(roomListMessage);
- }
- /// <summary>
- /// 更新房间
- /// </summary>
- /// <param name="roomID"></param>
- /// <param name="roomType"></param>
- /// <param name="roomName"></param>
- /// <param name="roomExplain"></param>
- /// <param name="roomPassword"></param>
- /// <param name="maxMember"></param>
- /// <param name="publicRoomType"></param>
- public void UpdateRoom(int roomID, int roomType, string roomName = "", string roomExplain = "", string roomPassword = "", int maxMember = 10, int publicRoomType = 1)
- {
- Studio.Scripts.HttpMessage.UpdateRoomMessage updateRoomMessage = new Studio.Scripts.HttpMessage.UpdateRoomMessage();
- updateRoomMessage.body.roomID = roomID;
- updateRoomMessage.body.type = roomType;
- updateRoomMessage.body.roomName = roomName;
- updateRoomMessage.body.roomExplain = roomExplain;
- updateRoomMessage.body.roomPassword = roomPassword;
- updateRoomMessage.body.maxMember = maxMember;
- updateRoomMessage.body.roomType = publicRoomType;
- updateRoomMessage.token = "262773181c66240b1529c3e2ebe076b1";
- DoHttp<Studio.Scripts.HttpMessage.UpdateRoomCallBackMessage>(updateRoomMessage);
- }
- /// <summary>
- /// 修改设置
- /// </summary>
- /// <param name="isOpenCamera"></param>
- /// <param name="isOpenMic"></param>
- /// <param name="ratioType"></param>
- /// <param name="fpsType"></param>
- public void UpdateSettings(string token, int fmss)
- {
- Studio.Scripts.HttpMessage.UpdateSettingsMessage updateSettingsMessage = new Studio.Scripts.HttpMessage.UpdateSettingsMessage();
- updateSettingsMessage.token = token;// "262773181c66240b1529c3e2ebe076b1";
- updateSettingsMessage.body.fmss = fmss;
- DoHttp<Studio.Scripts.HttpMessage.UpdateSettingsCallBackMessage>(updateSettingsMessage);
- }
- /// <summary>
- /// 修改设置
- /// </summary>
- /// <param name="isOpenCamera"></param>
- /// <param name="isOpenMic"></param>
- /// <param name="ratioType"></param>
- /// <param name="fpsType"></param>
- public void UpdateSettings(string token, bool isOpenCamera = false, bool isOpenMic = false, RatioType ratioType = RatioType.Low, FpsType fpsType = FpsType.Middle)
- {
- Studio.Scripts.HttpMessage.UpdateSettingsMessage updateSettingsMessage = new Studio.Scripts.HttpMessage.UpdateSettingsMessage();
- updateSettingsMessage.token = token;// "262773181c66240b1529c3e2ebe076b1";
- updateSettingsMessage.body.camera = isOpenCamera;
- updateSettingsMessage.body.mic = isOpenMic;
- updateSettingsMessage.body.fps = (int)fpsType;
- updateSettingsMessage.body.ratio = (int)ratioType;
- DoHttp<Studio.Scripts.HttpMessage.UpdateSettingsCallBackMessage>(updateSettingsMessage);
- }
- /// <summary>
- /// 获取我的设置
- /// </summary>
- public void GetSettings(string token)
- {
- Studio.Scripts.HttpMessage.GetSettingsMessage getSettingsMessage = new Studio.Scripts.HttpMessage.GetSettingsMessage();
- getSettingsMessage.token = token;// "262773181c66240b1529c3e2ebe076b1";
- // getSettingsMessage.body.roomID = roomID;
- DoHttp<Studio.Scripts.HttpMessage.GetSettingsCallBackMessage>(getSettingsMessage);
- }
- /// <summary>
- /// 获取资源列表
- /// </summary>
- /// <param name="token"></param>
- /// <param name="searchContent"></param>
- public void GetArtList(string token, int artType)
- {
- GetArtListMessage getArtListMessage = new GetArtListMessage();
- getArtListMessage.token = token;
- getArtListMessage.body.artType = artType;
- DoHttp<Studio.Scripts.HttpMessage.GetArtListCallBackMessage>(getArtListMessage);
- }
- public void SearchArtList(string token, int artType, int fileType,string keywords = "")
- {
- SearchArtListMessage searchArtListMessage = new SearchArtListMessage();
- searchArtListMessage.token = token;
- searchArtListMessage.body.artType = artType;
- searchArtListMessage.body.fileType = fileType;
- searchArtListMessage.body.keywords = keywords;
- DoHttp<Studio.Scripts.HttpMessage.SearchArtListCallBackMessage>(searchArtListMessage);
- }
- /// <summary>
- /// 获取详细资源
- /// </summary>
- /// <param name="token"></param>
- /// <param name="aid"></param>
- public void GetDetailAir(string token, string aid)
- {
- GetArtDetailMessage getArtDetailMessage = new GetArtDetailMessage();
- getArtDetailMessage.token = token;
- getArtDetailMessage.body.ArtID = aid;
- DoHttp<GetArtDetailCallBackMessage>(getArtDetailMessage);
- }
- /// <summary>
- /// 文件存储地址获取
- /// </summary>
- /// <param name="token"></param>
- public void GetFileAddress(string token)
- {
- FileAddressMessage fileAddressMessage = new FileAddressMessage();
- fileAddressMessage.token = token;
- DoHttp<FileAddressCallBackMessage>(fileAddressMessage);
- }
- private void DoHttp<T>(BaseHttpMessage httpMessage) where T : Studio.Scripts.HttpMessage.BaseHttpCallBackMessage
- {
- DoPost(httpMessage, delegate (bool isConnected, string content)
- {
- if (isConnected)
- {
- if (!string.IsNullOrEmpty(content))
- {
- UnityLog.Instance.Log("content:" + content);
- BaseHttpCallBackMessage httpHead = JsonUtility.FromJson<T>(content);
- UnityLog.Instance.Log("httpHead TYPE:" + httpHead .MessageType);
- /// 正常接口返回数据只需要解析头部数据,包含错误码,错误信息,如果是200 则解析data数据,错误码非200,则不需要解析data数据
- /// 返回正确
- if (httpHead.code == 200)
- {
- T callBackMessage = JsonUtility.FromJson<T>(content);
- //UnityLog.Instance.Log(Newtonsoft.Json.JsonConvert.SerializeObject(callBackMessage));
- HTTPDistribute.Instance.Distribute(callBackMessage);
- }
- else
- {
- // 账号登录 特殊处理,错误码非200的情况下也需要解析data数据
- if (httpHead.MessageType == MessageType.AccountLoginType || httpHead.MessageType == MessageType.PhoneLoginType || httpHead.MessageType == MessageType.SearchArtList)
- {
- T callBackMessage = JsonUtility.FromJson<T>(content);
- HTTPDistribute.Instance.DistributeError(callBackMessage);
- }
- else if (httpHead.MessageType == MessageType.GetAllArtType)
- {
- Debug.Log("资源不存在");
- }
- else
- {
- HTTPDistribute.Instance.DistributeError(httpHead);
- CommonMethod.ShowTextTusi();
- TuSi.Instance.ShowTextContent(httpHead.message.ToString());
- UnityLog.Instance.Log(httpHead.message.ToString());
- }
- }
- }
- }
- else
- {
- UnityLog.Instance.Log("net error:" + content.ToString());
- CommonMethod.ShowTextTusi();
- if (TuSi.Instance)
- {
- TuSi.Instance.ShowTextContent("net error:" + content.ToString());
- //LanguageMgr.Instance.GetMessage("1056").Message
- }
- // Debug.LogError("net work error:" + content);
- }
- });
- }
- /// <summary>
- /// caoting 2021.5.7 DoPost
- /// </summary>
- /// <param name="httpMessage"></param>
- /// <param name="callback"></param>
- private void DoPost(Studio.Scripts.HttpMessage.BaseHttpMessage httpMessage, Action<bool, string> callback)
- {
- string webMethod = "POST";
- switch (httpMessage.methodType)
- {
- case HttpMethodType.None:
- break;
- case HttpMethodType.Post:
- webMethod = UnityWebRequest.kHttpVerbPOST;
- break;
- case HttpMethodType.Get:
- webMethod = UnityWebRequest.kHttpVerbGET;
- break;
- default:
- break;
- }
- StartCoroutine(HttpRequest(httpMessage.path, webMethod, httpMessage.headers, httpMessage.ToBodyString(), httpMessage.wwwForm, callback));
- }
- /// <summary>
- /// caoting 2021.5.7 post
- /// </summary>
- /// <param name="methodName"></param>
- /// <param name="callback"></param>
- /// <returns></returns>
- private IEnumerator HttpRequest(string methodName, string webMethod, Dictionary<string, string> requestHeader, string parameters, WWWForm wwwForm, Action<bool, string> callback)
- {
- string url = SpaceHttpAction.Instance.loginUrl + methodName;
- UnityLog.Instance.Log($"parameters:{parameters} url { url} webMethod {webMethod}");
- using (UnityWebRequest webRequest = new UnityWebRequest(url, webMethod))
- {
- if (webMethod == UnityWebRequest.kHttpVerbPOST && !string.IsNullOrEmpty(parameters))
- {
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(System.Text.Encoding.Default.GetBytes(parameters));
- }
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
- foreach (var v in requestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- if (callback != null)
- {
- callback(false, webRequest.error);
- }
- }
- else
- {
- if (callback != null)
- {
- callback(true, webRequest.downloadHandler.text);
- }
- }
- }
- }
- #endregion
- }
|