Api.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using Newtonsoft.Json;
  2. using ReleaseHelper.Models;
  3. using ReleaseHelper.Utils;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. namespace ReleaseHelper.Services
  11. {
  12. public static class Api
  13. {
  14. public delegate void DeleLogin(bool runAfterLoginFn);
  15. public static DeleLogin LoginFn;
  16. private const int PAGE_SIZE = 999999;
  17. private const int PAGE_NO = 1;
  18. /// <summary>
  19. /// 登录
  20. /// </summary>
  21. /// <param name="username"></param>
  22. /// <param name="password"></param>
  23. /// <returns></returns>
  24. public static async Task<UserLoginPost> Login(string username, string password)
  25. {
  26. UserLoginPost loginResponse = null;
  27. var (code, response) = await Fetch(
  28. new Http.Request(@"v1/user/login", Http.Method.Post,
  29. new Dictionary<string, object> {
  30. { "username", username} ,
  31. { "password", password}
  32. }
  33. ));
  34. if (!string.IsNullOrEmpty(response))
  35. {
  36. loginResponse = Json.Decode<UserLoginPost>(response);
  37. Variables.Variables.RequestHeaders["Authorization"] = "Bearer " + loginResponse.Token;
  38. }
  39. return loginResponse;
  40. }
  41. /// <summary>
  42. /// 获取静态文件流
  43. /// </summary>
  44. /// <param name="fileName"></param>
  45. /// <returns></returns>
  46. public static async Task<byte[]> GetStaticFileBytes(string fileName)
  47. {
  48. byte[] result = null;
  49. fileName = Regex.Replace(fileName, @"^[\/\\]*", "");
  50. long begin = Functions.GetTimeStamp(), end;
  51. Http.Request request = new Http.Request($"{Variables.Const.HTTP_STATIC_HOST}{fileName}", Http.Method.Get);
  52. end = Functions.GetTimeStamp();
  53. Log.WriteLine($"request static file create http: {end - begin}");
  54. begin = end;
  55. using (HttpResponseMessage response = await Http.Fetch(request))
  56. {
  57. end = Functions.GetTimeStamp();
  58. Log.WriteLine($"request static file fetch http: {end - begin}");
  59. begin = end;
  60. if (response != null && response.StatusCode == HttpStatusCode.OK)
  61. {
  62. result = response.Content.ReadAsByteArrayAsync().Result;
  63. end = Functions.GetTimeStamp();
  64. Log.WriteLine($"request static file read byte: {end - begin}");
  65. begin = end;
  66. }
  67. }
  68. return result;
  69. }
  70. /// <summary>
  71. /// 获取升级信息
  72. /// </summary>
  73. /// <returns></returns>
  74. public static async Task<Models.UpdateInfo> GetUpdateInfo(string ossFileUri)
  75. {
  76. UpdateInfo info = new UpdateInfo();
  77. try
  78. {
  79. string url = Regex.Replace(ossFileUri, "\\.[^.]{2,5}$", ".txt", RegexOptions.IgnoreCase);
  80. HttpResponseMessage response = await Http.Fetch(new Http.Request(url, Http.Method.Get));
  81. if (response.IsSuccessStatusCode)
  82. {
  83. string txt = await response.Content.ReadAsStringAsync();
  84. info = Utils.Json.Decode<UpdateInfo>(txt);
  85. }
  86. }
  87. catch
  88. {
  89. }
  90. return info;
  91. }
  92. /// <summary>
  93. /// 获取阿里云OSS STS 权限
  94. /// </summary>
  95. /// <returns></returns>
  96. public static async Task<Models.AliyunAccessModel> GetAliyunAccess()
  97. {
  98. var (code, response) = await Fetch(new Http.Request(@"v1/oss/authorize", Http.Method.Post, null));
  99. return Json.Decode<Models.AliyunAccessModel>(response);
  100. //return GetResponse<Models.AliyunAccessModel>(@"v1/oss/authorize", Http.Method.Post, null);
  101. }
  102. /// <summary>
  103. /// 获取系统返回的内容
  104. /// </summary>
  105. /// <typeparam name="T"></typeparam>
  106. /// <param name="url"></param>
  107. /// <param name="method"></param>
  108. /// <param name="data"></param>
  109. /// <returns></returns>
  110. private static async Task<T> GetResponse<T>(string url, Http.Method method, Dictionary<string, object> data)
  111. {
  112. T res = default;
  113. var (code, response) = await Fetch(new Http.Request(url, method, data));
  114. if (!string.IsNullOrEmpty(response))
  115. {
  116. var tmp = Json.Decode<Dictionary<string, object>>(response);
  117. if (tmp != null && tmp.ContainsKey("result"))
  118. {
  119. res = Json.Decode<T>(tmp["result"].ToString());
  120. }
  121. }
  122. return res;
  123. }
  124. #region 私有方法
  125. /// <summary>
  126. /// 请求
  127. /// </summary>
  128. /// <param name="request"></param>
  129. /// <returns></returns>
  130. private static async Task<(int, string)> Fetch(Http.Request request)
  131. {
  132. foreach (var item in Variables.Variables.RequestHeaders)
  133. {
  134. // 添加请求头
  135. request.headers.Add(item.Key, item.Value);
  136. }
  137. // 请求地址
  138. request.uri = string.Concat(Variables.Const.HTTP_HOST, request.uri);
  139. string apiResponse = null;
  140. int code = 200;
  141. Dictionary<string, object> resp;
  142. using (HttpResponseMessage response = await Http.Fetch(request))
  143. {
  144. if (response != null)
  145. {
  146. switch (response.StatusCode)
  147. {
  148. case HttpStatusCode.OK:
  149. // 正常响应
  150. resp = JsonConvert.DeserializeObject<Dictionary<string, object>>(await response.Content.ReadAsStringAsync());
  151. //apiResponse = Json.Decode<ApiResponse>(response.Content.ReadAsStreamAsync().Result);
  152. code = Convert.ToInt32(resp["code"]);
  153. switch (code)
  154. {
  155. case 400:
  156. Response400();
  157. apiResponse = resp["errors"] != null ? resp["errors"].ToString() : "接口错误";
  158. break;
  159. case 401:
  160. Response401();
  161. break;
  162. case 4000:
  163. Response401();
  164. break;
  165. case 404:
  166. Response404();
  167. break;
  168. case 500:
  169. Response500();
  170. apiResponse = resp["errors"].ToString();
  171. break;
  172. case 502:
  173. Response500();
  174. break;
  175. //case 1001:
  176. // apiResponse = resp["msg"].ToString();
  177. //break;
  178. default:
  179. apiResponse = resp["data"] != null ? resp["data"].ToString() : "";
  180. break;
  181. }
  182. break;
  183. case HttpStatusCode.NotFound:
  184. // 页面不存在
  185. Response404();
  186. break;
  187. case HttpStatusCode.Unauthorized:
  188. // 无权限
  189. Response401();
  190. break;
  191. case HttpStatusCode.BadRequest:
  192. // 页面错误
  193. Response400();
  194. break;
  195. default:
  196. // 其它错误
  197. Response500();
  198. break;
  199. }
  200. }
  201. else
  202. {
  203. // 没有网络
  204. }
  205. }
  206. //Log.WriteLine($"{request.uri}:{apiResponse}");
  207. return (code, apiResponse);
  208. }
  209. private static void Response400()
  210. {
  211. }
  212. private static void Response401()
  213. {
  214. //void act()
  215. //{
  216. // Forms.Instances.FormLogin.ShowDialog(Forms.Instances.MainEntrance);
  217. // Forms.Instances.FormLogin.ReLogin();
  218. //}
  219. //Utils.ControlExtensions.FormInvoke(Forms.Instances.FormLogin, act);
  220. LoginFn?.Invoke(false);
  221. //Forms.Instances.FormLogin.ShowDialog(Forms.Instances.MainEntrance);
  222. //Forms.Instances.FormLogin.ReLogin();
  223. }
  224. private static void Response404()
  225. {
  226. }
  227. private static void Response500()
  228. {
  229. }
  230. #endregion
  231. }
  232. }