123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using LitJson;
- using System;
- using UnityEngine.SceneManagement;
- public class NetWorkManager : MonoBehaviour {
- private List<Network> queue;
- private static bool init;
- public static NetWorkManager instance;
- void Awake()
- {
- if (!init) {
- init = true;
- instance = this;
- queue = new List<Network> ();
- DontDestroyOnLoad (this.gameObject);
- } else {
- Destroy (this.gameObject);
- return;
- }
- }
- // Use this for initialization
- void Start () {
- // HotPool.getHotList ();
- // List<int> i = new List<int> ();
- // i.Add (1);
- // i.Add (2);
- // string s = JsonUtility.ToJson (i);
- // Debug.Log (s);
- }
- void checkQueue()
- {
- Debug.Log ("networkQueueCount: " + queue.Count);
- if (queue.Count == 0) {
- //Debug.Log ("networkQueue: " + 0);
- return;
- }
- if (queue [0].active) {
- //Debug.Log ("networkActive: " + queue[0].action);
- return;
- }
- StartCoroutine (post(queue[0]));
- }
- IEnumerator post(Network net)
- {
- //Debug.Log ("networkPost");
- net.active = true;
- string url = net.action;
- // string url = NetworkConfiguration.url + "?s=index/index1/index";
- WWWForm form = net.data;
- //UserInfo.Token = "f8009f01eb9b17e24aa57bf86aaadda9";
- //form.AddField ("user_token",UserInfo.Token);
- Debug.Log ("token===>"+UserInfo.Token);
- //form.AddField ("app_id", UserInfo.AppID);
- // form.AddField ("param", net.data);
- WWW www = new WWW (url,form);
- Debug.Log("url: " + www.url);
- Debug.Log("param: " + net.data);
- yield return www;//等待Web服务器的反应
- string result = string.Empty;
- if (www.error != null) {
- result = www.error;
- Debug.Log ("networkError: " + result);
- postError ("1009");
- } else {
- result = www.text;
- Debug.Log ("networkSuccess: " + result);
- //txt.text = result;
- postSuccess (result);
- }
- queue.RemoveAt (0);
- checkQueue ();
- }
- private TextMesh txt;
- public void getNetTexture(string action,WWWForm data,Action<Material> success = null)
- {
- StartCoroutine (LoadTextureFromLocal(action,data,success));
- }
- //读取纹理
- IEnumerator LoadTextureFromLocal(string action,WWWForm data=null,Action<Material> success = null)
- {
- Debug.Log ("tupian123................"+action);
- WWW w;
- if(data!=null)
- w = new WWW (action,data);
- else
- w = new WWW (action);
- yield return w;
- if (w.isDone) {
- Material material = new Material (Shader.Find ("Unlit/Texture"));
- Debug.Log ("tupian321................");
- material.mainTexture = w.texture;
- if(success!=null)
- success (material);
- }
- }
- void postSuccess(string postData)
- {
- JsonData data = null;
- string code = "";
- try
- {
- data = JsonMapper.ToObject (postData);
- //UserInfo.Token = data["token"].ToString();
- code = data ["code"].ToString();
- }
- catch(Exception e)
- {
- Debug.Log (e.Data);
- postError ("1009");
- }
- switch (code) {
- case "200":
- queue [0].scuess (data);
- break;
- default:
- Debug.Log("22222222222222");
- if (data ["message"].ToString () == "vaild account") {
- PlayerPrefs.SetString (UserInfo.Account,"");
- }
- queue [0].failed (postData);
- break;
- }
- }
- void postError(string errorData)
- {
- queue [0].error (errorData);
- }
- /// <summary>
- /// Adds the network.
- /// </summary>
- /// <param name="action">Action.</param>
- /// <param name="data">Data.</param>
- /// <param name="type">Type.</param>
- /// <param name="successFun">Success fun.</param>
- /// <param name="failedFun">Failed fun.</param>
- public void addNetwork(string action, WWWForm data,Action<JsonData> successFun = null ,Action<string> failedFun = null)
- {
- Network net = new Network ();
- net.init (action,data,successFun,failedFun);
- queue.Add (net);
- checkQueue ();
- }
- public void netWorkComplete()
- {
- netWorkComplete ();
- }
- public static void GetUser(string page, string limit, Action<JsonData> success = null, Action<string> failed = null)
- {
- WWWForm data = new WWWForm();
- data.AddField("user_token", UserInfo.User_Token);
- data.AddField("action", "20400");
- data.AddField("page", page);
- data.AddField("limit", limit);
- string action = NetworkConfiguration.get_User_token;
- NetWorkManager.instance.addNetwork(action, data, success, failed);
- }
- public static void getYuanChenToken(string token, Action<JsonData> success = null, Action<string> failed = null)
- {
- WWWForm data = new WWWForm();
- data.AddField("user_token", token);
- data.AddField("action", "10101");
- string action = NetworkConfiguration.get_User_token;
- NetWorkManager.instance.addNetwork(action, data, success, failed);
- }
- public static void SearchUser(string searchID, Action<JsonData> success = null, Action<string> failed = null)
- {
- WWWForm data = new WWWForm();
- data.AddField("user_token", UserInfo.User_Token);
- Debug.Log("user_token====>" + UserInfo.User_Token);
- data.AddField("action", "20103");
- data.AddField("search", searchID);
- Debug.Log("searchID====>" + searchID);
- string action = NetworkConfiguration.get_User_token;
- NetWorkManager.instance.addNetwork(action, data, success, failed);
- }
- /// <summary>
- /// 登录
- /// </summary>
- /// <param name="id">Identifier.</param>
- /// <param name="pas">Pas.</param>
- /// <param name="success">Success.</param>
- /// <param name="failed">Failed.</param>
- public static void login(string userName,string passWord,string code,string session,Action<JsonData> success = null,Action<string> failed = null)
- {
- WWWForm data = new WWWForm ();
- data.AddField("code", code);
- data.AddField("loginstr", userName);
- data.AddField("password", passWord);
- data.AddField("session_id", session);
- data.AddField("action", "10001");
- Debug.Log(NetworkConfiguration.yc_UrL);
- string action = NetworkConfiguration.yc_UrL;
- NetWorkManager.instance.addNetwork (action, data, success, failed);
- }
- public static void getPicList(string token, Action<JsonData> success = null, Action<string> failed = null)
- {
- WWWForm data = new WWWForm();
- //data.AddField("user_token", token);
- // data.AddField("action", "10101");
- string action = NetworkConfiguration.get_User_token;
- NetWorkManager.instance.addNetwork(action, data, success, failed);
- }
- public static void getSession(Action<JsonData> success = null,Action<string> failed = null)
- {
- WWWForm data = new WWWForm ();
- string action = NetworkConfiguration.get_session_id;
- NetWorkManager.instance.addNetwork (action, data, success, failed);
- }
- public static void TextureLoad(string session,Action<Material> success = null)
- {
- WWWForm data = new WWWForm ();
- data.AddField("session_id", session);
- string action = NetworkConfiguration.get_code_png_url;
- NetWorkManager.instance.getNetTexture (action,data,success);
- }
-
- public static void login_with_token(string token,Action<JsonData> success = null,Action<string> failed = null)
- {
- WWWForm data = new WWWForm ();
- data.AddField ("token",token);
- string action = NetworkConfiguration.login_with_token;
- NetWorkManager.instance.addNetwork (action, data, success,failed);
- }
- public static void updateXMl(string xmlName, byte[] info, Action<JsonData> success = null, Action<string> failed = null)
- {
- WWWForm data = new WWWForm();
- data.AddField("file_name", xmlName);
- data.AddBinaryData("file_body", info);
- string action = "api/upload_file";
- NetWorkManager.instance.addNetwork(action, data, success, failed);
- }
- }
|