You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
6.8 KiB

  1. using CounsellorBL.BLStructure;
  2. using log4net;
  3. using log4net.Config;
  4. using Microsoft.Extensions.Configuration;
  5. using MonumentDefine;
  6. using Newtonsoft.Json;
  7. using OT.COM.SignalerMessage;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Reflection;
  16. using System.Text;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using System.Windows.Forms;
  20. namespace TaskUserConnect
  21. {
  22. public partial class Form1 : Form
  23. {
  24. #region 自定義變量區
  25. private readonly static ILog mo_Log = LogManager.GetLogger(typeof(Form1));
  26. private readonly Dictionary<string, string> dicAppSetting = new Dictionary<string, string>();
  27. public Form1()
  28. {
  29. LoadLog4netConfig();
  30. mo_Log.Info("Form1 Constructor start");
  31. InitializeComponent();
  32. GetConfig();
  33. mo_Log.Info("Form1 Constructor End");
  34. }
  35. #endregion
  36. #region 頁面事件觸發區
  37. private void Form1_Load(object sender, EventArgs e)
  38. {
  39. string sMsg = null;
  40. mo_Log.Info("Form1 Form1_Load start");
  41. try
  42. {
  43. _HandlePost();
  44. Thread.Sleep(500);
  45. }
  46. catch (Exception ex)
  47. {
  48. sMsg = ex.Message;
  49. }
  50. if (sMsg != null)
  51. {
  52. mo_Log.Info($"Form1_Load end with exception. {sMsg}");
  53. }
  54. else
  55. {
  56. mo_Log.Info($"Form1_Load end Success.");
  57. }
  58. this.Close();
  59. }
  60. #endregion
  61. #region 私有方法區
  62. #region LoadLog4netConfig
  63. /// <summary>
  64. /// 加載Log4netConfig
  65. /// </summary>
  66. private static void LoadLog4netConfig()
  67. {
  68. var repository = LogManager.CreateRepository(
  69. Assembly.GetEntryAssembly(),
  70. typeof(log4net.Repository.Hierarchy.Hierarchy)
  71. );
  72. XmlConfigurator.Configure(repository, new FileInfo("Log4Net.config"));
  73. }
  74. #endregion
  75. #region GetConfig
  76. /// <summary>
  77. /// 取得設定值
  78. /// </summary>
  79. private void GetConfig()
  80. {
  81. mo_Log.Info($"Form1 GetConfig start. Current Dir={Directory.GetCurrentDirectory()}");
  82. IConfigurationRoot configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();
  83. IEnumerable<IConfigurationSection> ics = configuration.GetSection("appSettings").GetChildren();
  84. foreach (IConfigurationSection ic in ics)
  85. {
  86. dicAppSetting.Add(ic.Key, ic.Value);
  87. }
  88. mo_Log.Info("Form1 GetConfig end");
  89. }
  90. #endregion
  91. private static string _Action(string i_sURL, string i_sAppId, string i_sModule, string i_sMethod, out CResponseMessage o_crpResult,
  92. Dictionary<string, object> i_ldicPara)
  93. {
  94. string sMsg;
  95. CResponseMessage crpResult = null;
  96. try
  97. {
  98. do
  99. {
  100. SendWebApiRequest swr = new SendWebApiRequest();
  101. CRequestMessage crm = new CRequestMessage()
  102. {
  103. module = i_sModule,
  104. method = i_sMethod,
  105. param = i_ldicPara,
  106. customparam = new Dictionary<string, object>() { { BLWording.APP_ID, i_sAppId } }//把appid傳過去
  107. };
  108. CReqestPack crqp = new CReqestPack
  109. {
  110. Reqs = new List<CReqestItem>()
  111. {
  112. new CReqestItem(){ Req = crm }
  113. }
  114. };
  115. sMsg = swr.RunEncryptRequest(i_sURL, crqp, out string sResult);
  116. if (sMsg != null)
  117. {
  118. mo_Log.Error($"Result = {sMsg}");
  119. break;
  120. }
  121. else
  122. {
  123. mo_Log.Info($"Response = {sResult}");
  124. CResponsePack crppResult = JsonConvert.DeserializeObject<CResponsePack>(sResult);
  125. if (crppResult == null)
  126. {
  127. sMsg = "Covert CResponsePack Fail";
  128. break;
  129. }
  130. CResponseMessage crp = crppResult.Reps[0];
  131. if (crp.result == EResponseResult.RR_FALSE)
  132. {
  133. sMsg = crp.msg;
  134. break;
  135. }
  136. crpResult = crp;
  137. }
  138. }
  139. while (false);
  140. }
  141. catch (Exception ex)
  142. {
  143. sMsg = ex.Message;
  144. }
  145. o_crpResult = crpResult;
  146. return sMsg;
  147. }
  148. private string FbCheckToken(string i_sURL,string i_sAppId ,string i_sModule, out CResponseMessage o_crpResult,
  149. Dictionary<string, object> i_dicData, Dictionary<string, object> i_dicWhere)
  150. {
  151. mo_Log.Info("FbCheckToken");
  152. Dictionary<string, object> dicPara = new Dictionary<string, object>();
  153. dicPara.Add(BLWording.QRY_MASTER, new List<Dictionary<string, object>>() { new Dictionary<string, object>() {
  154. { BLWording.DATA, i_dicData },
  155. { BLWording.WHEREDATA, i_dicWhere }
  156. } });
  157. return _Action(i_sURL, i_sAppId, i_sModule, "FbCheckToken", out o_crpResult, dicPara);
  158. }
  159. private void _HandlePost()
  160. {
  161. string sMsg = null;
  162. mo_Log.Info("Start _HandlePost");
  163. try
  164. {
  165. do
  166. {
  167. string g_apiServer = dicAppSetting["apiserver"];
  168. string strAppId = dicAppSetting["app_id"];
  169. string sServerURL = g_apiServer;
  170. string sURL = $"{sServerURL}/Cmd";
  171. mo_Log.Debug("_HandlePost f1");
  172. FbCheckToken(sURL, strAppId, "GROUP.GroupUserConnectService", out CResponseMessage crpArticleWait, null,null);
  173. mo_Log.Debug("_HandlePost f2");
  174. }
  175. while (false);
  176. }
  177. catch (Exception ex)
  178. {
  179. sMsg = $"Process fail. ErrorMsg={ex.Message}";
  180. }
  181. if (sMsg != null)
  182. {
  183. mo_Log.Error(sMsg);
  184. }
  185. }
  186. #endregion
  187. }
  188. }