Setting.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace BellwetherBackend.Utility
  9. {
  10. public static class Setting
  11. {
  12. private static readonly string dataDirectory = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Data");
  13. public static readonly string settingJsonDataPath = System.IO.Path.Combine(dataDirectory, "setting.ini");
  14. public static readonly string frontPageDirectory = System.IO.Path.Combine(dataDirectory, "FrontPage");
  15. public static readonly string frontPageJsonDataPath = System.IO.Path.Combine(frontPageDirectory, "frontpage.ini");
  16. public static readonly List<string> PageNameList = new List<string>() { "基本設定", "首頁", "BellWether", "產品展示", "社會責任" };
  17. public enum PageType : int
  18. {
  19. FrontPage,
  20. History,
  21. Product,
  22. CSR,
  23. }
  24. public static readonly Dictionary<PageType, string> PageFolderName
  25. = new Dictionary<PageType, string>() {
  26. { PageType.History, "History" },
  27. { PageType.Product, "Product" },
  28. { PageType.CSR, "CSR" },
  29. };
  30. private static SettingJson _settingJsonFile;
  31. public static SettingJson settingJsonFile
  32. {
  33. get
  34. {
  35. if (_settingJsonFile == null)
  36. {
  37. AnalyzJsonFile();
  38. }
  39. return _settingJsonFile;
  40. }
  41. set
  42. {
  43. _settingJsonFile = value;
  44. //save Json
  45. SaveJson();
  46. }
  47. }
  48. private static FrontPageJson _frontPageJsonFile;
  49. public static FrontPageJson frontPageJsonFile
  50. {
  51. get
  52. {
  53. if (_frontPageJsonFile == null)
  54. {
  55. AnalyzJsonFile();
  56. }
  57. return _frontPageJsonFile;
  58. }
  59. set
  60. {
  61. _frontPageJsonFile = value;
  62. //save Json
  63. SaveJson();
  64. }
  65. }
  66. private static void AnalyzJsonFile()
  67. {
  68. string jsonResult;
  69. if (!File.Exists(settingJsonDataPath))
  70. {
  71. _settingJsonFile = new SettingJson()
  72. {
  73. BackHomeSec = 60,
  74. };
  75. }
  76. else
  77. {
  78. using (StreamReader sr = new StreamReader(settingJsonDataPath))
  79. {
  80. jsonResult = sr.ReadToEnd();
  81. }
  82. _settingJsonFile = JsonConvert.DeserializeObject<SettingJson>(jsonResult);
  83. }
  84. if (!File.Exists(frontPageJsonDataPath))
  85. {
  86. _frontPageJsonFile = new FrontPageJson()
  87. {
  88. SolarEnergyUrl = "https://dsegomspoctestenv.azurewebsites.net/api/siteoverview?act=9237&token=BWsolarenergy",
  89. MediaList = new List<string>(),
  90. };
  91. }
  92. else
  93. {
  94. using (StreamReader sr = new StreamReader(frontPageJsonDataPath))
  95. {
  96. jsonResult = sr.ReadToEnd();
  97. }
  98. _frontPageJsonFile = JsonConvert.DeserializeObject<FrontPageJson>(jsonResult);
  99. }
  100. }
  101. private static void SaveJson()
  102. {
  103. string jsonString;// = JsonConvert.SerializeObject(_settingJsonFile, Newtonsoft.Json.Formatting.Indented);
  104. jsonString = JsonConvert.SerializeObject(_settingJsonFile, Newtonsoft.Json.Formatting.Indented);
  105. using (StreamWriter sw = new StreamWriter(settingJsonDataPath, false, Encoding.Unicode))
  106. {
  107. sw.Write(jsonString);
  108. sw.Close();
  109. }
  110. jsonString = JsonConvert.SerializeObject(_frontPageJsonFile, Newtonsoft.Json.Formatting.Indented);
  111. using (StreamWriter sw = new StreamWriter(frontPageJsonDataPath, false, Encoding.Unicode))
  112. {
  113. sw.Write(jsonString);
  114. sw.Close();
  115. }
  116. }
  117. }
  118. public class SettingJson
  119. {
  120. public int BackHomeSec { get; set; }
  121. }
  122. public class FrontPageJson
  123. {
  124. public string SolarEnergyUrl { get; set; }
  125. public int AutoPlayIntervalSec { get; set; } = 20;
  126. public List<string> MediaList { get; set; }
  127. }
  128. }