Files
zszq-trs/YLErpDAL/OtcAppContext.cs
2024-05-09 14:06:26 +08:00

85 lines
2.5 KiB
C#

using YLErp.Models;
namespace YLErp
{
/// <summary>
/// OTC-WEB上下文
/// </summary>
public static class OtcAppContext
{
static object InitStateObject;
/// <summary>
/// 初始化OTC系统上下文
/// </summary>
public static void Initialize(Func<string, string> mapPath)
{
if (InitStateObject != null)
{
throw new Exception("已初始化");
}
InitStateObject = new object();
MapPath = mapPath ?? throw new ArgumentNullException(nameof(mapPath));
RootPath = mapPath("~/");
AppDocsPath = mapPath("~/App_Docs");
AppDataPath = mapPath("~/App_Data");
}
/// <summary>
/// 映射虚拟路径到物理路径
/// </summary>
public static Func<string, string> MapPath { get; private set; }
/// <summary>
/// WEB根目录(物理路径)
/// </summary>
public static string RootPath { get; private set; }
/// <summary>
/// App_Docs物理路径
/// </summary>
public static string AppDocsPath { get; private set; }
/// <summary>
/// App_Data物理路径
/// </summary>
public static string AppDataPath { get; private set; }
/// <summary>
/// 合成文件夹路径并确保文件夹存在
/// </summary>
public static string EnsureDirectory(string rootPath, string relativePath)
{
var path = Path.Combine(rootPath, relativePath);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
/// <summary>
/// 获取导出文件模板的物理路径(仅支持'App_Docs/导出模板'下的模板文件)
/// </summary>
public static string GetExportTemplateFilePath(string fileName)
{
return MapPath("/App_Docs/导出模板/" + fileName);
}
/// <summary>
/// 获取导出文件输出路径
/// </summary>
public static WebFileGenResult GetExportFileOutputPath(string fileName, bool isTemp = false)
{
var folder = isTemp ? "Temp" : "Download";
var result = new WebFileGenResult
{
WebPath = $"/App_Docs/{folder}/{DateTime.Today:yyyyMM}/{fileName}"
};
result.PhysicalPath = MapPath(result.WebPath);
return result;
}
}
}