KangFuTwo.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using LitJson;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using UnityEngine;
  7. using UnityEngine.Networking;
  8. using UnityEngine.UI;
  9. public class KangFuTwo : MonoBehaviour
  10. {
  11. List<KangFuItem> kangfulist = new List<KangFuItem>();
  12. public KangFuItem kitem;
  13. int sting;
  14. int fting;
  15. int zhen;
  16. int sji;
  17. int fji;
  18. int wo;
  19. public Text stingt;
  20. public Text ftingt;
  21. public Text zhent;
  22. public Text sjit;
  23. public Text fjit;
  24. public Text wot;
  25. public void add(int index)
  26. {
  27. switch(index)
  28. {
  29. case 0:
  30. sting++;
  31. break;
  32. case 1:
  33. fting++;
  34. break;
  35. case 2:
  36. zhen++;
  37. break;
  38. case 3:
  39. sji++;
  40. break;
  41. case 4:
  42. fji++;
  43. break;
  44. case 5:
  45. wo++;
  46. break;
  47. }
  48. initText();
  49. }
  50. void initText()
  51. {
  52. stingt.text = sting+"";
  53. ftingt.text = fting + "";
  54. zhent.text = zhen + "";
  55. sjit.text = sji + "";
  56. fjit.text = fji + "";
  57. wot.text = wo + "";
  58. }
  59. public void back()
  60. {
  61. for (int i = 1; i < WindowManager.Instance.KangFuDaList.Count; i++)
  62. {
  63. WindowManager.Instance.KangFuDaList[i].SetActive(false);
  64. }
  65. WindowManager.Instance.KangFuDaList[0].SetActive(true);
  66. }
  67. private void OnEnable()
  68. {
  69. initkangfulist();
  70. }
  71. public void shangbao()
  72. {
  73. StartCoroutine(SendHttp("cmcc-endustry/v1/report/dairyCattle/userInfo", (string str) => {
  74. JsonData datainfo = JsonMapper.ToObject(str);
  75. JsonData data = new JsonData();
  76. data["sting"] = sting;
  77. data["fting"] = fting;
  78. data["zhen"] = zhen;
  79. data["sji"] = sji;
  80. data["fji"] = fji;
  81. data["wo"] = wo;
  82. data["time"] = DateTime.Now.ToString();
  83. if (!datainfo["data"].Keys.Contains("info"))
  84. datainfo["data"]["info"] = new JsonData();
  85. try
  86. {
  87. datainfo["data"]["info"] = JsonMapper.ToObject(datainfo["data"]["info"].ToString());
  88. }
  89. catch
  90. {
  91. datainfo["data"]["info"] = new JsonData();
  92. }
  93. if (!datainfo["data"]["info"].IsArray)
  94. datainfo["data"]["info"] = new JsonData();
  95. datainfo["data"]["info"].Add(data);
  96. datainfo["data"]["info"] = datainfo["data"]["info"].ToJson();
  97. StartCoroutine(SendHttp2("cmcc-endustry/v1/report/dairyCattle/userInfo", datainfo["data"], (string msg) => {
  98. initkangfulist();
  99. }));
  100. }));
  101. }
  102. void initjishu()
  103. {
  104. sting = 0;
  105. fting = 0;
  106. zhen = 0;
  107. sji = 0;
  108. fji = 0;
  109. wo = 0;
  110. initText();
  111. }
  112. public void initkangfulist()
  113. {
  114. initjishu();
  115. for (int i = 0; i < kangfulist.Count; i++)
  116. {
  117. Destroy(kangfulist[i].gameObject);
  118. }
  119. kangfulist.Clear();
  120. StartCoroutine(SendHttp("cmcc-endustry/v1/report/dairyCattle/userInfo", (string str) => {
  121. JsonData data = JsonMapper.ToObject(str);
  122. if (!data["data"].Keys.Contains("info"))
  123. data["data"]["info"] = new JsonData();
  124. data["data"]["info"] = JsonMapper.ToObject(data["data"]["info"].ToString());
  125. if (!data["data"]["info"].IsArray)
  126. data["data"]["info"] = new JsonData();
  127. try
  128. {
  129. for (int i = 0; i < data["data"]["info"].Count; i++)
  130. {
  131. Debug.Log("jilu===>"+ data["data"]["info"].ToJson());
  132. if(data["data"]["info"][i].Keys.Contains("time"))
  133. {
  134. KangFuItem ki = GameObject.Instantiate(kitem, kitem.transform.parent);
  135. ki.setData(data["data"]["info"][i], i);
  136. ki.gameObject.SetActive(true);
  137. kangfulist.Add(ki);
  138. ki.transform.SetSiblingIndex(5);
  139. }
  140. }
  141. }
  142. catch
  143. {
  144. }
  145. }));
  146. }
  147. public IEnumerator SendHttp2(string methodName, JsonData fromDic, Action<string> CallBack)
  148. {
  149. Debug.Log("Start Queue SendHttp " + methodName + " fromDic===��"+ fromDic.ToJson());
  150. string url = "https://api-fat2.ghz-tech.com/" + methodName;
  151. UnityWebRequest webRequest;
  152. webRequest = UnityWebRequest.Post(url, "Post");
  153. byte[] bodyRaw = Encoding.UTF8.GetBytes(fromDic.ToJson());
  154. webRequest.uploadHandler = new UploadHandlerRaw(bodyRaw);
  155. webRequest.SetRequestHeader("Content-Type", "application/json");
  156. webRequest.SetRequestHeader("Authorization", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjMxNTM2MDAxNzEyNzQ0NjUxLCJpYXQiOjE3MTI3NDQ2NTEsInVzZXIiOnsiaWQiOjExLCJlbWFpbCI6IiIsInBob25lIjoieWIzIiwibmFtZSI6InliMyIsInJvbGVJZCI6M319.WGpVJHfbTr4Bp2cySjjlgCIon8nF4sJxkl58wcIion8");
  157. yield return webRequest.SendWebRequest();
  158. if (webRequest.result == UnityWebRequest.Result.ConnectionError)
  159. {
  160. Debug.LogError(webRequest.error);
  161. }
  162. else
  163. {
  164. string result = webRequest.downloadHandler.text;
  165. CallBack.Invoke(result);
  166. }
  167. }
  168. public IEnumerator SendHttp(string methodName, Action<string> CallBack)
  169. {
  170. string url = "https://api-fat2.ghz-tech.com/" + methodName+"?name=" + KangFuOne.choosename;
  171. UnityWebRequest webRequest = UnityWebRequest.Get(url);
  172. string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjMxNTM2MDAxNzEyNzQ0NjUxLCJpYXQiOjE3MTI3NDQ2NTEsInVzZXIiOnsiaWQiOjExLCJlbWFpbCI6IiIsInBob25lIjoieWIzIiwibmFtZSI6InliMyIsInJvbGVJZCI6M319.WGpVJHfbTr4Bp2cySjjlgCIon8nF4sJxkl58wcIion8";
  173. //UnityWebRequest webRequest = new UnityWebRequest(url, "GET");
  174. JsonData data = new JsonData();
  175. data["name"] = KangFuOne.choosename;
  176. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  177. webRequest.SetRequestHeader("authorization", token);//����ͷ�ļ�����
  178. yield return webRequest.SendWebRequest();
  179. if (webRequest.result == UnityWebRequest.Result.ConnectionError)
  180. {
  181. Debug.Log(webRequest.error);
  182. }
  183. else
  184. {
  185. Debug.LogError(webRequest.downloadHandler.text);
  186. CallBack.Invoke(webRequest.downloadHandler.text);
  187. }
  188. }
  189. }