浏览代码

添加接口验证

章政 7 年之前
父节点
当前提交
ceea6ebe9b

+ 6 - 5
UAS-出货标签管理/Login.cs

@@ -6,6 +6,7 @@ using System.IO;
 using System.Text;
 using System.Windows.Forms;
 using UAS_LabelMachine.Entity;
+using UAS_LabelMachine.PublicMethod;
 
 namespace UAS_LabelMachine
 {
@@ -30,7 +31,7 @@ namespace UAS_LabelMachine
         {
             dh = new DataHelper();
             //获取账套信息
-            dt = (DataTable)dh.ExecuteSql("select ma_function,ms_pwd,ma_user from datacenter.master ", "select");
+            dt = (DataTable)dh.ExecuteSql("select ma_function,ms_pwd,ma_user from master ", "select");
             MasterCombox.DisplayMember = "ma_function";
             MasterCombox.ValueMember = "ma_user";
             MasterCombox.DataSource = dt;
@@ -59,11 +60,12 @@ namespace UAS_LabelMachine
                     Ms_Pwd = dt.Rows[i]["ms_pwd"].ToString();
             }
             //切换至用户选择的数据源
-            string ConnectionString = "Data Source=" + IP.Text + "/orcl;User ID=" + MasterCombox.SelectedValue + ";PassWord=" + Ms_Pwd;
+            string ConnectionString = "Connection Timeout=0;Pooling=false;Password=" + Ms_Pwd + ";User ID=" + MasterCombox.SelectedValue.ToString() + ";Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" + IP.Text + ")(PORT=1523)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
             DataHelper.DBConnectionString = ConnectionString;
             //设置了Connection,重新执行构造函数,重置数据库连接
             dh = new DataHelper();
-            if (dh.CheckExist("employee", "em_code='" + UserName.Text + "' and em_password='" + PassWord.Text + "'"))
+            string oMsg = "";
+            if (HttpHandler.CheckUserLogin(UserName.Text, PassWord.Text, MasterCombox.SelectedValue.ToString(), out oMsg))
             {
                 dt = (DataTable)dh.ExecuteSql("select em_name,em_type from employee where em_code='" + UserName.Text + "'", "select");
                 User.UserName = dt.Rows[0]["em_name"].ToString();
@@ -81,8 +83,7 @@ namespace UAS_LabelMachine
                 main.ShowDialog();
                 Close();
             }
-            else
-                MessageBox.Show("用户名或者密码错误!");
+            else MessageBox.Show(oMsg);
         }
 
         /// <summary>

+ 47 - 0
UAS-出货标签管理/PublicMethod/BaseUtil.cs

@@ -9,6 +9,7 @@ using UAS_LabelMachine.CustomControl.GroupBoxWithBorder;
 using System.Security.Cryptography;
 using static System.Windows.Forms.Control;
 using System.IO;
+using System.Text.RegularExpressions;
 
 namespace UAS_LabelMachine
 {
@@ -871,6 +872,52 @@ namespace UAS_LabelMachine
             }
         }
 
+        public static Dictionary<string, object> ToDictionary(string JsonData)
+        {
+            object Data = null;
+            Dictionary<string, object> Dic = new Dictionary<string, object>();
+            if (JsonData.StartsWith("["))
+            {
+                //如果目标直接就为数组类型,则将会直接输出一个Key为List的List<Dictionary<string, object>>集合 
+                //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["List"]; 
+                List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
+                MatchCollection ListMatch = Regex.Matches(JsonData, @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组 
+                foreach (Match ListItem in ListMatch)
+                {
+                    List.Add(ToDictionary(ListItem.ToString()));//递归调用 
+                }
+                Data = List;
+                Dic.Add("List", Data);
+            }
+            else
+            {
+                MatchCollection Match = Regex.Matches(JsonData, @"""(.+?)"": {0,1}(\[[\s\S]+?\]|null|"".+?""|-{0,1}\d*)");//使用正则表达式匹配出JSON数据中的键与值 
+                foreach (Match item in Match)
+                {
+                    try
+                    {
+                        if (item.Groups[2].ToString().StartsWith("["))
+                        {
+                            //如果目标是数组,将会输出一个Key为当前Json的List<Dictionary<string, object>>集合 
+                            //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["Json中的Key"]; 
+                            List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
+                            MatchCollection ListMatch = Regex.Matches(item.Groups[2].ToString(), @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组 
+                            foreach (Match ListItem in ListMatch)
+                            {
+                                List.Add(ToDictionary(ListItem.ToString()));//递归调用 
+                            }
+                            Data = List;
+                        }
+                        else if (item.Groups[2].ToString().ToLower() == "null") Data = null;//如果数据为null(字符串类型),直接转换成null
+                        else Data = item.Groups[2].ToString(); //数据为数字、字符串中的一类,直接写入 
+                        Dic.Add(item.Groups[1].ToString(), Data);
+                    }
+                    catch { }
+                }
+            }
+            return Dic;
+        }
+
         public static string AddField(string[] Fields)
         {
             string sql = " ";

+ 11 - 21
UAS-出货标签管理/PublicMethod/DataHelper.cs

@@ -23,11 +23,16 @@ namespace UAS_LabelMachine
         //富为外网地址
         //private readonly string ConnectionStrings = "Data Source=richwellgroup.com.cn/orcl;User ID=DATACENTER;PassWord=select!#%*(";
         //怡海能达外网地址
-        private readonly string ConnectionStrings = "Data Source=sz.hi-mantech.com/orcl;User ID=YHND_SZ;PassWord=select!#%*(";
+        //private readonly string ConnectionStrings = "Data Source=sz.hi-mantech.com/orcl;User ID=YHND_SZ;PassWord=select!#%*(";
         //怡海能达内网地址
         //private readonly string ConnectionStrings = "Data Source=192.168.1.200/orcl;User ID=DATACENTER;PassWord=select!#%*(";
+        //怡海能达ERP地址
+        //public static readonly string ERPAddesss = "http://sz.hi-mantech.com:8099/ERP/";
         //华商龙外网地址
-        //private readonly string ConnectionStrings = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=YITOA_DATACENTER;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=218.18.115.198)(PORT=1523)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
+        private readonly string ConnectionStrings = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=YITOA_DATACENTER;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=218.18.115.198)(PORT=1523)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
+        //ERP的地址
+        public static readonly string ERPAddesss = "http://218.18.115.198:8888/ERP/";
+
         //用户选择的数据库的连接字符串
         public static string DBConnectionString;
         static OracleConnection connection = null;
@@ -766,7 +771,6 @@ namespace UAS_LabelMachine
             }
         }
 
-      
         /// <summary>
         /// 直接执行SQL,同时传入SQL的类型
         /// </summary>
@@ -810,24 +814,10 @@ namespace UAS_LabelMachine
             {
                 case "SELECT":
                     result = new DataTable();
-                    try
-                    {
-                        OracleDataAdapter ad = new OracleDataAdapter(command);
-                        ad.Fill((DataTable)result);
-                        ad.Dispose();
-                        //成功执行后将重复连接数置为0
-                        ReconnectTime = 0;
-                    }
-                    catch (Exception)
-                    {
-                        if (ReconnectTime == 0)
-                        {
-                            //重置的数据库链接后只执行一次
-                            ReconnectTime=ReconnectTime+1;
-                            connection = new OracleConnection(DBConnectionString);
-                            result = ExecuteSql(SQL, Type, names);
-                        }
-                    }
+                    OracleDataAdapter ad = new OracleDataAdapter(command);
+                    ad.Fill((DataTable)result);
+                    ad.Dispose();
+                    //成功执行后将重复连接数置为0
                     break;
                 case "DELETE":
                     result = command.ExecuteNonQuery();

+ 86 - 0
UAS-出货标签管理/PublicMethod/HttpHandler.cs

@@ -0,0 +1,86 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Net;
+using System.Text;
+using System.Web;
+
+namespace UAS_LabelMachine.PublicMethod
+{
+    public class HttpHandler : IHttpHandler
+    {
+        /// <summary>
+        /// 您将需要在网站的 Web.config 文件中配置此处理程序 
+        /// 并向 IIS 注册它,然后才能使用它。有关详细信息,
+        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
+        /// </summary>
+        public bool IsReusable
+        {
+            // 如果无法为其他请求重用托管处理程序,则返回 false。
+            // 如果按请求保留某些状态信息,则通常这将为 false。
+            get { return true; }
+        }
+        public void ProcessRequest(HttpContext context)
+        {
+
+        }
+
+
+        public static bool CheckUserLogin(string UserName, string PassWord, string Master, out string oMsg)
+        {
+            oMsg = "";
+            try
+            {
+                string url = DataHelper.ERPAddesss + "mobile/login.action";//html调用的地址              
+                HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
+                if (webrequest == null)
+                {
+                    return false;
+                }
+                webrequest.Method = "POST";
+                webrequest.Timeout = 1000;
+                webrequest.ContentType = "application/x-www-form-urlencoded";
+                System.Collections.Hashtable pars = new System.Collections.Hashtable();
+                pars.Add("username", UserName);
+                pars.Add("password", PassWord);
+                pars.Add("master", Master);
+                string buffer = "";
+                //发送POST数据 
+                if (!(pars == null || pars.Count == 0))
+                {
+                    foreach (string key in pars.Keys)
+                    {
+                        buffer = buffer + "&" + key + "=" + pars[key].ToString();
+                    }
+                    byte[] data = Encoding.UTF8.GetBytes(buffer);
+                    using (Stream stream = webrequest.GetRequestStream())
+                    {
+                        stream.Write(data, 0, data.Length);
+                    }
+                }
+
+                string[] values = webrequest.Headers.GetValues("Content-Type");
+                WebResponse myResponse = webrequest.GetResponse();
+
+                using (Stream resStream = myResponse.GetResponseStream())//得到回写的流
+                {
+                    StreamReader newReader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
+                    string Content = newReader.ReadToEnd();
+                    Dictionary<string, object> dic = new Dictionary<string, object>();
+                    dic = BaseUtil.ToDictionary(Content);
+                    if (!dic.ContainsKey("erpaccount"))
+                    {
+                        oMsg = dic["reason"].ToString();
+                        return false;
+                    }
+                    newReader.Close();
+                }
+            }
+            catch (Exception ex)
+            {
+                LogManager.DoLog(ex.Message.ToString());
+            }
+            return true;
+        }
+    }
+}

+ 2 - 0
UAS-出货标签管理/UAS-出货标签管理.csproj

@@ -115,6 +115,7 @@
     <Reference Include="System.Core" />
     <Reference Include="System.Runtime.Serialization" />
     <Reference Include="System.ServiceModel" />
+    <Reference Include="System.Web" />
     <Reference Include="System.Web.Extensions" />
     <Reference Include="System.Xml.Linq" />
     <Reference Include="System.Data.DataSetExtensions" />
@@ -229,6 +230,7 @@
     <Compile Include="PublicMethod\ExcelHandler.cs" />
     <Compile Include="PublicMethod\ftpOperater.cs" />
     <Compile Include="PublicMethod\GlobalEventsHandler.cs" />
+    <Compile Include="PublicMethod\HttpHandler.cs" />
     <Compile Include="PublicMethod\LogManager.cs" />
     <Compile Include="Service References\Vivo_BoxReelRelation\Reference.cs">
       <AutoGen>True</AutoGen>