| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- using Newtonsoft.Json;
- using ReleaseHelper.Models;
- using ReleaseHelper.Utils;
- using System;
- using System.Collections.Generic;
- using System.Net;
- using System.Net.Http;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace ReleaseHelper.Services
- {
- public static class Api
- {
- public delegate void DeleLogin(bool runAfterLoginFn);
- public static DeleLogin LoginFn;
- private const int PAGE_SIZE = 999999;
- private const int PAGE_NO = 1;
- /// <summary>
- /// 登录
- /// </summary>
- /// <param name="username"></param>
- /// <param name="password"></param>
- /// <returns></returns>
- public static async Task<UserLoginPost> Login(string username, string password)
- {
- UserLoginPost loginResponse = null;
- var (code, response) = await Fetch(
- new Http.Request(@"v1/user/login", Http.Method.Post,
- new Dictionary<string, object> {
- { "username", username} ,
- { "password", password}
- }
- ));
- if (!string.IsNullOrEmpty(response))
- {
- loginResponse = Json.Decode<UserLoginPost>(response);
- Variables.Variables.RequestHeaders["Authorization"] = "Bearer " + loginResponse.Token;
- }
- return loginResponse;
- }
- /// <summary>
- /// 获取静态文件流
- /// </summary>
- /// <param name="fileName"></param>
- /// <returns></returns>
- public static async Task<byte[]> GetStaticFileBytes(string fileName)
- {
- byte[] result = null;
- fileName = Regex.Replace(fileName, @"^[\/\\]*", "");
- long begin = Functions.GetTimeStamp(), end;
- Http.Request request = new Http.Request($"{Variables.Const.HTTP_STATIC_HOST}{fileName}", Http.Method.Get);
- end = Functions.GetTimeStamp();
- Log.WriteLine($"request static file create http: {end - begin}");
- begin = end;
- using (HttpResponseMessage response = await Http.Fetch(request))
- {
- end = Functions.GetTimeStamp();
- Log.WriteLine($"request static file fetch http: {end - begin}");
- begin = end;
- if (response != null && response.StatusCode == HttpStatusCode.OK)
- {
- result = response.Content.ReadAsByteArrayAsync().Result;
- end = Functions.GetTimeStamp();
- Log.WriteLine($"request static file read byte: {end - begin}");
- begin = end;
- }
- }
- return result;
- }
- /// <summary>
- /// 获取升级信息
- /// </summary>
- /// <returns></returns>
- public static async Task<Models.UpdateInfo> GetUpdateInfo(string ossFileUri)
- {
- UpdateInfo info = new UpdateInfo();
- try
- {
- string url = Regex.Replace(ossFileUri, "\\.[^.]{2,5}$", ".txt", RegexOptions.IgnoreCase);
- HttpResponseMessage response = await Http.Fetch(new Http.Request(url, Http.Method.Get));
- if (response.IsSuccessStatusCode)
- {
- string txt = await response.Content.ReadAsStringAsync();
- info = Utils.Json.Decode<UpdateInfo>(txt);
- }
- }
- catch
- {
- }
- return info;
- }
- /// <summary>
- /// 获取阿里云OSS STS 权限
- /// </summary>
- /// <returns></returns>
- public static async Task<Models.AliyunAccessModel> GetAliyunAccess()
- {
- var (code, response) = await Fetch(new Http.Request(@"v1/oss/authorize", Http.Method.Post, null));
- return Json.Decode<Models.AliyunAccessModel>(response);
- //return GetResponse<Models.AliyunAccessModel>(@"v1/oss/authorize", Http.Method.Post, null);
- }
- /// <summary>
- /// 获取系统返回的内容
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="url"></param>
- /// <param name="method"></param>
- /// <param name="data"></param>
- /// <returns></returns>
- private static async Task<T> GetResponse<T>(string url, Http.Method method, Dictionary<string, object> data)
- {
- T res = default;
- var (code, response) = await Fetch(new Http.Request(url, method, data));
- if (!string.IsNullOrEmpty(response))
- {
- var tmp = Json.Decode<Dictionary<string, object>>(response);
- if (tmp != null && tmp.ContainsKey("result"))
- {
- res = Json.Decode<T>(tmp["result"].ToString());
- }
- }
- return res;
- }
- #region 私有方法
- /// <summary>
- /// 请求
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- private static async Task<(int, string)> Fetch(Http.Request request)
- {
- foreach (var item in Variables.Variables.RequestHeaders)
- {
- // 添加请求头
- request.headers.Add(item.Key, item.Value);
- }
- // 请求地址
- request.uri = string.Concat(Variables.Const.HTTP_HOST, request.uri);
- string apiResponse = null;
- int code = 200;
- Dictionary<string, object> resp;
- using (HttpResponseMessage response = await Http.Fetch(request))
- {
- if (response != null)
- {
- switch (response.StatusCode)
- {
- case HttpStatusCode.OK:
- // 正常响应
- resp = JsonConvert.DeserializeObject<Dictionary<string, object>>(await response.Content.ReadAsStringAsync());
- //apiResponse = Json.Decode<ApiResponse>(response.Content.ReadAsStreamAsync().Result);
- code = Convert.ToInt32(resp["code"]);
- switch (code)
- {
- case 400:
- Response400();
- apiResponse = resp["errors"] != null ? resp["errors"].ToString() : "接口错误";
- break;
- case 401:
- Response401();
- break;
- case 4000:
- Response401();
- break;
- case 404:
- Response404();
- break;
- case 500:
- Response500();
- apiResponse = resp["errors"].ToString();
- break;
- case 502:
- Response500();
- break;
- //case 1001:
- // apiResponse = resp["msg"].ToString();
- //break;
- default:
- apiResponse = resp["data"] != null ? resp["data"].ToString() : "";
- break;
- }
- break;
- case HttpStatusCode.NotFound:
- // 页面不存在
- Response404();
- break;
- case HttpStatusCode.Unauthorized:
- // 无权限
- Response401();
- break;
- case HttpStatusCode.BadRequest:
- // 页面错误
- Response400();
- break;
- default:
- // 其它错误
- Response500();
- break;
- }
- }
- else
- {
- // 没有网络
- }
- }
- //Log.WriteLine($"{request.uri}:{apiResponse}");
- return (code, apiResponse);
- }
- private static void Response400()
- {
- }
- private static void Response401()
- {
- //void act()
- //{
- // Forms.Instances.FormLogin.ShowDialog(Forms.Instances.MainEntrance);
- // Forms.Instances.FormLogin.ReLogin();
- //}
- //Utils.ControlExtensions.FormInvoke(Forms.Instances.FormLogin, act);
- LoginFn?.Invoke(false);
- //Forms.Instances.FormLogin.ShowDialog(Forms.Instances.MainEntrance);
- //Forms.Instances.FormLogin.ReLogin();
- }
- private static void Response404()
- {
- }
- private static void Response500()
- {
- }
- #endregion
- }
- }
|