Newer
Older
IRIS_COLLECT_GA / IOM_cs / insertForm / sysSetting / CtrlServer.cs
yangqianqian on 1 Jun 2021 3 KB first commit
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IOM_cs.insertForm.data.dto;
using System.Text.RegularExpressions;

namespace IOM_cs.insertForm.sysSetting
{
    public partial class CtrlServer : UserControl
    {
        public CtrlServer()
        {
            InitializeComponent();
            comboBoxServerType.Items.Add("公安部虹膜库");
        }

        private void reFresh() {
            SyServer syServer = new SyServer();
            comboBoxServerType.SelectedItem = "公安部虹膜库";
            textBoxServerIp.Text = syServer.SyServerIp;
            textBoxServerPort.Text = syServer.SyServerPort;
            textBoxRequestId.Text = syServer.SyRequestId;
            textBoxRequestKey.Text = syServer.SyRequestKey;
        }

        private void reLownd() {
            SyServer syServer = new SyServer();
            comboBoxServerType.SelectedItem = "公安部虹膜库";
            textBoxServerIp.Text = syServer.GaServerIp;
            textBoxServerPort.Text = syServer.GaServerPort;
            textBoxRequestId.Text = syServer.GaRequestId;
            textBoxRequestKey.Text = syServer.GaRequestKey;
        }

        private void CtrlServer_Load(object sender, EventArgs e)
        {
            reFresh();
        }

        private void roundButton1_Click(object sender, EventArgs e)
        {
            reLownd();
        }

        private void roundButton2_Click(object sender, EventArgs e)
        {
            String syServerIp = textBoxServerIp.Text;
            String syServerPort = textBoxServerPort.Text;
            String syRequestId = textBoxRequestId.Text;
            String syRequestKey = textBoxRequestKey.Text;

            if (!IsIPAddress(syServerIp))
            {
                MessageBox.Show("请输入正确的IP", "提示");
                return;
            }
            else if (!IsIPPort(syServerPort))
            {
                MessageBox.Show("请输入正确的端口", "提示");
                return;
            }

            if (MessageBox.Show("确定保存信息?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                == DialogResult.Yes)
            {
                ConfigHelper.UpdateAppConfig("SyServerIp", syServerIp);
                ConfigHelper.UpdateAppConfig("SyServerPort", syServerPort);
                ConfigHelper.UpdateAppConfig("SyRequestId", syRequestId);
                ConfigHelper.UpdateAppConfig("SyRequestKey", syRequestKey);

                reFresh();

                MessageBox.Show("保存成功", "提示");
            }
        }

        public static bool IsIPAddress(string ip)
        {

            if (string.IsNullOrEmpty(ip) || ip.Length < 7 || ip.Length > 15) return false;

            string regformat = @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$";
            Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);
            return regex.IsMatch(ip);

        }
        
        public static bool IsIPPort(string port)
        {
            bool isPort = false;
            int portNum;
            isPort = Int32.TryParse(port, out portNum);
            if (isPort && portNum >= 0 && portNum <= 65535)
            {
                isPort = true;
            }
            else
            {
                isPort = false;
            }

            return isPort;
        }

        private void btn_dropServerType_Click(object sender, EventArgs e)
        {
            comboBoxServerType.DroppedDown = true;
        }

        private void comboBoxServerType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxServerType.SelectedIndex > -1) txt_ServerType.Text = comboBoxServerType.SelectedItem.ToString();
        }


    }
}