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;
///
/// 调用MES的WCF接口通用方法 xuwei 2019-10-26
///
public class WCF
{
///
/// MES服务器IP地址。
///
public string IP;
///
/// MES服务WCF调用参数。
///
public JObject Para;
///
/// 实例化MES服务WCF调用方法。
///
public WCF()
{
//从Web.config读取服务器IP
IP = ConfigurationManager.AppSettings["ProductCheckServer"].ToString();
Para = new JObject();
}
///
/// 初始化默认WCF参数,如果存在Session从Session中加载,否则实例化空参数
///
/// 参数名称字符串,以逗号分隔。
///
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;
}
///
/// 初始化默认登录参数
///
///
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;
}
///
/// 获取用户session状态
///
///
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;
}
///
/// 登录并记录Session数据
///
/// 完整URL,但不包括IP部分。
///
public string Login(string URL = "", string GoURL = "")
{
//提交post数据
string jsonStr = JsonClient.Post(IP + URL, JsonConvert.SerializeObject(Para));
if (jsonStr.IndexOf("") > 0) jsonStr = jsonStr.Substring(0, jsonStr.IndexOf(""));
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();
}
///
/// 从Para记录 Session 默认 accountCode,userCode,userPassword,sessionKey
///
/// 要清除的Session串用逗号分隔
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();
}
}
///
/// 清除 Session 默认 accountCode,userCode,userPassword,sessionKey
///
/// 要清除的Session串用逗号分隔
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]);
}
}
///
/// 退出登录,清除所有Session
///
///
public string LogOut()
{
ClearSessionByPara();
return "";
}
///
/// 向URL指定的WCF接口提交POST数据
///
/// 完整URL,但不包括IP部分。
///
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("") > 0) jsonStr = jsonStr.Substring(0, jsonStr.IndexOf(""));
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 "";
}
}
///
/// 向URL指定的WCF接口提交POST数据
///
/// 完整URL,但不包括IP部分。
///
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("") > 0) jsonStr = jsonStr.Substring(0, jsonStr.IndexOf(""));
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 "";
}
}
}