using YLErp.Models; namespace YLErp { /// /// OTC-WEB上下文 /// public static class OtcAppContext { static object InitStateObject; /// /// 初始化OTC系统上下文 /// public static void Initialize(Func 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"); } /// /// 映射虚拟路径到物理路径 /// public static Func MapPath { get; private set; } /// /// WEB根目录(物理路径) /// public static string RootPath { get; private set; } /// /// App_Docs物理路径 /// public static string AppDocsPath { get; private set; } /// /// App_Data物理路径 /// public static string AppDataPath { get; private set; } /// /// 合成文件夹路径并确保文件夹存在 /// public static string EnsureDirectory(string rootPath, string relativePath) { var path = Path.Combine(rootPath, relativePath); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } return path; } /// /// 获取导出文件模板的物理路径(仅支持'App_Docs/导出模板'下的模板文件) /// public static string GetExportTemplateFilePath(string fileName) { return MapPath("/App_Docs/导出模板/" + fileName); } /// /// 获取导出文件输出路径 /// 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; } } }