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 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(); DontDestroyOnLoad(this.gameObject); } else { Destroy(this.gameObject); return; } } //private void Update() //{ // if (Input.GetKeyDown(KeyCode.W)) // { // CommonMethod.ShowLoginAbnormalOutPop(); // } //} #region CaoTingGolang /// /// 图形验证码 /// public void CaptchaVerificationCode() { Studio.Scripts.HttpMessage.CaptchaCodeMessage captchaCodeMessage = new Studio.Scripts.HttpMessage.CaptchaCodeMessage(); DoHttp(captchaCodeMessage); } /// /// 手机验证码 /// /// /// 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(smsCodeMessage); } /// /// 用户sn登录 /// 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(snLoginMessage); } /// /// 手机验证登录 /// /// /// 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(phoneLoginMessage); } /// /// 用户账号登录 /// /// /// /// /// 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(accountLoginMessage); } /// /// 顶号登录 /// /// public void BreakAccountLogin(string cert) { Studio.Scripts.HttpMessage.BreakLoginMessage breakLoginMessage = new Studio.Scripts.HttpMessage.BreakLoginMessage(); breakLoginMessage.body.cert = cert; DoHttp(breakLoginMessage); } /// /// 用户账号预登录 /// /// /// 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(advanceAccountMessage); } /// /// 获取头像列表 /// /// public void GetUserAvatar(string token) { GetAvatarListMessage getAvatarListMessage = new GetAvatarListMessage(); getAvatarListMessage.token = token; //"262773181c66240b1529c3e2ebe076b1"; //getAvatarListMessage.body.source = Application.platform.ToString(); DoHttp(getAvatarListMessage); } /// /// 用户信息修改 /// /// /// /// 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(modifyRoleMessage); } /// /// 退出 /// 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(logOutMessage); } /// /// 创建房间 /// /// 房间类型1 是个人房间 2是公共房间 /// /// /// /// /// 公开房间类型 1 是只能浏览 2是 全功能 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(createRoomMessage); } /// /// 删除房间 /// /// 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(deleteRoomMessage); } /// /// 加入房间 /// /// public void JoinedRoom(int roomID) { Studio.Scripts.HttpMessage.JoinedRoomMessage joinedRoomMessage = new Studio.Scripts.HttpMessage.JoinedRoomMessage(); joinedRoomMessage.body.roomID = roomID; joinedRoomMessage.token = "262773181c66240b1529c3e2ebe076b1"; DoHttp(joinedRoomMessage); } /// /// 房间列表 /// public void GetRoomList() { Studio.Scripts.HttpMessage.RoomListMessage roomListMessage = new Studio.Scripts.HttpMessage.RoomListMessage(); roomListMessage.token = "262773181c66240b1529c3e2ebe076b1"; DoHttp(roomListMessage); } /// /// 更新房间 /// /// /// /// /// /// /// /// 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(updateRoomMessage); } /// /// 修改设置 /// /// /// /// /// 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(updateSettingsMessage); } /// /// 修改设置 /// /// /// /// /// 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(updateSettingsMessage); } /// /// 获取我的设置 /// public void GetSettings(string token) { Studio.Scripts.HttpMessage.GetSettingsMessage getSettingsMessage = new Studio.Scripts.HttpMessage.GetSettingsMessage(); getSettingsMessage.token = token;// "262773181c66240b1529c3e2ebe076b1"; // getSettingsMessage.body.roomID = roomID; DoHttp(getSettingsMessage); } /// /// 获取资源列表 /// /// /// public void GetArtList(string token, int artType) { GetArtListMessage getArtListMessage = new GetArtListMessage(); getArtListMessage.token = token; getArtListMessage.body.artType = artType; DoHttp(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(searchArtListMessage); } /// /// 获取详细资源 /// /// /// public void GetDetailAir(string token, string aid) { GetArtDetailMessage getArtDetailMessage = new GetArtDetailMessage(); getArtDetailMessage.token = token; getArtDetailMessage.body.ArtID = aid; DoHttp(getArtDetailMessage); } /// /// 文件存储地址获取 /// /// public void GetFileAddress(string token) { FileAddressMessage fileAddressMessage = new FileAddressMessage(); fileAddressMessage.token = token; DoHttp(fileAddressMessage); } private void DoHttp(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(content); UnityLog.Instance.Log("httpHead TYPE:" + httpHead .MessageType); /// 正常接口返回数据只需要解析头部数据,包含错误码,错误信息,如果是200 则解析data数据,错误码非200,则不需要解析data数据 /// 返回正确 if (httpHead.code == 200) { T callBackMessage = JsonUtility.FromJson(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(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); } }); } /// /// caoting 2021.5.7 DoPost /// /// /// private void DoPost(Studio.Scripts.HttpMessage.BaseHttpMessage httpMessage, Action 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)); } /// /// caoting 2021.5.7 post /// /// /// /// private IEnumerator HttpRequest(string methodName, string webMethod, Dictionary requestHeader, string parameters, WWWForm wwwForm, Action callback) { string url = BuildConfig.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 }