CMSCollection.cs 11 KB

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