123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- using LYFZ.BLL;
- using LYFZ.Model;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace lyfzAttendance.Controller
- {
- public static class ClientProcess
- {
- public static bool ConnectStatus { get; set; } = false;
- /// <summary>
- /// 客户端配置信息;
- /// </summary>
- public static Model_Config ClientConfig { get; set; } = null;
- private static BLL_Config BllConfig { get; set; } = new BLL_Config();
- public static lyfzAttendanceLib.MachineConfig DeviceConfig { get; set; } = new lyfzAttendanceLib.MachineConfig();
- public static lyfzAttendanceLib.IAttendance Attendance { get; set; } = null;
- /// <summary>
- /// 云客户端模式下的tcp客户端连接对象;
- /// </summary>
- private static readonly LYFZ.Network.CloudServer.CloudServerClient cloudClient = new LYFZ.Network.CloudServer.CloudServerClient();
- /// <summary>
- /// 本地客户端模式下载的tcp客户端连接对象;
- /// </summary>
- private static readonly LYFZ.Network.TCPNetworkClient.TCP_NetworkClient localClient = new LYFZ.Network.TCPNetworkClient.TCP_NetworkClient();
- public static bool Connect(out string result)
- {
- result = "连接服务器成功";
- // 获取配置文件信息;
- ClientConfig = BllConfig.GetModelSysConfig();
- // 是否是云客户端;
- if ( ClientConfig.IsRemoteCloudClient )
- {
- cloudClient.Initialization(ClientConfig.EnterpriseID, "", ClientConfig.DongleDomain, ClientConfig.CloudPort);
- if ( cloudClient.ConnectServer() )
- {
- HPSocketCS.Extended.ReturnData returnData = cloudClient.GetCloudServerDbConnInfo();
- if ( returnData.ReturnStatus )
- {
- string dbConnectString = returnData.DataByteToModelObject<string>();
- dynamic dbConn = Newtonsoft.Json.JsonConvert.DeserializeObject(dbConnectString);
- LYFZ.Helper.GetConnectionString connModel = new LYFZ.Helper.GetConnectionString();
- connModel.DataBaseServer = dbConn.DataBaseServer;
- connModel.DataBaseName = dbConn.DataBaseName;
- connModel.DataBaseUser = dbConn.DataBaseUser;
- connModel.DataBasePass = dbConn.DataBasePass;
- connModel.DataBasePort = dbConn.DataBasePort;
- connModel.UpdateConnInfo();
- HPSocketCS.Extended.HP_SQLHelper.HpClient = cloudClient.TcpClient.Client;
- LYFZ.Network.TCPNetworkClient.TCP_NetworkClient.IsRemoteCloudClient = true;
- }
- else
- {
- result = "获取“CloudServerConn”失败:" + returnData.ReturnMessage;
- return false;
- }
- }
- else
- {
- result = "连接服务器失败," + cloudClient.ReturnMessage;
- return false;
- }
- }
- else
- {// 本地客户端;
- if ( !localClient.ConnectionServer())
- {
- result = "连接服务器失败,请检查网络配置";
- return false;
- }
- else
- {
- HPSocketCS.Extended.HP_SQLHelper.HpClient = localClient.Client;
- LYFZ.Network.TCPNetworkClient.TCP_NetworkClient.IsRemoteCloudClient = false;
- try
- {
- string dataConneStr = localClient.SendCommandToServer(HPSocketCS.Extended.TransferRequestCommand.GetDataConne, millisecondsTimeout: 5000);
- if (dataConneStr.Trim().Length <= 0)
- {
- result = "向服务器发出请求失败,请检查服务器地址是否正确或服务端安装是否正确。";
- return false;
- }
- if (!dataConneStr.Contains("错误:"))
- {
- LYFZ.Helper.GetConnectionString connModel = GetConnectionStringObj(dataConneStr, ClientConfig.ServerHostOrIP);
- connModel.UpdateConnInfo();
- }
- }
- catch (Exception ex)
- {
- result = "向服务器发出请求失败:" + ex.Message;
- return false;
- }
- }
- }
- return ConnectStatus = true;
- }
- public static void DisConnect()
- {
- if (ClientConfig.IsRemoteCloudClient)
- cloudClient.CloseConnect();
- else
- localClient.CloseConnected();
- }
- /// <summary>
- /// 获取数据库连接
- /// </summary>
- /// <param name="_connectionString">传入从服务器端获取到的数据库连接字符串</param>
- /// <param name="_ServerHostOrIP">服务器的主机名或IP地址</param>
- /// <returns></returns>
- public static LYFZ.Helper.GetConnectionString GetConnectionStringObj(string _connectionString, string _ServerHostOrIP)
- {
- string[] connstrings = _connectionString.Split('|');
- if (!LYFZ.WinAPI.CustomPublicMethod.CheckInternaNetworkAddress(connstrings[4]))
- {
- _ServerHostOrIP = connstrings[4];
- }
- LYFZ.Helper.GetConnectionString connModel = new LYFZ.Helper.GetConnectionString();
- connModel.DataBaseServer = _ServerHostOrIP;
- connModel.DataBaseName = connstrings[0];
- connModel.DataBasePort = Convert.ToInt32(connstrings[1]);
- connModel.DataBaseUser = connstrings[2];
- connModel.DataBasePass = connstrings[3];
- try
- {
- if (connstrings.Length >= 7)
- {
- connModel.YS_EnableMapped = Convert.ToBoolean(connstrings[5]);
- connModel.YS_DatabasePort = Convert.ToInt32(connstrings[6]);
- }
- }
- catch { }
- try
- {
- bool isBranchStore = false;//是否为分店
- if (connstrings.Length >= 8)
- {
- try
- {
- isBranchStore = Convert.ToBoolean(connstrings[7]);
- }
- catch { }
- }
- string DataBaseServer = connstrings[4];
- if (isBranchStore)
- {
- connModel.DataBaseServer = DataBaseServer;
- }
- else
- {
- //WIN-92443G56CU2\SQLEXPRESS
- if (DataBaseServer.Contains("\\"))
- {
- connModel.DataBaseServer = _ServerHostOrIP + "\\" + DataBaseServer.Split('\\')[1];
- }
- }
- }
- catch { }
- return connModel;
- }
- public static void LoadMachineConfig()
- {
- if (File.Exists("machineconfig.json"))
- {
- try
- {
- string strJson = File.ReadAllText("machineconfig.json");
- ClientProcess.DeviceConfig = JsonConvert.DeserializeObject<lyfzAttendanceLib.MachineConfig>(strJson);
- }
- catch (Exception ex)
- {
- }
- }
- }
- public static void SaveMachineConfig()
- {
- if (File.Exists("machineconfig.json"))
- {
- File.Delete("machineconfig.json");
- }
- string strJson = JsonConvert.SerializeObject(DeviceConfig);
- File.WriteAllText("machineconfig.json", strJson);
- }
- }
- }
|