Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
894d6930f7 | ||
|
|
106d3eec19 | ||
|
|
a8c7a20a64 | ||
|
|
b6dbcd6cee |
@@ -20,7 +20,7 @@ public class CoceController(CoceApp coce, SimApiAuth auth, IServiceProvider sp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public SimApiBaseResponse<string> Login([FromBody] SimApiOneFieldRequest<string> request)
|
public string Login([FromBody] SimApiOneFieldRequest<string> request)
|
||||||
{
|
{
|
||||||
var data = coce.GetLevelToken(request.Data!);
|
var data = coce.GetLevelToken(request.Data!);
|
||||||
ErrorWhenNull(data, 400);
|
ErrorWhenNull(data, 400);
|
||||||
@@ -39,14 +39,14 @@ public class CoceController(CoceApp coce, SimApiAuth auth, IServiceProvider sp)
|
|||||||
};
|
};
|
||||||
var processor = sp.GetService<ICoceLoginProcessor>();
|
var processor = sp.GetService<ICoceLoginProcessor>();
|
||||||
processor?.Process(loginItem, groups.ToArray());
|
processor?.Process(loginItem, groups.ToArray());
|
||||||
return new SimApiBaseResponse<string>(auth.Login(loginItem));
|
return auth.Login(loginItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost, SimApiAuth]
|
[HttpPost, SimApiAuth]
|
||||||
public SimApiBaseResponse<GroupInfo[]> ListGroups()
|
public GroupInfo[] ListGroups()
|
||||||
{
|
{
|
||||||
var levelToken = coce.GetToken(LoginInfo.Id!);
|
var levelToken = coce.GetToken(LoginInfo.Id!);
|
||||||
var groups = coce.GetUserGroups(levelToken!)!;
|
var groups = coce.GetUserGroups(levelToken!)!;
|
||||||
return new SimApiBaseResponse<GroupInfo[]>(groups.ToArray());
|
return groups.ToArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -14,13 +14,10 @@ public class SimApiAuthController(SimApiAuth auth) : SimApiBaseController
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost, SimApiDoc("认证", "检测登陆")]
|
[HttpPost, SimApiDoc("认证", "检测登陆")]
|
||||||
public SimApiBaseResponse<string> CheckLogin()
|
public string CheckLogin()
|
||||||
{
|
{
|
||||||
ErrorWhenNull(LoginInfo, 401, "未登录");
|
ErrorWhenNull(LoginInfo, 401, "未登录");
|
||||||
return new SimApiBaseResponse<string>
|
return LoginInfo.Id;
|
||||||
{
|
|
||||||
Data = LoginInfo.Id
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -28,23 +25,15 @@ public class SimApiAuthController(SimApiAuth auth) : SimApiBaseController
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost, SimApiDoc("认证", "退出登陆")]
|
[HttpPost, SimApiDoc("认证", "退出登陆")]
|
||||||
public SimApiBaseResponse Logout()
|
public void Logout()
|
||||||
{
|
{
|
||||||
string? token = null;
|
|
||||||
|
|
||||||
if (Request.Headers.TryGetValue("Token", out var value))
|
if (Request.Headers.TryGetValue("Token", out var value))
|
||||||
{
|
{
|
||||||
token = value;
|
auth.Logout(value!);
|
||||||
}
|
}
|
||||||
|
|
||||||
auth.Logout(token!);
|
|
||||||
return new SimApiBaseResponse();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[HttpPost, SimApiAuth]
|
[HttpPost, SimApiAuth, SimApiDoc("认证", "获取已登录用户信息")]
|
||||||
public SimApiBaseResponse<SimApiLoginItem> UserInfo()
|
public SimApiLoginItem UserInfo() => LoginInfo;
|
||||||
{
|
|
||||||
return new SimApiBaseResponse<SimApiLoginItem>(LoginInfo);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using SimApi.Communications;
|
|
||||||
using SimApi.Helpers;
|
using SimApi.Helpers;
|
||||||
|
|
||||||
namespace SimApi.Controllers;
|
namespace SimApi.Controllers;
|
||||||
@@ -14,22 +13,19 @@ public class SimApiCommonController : SimApiBaseController
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet("exception/{code:int}")]
|
[HttpGet("exception/{code:int}")]
|
||||||
[ApiExplorerSettings(IgnoreApi = true)]
|
[ApiExplorerSettings(IgnoreApi = true)]
|
||||||
public SimApiBaseResponse ExceptionHandler(int code)
|
public void ExceptionHandler(int code)
|
||||||
{
|
{
|
||||||
return new SimApiBaseResponse(code);
|
SimApiError.Error(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[HttpPost, HttpGet]
|
[HttpPost, HttpGet]
|
||||||
public SimApiBaseResponse<Dictionary<string, string>> Versions()
|
public Dictionary<string, string> Versions()
|
||||||
{
|
{
|
||||||
return new SimApiBaseResponse<Dictionary<string, string>>()
|
return new Dictionary<string, string>
|
||||||
{
|
{
|
||||||
Data = new Dictionary<string, string>
|
{ "SimApi", SimApiUtil.SimApiVersion },
|
||||||
{
|
{ "App", SimApiUtil.AppVersion }
|
||||||
{ "SimApi", SimApiUtil.SimApiVersion },
|
|
||||||
{ "App", SimApiUtil.AppVersion }
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+14
-3
@@ -8,13 +8,14 @@ public class SimApiCache(IDistributedCache cache)
|
|||||||
private const string Prefix = "SimApi:Cache:";
|
private const string Prefix = "SimApi:Cache:";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设置缓存
|
/// 设置缓存 (值不能为null)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="key"></param>
|
/// <param name="key"></param>
|
||||||
/// <param name="value"></param>
|
/// <param name="value"></param>
|
||||||
/// <param name="options"></param>
|
/// <param name="options"></param>
|
||||||
public void Set(string key, object value, DistributedCacheEntryOptions? options = null)
|
public void Set(string key, object value, DistributedCacheEntryOptions? options = null)
|
||||||
{
|
{
|
||||||
|
SimApiError.ErrorWhenNull(value, 400, "缓存值不能为null");
|
||||||
if (options is not null)
|
if (options is not null)
|
||||||
{
|
{
|
||||||
cache.SetString(Prefix + key, SimApiUtil.Json(value), options);
|
cache.SetString(Prefix + key, SimApiUtil.Json(value), options);
|
||||||
@@ -34,6 +35,16 @@ public class SimApiCache(IDistributedCache cache)
|
|||||||
cache.Remove(Prefix + key);
|
cache.Remove(Prefix + key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 缓存Key是否存在
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public bool HasKey(string key)
|
||||||
|
{
|
||||||
|
return Get<string>(key) != null;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取string类型缓存
|
/// 获取string类型缓存
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -41,7 +52,7 @@ public class SimApiCache(IDistributedCache cache)
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public string? Get(string key)
|
public string? Get(string key)
|
||||||
{
|
{
|
||||||
return cache.GetString(Prefix + key);
|
return Get<string>(Prefix + key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -53,6 +64,6 @@ public class SimApiCache(IDistributedCache cache)
|
|||||||
public T? Get<T>(string key)
|
public T? Get<T>(string key)
|
||||||
{
|
{
|
||||||
var data = cache.GetString(Prefix + key);
|
var data = cache.GetString(Prefix + key);
|
||||||
return data == null ? default : JsonSerializer.Deserialize<T>(data);
|
return data == null ? default : SimApiUtil.FromJson<T>(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+1
-12
@@ -11,6 +11,7 @@ public static class SimApiError
|
|||||||
/// <param name="code">错误代码</param>
|
/// <param name="code">错误代码</param>
|
||||||
/// <param name="message">错误描述(若是常规错误,代码可自动带取描述)</param>
|
/// <param name="message">错误描述(若是常规错误,代码可自动带取描述)</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
[DoesNotReturn]
|
||||||
public static void Error(int code = 500, string message = "")
|
public static void Error(int code = 500, string message = "")
|
||||||
{
|
{
|
||||||
throw new SimApiException(code, message);
|
throw new SimApiException(code, message);
|
||||||
@@ -68,16 +69,4 @@ public static class SimApiError
|
|||||||
{
|
{
|
||||||
ErrorWhen(condition == null, code, message);
|
ErrorWhen(condition == null, code, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 检测给定的泛型变量是否为Null(提供更精确的编译器 null 流分析)
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="condition">检测条件</param>
|
|
||||||
/// <param name="code">错误代码</param>
|
|
||||||
/// <param name="message">错误描述</param>
|
|
||||||
public static void ErrorWhenNull<T>([NotNull] T? condition, int code = 404,
|
|
||||||
string message = "请求的资源不存在") where T : class
|
|
||||||
{
|
|
||||||
ErrorWhen(condition == null, code, message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net.Mail;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -94,6 +95,25 @@ public static class SimApiUtil
|
|||||||
return regex.IsMatch(cell);
|
return regex.IsMatch(cell);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 判断是否是Email地址
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="email"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool CheckEmail(string email)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var m = new MailAddress(email);
|
||||||
|
return m.Address == email;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// MD5加密字符串
|
/// MD5加密字符串
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -154,6 +174,17 @@ public static class SimApiUtil
|
|||||||
return JsonSerializer.Serialize(obj, JsonOption);
|
return JsonSerializer.Serialize(obj, JsonOption);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 将JSON解析为对象(控制台输出中文不会被编码)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="jsonString"></param>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static T? FromJson<T>(string jsonString)
|
||||||
|
{
|
||||||
|
return JsonSerializer.Deserialize<T>(jsonString, JsonOption);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 分页
|
/// 分页
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user