WebManager.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine.Networking;
  5. using System;
  6. using LitJson;
  7. using UnityEngine;
  8. using Newtonsoft.Json;
  9. public class WebManager : MonoBehaviour
  10. {
  11. public static WebManager GetInstance;
  12. public void Awake()
  13. {
  14. GetInstance = this;
  15. }
  16. Action mlogincomplete;
  17. public void Login(Action logincomplete)
  18. {
  19. mlogincomplete = logincomplete;
  20. var url = PROTOCOLS.URL + PROTOCOLS.Login;
  21. var code = MainAccountData.code;
  22. var nickname = MainAccountData.NickName;
  23. var avatar = MainAccountData.Avatar;
  24. var gender = MainAccountData.Gender;
  25. var country = MainAccountData.Country;
  26. var province = MainAccountData.Province;
  27. var city = MainAccountData.City;
  28. var device = MainAccountData.Device;
  29. var share_id = MainAccountData.ShareId;
  30. var scene = MainAccountData.Scene;
  31. var v = MainAccountData.ver;
  32. var args =
  33. "code=" + code + "&" +
  34. "nickname=" + nickname + "&" +
  35. "avatar=" + avatar + "&" +
  36. "gender=" + gender + "&" +
  37. "country=" + country + "&" +
  38. "province=" + province + "&" +
  39. "city=" + city + "&" +
  40. "device=" + device + "&" +
  41. "share_id=" + share_id + "&" +
  42. "scene=" + scene + "&" +
  43. "v=" + v;
  44. Debug.Log("--->>µÇ½¿ªÊ¼" + code);
  45. StartCoroutine(PostRequest(PROTOCOLS.Login, args, LoginCallback, null));
  46. }
  47. private void LoginCallback(JsonData jsonData)
  48. {
  49. //Debug.Log("LoadXJ_Completecb");
  50. //Debug.Log(_text);
  51. new MainAccountData(jsonData);
  52. MainAccountData.Instance.JundgCfg(jsonData["table_version"].ToString(), () =>
  53. {
  54. if (mlogincomplete != null) mlogincomplete();
  55. });
  56. }
  57. public void HttpPostRequest_Send(string protocol, Dictionary<string, string> param, System.Action<JsonData> completecb, System.Action<JsonData> errorcb, bool hasToken = true)
  58. {
  59. var jsonString = "";
  60. if (hasToken)
  61. {
  62. jsonString = "token=" + MainAccountData.Instance.token + "&";
  63. }
  64. if (protocol == PROTOCOLS.game || protocol == PROTOCOLS.end || protocol == PROTOCOLS.fission || protocol == PROTOCOLS.vd || protocol == PROTOCOLS.themefission || protocol == PROTOCOLS.revival || protocol == PROTOCOLS.Sign || protocol == PROTOCOLS.Buy)
  65. {
  66. string _newStr = "abcdefghijkimnopqrstuvwxyz0123456789";
  67. string _code = "{";
  68. string _mask = "";
  69. for (int i = 0; i < 4; i++)
  70. {
  71. _mask += _newStr[UnityEngine.Random.Range(0, 100) % _newStr.Length];
  72. }
  73. int _newIndex = 0;
  74. if (param != null)
  75. {
  76. foreach (var item in param)
  77. {
  78. _newIndex++;
  79. string key = item.Key;
  80. string value = item.Value;
  81. _code += "\"" + key + "\":\"" + value + "\"";
  82. if (_newIndex < param.Count)
  83. {
  84. _code += ",";
  85. }
  86. else
  87. {
  88. _code += "}";
  89. }
  90. }
  91. }
  92. jsonString += "_r=" + _mask + this.base64Encode(_code);
  93. }
  94. else
  95. {
  96. if (param != null)
  97. {
  98. var _index = 0;
  99. foreach (var item in param)
  100. {
  101. _index++;
  102. string key = item.Key;
  103. string value = item.Value;
  104. jsonString += key + "=" + value;
  105. if (_index < param.Count)
  106. {
  107. jsonString += "&";
  108. }
  109. }
  110. }
  111. }
  112. StartCoroutine(this.PostRequest(protocol, jsonString, completecb, errorcb)); ;
  113. }
  114. private IEnumerator PostRequest(string protocol, string jsonString, Action<JsonData> completecb, Action<JsonData> errorcb)
  115. {
  116. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  117. string url = PROTOCOLS.URL + protocol + "?" + jsonString;
  118. Debug.Log("PostRequest--->>>url-->" + url);
  119. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST", new DownloadHandlerBuffer(), new UploadHandlerRaw(bodyRaw)))
  120. {
  121. webRequest.SetRequestHeader("Content-Type", "application/json");
  122. yield return webRequest.SendWebRequest();
  123. if (webRequest.isHttpError || webRequest.isNetworkError)
  124. {
  125. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  126. if (errorcb != null)
  127. {
  128. this.onHttpRequestErrorcb(webRequest.downloadHandler.text, errorcb);
  129. }
  130. }
  131. else
  132. {
  133. if (completecb != null)
  134. {
  135. this.onHttpRequestComplete(webRequest.downloadHandler.text, completecb, errorcb);
  136. }
  137. }
  138. }
  139. }
  140. private void onHttpRequestComplete(string _text, Action<JsonData> completecb, Action<JsonData> errorcb)
  141. {
  142. //JObject obj = JsonConvert.DeserializeObject(_text);
  143. JsonData valuePairs = JsonMapper.ToObject(_text);
  144. if (valuePairs["code"].ToString() != "0")
  145. {
  146. Debug.Log("Error! Http request complete error" + valuePairs["message"]);
  147. if (errorcb != null)
  148. {
  149. errorcb(valuePairs);
  150. }
  151. return;
  152. }
  153. if (completecb != null)
  154. {
  155. Debug.Log(_text);
  156. completecb(valuePairs);
  157. }
  158. }
  159. private void onHttpRequestErrorcb(string _text, Action<JsonData> errorcb)
  160. {
  161. JsonData valuePairs = JsonMapper.ToObject(_text);
  162. Debug.Log("Error! Http request error..." + valuePairs["message"]);
  163. if (errorcb != null)
  164. {
  165. errorcb(valuePairs);
  166. }
  167. }
  168. private string base64Encode(string str)
  169. {
  170. Debug.Log("base64Encode-->>" + str);
  171. int c1, c2, c3;
  172. string _base64Encode = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  173. char[] base64EncodeChars = _base64Encode.ToCharArray();
  174. char[] strChars = str.ToCharArray();
  175. int i = 0;
  176. int len = str.Length;
  177. string stringStr = "";
  178. while (i < len)
  179. {
  180. c1 = strChars[i++] & 0xff;
  181. if (i == len)
  182. {
  183. stringStr += base64EncodeChars[c1 >> 2];
  184. stringStr += base64EncodeChars[(c1 & 0x3) << 4];
  185. stringStr += "==";
  186. break;
  187. }
  188. c2 = str[i++];
  189. if (i == len)
  190. {
  191. stringStr += base64EncodeChars[c1 >> 2];
  192. stringStr += base64EncodeChars[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)];
  193. stringStr += base64EncodeChars[(c2 & 0xF) << 2];
  194. stringStr += "=";
  195. break;
  196. }
  197. c3 = str[i++];
  198. stringStr += base64EncodeChars[c1 >> 2];
  199. stringStr += base64EncodeChars[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)];
  200. stringStr += base64EncodeChars[((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)];
  201. stringStr += base64EncodeChars[c3 & 0x3F];
  202. }
  203. Debug.Log(stringStr.Length + " " + stringStr);
  204. return stringStr;
  205. }
  206. void OnHpptError()
  207. {
  208. }
  209. }