NetWorkManager.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. WWWForm data = new WWWForm();
  152. data.AddField("user_token", UserInfo.User_Token);
  153. data.AddField("action", "20400");
  154. data.AddField("page", page);
  155. data.AddField("limit", limit);
  156. string action = NetworkConfiguration.get_User_token;
  157. NetWorkManager.instance.addNetwork(action, data, success, failed);
  158. }
  159. public static void getYuanChenToken(string token, Action<JsonData> success = null, Action<string> failed = null)
  160. {
  161. WWWForm data = new WWWForm();
  162. data.AddField("user_token", token);
  163. data.AddField("action", "10101");
  164. string action = NetworkConfiguration.get_User_token;
  165. NetWorkManager.instance.addNetwork(action, data, success, failed);
  166. }
  167. public static void SearchUser(string searchID, Action<JsonData> success = null, Action<string> failed = null)
  168. {
  169. WWWForm data = new WWWForm();
  170. data.AddField("user_token", UserInfo.User_Token);
  171. Debug.Log("user_token====>" + UserInfo.User_Token);
  172. data.AddField("action", "20103");
  173. data.AddField("search", searchID);
  174. Debug.Log("searchID====>" + searchID);
  175. string action = NetworkConfiguration.get_User_token;
  176. NetWorkManager.instance.addNetwork(action, data, success, failed);
  177. }
  178. /// <summary>
  179. /// 登录
  180. /// </summary>
  181. /// <param name="id">Identifier.</param>
  182. /// <param name="pas">Pas.</param>
  183. /// <param name="success">Success.</param>
  184. /// <param name="failed">Failed.</param>
  185. public static void login(string userName,string passWord,string code,string session,Action<JsonData> success = null,Action<string> failed = null)
  186. {
  187. WWWForm data = new WWWForm ();
  188. data.AddField("code", code);
  189. data.AddField("loginstr", userName);
  190. data.AddField("password", passWord);
  191. data.AddField("session_id", session);
  192. data.AddField("action", "10001");
  193. Debug.Log(NetworkConfiguration.yc_UrL);
  194. string action = NetworkConfiguration.yc_UrL;
  195. NetWorkManager.instance.addNetwork (action, data, success, failed);
  196. }
  197. public static void getPicList(string token, Action<JsonData> success = null, Action<string> failed = null)
  198. {
  199. WWWForm data = new WWWForm();
  200. //data.AddField("user_token", token);
  201. // data.AddField("action", "10101");
  202. string action = NetworkConfiguration.get_User_token;
  203. NetWorkManager.instance.addNetwork(action, data, success, failed);
  204. }
  205. public static void getSession(Action<JsonData> success = null,Action<string> failed = null)
  206. {
  207. WWWForm data = new WWWForm ();
  208. string action = NetworkConfiguration.get_session_id;
  209. NetWorkManager.instance.addNetwork (action, data, success, failed);
  210. }
  211. public static void TextureLoad(string session,Action<Material> success = null)
  212. {
  213. WWWForm data = new WWWForm ();
  214. data.AddField("session_id", session);
  215. string action = NetworkConfiguration.get_code_png_url;
  216. NetWorkManager.instance.getNetTexture (action,data,success);
  217. }
  218. public static void login_with_token(string token,Action<JsonData> success = null,Action<string> failed = null)
  219. {
  220. WWWForm data = new WWWForm ();
  221. data.AddField ("token",token);
  222. string action = NetworkConfiguration.login_with_token;
  223. NetWorkManager.instance.addNetwork (action, data, success,failed);
  224. }
  225. public static void updateXMl(string xmlName, byte[] info, Action<JsonData> success = null, Action<string> failed = null)
  226. {
  227. WWWForm data = new WWWForm();
  228. data.AddField("file_name", xmlName);
  229. data.AddBinaryData("file_body", info);
  230. string action = "api/upload_file";
  231. NetWorkManager.instance.addNetwork(action, data, success, failed);
  232. }
  233. }