ClientProcess.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using LYFZ.BLL;
  2. using LYFZ.Model;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace lyfzAttendance.Controller
  12. {
  13. public static class ClientProcess
  14. {
  15. public static bool ConnectStatus { get; set; } = false;
  16. /// <summary>
  17. /// 客户端配置信息;
  18. /// </summary>
  19. public static Model_Config ClientConfig { get; set; } = null;
  20. private static BLL_Config BllConfig { get; set; } = new BLL_Config();
  21. public static lyfzAttendanceLib.MachineConfig DeviceConfig { get; set; } = new lyfzAttendanceLib.MachineConfig();
  22. public static lyfzAttendanceLib.IAttendance Attendance { get; set; } = null;
  23. /// <summary>
  24. /// 云客户端模式下的tcp客户端连接对象;
  25. /// </summary>
  26. private static readonly LYFZ.Network.CloudServer.CloudServerClient cloudClient = new LYFZ.Network.CloudServer.CloudServerClient();
  27. /// <summary>
  28. /// 本地客户端模式下载的tcp客户端连接对象;
  29. /// </summary>
  30. private static readonly LYFZ.Network.TCPNetworkClient.TCP_NetworkClient localClient = new LYFZ.Network.TCPNetworkClient.TCP_NetworkClient();
  31. public static bool Connect(out string result)
  32. {
  33. result = "连接服务器成功";
  34. // 获取配置文件信息;
  35. ClientConfig = BllConfig.GetModelSysConfig();
  36. // 是否是云客户端;
  37. if ( ClientConfig.IsRemoteCloudClient )
  38. {
  39. cloudClient.Initialization(ClientConfig.EnterpriseID, "", ClientConfig.DongleDomain, ClientConfig.CloudPort);
  40. if ( cloudClient.ConnectServer() )
  41. {
  42. HPSocketCS.Extended.ReturnData returnData = cloudClient.GetCloudServerDbConnInfo();
  43. if ( returnData.ReturnStatus )
  44. {
  45. string dbConnectString = returnData.DataByteToModelObject<string>();
  46. dynamic dbConn = Newtonsoft.Json.JsonConvert.DeserializeObject(dbConnectString);
  47. LYFZ.Helper.GetConnectionString connModel = new LYFZ.Helper.GetConnectionString();
  48. connModel.DataBaseServer = dbConn.DataBaseServer;
  49. connModel.DataBaseName = dbConn.DataBaseName;
  50. connModel.DataBaseUser = dbConn.DataBaseUser;
  51. connModel.DataBasePass = dbConn.DataBasePass;
  52. connModel.DataBasePort = dbConn.DataBasePort;
  53. connModel.UpdateConnInfo();
  54. HPSocketCS.Extended.HP_SQLHelper.HpClient = cloudClient.TcpClient.Client;
  55. LYFZ.Network.TCPNetworkClient.TCP_NetworkClient.IsRemoteCloudClient = true;
  56. }
  57. else
  58. {
  59. result = "获取“CloudServerConn”失败:" + returnData.ReturnMessage;
  60. return false;
  61. }
  62. }
  63. else
  64. {
  65. result = "连接服务器失败," + cloudClient.ReturnMessage;
  66. return false;
  67. }
  68. }
  69. else
  70. {// 本地客户端;
  71. if ( !localClient.ConnectionServer())
  72. {
  73. result = "连接服务器失败,请检查网络配置";
  74. return false;
  75. }
  76. else
  77. {
  78. HPSocketCS.Extended.HP_SQLHelper.HpClient = localClient.Client;
  79. LYFZ.Network.TCPNetworkClient.TCP_NetworkClient.IsRemoteCloudClient = false;
  80. try
  81. {
  82. string dataConneStr = localClient.SendCommandToServer(HPSocketCS.Extended.TransferRequestCommand.GetDataConne, millisecondsTimeout: 5000);
  83. if (dataConneStr.Trim().Length <= 0)
  84. {
  85. result = "向服务器发出请求失败,请检查服务器地址是否正确或服务端安装是否正确。";
  86. return false;
  87. }
  88. if (!dataConneStr.Contains("错误:"))
  89. {
  90. LYFZ.Helper.GetConnectionString connModel = GetConnectionStringObj(dataConneStr, ClientConfig.ServerHostOrIP);
  91. connModel.UpdateConnInfo();
  92. }
  93. }
  94. catch (Exception ex)
  95. {
  96. result = "向服务器发出请求失败:" + ex.Message;
  97. return false;
  98. }
  99. }
  100. }
  101. return ConnectStatus = true;
  102. }
  103. public static void DisConnect()
  104. {
  105. if (ClientConfig.IsRemoteCloudClient)
  106. cloudClient.CloseConnect();
  107. else
  108. localClient.CloseConnected();
  109. }
  110. /// <summary>
  111. /// 获取数据库连接
  112. /// </summary>
  113. /// <param name="_connectionString">传入从服务器端获取到的数据库连接字符串</param>
  114. /// <param name="_ServerHostOrIP">服务器的主机名或IP地址</param>
  115. /// <returns></returns>
  116. public static LYFZ.Helper.GetConnectionString GetConnectionStringObj(string _connectionString, string _ServerHostOrIP)
  117. {
  118. string[] connstrings = _connectionString.Split('|');
  119. if (!LYFZ.WinAPI.CustomPublicMethod.CheckInternaNetworkAddress(connstrings[4]))
  120. {
  121. _ServerHostOrIP = connstrings[4];
  122. }
  123. LYFZ.Helper.GetConnectionString connModel = new LYFZ.Helper.GetConnectionString();
  124. connModel.DataBaseServer = _ServerHostOrIP;
  125. connModel.DataBaseName = connstrings[0];
  126. connModel.DataBasePort = Convert.ToInt32(connstrings[1]);
  127. connModel.DataBaseUser = connstrings[2];
  128. connModel.DataBasePass = connstrings[3];
  129. try
  130. {
  131. if (connstrings.Length >= 7)
  132. {
  133. connModel.YS_EnableMapped = Convert.ToBoolean(connstrings[5]);
  134. connModel.YS_DatabasePort = Convert.ToInt32(connstrings[6]);
  135. }
  136. }
  137. catch { }
  138. try
  139. {
  140. bool isBranchStore = false;//是否为分店
  141. if (connstrings.Length >= 8)
  142. {
  143. try
  144. {
  145. isBranchStore = Convert.ToBoolean(connstrings[7]);
  146. }
  147. catch { }
  148. }
  149. string DataBaseServer = connstrings[4];
  150. if (isBranchStore)
  151. {
  152. connModel.DataBaseServer = DataBaseServer;
  153. }
  154. else
  155. {
  156. //WIN-92443G56CU2\SQLEXPRESS
  157. if (DataBaseServer.Contains("\\"))
  158. {
  159. connModel.DataBaseServer = _ServerHostOrIP + "\\" + DataBaseServer.Split('\\')[1];
  160. }
  161. }
  162. }
  163. catch { }
  164. return connModel;
  165. }
  166. public static void LoadMachineConfig()
  167. {
  168. if (File.Exists("machineconfig.json"))
  169. {
  170. try
  171. {
  172. string strJson = File.ReadAllText("machineconfig.json");
  173. ClientProcess.DeviceConfig = JsonConvert.DeserializeObject<lyfzAttendanceLib.MachineConfig>(strJson);
  174. }
  175. catch (Exception ex)
  176. {
  177. }
  178. }
  179. }
  180. public static void SaveMachineConfig()
  181. {
  182. if (File.Exists("machineconfig.json"))
  183. {
  184. File.Delete("machineconfig.json");
  185. }
  186. string strJson = JsonConvert.SerializeObject(DeviceConfig);
  187. File.WriteAllText("machineconfig.json", strJson);
  188. }
  189. }
  190. }