| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System.Configuration;
- using Curtain.DataAccess;
- using DK.XuWei.WebMes;
- using Curtain.Log;
- using Curtain.Extension.ExCompareOperator;
- /// <summary>
- /// 调用MES的WCF接口通用方法 xuwei 2019-10-26
- /// </summary>
- public class WCF
- {
- /// <summary>
- /// MES服务器IP地址。
- /// </summary>
- public string IP;
- /// <summary>
- /// MES服务WCF调用参数。
- /// </summary>
- public JObject Para;
- /// <summary>
- /// 实例化MES服务WCF调用方法。
- /// </summary>
- public WCF()
- {
- //从Web.config读取服务器IP
- IP = ConfigurationManager.AppSettings["ProductCheckServer"].ToString();
- Para = new JObject();
- }
- /// <summary>
- /// 初始化默认WCF参数,如果存在Session从Session中加载,否则实例化空参数
- /// </summary>
- /// <param name="paraNames">参数名称字符串,以逗号分隔。</param>
- /// <returns></returns>
- public JObject LoadParaBySession(string paraNames = "accountId,accountCode,userId,userName,userCode,userPassword,sessionKey")
- {
- JObject para = new JObject();
- string[] sessionNames = paraNames.Split(',');
- for (int i = 0; i < sessionNames.Length; i++)
- {
- if (HttpContext.Current.Session[sessionNames[i]] is object)
- para.Add(new JProperty(sessionNames[i], HttpContext.Current.Session[sessionNames[i]]));
- }
- return para;
- }
- /// <summary>
- /// 初始化默认登录参数
- /// </summary>
- /// <returns></returns>
- public JObject LoadParaByLoginDefault()
- {
- JObject para = new JObject(
- new JProperty("macAddress", "xx:xx:xx:xx:xx:xx"),
- new JProperty("ipAddress", "250.250.250.250"),
- new JProperty("phoneCode", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"),
- new JProperty("phoneType", "WebMes"),
- new JProperty("appVersion", "1.0"),
- new JProperty("systemType", "WindowsServer"),
- new JProperty("systemVersion", "2008R2")
- );
- return para;
- }
- /// <summary>
- /// 获取用户session状态
- /// </summary>
- /// <returns></returns>
- public JObject GetSession()
- {
- JObject para = new JObject();
- for (int i = 0; i < HttpContext.Current.Session.Count; i++)
- {
- if (HttpContext.Current.Session[i] is object)
- para.Add(new JProperty(HttpContext.Current.Session.Keys[i], HttpContext.Current.Session[i]));
- }
- return para;
- }
- /// <summary>
- /// 登录并记录Session数据
- /// </summary>
- /// <param name="URL">完整URL,但不包括IP部分。</param>
- /// <returns></returns>
- public string Login(string URL = "", string GoURL = "")
- {
- //提交post数据
- string jsonStr = JsonClient.Post(IP + URL, JsonConvert.SerializeObject(Para));
- if (jsonStr.IndexOf("<html>") > 0) jsonStr = jsonStr.Substring(0, jsonStr.IndexOf("<html>"));
- JObject json = JObject.Parse(jsonStr);
- JObject result = new JObject(
- new JProperty("Status", json["d"]["Status"].ToString()),
- new JProperty("Message", json["d"]["Message"].ToString()),
- new JProperty("GoUrl", GoURL)
- );
- //登录成功记录Session
- if (json["d"]["Status"].ToString() == "0")
- {
- //检查工号是否配置了工位
- object isWorkStation = null;
- using (IDataAccess conn = DataAccess.Create())
- {
- isWorkStation = conn.ExecuteScalar(@"
- SELECT
- w.WORKSTATIONNAME
- FROM
- TP_MST_WORKSTATIONUSER u
- LEFT JOIN TP_MST_WORKSTATION w ON u.WORKSTATIONID = w.WORKSTATIONID
- WHERE
- w.VALUEFLAG = '1'
- AND u.VALUEFLAG = '1'
- AND u.USERCODE = @USERCODE@
- ",
- new CDAParameter("USERCODE", Para["userCode"].ToString())
- );
- }
- if(isWorkStation is object)
- {
- Para.Add(new JProperty("accountId", json["d"]["AccountID"].ToString()));
- Para.Add(new JProperty("userId", json["d"]["UserID"].ToString()));
- Para.Add(new JProperty("userName", json["d"]["UserName"].ToString()));
- Para.Add(new JProperty("sessionKey", json["d"]["SessionKey"].ToString()));
- Para.Add(new JProperty("workstationName", isWorkStation.ToString()));
- SetSessionByPara();
- }
- else
- {
- result["Status"] = -1;
- result["Message"] = "当前工号未配置工位!";
- result["GoUrl"] = GoURL;
- }
- }
- return result.ToString();
- }
- /// <summary>
- /// 从Para记录 Session 默认 accountCode,userCode,userPassword,sessionKey
- /// </summary>
- /// <param name="paraNames">要清除的Session串用逗号分隔</param>
- public void SetSessionByPara(string paraNames = "accountId,accountCode,userId,userName,userCode,userPassword,sessionKey")
- {
- string[] sessionNames = paraNames.Split(',');
- for (int i = 0; i < sessionNames.Length; i++)
- {
- HttpContext.Current.Session[sessionNames[i]] = Para[sessionNames[i]].ToString();
- }
- }
- /// <summary>
- /// 清除 Session 默认 accountCode,userCode,userPassword,sessionKey
- /// </summary>
- /// <param name="paraNames">要清除的Session串用逗号分隔</param>
- public void ClearSessionByPara(string paraNames = "accountCode,userCode,userPassword,sessionKey")
- {
- string[] sessionNames = paraNames.Split(',');
- for (int i = 0; i < sessionNames.Length; i++)
- {
- HttpContext.Current.Session[sessionNames[i]] = null;
- HttpContext.Current.Session.Remove(sessionNames[i]);
- }
- }
- /// <summary>
- /// 退出登录,清除所有Session
- /// </summary>
- /// <returns></returns>
- public string LogOut()
- {
- ClearSessionByPara();
- return "";
- }
- /// <summary>
- /// 向URL指定的WCF接口提交POST数据
- /// </summary>
- /// <param name="URL">完整URL,但不包括IP部分。</param>
- /// <returns></returns>
- public string Post(string URL = "")
- {
- string jsonStr = "";
- //xuwei fix 2020-06-17 检查出错接口详细问题 写入log
- try
- {
- //提交post数据
- jsonStr = JsonClient.Post(IP + URL, JsonConvert.SerializeObject(Para));
- if (jsonStr.IndexOf("<html>") > 0) jsonStr = jsonStr.Substring(0, jsonStr.IndexOf("<html>"));
- JObject json = JObject.Parse(jsonStr);
- //状态为0,返回内容不为空才是查询到结果
- if (json["d"]["Status"].ToString() == "0" && json["d"]["Result"].ToString() != "[]")
- {
- jsonStr = json["d"]["Result"].ToString();
- if (jsonStr == "") jsonStr = json["d"]["Message"].ToString();
- }
- //成检重复判断 2020-06-28 xuwei
- else if (json["d"]["Status"].ToString() == "2" && json["d"]["Result"].ToString() != "[]")
- {
- jsonStr = "重复提交";
- }
- //成检重复判断 2020-06-28 xuwei
- else if (json["d"]["Status"].ToString() == "666" && json["d"]["Result"].ToString() != "[]")
- {
- jsonStr = "666:" + json["d"]["Result"].ToString();
- }
- else
- {
- //jsonStr = json["d"]["Message"].ToString();
- jsonStr = "";
- }
- //Exception ex = new Exception("OK 数据接口返回值(" + IP + URL + "):" + jsonStr);
- //Curtain.Log.Logger.Error(ex);
- return jsonStr;
- }
- catch
- {
- Exception ex = new Exception("ERR 数据接口返回值(IP:" + IP + " URL:" + URL + " PARA:" + JsonConvert.SerializeObject(Para) + ") RESULT:" + jsonStr);
- Logger.Error(ex);
- return "";
- }
- }
- /// <summary>
- /// 向URL指定的WCF接口提交POST数据
- /// </summary>
- /// <param name="URL">完整URL,但不包括IP部分。</param>
- /// <returns></returns>
- public string Post1(string URL = "")
- {
- string jsonStr = "";
- //xuwei fix 2020-06-17 检查出错接口详细问题 写入log
- try
- {
- //提交post数据
- jsonStr = JsonClient.Post(IP + URL, JsonConvert.SerializeObject(Para));
- if (jsonStr.IndexOf("<html>") > 0) jsonStr = jsonStr.Substring(0, jsonStr.IndexOf("<html>"));
- JObject json = JObject.Parse(jsonStr);
- //状态为0,返回内容不为空才是查询到结果
- if (json["d"]["Status"].ToString() == "0" && json["d"]["Result"].ToString() != "[]")
- {
- jsonStr = json["d"]["Result"].ToString();
- if (jsonStr == "") jsonStr = json["d"]["Message"].ToString();
- }
- //成检重复判断 2020-06-28 xuwei
- else if (json["d"]["Status"].ToString() == "2" && json["d"]["Result"].ToString() != "[]")
- {
- jsonStr = "重复提交";
- }
- else if (json["d"]["Status"].ToString() == "999")
- {
- jsonStr = json["d"].ToString();
- }
- else
- {
- //jsonStr = json["d"]["Message"].ToString();
- jsonStr = "";
- }
- //Exception ex = new Exception("OK 数据接口返回值(" + IP + URL + "):" + jsonStr);
- //Curtain.Log.Logger.Error(ex);
- return jsonStr;
- }
- catch
- {
- Exception ex = new Exception("ERR 数据接口返回值(IP:" + IP + " URL:" + URL + " PARA:" + JsonConvert.SerializeObject(Para) + ") RESULT:" + jsonStr);
- Logger.Error(ex);
- return "";
- }
- }
- }
|