|
|
@@ -6,7 +6,10 @@ using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Data;
|
|
|
using System.Drawing;
|
|
|
+using System.IO;
|
|
|
+using System.Net;
|
|
|
using System.Text;
|
|
|
+using System.Web.Script.Serialization;
|
|
|
using System.Windows;
|
|
|
using System.Windows.Forms;
|
|
|
using UAS_MES_NEW.DataOperate;
|
|
|
@@ -2127,5 +2130,160 @@ namespace UAS_MES_NEW.PublicMethod
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ #region EIS dllMain 接口
|
|
|
+
|
|
|
+ //EIS 接口地址与鉴权信息
|
|
|
+ private const string EIS_URL = "http://192.168.0.85:8099/EIS/MES/helper/dllMain";
|
|
|
+ private const string EIS_ACCESSKEY = "CYZZ";
|
|
|
+ private const string EIS_ACCESSSECRET = "aqwe@1143erggG";
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 通用调用 EIS/MES/helper/dllMain 接口(POST JSON)
|
|
|
+ /// 请求体: {"type":"<iType>","detail":{...}}
|
|
|
+ /// 请求头: Accesskey / Requestid / AccessSecret / Signature / Timestamp
|
|
|
+ /// 返回接口响应原文;请求失败返回 null 并给出 iErrorMessage
|
|
|
+ /// </summary>
|
|
|
+ public static string PostEisHelper(string iType, Dictionary<string, object> iDetail, out string iErrorMessage)
|
|
|
+ {
|
|
|
+ iErrorMessage = "";
|
|
|
+ try
|
|
|
+ {
|
|
|
+ //毫秒级时间戳
|
|
|
+ string timestamp = ((long)(System.DateTime.UtcNow - new System.DateTime(1970, 1, 1)).TotalMilliseconds).ToString();
|
|
|
+ //每次请求生成一个 GUID 作为请求标识
|
|
|
+ string requestId = Guid.NewGuid().ToString().ToUpper();
|
|
|
+ string signature = GetEisSignature(timestamp);
|
|
|
+ //组装请求体 {"type":"...","detail":{...}}
|
|
|
+ Dictionary<string, object> body = new Dictionary<string, object>();
|
|
|
+ body.Add("type", iType);
|
|
|
+ body.Add("detail", iDetail);
|
|
|
+ JavaScriptSerializer jss = new JavaScriptSerializer();
|
|
|
+ string postData = jss.Serialize(body);
|
|
|
+
|
|
|
+ HttpWebRequest request = (HttpWebRequest)WebRequest.Create(EIS_URL);
|
|
|
+ request.Method = "POST";
|
|
|
+ request.ContentType = "application/json";
|
|
|
+ request.Timeout = 30 * 1000;
|
|
|
+ request.Headers.Add("Accesskey", EIS_ACCESSKEY);
|
|
|
+ request.Headers.Add("Requestid", requestId);
|
|
|
+ request.Headers.Add("AccessSecret", EIS_ACCESSSECRET);
|
|
|
+ request.Headers.Add("Signature", signature);
|
|
|
+ request.Headers.Add("Timestamp", timestamp);
|
|
|
+ byte[] data = Encoding.UTF8.GetBytes(postData);
|
|
|
+ request.ContentLength = data.Length;
|
|
|
+ using (Stream rs = request.GetRequestStream())
|
|
|
+ {
|
|
|
+ rs.Write(data, 0, data.Length);
|
|
|
+ }
|
|
|
+ HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|
|
+ using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
|
|
|
+ {
|
|
|
+ return sr.ReadToEnd();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ iErrorMessage = "请求EIS接口失败:" + ex.Message;
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 计算 EIS 接口 Signature
|
|
|
+ /// 注意:签名算法未在接口文档中确认,当前按 MD5(Accesskey + AccessSecret + Timestamp) 大写实现,
|
|
|
+ /// 与 Postman 样例值不一致,若联调返回签名错误,请按接口方规则修改本方法
|
|
|
+ /// </summary>
|
|
|
+ private static string GetEisSignature(string timestamp)
|
|
|
+ {
|
|
|
+ string source = EIS_ACCESSKEY + EIS_ACCESSSECRET + timestamp;
|
|
|
+ using (var md5 = System.Security.Cryptography.MD5.Create())
|
|
|
+ {
|
|
|
+ byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(source));
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (int i = 0; i < bytes.Length; i++)
|
|
|
+ {
|
|
|
+ sb.Append(bytes[i].ToString("X2"));
|
|
|
+ }
|
|
|
+ return sb.ToString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 校验 MDM 注册:调用 EIS 接口 type=CheckMDMRegistration
|
|
|
+ /// 返回报文结构:code/message/requestId/data,业务结果字段为 data.oErrMessage
|
|
|
+ /// 所有提示信息均取自接口实际返回内容(不做硬编码),oErrMessage 为空视为校验通过
|
|
|
+ /// </summary>
|
|
|
+ public static bool CheckMDMRegistration(string iSN, string iMakeCode, string iSourceCode, string iUserCode, out string iErrorMessage)
|
|
|
+ {
|
|
|
+ iErrorMessage = "";
|
|
|
+ Dictionary<string, object> detail = new Dictionary<string, object>();
|
|
|
+ detail.Add("iSN", iSN);
|
|
|
+ detail.Add("iMakeCode", iMakeCode);
|
|
|
+ detail.Add("iSourceCode", iSourceCode);
|
|
|
+ detail.Add("iUserCode", iUserCode);
|
|
|
+ string response = PostEisHelper("CheckMDMRegistration", detail, out iErrorMessage);
|
|
|
+ //请求本身失败(网络/超时等),PostEisHelper 已带出错误信息
|
|
|
+ if (response == null)
|
|
|
+ {
|
|
|
+ if (iErrorMessage == "")
|
|
|
+ {
|
|
|
+ iErrorMessage = "MDM注册校验请求失败";
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ try
|
|
|
+ {
|
|
|
+ JavaScriptSerializer jss = new JavaScriptSerializer();
|
|
|
+ Dictionary<string, object> root = jss.Deserialize<Dictionary<string, object>>(response);
|
|
|
+ if (root == null)
|
|
|
+ {
|
|
|
+ //解析不出结构,直接把接口原文带出去
|
|
|
+ iErrorMessage = response.Trim();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ //接口整体返回码,0 为成功;非 0 时优先展示 message,没有 message 就展示接口原文
|
|
|
+ string code = GetJsonValue(root, "code");
|
|
|
+ if (code != "" && code != "0")
|
|
|
+ {
|
|
|
+ string message = GetJsonValue(root, "message");
|
|
|
+ iErrorMessage = message == "" ? response.Trim() : message;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ //业务结果看 data.oErrMessage,为空表示校验通过
|
|
|
+ Dictionary<string, object> data = null;
|
|
|
+ if (root.ContainsKey("data") && root["data"] != null)
|
|
|
+ {
|
|
|
+ data = root["data"] as Dictionary<string, object>;
|
|
|
+ }
|
|
|
+ string oErrMessage = data == null ? "" : GetJsonValue(data, "oErrMessage");
|
|
|
+ if (oErrMessage != "")
|
|
|
+ {
|
|
|
+ iErrorMessage = oErrMessage;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ //解析异常时保留接口原始返回,便于排查
|
|
|
+ iErrorMessage = "解析MDM注册校验报文失败:" + ex.Message + ",接口返回:" + response.Trim();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 取 JSON 对象里的字符串值(键不存在或值为 null 时返回空串)
|
|
|
+ /// </summary>
|
|
|
+ private static string GetJsonValue(Dictionary<string, object> iJson, string iKey)
|
|
|
+ {
|
|
|
+ if (iJson == null || !iJson.ContainsKey(iKey) || iJson[iKey] == null)
|
|
|
+ {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return iJson[iKey].ToString().Trim();
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
}
|
|
|
}
|