using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BellwetherBackend.Utility { public static class Setting { private static readonly string dataDirectory = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Data"); public static readonly string settingJsonDataPath = System.IO.Path.Combine(dataDirectory, "setting.ini"); public static readonly string frontPageDirectory = System.IO.Path.Combine(dataDirectory, "FrontPage"); public static readonly string frontPageJsonDataPath = System.IO.Path.Combine(frontPageDirectory, "frontpage.ini"); public static readonly List PageNameList = new List() { "基本設定", "首頁", "BellWether", "產品展示", "社會責任" }; public enum PageType : int { FrontPage, History, Product, CSR, } public static readonly Dictionary PageFolderName = new Dictionary() { { PageType.History, "History" }, { PageType.Product, "Product" }, { PageType.CSR, "CSR" }, }; private static SettingJson _settingJsonFile; public static SettingJson settingJsonFile { get { if (_settingJsonFile == null) { AnalyzJsonFile(); } return _settingJsonFile; } set { _settingJsonFile = value; //save Json SaveJson(); } } private static FrontPageJson _frontPageJsonFile; public static FrontPageJson frontPageJsonFile { get { if (_frontPageJsonFile == null) { AnalyzJsonFile(); } return _frontPageJsonFile; } set { _frontPageJsonFile = value; //save Json SaveJson(); } } private static void AnalyzJsonFile() { string jsonResult; if (!File.Exists(settingJsonDataPath)) { _settingJsonFile = new SettingJson() { BackHomeSec = 60, }; } else { using (StreamReader sr = new StreamReader(settingJsonDataPath)) { jsonResult = sr.ReadToEnd(); } _settingJsonFile = JsonConvert.DeserializeObject(jsonResult); } if (!File.Exists(frontPageJsonDataPath)) { _frontPageJsonFile = new FrontPageJson() { SolarEnergyUrl = "https://dsegomspoctestenv.azurewebsites.net/api/siteoverview?act=9237&token=BWsolarenergy", MediaList = new List(), }; } else { using (StreamReader sr = new StreamReader(frontPageJsonDataPath)) { jsonResult = sr.ReadToEnd(); } _frontPageJsonFile = JsonConvert.DeserializeObject(jsonResult); } } private static void SaveJson() { string jsonString;// = JsonConvert.SerializeObject(_settingJsonFile, Newtonsoft.Json.Formatting.Indented); jsonString = JsonConvert.SerializeObject(_settingJsonFile, Newtonsoft.Json.Formatting.Indented); using (StreamWriter sw = new StreamWriter(settingJsonDataPath, false, Encoding.Unicode)) { sw.Write(jsonString); sw.Close(); } jsonString = JsonConvert.SerializeObject(_frontPageJsonFile, Newtonsoft.Json.Formatting.Indented); using (StreamWriter sw = new StreamWriter(frontPageJsonDataPath, false, Encoding.Unicode)) { sw.Write(jsonString); sw.Close(); } } } public class SettingJson { public int BackHomeSec { get; set; } } public class FrontPageJson { public string SolarEnergyUrl { get; set; } public int AutoPlayIntervalSec { get; set; } = 20; public List MediaList { get; set; } } }