NetWorkManager.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LitJson;
  5. using System;
  6. using UnityEngine.SceneManagement;
  7. public class NetWorkManager : MonoBehaviour {
  8. private List<Network> queue;
  9. private static bool init;
  10. public static NetWorkManager instance;
  11. void Awake()
  12. {
  13. if (!init) {
  14. init = true;
  15. instance = this;
  16. queue = new List<Network> ();
  17. DontDestroyOnLoad (this.gameObject);
  18. } else {
  19. Destroy (this.gameObject);
  20. return;
  21. }
  22. }
  23. // Use this for initialization
  24. void Start () {
  25. // HotPool.getHotList ();
  26. // List<int> i = new List<int> ();
  27. // i.Add (1);
  28. // i.Add (2);
  29. // string s = JsonUtility.ToJson (i);
  30. // Debug.Log (s);
  31. }
  32. void checkQueue()
  33. {
  34. Debug.Log ("networkQueueCount: " + queue.Count);
  35. if (queue.Count == 0) {
  36. //Debug.Log ("networkQueue: " + 0);
  37. return;
  38. }
  39. if (queue [0].active) {
  40. //Debug.Log ("networkActive: " + queue[0].action);
  41. return;
  42. }
  43. StartCoroutine (post(queue[0]));
  44. }
  45. IEnumerator post(Network net)
  46. {
  47. //Debug.Log ("networkPost");
  48. net.active = true;
  49. string url = net.action;
  50. // string url = NetworkConfiguration.url + "?s=index/index1/index";
  51. WWWForm form = net.data;
  52. //UserInfo.Token = "f8009f01eb9b17e24aa57bf86aaadda9";
  53. //form.AddField ("user_token",UserInfo.Token);
  54. Debug.Log ("token===>"+UserInfo.Token);
  55. //form.AddField ("app_id", UserInfo.AppID);
  56. // form.AddField ("param", net.data);
  57. WWW www = new WWW (url,form);
  58. Debug.Log("url: " + www.url);
  59. Debug.Log("param: " + net.data);
  60. yield return www;//等待Web服务器的反应
  61. string result = string.Empty;
  62. if (www.error != null) {
  63. result = www.error;
  64. Debug.Log ("networkError: " + result);
  65. postError ("1009");
  66. } else {
  67. result = www.text;
  68. Debug.Log ("networkSuccess: " + result);
  69. //txt.text = result;
  70. postSuccess (result);
  71. }
  72. queue.RemoveAt (0);
  73. checkQueue ();
  74. }
  75. private TextMesh txt;
  76. public void getNetTexture(string action,WWWForm data,Action<Material> success = null)
  77. {
  78. StartCoroutine (LoadTextureFromLocal(action,data,success));
  79. }
  80. //读取纹理
  81. IEnumerator LoadTextureFromLocal(string action,WWWForm data=null,Action<Material> success = null)
  82. {
  83. Debug.Log ("tupian123................"+action);
  84. WWW w;
  85. if(data!=null)
  86. w = new WWW (action,data);
  87. else
  88. w = new WWW (action);
  89. yield return w;
  90. if (w.isDone) {
  91. Material material = new Material (Shader.Find ("Unlit/Texture"));
  92. Debug.Log ("tupian321................");
  93. material.mainTexture = w.texture;
  94. if(success!=null)
  95. success (material);
  96. }
  97. }
  98. void postSuccess(string postData)
  99. {
  100. JsonData data = null;
  101. string code = "";
  102. try
  103. {
  104. data = JsonMapper.ToObject (postData);
  105. //UserInfo.Token = data["token"].ToString();
  106. code = data ["code"].ToString();
  107. }
  108. catch(Exception e)
  109. {
  110. Debug.Log (e.Data);
  111. postError ("1009");
  112. }
  113. switch (code) {
  114. case "200":
  115. queue [0].scuess (data);
  116. break;
  117. default:
  118. Debug.Log("22222222222222");
  119. if (data ["message"].ToString () == "vaild account") {
  120. PlayerPrefs.SetString (UserInfo.Account,"");
  121. }
  122. queue [0].failed (postData);
  123. break;
  124. }
  125. }
  126. void postError(string errorData)
  127. {
  128. queue [0].error (errorData);
  129. }
  130. /// <summary>
  131. /// Adds the network.
  132. /// </summary>
  133. /// <param name="action">Action.</param>
  134. /// <param name="data">Data.</param>
  135. /// <param name="type">Type.</param>
  136. /// <param name="successFun">Success fun.</param>
  137. /// <param name="failedFun">Failed fun.</param>
  138. public void addNetwork(string action, WWWForm data,Action<JsonData> successFun = null ,Action<string> failedFun = null)
  139. {
  140. Network net = new Network ();
  141. net.init (action,data,successFun,failedFun);
  142. queue.Add (net);
  143. checkQueue ();
  144. }
  145. public void netWorkComplete()
  146. {
  147. netWorkComplete ();
  148. }
  149. public static void GetUser(string page, string limit, Action<JsonData> success = null, Action<string> failed = null)
  150. {
  151. }
  152. public static void getYuanChenToken(string token, Action<JsonData> success = null, Action<string> failed = null)
  153. {
  154. }
  155. public static void SearchUser(string searchID, Action<JsonData> success = null, Action<string> failed = null)
  156. {
  157. }
  158. /// <summary>
  159. /// 登录
  160. /// </summary>
  161. /// <param name="id">Identifier.</param>
  162. /// <param name="pas">Pas.</param>
  163. /// <param name="success">Success.</param>
  164. /// <param name="failed">Failed.</param>
  165. public static void login(string userName,string passWord,string code,string session,Action<JsonData> success = null,Action<string> failed = null)
  166. {
  167. }
  168. public static void getPicList(string token, Action<JsonData> success = null, Action<string> failed = null)
  169. {
  170. }
  171. public static void getSession(Action<JsonData> success = null,Action<string> failed = null)
  172. {
  173. }
  174. public static void TextureLoad(string session,Action<Material> success = null)
  175. {
  176. }
  177. public static void login_with_token(string token,Action<JsonData> success = null,Action<string> failed = null)
  178. {
  179. }
  180. public static void updateXMl(string xmlName, byte[] info, Action<JsonData> success = null, Action<string> failed = null)
  181. {
  182. WWWForm data = new WWWForm();
  183. data.AddField("file_name", xmlName);
  184. data.AddBinaryData("file_body", info);
  185. string action = "api/upload_file";
  186. NetWorkManager.instance.addNetwork(action, data, success, failed);
  187. }
  188. }