BaseHttpMessage.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. namespace Studio.Scripts.HttpMessage
  6. {
  7. public enum HttpMethodType
  8. {
  9. None = 0,
  10. Post = 1,
  11. Get = 2,
  12. }
  13. public enum MessageModuleType
  14. {
  15. None = 0,
  16. Common = 1,
  17. Login = 2,
  18. Room = 3,
  19. Set = 4,
  20. }
  21. public enum MessageType
  22. {
  23. None = 0,
  24. SNLoginType = 1,// sn 登录
  25. PhoneLoginType = 2,// 手机登录
  26. AccountLoginType = 3,// 账号登录
  27. AdvanceAccountLoginType = 4,// 预登录
  28. LogoutType = 5,// 退出
  29. GetAvatarType =6,// 获取头像
  30. ModifyRoleType =7,// 修改信息
  31. CaptchaCodeType = 10,// 图形验证码
  32. SmsCodeType = 11,//手机验证码
  33. GetFileAddressType = 12,//文件存储地址
  34. CreaterRoomType = 20,// 创建房间
  35. DeleteRoomType = 21,// 删除房间
  36. GetMySetType = 22,// 获取我的设置
  37. UpdateSetType = 23,//修改我的设置
  38. GetAllArtType = 30,//获取资源列表
  39. GetDetailArtType = 31,//获取资源详情
  40. SearchArtList = 32,//搜索资源列表
  41. }
  42. public enum PhoneSMSCodeType
  43. {
  44. None = 0,
  45. Register = 1,// 注册
  46. Login = 2,// 登录
  47. RetrievePassword = 3,// 找回密码
  48. ChangePassword = 4,// 修改密码
  49. Reserve = 5,// 预留
  50. ChangePhone = 6,// 手机号修改
  51. }
  52. /// <summary>
  53. /// 分辨率
  54. /// </summary>
  55. public enum RatioType
  56. {
  57. None = 0,
  58. High = 1,// 高 1290 *960
  59. Middle =2,//中 640*480
  60. Low = 3,// 低 320*240
  61. }
  62. /// <summary>
  63. /// fps 帧率
  64. /// </summary>
  65. public enum FpsType
  66. {
  67. None = 0,
  68. Low = 1,// 低 15
  69. Middle = 2,//中 30
  70. High = 3,// 高 60
  71. }
  72. public enum IsActivateType
  73. {
  74. Unactivated = 0,// 未激活
  75. Activated = 1,// 激活
  76. }
  77. public struct ScenesData
  78. {
  79. public int scenesID;
  80. public int roomID;
  81. }
  82. public class BaseHttpMessage
  83. {
  84. public MessageModuleType messageModuleType = MessageModuleType.None;
  85. public HttpMethodType methodType = HttpMethodType.None;
  86. protected string _path;
  87. public virtual string path
  88. {
  89. get
  90. {
  91. return _path;
  92. }
  93. }
  94. public Dictionary<string, string> headers;
  95. protected WWWForm _wwwForm;
  96. public virtual WWWForm wwwForm
  97. {
  98. get
  99. {
  100. return _wwwForm ;
  101. }
  102. }
  103. public virtual string ToBodyString()
  104. {
  105. return string.Empty;
  106. }
  107. protected WWWForm GetWWWFrom<T>(T body) where T: struct
  108. {
  109. WWWForm wf = new WWWForm();
  110. System.Reflection.FieldInfo[] fields = body.GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
  111. foreach (System.Reflection.FieldInfo item in fields)
  112. {
  113. if (item.FieldType == typeof(string))
  114. {
  115. wf.AddField(item.Name, (item.GetValue(body)??"").ToString());
  116. }
  117. else if (item.FieldType == typeof(int))
  118. {
  119. wf.AddField(item.Name, (int)item.GetValue(body));
  120. }
  121. }
  122. return wf;
  123. }
  124. protected WWWForm GetWWWFrom<T>(IList<T> body) where T : struct
  125. {
  126. WWWForm wf = new WWWForm();
  127. foreach (var bodyItem in body)
  128. {
  129. System.Reflection.FieldInfo[] fields = bodyItem.GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
  130. foreach (System.Reflection.FieldInfo item in fields)
  131. {
  132. Debug.LogWarning(item.Name + ":" + item.GetValue(bodyItem));
  133. if (item.FieldType == typeof(string))
  134. {
  135. wf.AddField(item.Name, item.GetValue(bodyItem).ToString());
  136. }
  137. else if (item.FieldType == typeof(int))
  138. {
  139. wf.AddField(item.Name, (int)item.GetValue(bodyItem));
  140. }
  141. }
  142. }
  143. return wf;
  144. }
  145. protected string GetHttpGetForm<T>(string url, T body) where T : struct
  146. {
  147. string parameters = "";
  148. System.Reflection.FieldInfo[] fields = body.GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
  149. foreach (System.Reflection.FieldInfo item in fields)
  150. {
  151. parameters += item.Name + "=";
  152. parameters += item.GetValue(body).ToString() + "&";
  153. }
  154. if (url.IndexOf("?") == -1)
  155. url += "?";
  156. else
  157. url += "&";
  158. url += parameters.TrimEnd(new char[] { '&' });
  159. return url;
  160. }
  161. protected string CreateGetData(string url, Dictionary<string, object> form)
  162. {
  163. string data = "";
  164. if (form != null && form.Count > 0)
  165. {
  166. foreach (var item in form)
  167. {
  168. data += item.Key + "=";
  169. data += item.Value.ToString() + "&";
  170. }
  171. }
  172. if (url.IndexOf("?") == -1)
  173. url += "?";
  174. else
  175. url += "&";
  176. url += data.TrimEnd(new char[] { '&' });
  177. return url;
  178. }
  179. }
  180. }