CMSCollection.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using KdanCommon.CMSCollection.Data;
  2. using KdanCommon.Helpers;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Net.NetworkInformation;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using Windows.Data.Json;
  12. using Windows.Globalization;
  13. using Windows.Networking.Connectivity;
  14. using Windows.UI.Popups;
  15. using Windows.Web.Http;
  16. using Windows.Web.Http.Filters;
  17. namespace KdanCommon.CMSCollection
  18. {
  19. public class CMSCollection
  20. {
  21. public static string CMSDomain = "https://cms.kdanmobile.com";
  22. private List<ISetting> _settingList = null;
  23. private HttpClient _httpClient = null;
  24. private Uri _windowsCardsUri = null;
  25. private Uri _windowsEventbarUri = null;
  26. private Uri _pdfViewerEventBarSettingUri = null;
  27. private Uri _pdfOtherSettingUri = null;
  28. private string _appType = null;
  29. private bool _loadCardsOnly = false;
  30. private SemaphoreSlim _loadSettingSS = new SemaphoreSlim(1);
  31. private bool _isLoaded = false;
  32. private object settingLock = new object();
  33. public CMSCollection(string appType, bool loadCardOnly = true)
  34. {
  35. var filter = new HttpBaseProtocolFilter();
  36. filter.CacheControl.ReadBehavior = HttpCacheReadBehavior.MostRecent;
  37. _httpClient = new HttpClient(filter);
  38. _windowsCardsUri = new Uri($"{CMSDomain}/items/windows_cards");
  39. _pdfViewerEventBarSettingUri = new Uri($"{CMSDomain}/items/pdf_remote_config");
  40. _pdfOtherSettingUri = new Uri($"{CMSDomain}/items/PDF_UWP_RemoteConfig");
  41. _windowsEventbarUri = new Uri($"{CMSDomain}/items/windows_eventbar");
  42. _appType = appType;
  43. _loadCardsOnly = loadCardOnly;
  44. _settingList = new List<ISetting>();
  45. Windows.Networking.Connectivity.NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged;
  46. }
  47. ~CMSCollection()
  48. {
  49. _httpClient.Dispose();
  50. Windows.Networking.Connectivity.NetworkInformation.NetworkStatusChanged -= NetworkInformation_NetworkStatusChanged;
  51. }
  52. private async void NetworkInformation_NetworkStatusChanged(object sender)
  53. {
  54. if (IsInternetAvailable())
  55. await LoadSettings();
  56. }
  57. #region Public Method
  58. public async Task<ISetting> GetSetting(string settingId)
  59. {
  60. await LoadSettings();
  61. lock (settingLock)
  62. return _settingList.FirstOrDefault(s => s.SettingName == settingId);
  63. }
  64. public async Task<List<WindowsCardSetting>> GetCardSettings()
  65. {
  66. await LoadSettings();
  67. lock (settingLock)
  68. return _settingList.Where(s => s is WindowsCardSetting).Select(s => s as WindowsCardSetting).OrderByDescending(w => w.Weight).ToList();
  69. }
  70. #endregion
  71. #region Private Method
  72. private async Task LoadSettings()
  73. {
  74. try
  75. {
  76. await _loadSettingSS.WaitAsync();
  77. if (!_isLoaded)
  78. {
  79. lock (settingLock)
  80. _settingList.Clear();
  81. if (!IsInternetAvailable())
  82. return;
  83. var allTask = new List<Task>();
  84. allTask.Add(GetWindowsCards());
  85. if (!_loadCardsOnly)
  86. {
  87. allTask.Add(GetViewerEventBarSetting());
  88. allTask.Add(GetOtherSetting());
  89. }
  90. await Task.WhenAll(allTask);
  91. _isLoaded = true;
  92. }
  93. }
  94. catch
  95. {
  96. }
  97. finally
  98. {
  99. _loadSettingSS.Release();
  100. }
  101. }
  102. private async Task GetViewerEventBarSetting()
  103. {
  104. var region = GetSystemRegion();
  105. var appLang = GetAppLanguage();
  106. var querys = new List<KeyValuePair<string, string>>();
  107. querys.Add(new KeyValuePair<string, string>("deep[evnet_bar_text_translations][_filter][languages_code][_eq]", appLang));
  108. querys.Add(new KeyValuePair<string, string>("deep[dialog_text_translations][_filter][languages_code][_eq]", appLang));
  109. querys.Add(new KeyValuePair<string, string>("deep[dialog_button][dialog_button_translations][_filter][languages_code][_eq]", appLang));
  110. querys.Add(new KeyValuePair<string, string>("deep[do_not_show_translations][_filter][languages_code][_eq]", appLang));
  111. querys.Add(new KeyValuePair<string, string>("filter[platform][_eq]", "windows"));
  112. querys.Add(new KeyValuePair<string, string>("filter[event_visible][_eq]", "true"));
  113. querys.Add(new KeyValuePair<string, string>("filter[regions][_contains]", region));
  114. querys.Add(new KeyValuePair<string, string>("fields", "*,do_not_show_translations.*,evnet_bar_text_translations.*,dialog_text_translations.*,dialog_button.*,dialog_button.dialog_button_translations.*"));
  115. var result = await _httpClient.GetRequest(_pdfViewerEventBarSettingUri, querys);
  116. if (result.StatusCode == HttpStatusCode.Ok)
  117. {
  118. var jsonString = await result.Content.ReadAsStringAsync();
  119. var response = JsonTool.DeserializeJSON<ViewerEventBarSettingResponse>(jsonString);
  120. if (response != null && response.Data != null && response.Data.Length > 0)
  121. {
  122. lock (settingLock)
  123. _settingList.Add(response.Data[0]);
  124. }
  125. }
  126. }
  127. private async Task GetOtherSetting()
  128. {
  129. var querys = new List<KeyValuePair<string, string>>();
  130. querys.Add(new KeyValuePair<string, string>("filter[status][_eq]", "published"));
  131. querys.Add(new KeyValuePair<string, string>("filter[appTypes][_contains]", _appType));
  132. querys.Add(new KeyValuePair<string, string>("fields", "*"));
  133. var result = await _httpClient.GetRequest(_pdfOtherSettingUri, querys);
  134. if (result.StatusCode == HttpStatusCode.Ok)
  135. {
  136. var jsonString = await result.Content.ReadAsStringAsync();
  137. dynamic dyna = DynamicJsonHelper.ToDynamicJson(jsonString);
  138. if (dyna == null)
  139. return;
  140. List<dynamic> settings = DynamicJsonHelper.GetArraySafety(dyna.data);
  141. if (settings == null)
  142. return;
  143. foreach (var setting in settings)
  144. {
  145. string configName = DynamicJsonHelper.GetValueSafety<string>(setting.config_name, null);
  146. if (!string.IsNullOrEmpty(configName) && setting.json_data != null)
  147. {
  148. lock (settingLock)
  149. _settingList.Add(new JsonSetting(configName, setting.json_data));
  150. }
  151. }
  152. }
  153. }
  154. private async Task GetWindowsCards()
  155. {
  156. string region = GetSystemRegion();
  157. string lang = GetAppLanguage();
  158. var querys = new List<KeyValuePair<string, string>>();
  159. querys.Add(new KeyValuePair<string, string>("deep[Information][_filter][languages_code][_eq]", lang));
  160. querys.Add(new KeyValuePair<string, string>("filter[_or][0][include_regions][_eq]", "true"));
  161. querys.Add(new KeyValuePair<string, string>("filter[_or][0][regions][_contains]", region));
  162. querys.Add(new KeyValuePair<string, string>("filter[_or][1][include_regions][_eq]", "false"));
  163. querys.Add(new KeyValuePair<string, string>("filter[_or][1][regions][_ncontains]", region));
  164. querys.Add(new KeyValuePair<string, string>("filter[appTypes][_contains]", _appType));
  165. querys.Add(new KeyValuePair<string, string>("filter[status][_eq]", "published"));
  166. querys.Add(new KeyValuePair<string, string>("fields", "*,Information.*"));
  167. var result = await _httpClient.GetRequest(_windowsCardsUri, querys);
  168. if (result.StatusCode == HttpStatusCode.Ok)
  169. {
  170. var jsonString = await result.Content.ReadAsStringAsync();
  171. var response = JsonTool.DeserializeJSON<WindowsCardsResponse>(jsonString);
  172. if (response != null && response.Data != null && response.Data.Length > 0)
  173. {
  174. foreach (var card in response.Data)
  175. {
  176. lock (settingLock)
  177. _settingList.Add(card);
  178. }
  179. }
  180. }
  181. }
  182. public async Task<List<WindowsEventbarSetting>> GetWindowsEventbarAsync()
  183. {
  184. var current = DateTime.UtcNow.ToString("s", CultureInfo.GetCultureInfo("en-us"));
  185. string region = GetSystemRegion();
  186. string lang = GetAppLanguage();
  187. var querys = new List<KeyValuePair<string, string>>();
  188. querys.Add(new KeyValuePair<string, string>("deep[event_information][_filter][languages_code][_eq]", lang));
  189. querys.Add(new KeyValuePair<string, string>("filter[_or][0][include_regions][_eq]", "true"));
  190. querys.Add(new KeyValuePair<string, string>("filter[_or][0][regions][_contains]", region));
  191. querys.Add(new KeyValuePair<string, string>("filter[_or][1][include_regions][_eq]", "false"));
  192. querys.Add(new KeyValuePair<string, string>("filter[_or][1][regions][_ncontains]", region));
  193. querys.Add(new KeyValuePair<string, string>("filter[_or][2][include_regions][_eq]", "false"));
  194. querys.Add(new KeyValuePair<string, string>("filter[_or][2][regions][_null]", "true"));
  195. querys.Add(new KeyValuePair<string, string>("filter[start][_lte]", current));
  196. querys.Add(new KeyValuePair<string, string>("filter[end][_gte]", current));
  197. querys.Add(new KeyValuePair<string, string>("filter[app_types][_contains]", _appType));
  198. querys.Add(new KeyValuePair<string, string>("filter[status][_eq]", "published"));
  199. querys.Add(new KeyValuePair<string, string>("fields", "*,event_information.*"));
  200. var result = await _httpClient.GetRequest(_windowsEventbarUri, querys);
  201. if (result.StatusCode == HttpStatusCode.Ok)
  202. {
  203. var jsonString = await result.Content.ReadAsStringAsync();
  204. var response = JsonTool.DeserializeJSON<WindowsEventbarResponse>(jsonString);
  205. if (response != null && response.Data != null && response.Data.Length > 0)
  206. {
  207. return response.Data.ToList();
  208. }
  209. else
  210. return new List<WindowsEventbarSetting>();
  211. }
  212. return null;
  213. }
  214. private string GetSystemRegion()
  215. {
  216. var geographyCountryCode = new GeographicRegion().CodeTwoLetter;
  217. switch (geographyCountryCode)
  218. {
  219. case "GB":
  220. case "US":
  221. case "TW":
  222. case "CN":
  223. case "JP":
  224. case "ES":
  225. case "IT":
  226. case "FR":
  227. case "DE":
  228. case "RU":
  229. case "PT":
  230. case "KR":
  231. return geographyCountryCode;
  232. default:
  233. return "OTHER";
  234. }
  235. }
  236. private string GetAppLanguage()
  237. {
  238. try
  239. {
  240. var currentLang = Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Languages[0];
  241. switch (currentLang)
  242. {
  243. case "de-DE":
  244. case "de":
  245. return "de";
  246. case "es-ES":
  247. case "es":
  248. return "es";
  249. case "fr-FR":
  250. case "fr":
  251. return "fr";
  252. case "it-IT":
  253. case "it":
  254. return "it";
  255. case "ja-JP":
  256. case "ja":
  257. return "ja";
  258. case "zh-Hans-CN":
  259. case "zh-Hans":
  260. case "zh-CN":
  261. return "zh-cn";
  262. case "zh-Hant-TW":
  263. case "zh-Hant":
  264. case "zh-TW":
  265. return "zh-tw";
  266. case "ru-RU":
  267. case "ru":
  268. return "ru";
  269. case "ko-KR":
  270. case "ko":
  271. return "ko";
  272. case "pt-PT":
  273. case "pt":
  274. return "pt";
  275. default:
  276. return "en";
  277. }
  278. }
  279. catch
  280. {
  281. return "en";
  282. }
  283. }
  284. private bool IsInternetAvailable()
  285. {
  286. ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
  287. bool isInternetAvailable = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
  288. return isInternetAvailable;
  289. }
  290. #endregion
  291. }
  292. }