using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lskj.Core;
using Lskj.Business.Impl;
using Lskj.Control;
using Lskj.Data;
using Lskj.Util;
using System.IO;
using System.Net;
namespace Lskj.Main
{
public partial class FrmConfigLocal : XtraForm
{
///
/// 默认下载地址
///
public string filePath = string.Empty;
///
/// 返回地址值
///
public string outText = string.Empty;
///
/// 账套数据
///
public DataTable sourceTab = new DataTable();
public FrmConfigLocal()
{
InitializeComponent();
this.txtIP.KeyUp += OnTxtEdit_KeyUp;
this.txtPort.KeyUp += OnTxtEdit_KeyUp;
this.txtZT.EditValueChanged += OnTxtZT_EditValueChanged;
}
private void OnTxtZT_EditValueChanged(object sender, EventArgs e)
{
try
{
DataRow focusRow = (this.txtZT.GetSelectedDataRow() as DataRowView).Row;
this.txtIP.Text = (focusRow["dm"] + "").Split(':')[0];
this.txtPort.Text = (focusRow["dm"] + "").Split(':')[1];
this.txtZTName.Text = focusRow["mc"] + "";
}
catch (Exception)
{
}
}
private void OnTxtEdit_KeyUp(object sender, KeyEventArgs e)
{
string IPProt = string.Format("{0}:{1}", this.txtIP.Text, this.txtPort.Text);
DataRow[] rows = sourceTab.Select(string.Format("dm='{0}'", IPProt));
if (rows != null && rows.Count() > 0)
{
this.txtZT.EditValue = IPProt;
}
}
///
/// 说明:窗口加载
/// 创建人:龚宇超
/// 创建日期:2017-08-09
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The source of the event.
/// The instance containing the event data.
private void FrmConfig_Load(object sender, EventArgs e)
{
sourceTab.Columns.Add("dm");
sourceTab.Columns.Add("mc");
try
{
Dictionary sourceDic = IniHelper.GetSectionKeys("SystemResources.ini", "SystemResources");
foreach (string key in sourceDic.Keys)
{
if (key.Contains("DownloadAESStr_"))
{
DataRow row = sourceTab.NewRow();
string[] keyArgs = key.Split('_');
if (keyArgs.Length == 2)
{
row["dm"] = keyArgs[1];//ip+port
row["mc"] = sourceDic[key].Split('^')[0];//账套名
sourceTab.Rows.Add(row);
}
else
continue;
}
}
this.txtZT.Properties.View.Columns.AddVisible("dm", "地址");
this.txtZT.Properties.View.Columns.AddVisible("mc", "账套名称");
this.txtZT.Properties.DataSource = sourceTab; //数据源
this.txtZT.Properties.ValueMember = "dm";
this.txtZT.Properties.DisplayMember = "mc";
this.txtZT.Properties.View.BestFitColumns();
this.txtZT.Properties.View.Columns["dm"].Visible = false;
this.txtZT.EditValue = string.Format("{0}:{1}", IniHelper.Read("SystemResources.ini", "LocalLastIP"), IniHelper.Read("SystemResources.ini", "LocalLastPort"));
//this.txtIP.Text = IniHelper.Read("SystemResources.ini", "LocalLastIP");
//this.txtPort.Text = IniHelper.Read("SystemResources.ini", "LocalLastPort");
//this.txtZTName.Text = IniHelper.Read("SystemResources.ini", "LocalLastPortName");
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
///
/// 说明:测试数据库连接
/// 创建人:龚宇超
/// 创建日期:2017-08-09
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The source of the event.
/// The instance containing the event data.
private void btnConnect_Click(object sender, EventArgs e)
{
try
{
ConnectServer();
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
///
/// 说明:保存配置
/// 创建人:龚宇超
/// 创建日期:2017-08-09
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The source of the event.
/// The instance containing the event data.
private void btnSave_Click(object sender, EventArgs e)
{
try
{
ConnectServer(true);
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
///
/// 说明:退出配置
/// 创建人:龚宇超
/// 创建日期:2017-08-09
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The source of the event.
/// The instance containing the event data.
private void btnExit_Click(object sender, EventArgs e)
{
try
{
this.Close();
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
///
/// 说明:连接服务器
/// 创建人:龚宇超
/// 创建日期:2017-08-09
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// if set to true [is save].
private void ConnectServer(bool isSave = false)
{
string serverName = DBConfig.Instance.ServerName;
string dbName = DBConfig.Instance.DataBase;
// 测试数据库连接
try
{
string ip = this.txtIP.Text;
string defaultPort = ":" + this.txtPort.Text;
//服务器下载
string newServerName = !serverName.StartsWith("http") ? "http://" + ip + defaultPort : ip + defaultPort;
newServerName = !newServerName.EndsWith("/") ? newServerName + "/" : newServerName;
string newfilePath = Path.Combine(newServerName, "SystemResources.txt");
//string name = WebRequest.Create(filePath).GetResponse().GetResponseStream().ToString();
string fileName = "SystemResources.txt";//客户端保存的文件名
WebClient webClient = new WebClient();
webClient.Encoding = Encoding.UTF8;
//这里使用DownloadString方法,如果是不需要对文件的文本内容做处理,直接保存,那么可以直接使用功能DownloadFile(url,savepath)直接进行文件保存。
string newoutText = newfilePath != filePath ? webClient.DownloadString(newfilePath) : outText;
outText = newoutText.Trim();
filePath = newfilePath;
//File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SystemResources.txt"), outText);
string args = AESUtil.Decrypt(outText);
string[] controlAfgs = args.Split('^');
if (controlAfgs != null && controlAfgs.Length == 5)
{
DBConfig.Instance.ServerName = controlAfgs[0];
DBConfig.Instance.DataBase = controlAfgs[1];
string Connection = "Server={0};Database={1};Persist Security Info=True;User ID=" + controlAfgs[2] + ";Password=" + controlAfgs[3] + ";Connection Timeout=5;MultipleActiveResultSets=true";
string dephistr = "Provider=SQLOLEDB.1;Server={0};Database={1};Persist Security Info=True;User ID=" + controlAfgs[2] + ";Password=" + controlAfgs[3] + ";Connection Timeout=5";
Connection = AESUtil.Encrypt(Connection);
dephistr = AESUtil.Encrypt(dephistr);
DBConfig.Instance.dephiConnection = dephistr;
DBConfig.Instance.Connection = Connection;
if (DBConfig.Instance.CreateConnection(Connection))
{
if (!DBConfig.Instance.ServerType.Equals("达梦数据库"))
{
if (DelphiHelper.Delphi_Init(new StringBuilder(DBConfig.Instance.GetDelphiConnection(DBConfig.Instance.dephiConnection))) == 0)
{
MessageUtil.Show(ResourceKeys.UnConnectServer + "[By Delphi]");
return;
}
}
if (isSave)
{
if (!string.IsNullOrEmpty(this.txtZTName.Text))
{
string IPPort = string.Format("{0}:{1}", this.txtIP.Text, this.txtPort.Text);
string writeText = string.Format("{0}^{1}", this.txtZTName.Text, outText);
IniHelper.Write("SystemResources.ini", string.Format("DownloadAESStr_{0}", IPPort), writeText);
IniHelper.Write("SystemResources.ini", "LocalLastIP", this.txtIP.Text);
IniHelper.Write("SystemResources.ini", "LocalLastPort", this.txtPort.Text);
IniHelper.Write("SystemResources.ini", "LocalLastPortName", this.txtZTName.Text);
DBConfig.Instance.WriteConfig("LocalLastIP", this.txtIP.Text);
DBConfig.Instance.WriteConfig("LocalLastPort", this.txtPort.Text);
DBConfig.Instance.WriteConfig("LocalLastPortName", this.txtZTName.Text);
t