CMSCollection.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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<ViewerEventBarSetting>> GetPdfEventBarSettings()
  65. {
  66. await LoadSettings();
  67. lock (settingLock)
  68. return _settingList.Where(s => s is ViewerEventBarSetting).Select(s => s as ViewerEventBarSetting).ToList();
  69. }
  70. public async Task<List<WindowsCardSetting>> GetCardSettings()
  71. {
  72. await LoadSettings();
  73. lock (settingLock)
  74. return _settingList.Where(s => s is WindowsCardSetting).Select(s => s as WindowsCardSetting).OrderByDescending(w => w.Weight).ToList();
  75. }
  76. #endregion
  77. #region Private Method
  78. private async Task LoadSettings()
  79. {
  80. try
  81. {
  82. await _loadSettingSS.WaitAsync();
  83. if (!_isLoaded)
  84. {
  85. lock (settingLock)
  86. _settingList.Clear();
  87. if (!IsInternetAvailable())
  88. return;
  89. var allTask = new List<Task>();
  90. allTask.Add(GetWindowsCards());
  91. if (!_loadCardsOnly)
  92. {
  93. allTask.Add(GetViewerEventBarSetting());
  94. allTask.Add(GetOtherSetting());
  95. }
  96. await Task.WhenAll(allTask);
  97. _isLoaded = true;
  98. }
  99. }
  100. catch
  101. {
  102. }
  103. finally
  104. {
  105. _loadSettingSS.Release();
  106. }
  107. }
  108. private async Task GetViewerEventBarSetting()
  109. {
  110. var region = GetSystemRegion();
  111. var appLang = GetAppLanguage();
  112. var querys = new List<KeyValuePair<string, string>>();
  113. querys.Add(new KeyValuePair<string, string>("deep[evnet_bar_text_translations][_filter][languages_code][_eq]", appLang));
  114. querys.Add(new KeyValuePair<string, string>("deep[dialog_text_translations][_filter][languages_code][_eq]", appLang));
  115. querys.Add(new KeyValuePair<string, string>("deep[dialog_button][dialog_button_translations][_filter][languages_code][_eq]", appLang));
  116. querys.Add(new KeyValuePair<string, string>("deep[do_not_show_translations][_filter][languages_code][_eq]", appLang));
  117. querys.Add(new KeyValuePair<string, string>("filter[platform][_eq]", "windows"));
  118. querys.Add(new KeyValuePair<string, string>("filter[event_visible][_eq]", "true"));
  119. querys.Add(new KeyValuePair<string, string>("filter[regions][_contains]", region));
  120. 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.*"));
  121. var result = await _httpClient.GetRequest(_pdfViewerEventBarSettingUri, querys);
  122. if (result.StatusCode == HttpStatusCode.Ok)
  123. {
  124. var jsonString = await result.Content.ReadAsStringAsync();
  125. var response = JsonTool.DeserializeJSON<ViewerEventBarSettingResponse>(jsonString);
  126. if (response != null && response.Data != null)
  127. {
  128. for (int i = 0; i < response.Data.Length; i++)
  129. {
  130. lock (settingLock)
  131. _settingList.Add(response.Data[i]);
  132. }
  133. }
  134. }
  135. }
  136. private async Task GetOtherSetting()
  137. {
  138. var querys = new List<KeyValuePair<string, string>>();
  139. querys.Add(new KeyValuePair<string, string>("filter[status][_eq]", "published"));
  140. querys.Add(new KeyValuePair<string, string>("filter[appTypes][_contains]", _appType));
  141. querys.Add(new KeyValuePair<string, string>("fields", "*"));
  142. var result = await _httpClient.GetRequest(_pdfOtherSettingUri, querys);
  143. if (result.StatusCode == HttpStatusCode.Ok)
  144. {
  145. var jsonString = await result.Content.ReadAsStringAsync();
  146. dynamic dyna = DynamicJsonHelper.ToDynamicJson(jsonString);
  147. if (dyna == null)
  148. return;
  149. List<dynamic> settings = DynamicJsonHelper.GetArraySafety(dyna.data);
  150. if (settings == null)
  151. return;
  152. foreach (var setting in settings)
  153. {
  154. string configName = DynamicJsonHelper.GetValueSafety<string>(setting.config_name, null);
  155. if (!string.IsNullOrEmpty(configName) && setting.json_data != null)
  156. {
  157. lock (settingLock)
  158. _settingList.Add(new JsonSetting(configName, setting.json_data));
  159. }
  160. }
  161. }
  162. }
  163. private async Task GetWindowsCards()
  164. {
  165. string region = GetSystemRegion();
  166. string lang = GetAppLanguage();
  167. var querys = new List<KeyValuePair<string, string>>();
  168. querys.Add(new KeyValuePair<string, string>("deep[Information][_filter][languages_code][_eq]", lang));
  169. querys.Add(new KeyValuePair<string, string>("filter[_or][0][include_regions][_eq]", "true"));
  170. querys.Add(new KeyValuePair<string, string>("filter[_or][0][regions][_contains]", region));
  171. querys.Add(new KeyValuePair<string, string>("filter[_or][1][include_regions][_eq]", "false"));
  172. querys.Add(new KeyValuePair<string, string>("filter[_or][1][regions][_ncontains]", region));
  173. querys.Add(new KeyValuePair<string, string>("filter[appTypes][_contains]", _appType));
  174. querys.Add(new KeyValuePair<string, string>("filter[status][_eq]", "published"));
  175. querys.Add(new KeyValuePair<string, string>("fields", "*,Information.*"));
  176. var result = await _httpClient.GetRequest(_windowsCardsUri, querys);
  177. if (result.StatusCode == HttpStatusCode.Ok)
  178. {
  179. var jsonString = await result.Content.ReadAsStringAsync();
  180. var response = JsonTool.DeserializeJSON<WindowsCardsResponse>(jsonString);
  181. if (response != null && response.Data != null && response.Data.Length > 0)
  182. {
  183. foreach (var card in response.Data)
  184. {
  185. lock (settingLock)
  186. _settingList.Add(card);
  187. }
  188. }
  189. }
  190. }
  191. public async Task<List<WindowsEventbarSetting>> GetWindowsEventbarAsync()
  192. {
  193. var current = DateTime.UtcNow.ToString("s", CultureInfo.GetCultureInfo("en-us"));
  194. string region = GetSystemRegion();
  195. string lang = GetAppLanguage();
  196. var querys = new List<KeyValuePair<string, string>>();
  197. querys.Add(new KeyValuePair<string, string>("deep[event_information][_filter][languages_code][_eq]", lang));
  198. querys.Add(new KeyValuePair<string, string>("filter[_or][0][include_regions][_eq]", "true"));
  199. querys.Add(new KeyValuePair<string, string>("filter[_or][0][regions][_contains]", region));
  200. querys.Add(new KeyValuePair<string, string>("filter[_or][1][include_regions][_eq]", "false"));
  201. querys.Add(new KeyValuePair<string, string>("filter[_or][1][regions][_ncontains]", region));
  202. querys.Add(new KeyValuePair<string, string>("filter[_or][2][include_regions][_eq]", "false"));
  203. querys.Add(new KeyValuePair<string, string>("filter[_or][2][regions][_null]", "true"));
  204. querys.Add(new KeyValuePair<string, string>("filter[start][_lte]", current));
  205. querys.Add(new KeyValuePair<string, string>("filter[end][_gte]", current));
  206. querys.Add(new KeyValuePair<string, string>("filter[app_types][_contains]", _appType));
  207. querys.Add(new KeyValuePair<string, string>("filter[status][_eq]", "published"));
  208. querys.Add(new KeyValuePair<string, string>("fields", "*,event_information.*"));
  209. var result = await _httpClient.GetRequest(_windowsEventbarUri, querys);
  210. if (result.StatusCode == HttpStatusCode.Ok)
  211. {
  212. var jsonString = await result.Content.ReadAsStringAsync();
  213. var response = JsonTool.DeserializeJSON<WindowsEventbarResponse>(jsonString);
  214. if (response != null && response.Data != null && response.Data.Length > 0)
  215. {
  216. return response.Data.ToList();
  217. }
  218. else
  219. return new List<WindowsEventbarSetting>();
  220. }
  221. return null;
  222. }
  223. private string GetSystemRegion()
  224. {
  225. var geographyCountryCode = new GeographicRegion().CodeTwoLetter;
  226. switch (geographyCountryCode)
  227. {
  228. case "GB":
  229. case "US":
  230. case "TW":
  231. case "CN":
  232. case "JP":
  233. case "ES":
  234. case "IT":
  235. case "FR":
  236. case "DE":
  237. case "RU":
  238. case "PT":
  239. case "KR":
  240. return geographyCountryCode;
  241. default:
  242. return "OTHER";
  243. }
  244. }
  245. private string GetAppLanguage()
  246. {
  247. try
  248. {
  249. var currentLang = Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Languages[0];
  250. switch (currentLang)
  251. {
  252. case "de-DE":
  253. case "de":
  254. return "de";
  255. case "es-ES":
  256. case "es":
  257. return "es";
  258. case "fr-FR":
  259. case "fr":
  260. return "fr";
  261. case "it-IT":
  262. case "it":
  263. return "it";
  264. case "ja-JP":
  265. case "ja":
  266. return "ja";
  267. case "zh-Hans-CN":
  268. case "zh-Hans":
  269. case "zh-CN":
  270. return "zh-cn";
  271. case "zh-Hant-TW":
  272. case "zh-Hant":
  273. case "zh-TW":
  274. return "zh-tw";
  275. case "ru-RU":
  276. case "ru":
  277. return "ru";
  278. case "ko-KR":
  279. case "ko":
  280. return "ko";
  281. case "pt-PT":
  282. case "pt":
  283. return "pt";
  284. default:
  285. return "en";
  286. }
  287. }
  288. catch
  289. {
  290. return "en";
  291. }
  292. }
  293. private bool IsInternetAvailable()
  294. {
  295. ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
  296. bool isInternetAvailable = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
  297. return isInternetAvailable;
  298. }
  299. #endregion
  300. }
  301. }