| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- using DaJiaoYan.Models;
- using DaJiaoYan.Utils;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Net;
- using System.Net.Http;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace DaJiaoYan.Services
- {
- public static class Api
- {
- public delegate void DeleLogin(bool runAfterLoginFn);
- public static DeleLogin LoginFn;
- /// <summary>
- /// 枚举类型
- /// </summary>
- public static class Enumerates
- {
- /// <summary>
- /// 考号类型
- /// </summary>
- public enum ExamNumberType
- {
- /// <summary>
- /// 系统号(云学情学号)
- /// </summary>
- System = 1,
- /// <summary>
- /// 学校学号
- /// </summary>
- School = 2,
- }
- /// <summary>
- /// 是否获取所有有权限参考学生
- /// </summary>
- public enum AllExamStudentType
- {
- /// <summary>
- /// 是
- /// </summary>
- Yes = 1,
- /// <summary>
- /// 只获取考试的学生
- /// </summary>
- No = 0,
- }
- /// <summary>
- /// 搜索名称类型
- /// </summary>
- public enum ExamStudentSearchNameType
- {
- /// <summary>
- /// 所有
- /// </summary>
- All = 0,
- /// <summary>
- /// 只搜索账号名
- /// </summary>
- OnlyUserName = 1,
- /// <summary>
- /// 用户名跟真实姓名
- /// </summary>
- UserNameAndRealName = 2,
- }
- }
- private const int PAGE_SIZE = 999999;
- private const int PAGE_NO = 1;
- /// <summary>
- /// 获取试卷划分区域信息
- /// </summary>
- /// <param name="testId">试卷ID</param>
- /// <returns></returns>
- public static async Task<DividePartitionConfig> GetDividePartitionConfig(int testId, string token = null)
- {
- DividePartitionConfig res = null;
- Dictionary<string, object> keyValuePairs;
- var (code, response) = await Fetch(new Http.Request(@"v1/examination/divide-partition", Http.Method.Get, new Dictionary<string, object>()
- {
- {"test_id", testId },
- {"page_size", 99 },
- {"page_no", 1 },
- {"correct_type", 0 },
- }), token);
- if (!string.IsNullOrEmpty(response) && code == 200)
- {
- res = Json.Decode<DividePartitionConfig>(response);
- keyValuePairs = Json.Decode<Dictionary<string, object>>(response);
- keyValuePairs.TryGetValue("template_info", out object templateInfoObj);
- if (templateInfoObj != null)
- {
- Dictionary<string, object> templateInfo = Json.Decode<Dictionary<string, object>>(Json.Encode(templateInfoObj));
- templateInfo.TryGetValue("temp_rect", out object tempRect);
- if (tempRect != null)
- {
- res.Template.TemplateRect = new TemplateRect(res.Template.ParsedVersion, Json.Encode(tempRect));
- }
- }
- }
- return res;
- }
- /// <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()
- {
- UpdateInfo info = new UpdateInfo();
- try
- {
- string url = Regex.Replace(Variables.Const.HTTP_UPDATE_FILE, "\\.[^.]{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(string token = null)
- {
- var (_, response) = await Fetch(new Http.Request(@"v1/oss/authorize", Http.Method.Post, null), token);
- Models.AliyunAccessModel res = null;
- if (response != null)
- {
- res = Json.Decode<Models.AliyunAccessModel>(response);
- }
- return res;
- }
- public static async Task<(int, string)> PostExaminationScanResult(Models.ScanResultPost p, string token = null)
- {
- var req = new Http.Request(@"v1/examination/scan-result", Http.Method.Post)
- {
- data = Json.Decode<Dictionary<string, object>>(Json.Encode(p))
- };
- return await Fetch(req, token);
- }
- /// <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 (_, 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, string token = null)
- {
- foreach (var item in Variables.Vars.RequestHeaders)
- {
- // 添加请求头
- request.headers.Add(item.Key, item.Value);
- }
- if (!string.IsNullOrEmpty(token))
- {
- request.headers["Authorization"] = $"Bearer {token}";
- request.headers["AppName"] = "com.teacherUploadMark.web";
- }
- // 请求地址
- #if DEBUG
- if (request.uri.IndexOf("/oss/") >= 0 && Variables.Const.HTTP_HOST.IndexOf("local") >= 0)
- {
- request.uri = string.Concat("http://local.api.dajiaoyan.com/", request.uri);
- }
- else
- {
- request.uri = string.Concat(Variables.Const.HTTP_HOST, request.uri);
- }
- #else
- // 请求地址
- request.uri = string.Concat(Variables.Const.HTTP_HOST, request.uri);
- #endif
- 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());
- 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;
- default:
- apiResponse = resp["data"] != null ? resp["data"].ToString() : "";
- break;
- }
- break;
- case HttpStatusCode.NotFound:
- // 页面不存在
- Response404();
- break;
- case HttpStatusCode.Unauthorized:
- // 无权限
- if (string.IsNullOrEmpty(token))
- {
- Response401();
- }
- break;
- case HttpStatusCode.BadRequest:
- // 页面错误
- Response400();
- break;
- default:
- // 其它错误
- Response500();
- break;
- }
- }
- else
- {
- // 没有网络
- }
- }
- return (code, apiResponse);
- }
- private static void Response400()
- {
- }
- private static void Response401()
- {
- LoginFn?.Invoke(false);
- }
- private static void Response404()
- {
- }
- private static void Response500()
- {
- }
- #endregion
- }
- }
|