NetWorkHeaders.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Security.Cryptography;
  5. using XRTool.Util;
  6. using LitJson;
  7. using System;
  8. using System.Text;
  9. using System.Linq;
  10. using ShadowStudio.UI;
  11. using UnityEngine.Networking;
  12. using Studio.Scripts.HttpMessage;
  13. using Studio.Scripts;
  14. public class NetWorkHeaders : MonoBehaviour
  15. {
  16. private List<Network> queue;
  17. private static bool init;
  18. public static NetWorkHeaders Instance;
  19. public static string MD5STR = "sTuDi0jH7FlCgd";
  20. //public string baseUrl = "http://192.168.60.240:8899/";
  21. void Awake()
  22. {
  23. if (!init)
  24. {
  25. init = true;
  26. Instance = this;
  27. queue = new List<Network>();
  28. DontDestroyOnLoad(this.gameObject);
  29. }
  30. else
  31. {
  32. Destroy(this.gameObject);
  33. return;
  34. }
  35. }
  36. //private void Update()
  37. //{
  38. // if (Input.GetKeyDown(KeyCode.W))
  39. // {
  40. // CommonMethod.ShowLoginAbnormalOutPop();
  41. // }
  42. //}
  43. #region CaoTingGolang
  44. /// <summary>
  45. /// 图形验证码
  46. /// </summary>
  47. public void CaptchaVerificationCode()
  48. {
  49. Studio.Scripts.HttpMessage.CaptchaCodeMessage captchaCodeMessage = new Studio.Scripts.HttpMessage.CaptchaCodeMessage();
  50. DoHttp<Studio.Scripts.HttpMessage.CapthcaCodeCallBackMessage>(captchaCodeMessage);
  51. }
  52. /// <summary>
  53. /// 手机验证码
  54. /// </summary>
  55. /// <param name="phoneNumb"></param>
  56. /// <param name="phoneSMSCodeType"></param>
  57. public void PhoneVerificationCode(string phoneNumb, PhoneSMSCodeType phoneSMSCodeType)
  58. {
  59. Studio.Scripts.HttpMessage.SMSCodeMessage smsCodeMessage = new Studio.Scripts.HttpMessage.SMSCodeMessage();
  60. smsCodeMessage.body.phone = phoneNumb;
  61. smsCodeMessage.body.type = (int)phoneSMSCodeType;
  62. DoHttp<Studio.Scripts.HttpMessage.SMSCodeCallBackMessage>(smsCodeMessage);
  63. }
  64. /// <summary>
  65. /// 用户sn登录
  66. /// </summary>
  67. public void SNLogin()
  68. {
  69. Studio.Scripts.HttpMessage.SnLoginMessage snLoginMessage = new Studio.Scripts.HttpMessage.SnLoginMessage();
  70. string sn = DeviceInfo.SN;
  71. if (string.IsNullOrEmpty(sn))
  72. {
  73. sn = "";
  74. }
  75. snLoginMessage.body.sn = sn;
  76. snLoginMessage.body.source = Application.platform.ToString();
  77. DoHttp<Studio.Scripts.HttpMessage.SnLoginCallBackMessage>(snLoginMessage);
  78. }
  79. /// <summary>
  80. /// 手机验证登录
  81. /// </summary>
  82. /// <param name="phone"></param>
  83. /// <param name="smsCode"></param>
  84. public void PhoneNumLogin(string phone, string smsCode)
  85. {
  86. Studio.Scripts.HttpMessage.PhoneLoginMessage phoneLoginMessage = new Studio.Scripts.HttpMessage.PhoneLoginMessage();
  87. phoneLoginMessage.body.phone = phone;
  88. phoneLoginMessage.body.smsCode = smsCode;
  89. phoneLoginMessage.body.deviceID = string.IsNullOrEmpty(DeviceInfo.SN) ? "Test" : DeviceInfo.SN;
  90. phoneLoginMessage.body.deviceType = string.IsNullOrEmpty(DeviceInfo.MODEL) ? "Test" : DeviceInfo.MODEL;
  91. phoneLoginMessage.body.device_os = string.IsNullOrEmpty(DeviceInfo.RELEASE_VERSION) ? "Test" : DeviceInfo.RELEASE_VERSION;
  92. phoneLoginMessage.body.source = Application.platform.ToString();
  93. phoneLoginMessage.body.codeType = 2;//目前只有2 登陆验证码
  94. DoHttp<Studio.Scripts.HttpMessage.PhoneLoginCallBackMessage>(phoneLoginMessage);
  95. }
  96. /// <summary>
  97. /// 用户账号登录
  98. /// </summary>
  99. /// <param name="account"></param>
  100. /// <param name="password"></param>
  101. /// <param name="captchaID"></param>
  102. /// <param name="captchaValue"></param>
  103. public void AccountLogin(string account, string password, string captchaID, string captchaValue, string diallingCode)
  104. {
  105. Studio.Scripts.HttpMessage.AccountLoginMessage accountLoginMessage = new Studio.Scripts.HttpMessage.AccountLoginMessage();
  106. accountLoginMessage.body.account = account;
  107. accountLoginMessage.body.password = password;
  108. accountLoginMessage.body.deviceID = string.IsNullOrEmpty(DeviceInfo.SN) ? "Test" : DeviceInfo.SN;
  109. accountLoginMessage.body.deviceType = string.IsNullOrEmpty(DeviceInfo.MODEL) ? "Test" : DeviceInfo.MODEL;
  110. accountLoginMessage.body.deviceOs = string.IsNullOrEmpty(DeviceInfo.RELEASE_VERSION) ? "Test" : DeviceInfo.RELEASE_VERSION;
  111. accountLoginMessage.body.source = Application.platform.ToString();
  112. accountLoginMessage.body.diallingCode = diallingCode;
  113. accountLoginMessage.body.captchaID = captchaID;
  114. accountLoginMessage.body.captchaValue = captchaValue;
  115. DoHttp<Studio.Scripts.HttpMessage.AccountLoginCallBackMessage>(accountLoginMessage);
  116. }
  117. /// <summary>
  118. /// 顶号登录
  119. /// </summary>
  120. /// <param name="cert"></param>
  121. public void BreakAccountLogin(string cert)
  122. {
  123. Studio.Scripts.HttpMessage.BreakLoginMessage breakLoginMessage = new Studio.Scripts.HttpMessage.BreakLoginMessage();
  124. breakLoginMessage.body.cert = cert;
  125. DoHttp<Studio.Scripts.HttpMessage.AccountLoginCallBackMessage>(breakLoginMessage);
  126. }
  127. /// <summary>
  128. /// 用户账号预登录
  129. /// </summary>
  130. /// <param name="account"></param>
  131. /// <param name="password"></param>
  132. public void AdvanceAccount(string account, string password)
  133. {
  134. Studio.Scripts.HttpMessage.AdvanceAccountMessage advanceAccountMessage = new Studio.Scripts.HttpMessage.AdvanceAccountMessage();
  135. advanceAccountMessage.body.account = account;
  136. advanceAccountMessage.body.password = password;
  137. advanceAccountMessage.body.deviceID = string.IsNullOrEmpty(DeviceInfo.SN) ? "Test" : DeviceInfo.SN;
  138. advanceAccountMessage.body.source = Application.platform.ToString();
  139. DoHttp<Studio.Scripts.HttpMessage.AdvanceAccountCallBackMessage>(advanceAccountMessage);
  140. }
  141. /// <summary>
  142. /// 获取头像列表
  143. /// </summary>
  144. /// <param name="token"></param>
  145. public void GetUserAvatar(string token)
  146. {
  147. GetAvatarListMessage getAvatarListMessage = new GetAvatarListMessage();
  148. getAvatarListMessage.token = token; //"262773181c66240b1529c3e2ebe076b1";
  149. //getAvatarListMessage.body.source = Application.platform.ToString();
  150. DoHttp<Studio.Scripts.HttpMessage.GetAvatarListCallBackMessage>(getAvatarListMessage);
  151. }
  152. /// <summary>
  153. /// 用户信息修改
  154. /// </summary>
  155. /// <param name="token"></param>
  156. /// <param name="avatar"></param>
  157. /// <param name="nickName"></param>
  158. public void ModifyRole(string token, string avatar, string nickName)
  159. {
  160. ModifyRoleMessage modifyRoleMessage = new ModifyRoleMessage();
  161. modifyRoleMessage.token = token;
  162. modifyRoleMessage.body.avatar = avatar;
  163. modifyRoleMessage.body.nickName = nickName;
  164. DoHttp<Studio.Scripts.HttpMessage.ModifyRoleCallBackMessage>(modifyRoleMessage);
  165. }
  166. /// <summary>
  167. /// 退出
  168. /// </summary>
  169. public void LogOut(string token)
  170. {
  171. Studio.Scripts.HttpMessage.LogOutMessage logOutMessage = new Studio.Scripts.HttpMessage.LogOutMessage();
  172. logOutMessage.body.source = Application.platform.ToString();
  173. logOutMessage.token = token; //"262773181c66240b1529c3e2ebe076b1";
  174. DoHttp<Studio.Scripts.HttpMessage.LogOutCallBackMessage>(logOutMessage);
  175. }
  176. /// <summary>
  177. /// 创建房间
  178. /// </summary>
  179. /// <param name="roomType">房间类型1 是个人房间 2是公共房间</param>
  180. /// <param name="roomName"></param>
  181. /// <param name="roomExplain"></param>
  182. /// <param name="roomPassword"></param>
  183. /// <param name="maxMember"></param>
  184. /// <param name="publicRoomType">公开房间类型 1 是只能浏览 2是 全功能</param>
  185. public void CreateRoom(string token, int roomType, string roomName = "", string roomExplain = "", string roomPassword = "", int maxMember = 1, int publicRoomType = 1)
  186. {
  187. Studio.Scripts.HttpMessage.CreateRoomMessage createRoomMessage = new Studio.Scripts.HttpMessage.CreateRoomMessage();
  188. createRoomMessage.body.type = roomType;
  189. createRoomMessage.body.roomName = roomName;
  190. createRoomMessage.body.roomExplain = roomExplain;
  191. createRoomMessage.body.roomPassword = roomPassword;
  192. createRoomMessage.body.roomType = publicRoomType;
  193. createRoomMessage.token = token;// "262773181c66240b1529c3e2ebe076b1";
  194. createRoomMessage.body.maxMember = maxMember;
  195. DoHttp<Studio.Scripts.HttpMessage.CreateRoomCallBackMessage>(createRoomMessage);
  196. }
  197. /// <summary>
  198. /// 删除房间
  199. /// </summary>
  200. /// <param name="roomID"></param>
  201. public void DeleteRoom(int roomID, string token)
  202. {
  203. Studio.Scripts.HttpMessage.DeleteRoomMessage deleteRoomMessage = new Studio.Scripts.HttpMessage.DeleteRoomMessage();
  204. deleteRoomMessage.body.roomID = roomID;
  205. deleteRoomMessage.token = token;// "262773181c66240b1529c3e2ebe076b1";
  206. DoHttp<Studio.Scripts.HttpMessage.DeleteRoomCallBackMessage>(deleteRoomMessage);
  207. }
  208. /// <summary>
  209. /// 加入房间
  210. /// </summary>
  211. /// <param name="roomID"></param>
  212. public void JoinedRoom(int roomID)
  213. {
  214. Studio.Scripts.HttpMessage.JoinedRoomMessage joinedRoomMessage = new Studio.Scripts.HttpMessage.JoinedRoomMessage();
  215. joinedRoomMessage.body.roomID = roomID;
  216. joinedRoomMessage.token = "262773181c66240b1529c3e2ebe076b1";
  217. DoHttp<Studio.Scripts.HttpMessage.JoinedRoomCallBackMessage>(joinedRoomMessage);
  218. }
  219. /// <summary>
  220. /// 房间列表
  221. /// </summary>
  222. public void GetRoomList()
  223. {
  224. Studio.Scripts.HttpMessage.RoomListMessage roomListMessage = new Studio.Scripts.HttpMessage.RoomListMessage();
  225. roomListMessage.token = "262773181c66240b1529c3e2ebe076b1";
  226. DoHttp<Studio.Scripts.HttpMessage.RoomListCallBackMessage>(roomListMessage);
  227. }
  228. /// <summary>
  229. /// 更新房间
  230. /// </summary>
  231. /// <param name="roomID"></param>
  232. /// <param name="roomType"></param>
  233. /// <param name="roomName"></param>
  234. /// <param name="roomExplain"></param>
  235. /// <param name="roomPassword"></param>
  236. /// <param name="maxMember"></param>
  237. /// <param name="publicRoomType"></param>
  238. public void UpdateRoom(int roomID, int roomType, string roomName = "", string roomExplain = "", string roomPassword = "", int maxMember = 10, int publicRoomType = 1)
  239. {
  240. Studio.Scripts.HttpMessage.UpdateRoomMessage updateRoomMessage = new Studio.Scripts.HttpMessage.UpdateRoomMessage();
  241. updateRoomMessage.body.roomID = roomID;
  242. updateRoomMessage.body.type = roomType;
  243. updateRoomMessage.body.roomName = roomName;
  244. updateRoomMessage.body.roomExplain = roomExplain;
  245. updateRoomMessage.body.roomPassword = roomPassword;
  246. updateRoomMessage.body.maxMember = maxMember;
  247. updateRoomMessage.body.roomType = publicRoomType;
  248. updateRoomMessage.token = "262773181c66240b1529c3e2ebe076b1";
  249. DoHttp<Studio.Scripts.HttpMessage.UpdateRoomCallBackMessage>(updateRoomMessage);
  250. }
  251. /// <summary>
  252. /// 修改设置
  253. /// </summary>
  254. /// <param name="isOpenCamera"></param>
  255. /// <param name="isOpenMic"></param>
  256. /// <param name="ratioType"></param>
  257. /// <param name="fpsType"></param>
  258. public void UpdateSettings(string token, int fmss)
  259. {
  260. Studio.Scripts.HttpMessage.UpdateSettingsMessage updateSettingsMessage = new Studio.Scripts.HttpMessage.UpdateSettingsMessage();
  261. updateSettingsMessage.token = token;// "262773181c66240b1529c3e2ebe076b1";
  262. updateSettingsMessage.body.fmss = fmss;
  263. DoHttp<Studio.Scripts.HttpMessage.UpdateSettingsCallBackMessage>(updateSettingsMessage);
  264. }
  265. /// <summary>
  266. /// 修改设置
  267. /// </summary>
  268. /// <param name="isOpenCamera"></param>
  269. /// <param name="isOpenMic"></param>
  270. /// <param name="ratioType"></param>
  271. /// <param name="fpsType"></param>
  272. public void UpdateSettings(string token, bool isOpenCamera = false, bool isOpenMic = false, RatioType ratioType = RatioType.Low, FpsType fpsType = FpsType.Middle)
  273. {
  274. Studio.Scripts.HttpMessage.UpdateSettingsMessage updateSettingsMessage = new Studio.Scripts.HttpMessage.UpdateSettingsMessage();
  275. updateSettingsMessage.token = token;// "262773181c66240b1529c3e2ebe076b1";
  276. updateSettingsMessage.body.camera = isOpenCamera;
  277. updateSettingsMessage.body.mic = isOpenMic;
  278. updateSettingsMessage.body.fps = (int)fpsType;
  279. updateSettingsMessage.body.ratio = (int)ratioType;
  280. DoHttp<Studio.Scripts.HttpMessage.UpdateSettingsCallBackMessage>(updateSettingsMessage);
  281. }
  282. /// <summary>
  283. /// 获取我的设置
  284. /// </summary>
  285. public void GetSettings(string token)
  286. {
  287. Studio.Scripts.HttpMessage.GetSettingsMessage getSettingsMessage = new Studio.Scripts.HttpMessage.GetSettingsMessage();
  288. getSettingsMessage.token = token;// "262773181c66240b1529c3e2ebe076b1";
  289. // getSettingsMessage.body.roomID = roomID;
  290. DoHttp<Studio.Scripts.HttpMessage.GetSettingsCallBackMessage>(getSettingsMessage);
  291. }
  292. /// <summary>
  293. /// 获取资源列表
  294. /// </summary>
  295. /// <param name="token"></param>
  296. /// <param name="searchContent"></param>
  297. public void GetArtList(string token, int artType)
  298. {
  299. GetArtListMessage getArtListMessage = new GetArtListMessage();
  300. getArtListMessage.token = token;
  301. getArtListMessage.body.artType = artType;
  302. DoHttp<Studio.Scripts.HttpMessage.GetArtListCallBackMessage>(getArtListMessage);
  303. }
  304. public void SearchArtList(string token, int artType, int fileType,string keywords = "")
  305. {
  306. SearchArtListMessage searchArtListMessage = new SearchArtListMessage();
  307. searchArtListMessage.token = token;
  308. searchArtListMessage.body.artType = artType;
  309. searchArtListMessage.body.fileType = fileType;
  310. searchArtListMessage.body.keywords = keywords;
  311. DoHttp<Studio.Scripts.HttpMessage.SearchArtListCallBackMessage>(searchArtListMessage);
  312. }
  313. /// <summary>
  314. /// 获取详细资源
  315. /// </summary>
  316. /// <param name="token"></param>
  317. /// <param name="aid"></param>
  318. public void GetDetailAir(string token, string aid)
  319. {
  320. GetArtDetailMessage getArtDetailMessage = new GetArtDetailMessage();
  321. getArtDetailMessage.token = token;
  322. getArtDetailMessage.body.ArtID = aid;
  323. DoHttp<GetArtDetailCallBackMessage>(getArtDetailMessage);
  324. }
  325. /// <summary>
  326. /// 文件存储地址获取
  327. /// </summary>
  328. /// <param name="token"></param>
  329. public void GetFileAddress(string token)
  330. {
  331. FileAddressMessage fileAddressMessage = new FileAddressMessage();
  332. fileAddressMessage.token = token;
  333. DoHttp<FileAddressCallBackMessage>(fileAddressMessage);
  334. }
  335. private void DoHttp<T>(BaseHttpMessage httpMessage) where T : Studio.Scripts.HttpMessage.BaseHttpCallBackMessage
  336. {
  337. DoPost(httpMessage, delegate (bool isConnected, string content)
  338. {
  339. if (isConnected)
  340. {
  341. if (!string.IsNullOrEmpty(content))
  342. {
  343. UnityLog.Instance.Log("content:" + content);
  344. BaseHttpCallBackMessage httpHead = JsonUtility.FromJson<T>(content);
  345. UnityLog.Instance.Log("httpHead TYPE:" + httpHead .MessageType);
  346. /// 正常接口返回数据只需要解析头部数据,包含错误码,错误信息,如果是200 则解析data数据,错误码非200,则不需要解析data数据
  347. /// 返回正确
  348. if (httpHead.code == 200)
  349. {
  350. T callBackMessage = JsonUtility.FromJson<T>(content);
  351. //UnityLog.Instance.Log(Newtonsoft.Json.JsonConvert.SerializeObject(callBackMessage));
  352. HTTPDistribute.Instance.Distribute(callBackMessage);
  353. }
  354. else
  355. {
  356. // 账号登录 特殊处理,错误码非200的情况下也需要解析data数据
  357. if (httpHead.MessageType == MessageType.AccountLoginType || httpHead.MessageType == MessageType.PhoneLoginType || httpHead.MessageType == MessageType.SearchArtList)
  358. {
  359. T callBackMessage = JsonUtility.FromJson<T>(content);
  360. HTTPDistribute.Instance.DistributeError(callBackMessage);
  361. }
  362. else if (httpHead.MessageType == MessageType.GetAllArtType)
  363. {
  364. Debug.Log("资源不存在");
  365. }
  366. else
  367. {
  368. HTTPDistribute.Instance.DistributeError(httpHead);
  369. CommonMethod.ShowTextTusi();
  370. TuSi.Instance.ShowTextContent(httpHead.message.ToString());
  371. UnityLog.Instance.Log(httpHead.message.ToString());
  372. }
  373. }
  374. }
  375. }
  376. else
  377. {
  378. UnityLog.Instance.Log("net error:" + content.ToString());
  379. CommonMethod.ShowTextTusi();
  380. if (TuSi.Instance)
  381. {
  382. TuSi.Instance.ShowTextContent("net error:" + content.ToString());
  383. //LanguageMgr.Instance.GetMessage("1056").Message
  384. }
  385. // Debug.LogError("net work error:" + content);
  386. }
  387. });
  388. }
  389. /// <summary>
  390. /// caoting 2021.5.7 DoPost
  391. /// </summary>
  392. /// <param name="httpMessage"></param>
  393. /// <param name="callback"></param>
  394. private void DoPost(Studio.Scripts.HttpMessage.BaseHttpMessage httpMessage, Action<bool, string> callback)
  395. {
  396. string webMethod = "POST";
  397. switch (httpMessage.methodType)
  398. {
  399. case HttpMethodType.None:
  400. break;
  401. case HttpMethodType.Post:
  402. webMethod = UnityWebRequest.kHttpVerbPOST;
  403. break;
  404. case HttpMethodType.Get:
  405. webMethod = UnityWebRequest.kHttpVerbGET;
  406. break;
  407. default:
  408. break;
  409. }
  410. StartCoroutine(HttpRequest(httpMessage.path, webMethod, httpMessage.headers, httpMessage.ToBodyString(), httpMessage.wwwForm, callback));
  411. }
  412. /// <summary>
  413. /// caoting 2021.5.7 post
  414. /// </summary>
  415. /// <param name="methodName"></param>
  416. /// <param name="callback"></param>
  417. /// <returns></returns>
  418. private IEnumerator HttpRequest(string methodName, string webMethod, Dictionary<string, string> requestHeader, string parameters, WWWForm wwwForm, Action<bool, string> callback)
  419. {
  420. string url = SpaceHttpAction.Instance.loginUrl + methodName;
  421. UnityLog.Instance.Log($"parameters:{parameters} url { url} webMethod {webMethod}");
  422. using (UnityWebRequest webRequest = new UnityWebRequest(url, webMethod))
  423. {
  424. if (webMethod == UnityWebRequest.kHttpVerbPOST && !string.IsNullOrEmpty(parameters))
  425. {
  426. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(System.Text.Encoding.Default.GetBytes(parameters));
  427. }
  428. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  429. foreach (var v in requestHeader)
  430. {
  431. webRequest.SetRequestHeader(v.Key, v.Value);
  432. }
  433. yield return webRequest.SendWebRequest();
  434. if (webRequest.isHttpError || webRequest.isNetworkError)
  435. {
  436. if (callback != null)
  437. {
  438. callback(false, webRequest.error);
  439. }
  440. }
  441. else
  442. {
  443. if (callback != null)
  444. {
  445. callback(true, webRequest.downloadHandler.text);
  446. }
  447. }
  448. }
  449. }
  450. #endregion
  451. }