身份验证
This commit is contained in:
88
Hotfix/Api/Base/NBControllerBase.cs
Normal file
88
Hotfix/Api/Base/NBControllerBase.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace NBF;
|
||||
|
||||
public abstract class NBControllerBase : ControllerBase
|
||||
{
|
||||
protected static readonly string BaseRootPath = $"{AppContext.BaseDirectory}wwwroot/";
|
||||
|
||||
|
||||
public IActionResult Html(string fileName)
|
||||
{
|
||||
var filePath = Path.Combine(BaseRootPath, $"{fileName}.html");
|
||||
|
||||
if (System.IO.File.Exists(filePath))
|
||||
{
|
||||
return PhysicalFile(filePath, "text/html");
|
||||
}
|
||||
|
||||
return Content($"{fileName}.html not found");
|
||||
}
|
||||
|
||||
public OkObjectResult Error(ErrorCode code, string msg = "")
|
||||
{
|
||||
var res = new ResponseData<string>
|
||||
{
|
||||
Code = (int)code,
|
||||
Data = msg
|
||||
};
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
public OkObjectResult Success()
|
||||
{
|
||||
var res = new ResponseData<string>
|
||||
{
|
||||
Code = 0,
|
||||
Data = string.Empty
|
||||
};
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
public OkObjectResult Success<T>(T data)
|
||||
{
|
||||
var res = new ResponseData<T>
|
||||
{
|
||||
Code = 0,
|
||||
Data = data
|
||||
};
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
#region 工具方法
|
||||
|
||||
/// <summary>
|
||||
/// 获得请求url参数
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected Dictionary<string, string> GetQuery()
|
||||
{
|
||||
Dictionary<string, string> paramMap = new Dictionary<string, string>();
|
||||
var request = HttpContext.Request;
|
||||
foreach (var keyValuePair in request.Query)
|
||||
paramMap.Add(keyValuePair.Key, keyValuePair.Value[0]);
|
||||
|
||||
return paramMap;
|
||||
}
|
||||
|
||||
protected Dictionary<string, string> GetHeaders()
|
||||
{
|
||||
Dictionary<string, string> paramMap = new Dictionary<string, string>();
|
||||
var request = HttpContext.Request;
|
||||
foreach (var keyValuePair in request.Headers)
|
||||
paramMap.Add(keyValuePair.Key, keyValuePair.Value[0]);
|
||||
|
||||
return paramMap;
|
||||
}
|
||||
|
||||
protected static void TryCreateDir(string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path)) return;
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
Directory.CreateDirectory(path);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
9
Hotfix/Api/Base/ResponseData.cs
Normal file
9
Hotfix/Api/Base/ResponseData.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace NBF;
|
||||
|
||||
public class ResponseData<T>
|
||||
{
|
||||
[JsonPropertyName("code")] public int Code { get; set; }
|
||||
[JsonPropertyName("data")] public T Data { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user