WCF.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. using System.Configuration;
  8. using Curtain.DataAccess;
  9. using DK.XuWei.WebMes;
  10. using Curtain.Log;
  11. using Curtain.Extension.ExCompareOperator;
  12. /// <summary>
  13. /// 调用MES的WCF接口通用方法 xuwei 2019-10-26
  14. /// </summary>
  15. public class WCF
  16. {
  17. /// <summary>
  18. /// MES服务器IP地址。
  19. /// </summary>
  20. public string IP;
  21. /// <summary>
  22. /// MES服务WCF调用参数。
  23. /// </summary>
  24. public JObject Para;
  25. /// <summary>
  26. /// 实例化MES服务WCF调用方法。
  27. /// </summary>
  28. public WCF()
  29. {
  30. //从Web.config读取服务器IP
  31. IP = ConfigurationManager.AppSettings["ProductCheckServer"].ToString();
  32. Para = new JObject();
  33. }
  34. /// <summary>
  35. /// 初始化默认WCF参数,如果存在Session从Session中加载,否则实例化空参数
  36. /// </summary>
  37. /// <param name="paraNames">参数名称字符串,以逗号分隔。</param>
  38. /// <returns></returns>
  39. public JObject LoadParaBySession(string paraNames = "accountId,accountCode,userId,userName,userCode,userPassword,sessionKey")
  40. {
  41. JObject para = new JObject();
  42. string[] sessionNames = paraNames.Split(',');
  43. for (int i = 0; i < sessionNames.Length; i++)
  44. {
  45. if (HttpContext.Current.Session[sessionNames[i]] is object)
  46. para.Add(new JProperty(sessionNames[i], HttpContext.Current.Session[sessionNames[i]]));
  47. }
  48. return para;
  49. }
  50. /// <summary>
  51. /// 初始化默认登录参数
  52. /// </summary>
  53. /// <returns></returns>
  54. public JObject LoadParaByLoginDefault()
  55. {
  56. JObject para = new JObject(
  57. new JProperty("macAddress", "xx:xx:xx:xx:xx:xx"),
  58. new JProperty("ipAddress", "250.250.250.250"),
  59. new JProperty("phoneCode", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"),
  60. new JProperty("phoneType", "WebMes"),
  61. new JProperty("appVersion", "1.0"),
  62. new JProperty("systemType", "WindowsServer"),
  63. new JProperty("systemVersion", "2008R2")
  64. );
  65. return para;
  66. }
  67. /// <summary>
  68. /// 获取用户session状态
  69. /// </summary>
  70. /// <returns></returns>
  71. public JObject GetSession()
  72. {
  73. JObject para = new JObject();
  74. for (int i = 0; i < HttpContext.Current.Session.Count; i++)
  75. {
  76. if (HttpContext.Current.Session[i] is object)
  77. para.Add(new JProperty(HttpContext.Current.Session.Keys[i], HttpContext.Current.Session[i]));
  78. }
  79. return para;
  80. }
  81. /// <summary>
  82. /// 登录并记录Session数据
  83. /// </summary>
  84. /// <param name="URL">完整URL,但不包括IP部分。</param>
  85. /// <returns></returns>
  86. public string Login(string URL = "", string GoURL = "")
  87. {
  88. //提交post数据
  89. string jsonStr = JsonClient.Post(IP + URL, JsonConvert.SerializeObject(Para));
  90. if (jsonStr.IndexOf("<html>") > 0) jsonStr = jsonStr.Substring(0, jsonStr.IndexOf("<html>"));
  91. JObject json = JObject.Parse(jsonStr);
  92. JObject result = new JObject(
  93. new JProperty("Status", json["d"]["Status"].ToString()),
  94. new JProperty("Message", json["d"]["Message"].ToString()),
  95. new JProperty("GoUrl", GoURL)
  96. );
  97. //登录成功记录Session
  98. if (json["d"]["Status"].ToString() == "0")
  99. {
  100. //检查工号是否配置了工位
  101. object isWorkStation = null;
  102. using (IDataAccess conn = DataAccess.Create())
  103. {
  104. isWorkStation = conn.ExecuteScalar(@"
  105. SELECT
  106. w.WORKSTATIONNAME
  107. FROM
  108. TP_MST_WORKSTATIONUSER u
  109. LEFT JOIN TP_MST_WORKSTATION w ON u.WORKSTATIONID = w.WORKSTATIONID
  110. WHERE
  111. w.VALUEFLAG = '1'
  112. AND u.VALUEFLAG = '1'
  113. AND u.USERCODE = @USERCODE@
  114. ",
  115. new CDAParameter("USERCODE", Para["userCode"].ToString())
  116. );
  117. }
  118. if(isWorkStation is object)
  119. {
  120. Para.Add(new JProperty("accountId", json["d"]["AccountID"].ToString()));
  121. Para.Add(new JProperty("userId", json["d"]["UserID"].ToString()));
  122. Para.Add(new JProperty("userName", json["d"]["UserName"].ToString()));
  123. Para.Add(new JProperty("sessionKey", json["d"]["SessionKey"].ToString()));
  124. Para.Add(new JProperty("workstationName", isWorkStation.ToString()));
  125. SetSessionByPara();
  126. }
  127. else
  128. {
  129. result["Status"] = -1;
  130. result["Message"] = "当前工号未配置工位!";
  131. result["GoUrl"] = GoURL;
  132. }
  133. }
  134. return result.ToString();
  135. }
  136. /// <summary>
  137. /// 从Para记录 Session 默认 accountCode,userCode,userPassword,sessionKey
  138. /// </summary>
  139. /// <param name="paraNames">要清除的Session串用逗号分隔</param>
  140. public void SetSessionByPara(string paraNames = "accountId,accountCode,userId,userName,userCode,userPassword,sessionKey")
  141. {
  142. string[] sessionNames = paraNames.Split(',');
  143. for (int i = 0; i < sessionNames.Length; i++)
  144. {
  145. HttpContext.Current.Session[sessionNames[i]] = Para[sessionNames[i]].ToString();
  146. }
  147. }
  148. /// <summary>
  149. /// 清除 Session 默认 accountCode,userCode,userPassword,sessionKey
  150. /// </summary>
  151. /// <param name="paraNames">要清除的Session串用逗号分隔</param>
  152. public void ClearSessionByPara(string paraNames = "accountCode,userCode,userPassword,sessionKey")
  153. {
  154. string[] sessionNames = paraNames.Split(',');
  155. for (int i = 0; i < sessionNames.Length; i++)
  156. {
  157. HttpContext.Current.Session[sessionNames[i]] = null;
  158. HttpContext.Current.Session.Remove(sessionNames[i]);
  159. }
  160. }
  161. /// <summary>
  162. /// 退出登录,清除所有Session
  163. /// </summary>
  164. /// <returns></returns>
  165. public string LogOut()
  166. {
  167. ClearSessionByPara();
  168. return "";
  169. }
  170. /// <summary>
  171. /// 向URL指定的WCF接口提交POST数据
  172. /// </summary>
  173. /// <param name="URL">完整URL,但不包括IP部分。</param>
  174. /// <returns></returns>
  175. public string Post(string URL = "")
  176. {
  177. string jsonStr = "";
  178. //xuwei fix 2020-06-17 检查出错接口详细问题 写入log
  179. try
  180. {
  181. //提交post数据
  182. jsonStr = JsonClient.Post(IP + URL, JsonConvert.SerializeObject(Para));
  183. if (jsonStr.IndexOf("<html>") > 0) jsonStr = jsonStr.Substring(0, jsonStr.IndexOf("<html>"));
  184. JObject json = JObject.Parse(jsonStr);
  185. //状态为0,返回内容不为空才是查询到结果
  186. if (json["d"]["Status"].ToString() == "0" && json["d"]["Result"].ToString() != "[]")
  187. {
  188. jsonStr = json["d"]["Result"].ToString();
  189. if (jsonStr == "") jsonStr = json["d"]["Message"].ToString();
  190. }
  191. //成检重复判断 2020-06-28 xuwei
  192. else if (json["d"]["Status"].ToString() == "2" && json["d"]["Result"].ToString() != "[]")
  193. {
  194. jsonStr = "重复提交";
  195. }
  196. //成检重复判断 2020-06-28 xuwei
  197. else if (json["d"]["Status"].ToString() == "666" && json["d"]["Result"].ToString() != "[]")
  198. {
  199. jsonStr = "666:" + json["d"]["Result"].ToString();
  200. }
  201. else
  202. {
  203. //jsonStr = json["d"]["Message"].ToString();
  204. jsonStr = "";
  205. }
  206. //Exception ex = new Exception("OK 数据接口返回值(" + IP + URL + "):" + jsonStr);
  207. //Curtain.Log.Logger.Error(ex);
  208. return jsonStr;
  209. }
  210. catch
  211. {
  212. Exception ex = new Exception("ERR 数据接口返回值(IP:" + IP + " URL:" + URL + " PARA:" + JsonConvert.SerializeObject(Para) + ") RESULT:" + jsonStr);
  213. Logger.Error(ex);
  214. return "";
  215. }
  216. }
  217. /// <summary>
  218. /// 向URL指定的WCF接口提交POST数据
  219. /// </summary>
  220. /// <param name="URL">完整URL,但不包括IP部分。</param>
  221. /// <returns></returns>
  222. public string Post1(string URL = "")
  223. {
  224. string jsonStr = "";
  225. //xuwei fix 2020-06-17 检查出错接口详细问题 写入log
  226. try
  227. {
  228. //提交post数据
  229. jsonStr = JsonClient.Post(IP + URL, JsonConvert.SerializeObject(Para));
  230. if (jsonStr.IndexOf("<html>") > 0) jsonStr = jsonStr.Substring(0, jsonStr.IndexOf("<html>"));
  231. JObject json = JObject.Parse(jsonStr);
  232. //状态为0,返回内容不为空才是查询到结果
  233. if (json["d"]["Status"].ToString() == "0" && json["d"]["Result"].ToString() != "[]")
  234. {
  235. jsonStr = json["d"]["Result"].ToString();
  236. if (jsonStr == "") jsonStr = json["d"]["Message"].ToString();
  237. }
  238. //成检重复判断 2020-06-28 xuwei
  239. else if (json["d"]["Status"].ToString() == "2" && json["d"]["Result"].ToString() != "[]")
  240. {
  241. jsonStr = "重复提交";
  242. }
  243. else if (json["d"]["Status"].ToString() == "999")
  244. {
  245. jsonStr = json["d"].ToString();
  246. }
  247. else
  248. {
  249. //jsonStr = json["d"]["Message"].ToString();
  250. jsonStr = "";
  251. }
  252. //Exception ex = new Exception("OK 数据接口返回值(" + IP + URL + "):" + jsonStr);
  253. //Curtain.Log.Logger.Error(ex);
  254. return jsonStr;
  255. }
  256. catch
  257. {
  258. Exception ex = new Exception("ERR 数据接口返回值(IP:" + IP + " URL:" + URL + " PARA:" + JsonConvert.SerializeObject(Para) + ") RESULT:" + jsonStr);
  259. Logger.Error(ex);
  260. return "";
  261. }
  262. }
  263. }