選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

457 行
18 KiB

  1. using Nancy;
  2. using Learun.Util;
  3. using System.Collections.Generic;
  4. using Learun.Application.TwoDevelopment.EducationalAdministration;
  5. using System;
  6. using System.IO;
  7. using Learun.Application.Base.SystemModule;
  8. using System.Net;
  9. using System.Linq;
  10. using System.Text;
  11. using Newtonsoft.Json;
  12. using System.Collections;
  13. using System.Web;
  14. using Learun.Application.Organization;
  15. namespace Learun.Application.WebApi
  16. {
  17. /// <summary>
  18. /// 版 本 Learun-ADMS-Ultimate V7.0.0 力软敏捷开发框架
  19. /// Copyright (c) 2013-2018 上海力软信息技术有限公司
  20. /// 创 建:超级管理员
  21. /// 日 期:2019-08-19 17:50
  22. /// 描 述:传送接口
  23. /// </summary>
  24. public class TransferApi : BaseNoAuthentication
  25. {
  26. private EmpInfoIBLL empInfoIBLL = new EmpInfoBLL();
  27. private StuInfoBasicIBLL stuInfoBasicIBLL = new StuInfoBasicBLL();
  28. private CdDeptIBLL cdDeptIBLL = new CdDeptBLL();
  29. private UserIBLL userIBLL = new UserBLL();
  30. /// <summary>
  31. /// 注册接口
  32. /// <summary>
  33. public TransferApi()
  34. : base("/TransferApi/BasicInfo")
  35. {
  36. //发送部门基础信息
  37. Get["/sendDeptInfo"] = SendDeptInfo;
  38. //发送教师基础信息
  39. Get["/sendTeacherInfo"] = SendTeacherInfo;
  40. //发送学生基础信息
  41. Get["/sendStudentInfo"] = SendStudentInfo;
  42. }
  43. /// <summary>
  44. /// cookie
  45. /// </summary>
  46. public static CookieCollection cookie { get; set; }
  47. public static string cookiestr { get; set; }
  48. public static DateTime cookiestrTime { get; set; }
  49. #region 获取数据
  50. /// <summary>
  51. /// 获取cookie接口
  52. /// </summary>
  53. /// <returns></returns>
  54. public static bool GetCookie()
  55. {
  56. var loginParam = new
  57. {
  58. Id = "admin",
  59. Pwd = Md5Helper.Encrypt("Admin_123456", 32)
  60. };
  61. var responseLogin = SendRequest("POST", @"http://172.70.0.4:8088/fastgate/user/login", Encoding.UTF8, JsonConvert.SerializeObject(loginParam));
  62. var responseLoginObj = JsonConvert.DeserializeObject<dynamic>(responseLogin);
  63. if (responseLoginObj.ErrCode == 200)
  64. {
  65. return true;
  66. }
  67. else
  68. {
  69. return GetCookie();
  70. }
  71. }
  72. /// <summary>
  73. /// 发送部门基础信息
  74. /// <summary>
  75. /// <param name="_"></param>
  76. /// <returns></returns>
  77. public Response SendDeptInfo(dynamic _)
  78. {
  79. var result = new List<string>();
  80. if (string.IsNullOrEmpty(cookiestr) || ((DateTime.Now - cookiestrTime).Minutes > 10))
  81. {
  82. //调登录接口拿到cookie
  83. GetCookie();
  84. }
  85. var deptList = cdDeptIBLL.GetAllList();
  86. foreach (var item in deptList)
  87. {
  88. var param = new
  89. {
  90. ParentCode = "iccsid",
  91. pageSize = 10,
  92. Name = "部门5",
  93. Code = "5",
  94. };
  95. var responseSend = SendRequestWithCookie("POST", "application/json;charset=utf-8", @"http://172.70.0.4:8088/fastgate/department", Encoding.UTF8, JsonConvert.SerializeObject(param));
  96. var responseSendObj = JsonConvert.DeserializeObject<dynamic>(responseSend);
  97. result.Add("部门编号" + item.DeptNo + ",新增接口结果:" + responseSendObj.ErrCode + responseSendObj.ErrMsg);
  98. if (responseSendObj.ErrCode == 200)
  99. {
  100. //第三方新增成功,修改表-发送标识为true
  101. //TODO:
  102. }
  103. else if (responseSendObj.ErrCode == 1010) //{ "ErrCode":1010,"ErrMsg":"登录状态已失效"}
  104. {
  105. //获取cookie
  106. GetCookie();
  107. }
  108. else if (responseSendObj.ErrCode == 10000) //10000 code repetition
  109. {
  110. //调修改接口
  111. var param2 = new
  112. {
  113. Code = "5",
  114. Name = "部门bm5",
  115. pageSize = 10,
  116. };
  117. var responseSend2 = SendRequestWithCookie("PUT", "application/json;charset=utf-8", @"http://172.70.0.4:8088/fastgate/department", Encoding.UTF8, JsonConvert.SerializeObject(param2));
  118. var responseSendObj2 = JsonConvert.DeserializeObject<dynamic>(responseSend2);
  119. result.Add("部门编号" + item.DeptNo + ",修改接口结果:" + responseSendObj2.ErrCode + responseSendObj2.ErrMsg);
  120. if (responseSendObj2.ErrCode == 200)
  121. {
  122. //第三方修改成功,修改表-发送标识为true
  123. //TODO:
  124. }
  125. else
  126. {
  127. //其他错误
  128. }
  129. }
  130. else
  131. {
  132. //其他错误
  133. }
  134. }
  135. return Success(result);
  136. }
  137. /// <summary>
  138. /// 发送教师基础信息
  139. /// <summary>
  140. /// <param name="_"></param>
  141. /// <returns></returns>
  142. public Response SendTeacherInfo(dynamic _)
  143. {
  144. var result = new List<string>();
  145. if (string.IsNullOrEmpty(cookiestr) || ((DateTime.Now - cookiestrTime).Minutes > 10))
  146. {
  147. //调登录接口拿到cookie
  148. GetCookie();
  149. }
  150. //var teacherList = empInfoIBLL.GetAllList();
  151. //foreach (var item in teacherList)
  152. {
  153. Dictionary<string, string> parameters = new Dictionary<string, string>(); //参数列表
  154. parameters.Add("Sex", "1");
  155. parameters.Add("Depart", "5");
  156. parameters.Add("Name", "测试1");
  157. parameters.Add("Code", "ceshi1");
  158. var param = BuildQuery2(parameters, "----WebKitFormBoundaryAOT8GpQYYAP9jgGf");
  159. var contentType = "multipart/form-data; boundary=----WebKitFormBoundaryAOT8GpQYYAP9jgGf";
  160. var responseSend = SendRequestWithCookie("POST",contentType, @"http://172.70.0.4:8088/fastgate/person", Encoding.UTF8, param);
  161. var responseSendObj = JsonConvert.DeserializeObject<dynamic>(responseSend);
  162. result.Add("教师编号,新增接口结果:" + responseSendObj.ErrCode + responseSendObj.ErrMsg);
  163. if (responseSendObj.ErrCode == 200)
  164. {
  165. //第三方新增成功,修改表-发送标识为true
  166. //TODO:
  167. }
  168. else if (responseSendObj.ErrCode == 1010) //{ "ErrCode":1010,"ErrMsg":"登录状态已失效"}
  169. {
  170. //获取cookie
  171. GetCookie();
  172. }
  173. else if (responseSendObj.ErrCode == 202) //202 code repeat
  174. {
  175. //调查询接口
  176. var responseSend3 = SendRequestWithCookie("GET", "application/json;charset=utf-8", @"http://172.70.0.4:8088/fastgate/personCode/ceshi1", Encoding.UTF8, null);
  177. var responseSendObj3 = JsonConvert.DeserializeObject<PersonInfo>(responseSend3);
  178. result.Add("教师编号,查询接口结果:" + responseSendObj3.ErrCode + responseSendObj3.ErrMsg + JsonConvert.SerializeObject(responseSendObj3.data));
  179. if (responseSendObj3.ErrCode == 200)
  180. {
  181. //第三方查询成功
  182. //调修改接口
  183. Dictionary<string, string> parameters2 = new Dictionary<string, string>(); //参数列表
  184. parameters2.Add("Sex", "1");
  185. parameters2.Add("Depart", "5");
  186. parameters2.Add("Name", "测试ceshi1");
  187. parameters2.Add("Code", "ceshi1");
  188. parameters2.Add("Seqid", responseSendObj3.data.Seqid.ToString());
  189. parameters2.Add("ImageList", string.Join(",", responseSendObj3.data.Pictures.Select(x => x.PersonPicturePath)));
  190. var param2 = BuildQuery2(parameters2, "----WebKitFormBoundaryMAj7ABMLt3aJpmGH");
  191. var contentType2 = "multipart/form-data; boundary=----WebKitFormBoundaryMAj7ABMLt3aJpmGH";
  192. var responseSend2 = SendRequestWithCookie("PUT", contentType2, @"http://172.70.0.4:8088/fastgate/person", Encoding.UTF8, param2);
  193. var responseSendObj2 = JsonConvert.DeserializeObject<dynamic>(responseSend2);
  194. result.Add("教师编号,修改接口结果:" + responseSendObj2.ErrCode + responseSendObj2.ErrMsg);
  195. if (responseSendObj2.ErrCode == 200)
  196. {
  197. //第三方修改成功,修改表-发送标识为true
  198. //TODO:
  199. }
  200. else if (responseSendObj2.ErrCode == 1010) //{ "ErrCode":1010,"ErrMsg":"登录状态已失效"}
  201. {
  202. //获取cookie
  203. GetCookie();
  204. }
  205. else
  206. {
  207. //其他错误
  208. }
  209. }
  210. else if (responseSendObj3.ErrCode == 1010) //{ "ErrCode":1010,"ErrMsg":"登录状态已失效"}
  211. {
  212. //获取cookie
  213. GetCookie();
  214. }
  215. }
  216. else
  217. {
  218. //其他错误
  219. }
  220. }
  221. return Success(result);
  222. }
  223. /// <summary>
  224. /// 发送学生基础信息
  225. /// </summary>
  226. /// <returns></returns>
  227. public Response SendStudentInfo(dynamic _)
  228. {
  229. return Success("成功了!");
  230. }
  231. #endregion
  232. #region 公共方法
  233. /// <summary>
  234. /// 请求获取cookie:
  235. /// </summary>
  236. /// <param name="type">GET,POST</param>
  237. /// <param name="url"></param>
  238. /// <param name="encoding">UTF-8</param>
  239. /// <param name="data">传送需要的参数</param>
  240. /// <returns></returns>
  241. public static string SendRequest(string type, string url, Encoding encoding, string data)
  242. {
  243. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
  244. webRequest.Method = type;
  245. webRequest.ContentType = "application/json";
  246. if (type == "POST")
  247. {
  248. byte[] postdata = Encoding.UTF8.GetBytes(data);
  249. webRequest.ContentLength = postdata.Length;
  250. Stream newStream = webRequest.GetRequestStream();
  251. newStream.Write(postdata, 0, postdata.Length);
  252. newStream.Close();
  253. }
  254. //使用HttpWebRequest的CookieContainer属性进行设置
  255. CookieContainer ckCtnr = new CookieContainer();
  256. if (cookie != null)
  257. {
  258. ckCtnr.Add(cookie); //此处的cookie是HttpWebResponse对象的Cookies属性
  259. }
  260. //获取应答网页
  261. webRequest.CookieContainer = ckCtnr;
  262. //webRequest.Headers.Add("Cookie", "JSESSIONID=518211FE9243E875A3C570D510E7262F; Path=/fastgate; HttpOnly");
  263. HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
  264. cookie = webResponse.Cookies;//获取Cookie
  265. cookiestr = webResponse.Headers.Get("Set-Cookie");//获取Cookie字符串
  266. if (!string.IsNullOrEmpty(cookiestr))
  267. {
  268. cookiestrTime = DateTime.Now;//获取Cookie的时间
  269. }
  270. StreamReader sr = new StreamReader(webResponse.GetResponseStream(), encoding);
  271. string str = sr.ReadToEnd();
  272. sr.Close();
  273. webResponse.Close();
  274. return str;
  275. }
  276. /// <summary>
  277. /// 请求携带cookie:
  278. /// </summary>
  279. /// <param name="type">GET,POST,PUT</param>
  280. /// <param name="contentType">请求头类型(json是application/json;charset=utf-8,form-data是multipart/form-data; boundary=----WebKitFormBoundaryAOT8GpQYYAP9jgGf,)</param>
  281. /// <param name="url"></param>
  282. /// <param name="encoding">UTF-8</param>
  283. /// <param name="data">传送需要的参数</param>
  284. /// <returns></returns>
  285. public static string SendRequestWithCookie(string type, string contentType, string url, Encoding encoding, string data)
  286. {
  287. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
  288. webRequest.Method = type;
  289. webRequest.ContentType = contentType;
  290. webRequest.Headers.Add("Cookie", cookiestr);//携带Cookie
  291. if (type == "POST" || type == "PUT")
  292. {
  293. byte[] postdata = Encoding.UTF8.GetBytes(data);
  294. webRequest.ContentLength = postdata.Length;
  295. Stream newStream = webRequest.GetRequestStream();
  296. newStream.Write(postdata, 0, postdata.Length);
  297. newStream.Close();
  298. }
  299. HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
  300. StreamReader sr = new StreamReader(webResponse.GetResponseStream(), encoding);
  301. string str = sr.ReadToEnd();
  302. sr.Close();
  303. webResponse.Close();
  304. return str;
  305. }
  306. /// <summary>
  307. /// 组装请求参数
  308. /// </summary>
  309. /// <param name="parameters"></param>
  310. /// <param name="encode"></param>
  311. /// <returns></returns>
  312. private static string BuildQuery(IDictionary<string, string> parameters, string encode)
  313. {
  314. StringBuilder postData = new StringBuilder();
  315. bool hasParam = false;
  316. IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
  317. while (dem.MoveNext())
  318. {
  319. string name = dem.Current.Key;
  320. string value = dem.Current.Value;
  321. // 忽略参数名或参数值为空的参数
  322. if (!string.IsNullOrEmpty(name))
  323. {
  324. if (hasParam)
  325. {
  326. postData.Append("&");
  327. }
  328. postData.Append(name);
  329. postData.Append("=");
  330. if (encode == "gb2312")
  331. {
  332. postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding("gb2312")));
  333. }
  334. else if (encode == "utf8")
  335. {
  336. postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
  337. }
  338. else
  339. {
  340. postData.Append(value);
  341. }
  342. hasParam = true;
  343. }
  344. }
  345. return postData.ToString();
  346. }
  347. /// <summary>
  348. /// 组装请求参数
  349. /// </summary>
  350. /// <param name="parameters">key-value</param>
  351. /// <param name="boundary">分隔符</param>
  352. /// <returns></returns>
  353. private static string BuildQuery2(IDictionary<string, string> parameters, string boundary)
  354. {
  355. StringBuilder buffer = new StringBuilder();
  356. foreach (string key in parameters.Keys)
  357. {
  358. buffer.Append("\r\n").Append("--").Append(boundary).Append("\r\n");
  359. if (key == "file")
  360. {
  361. buffer.Append("Content-Disposition: form-data; name=\"" + key + "\";filename=\"920_complete_1.jpg\"\r\n");
  362. buffer.Append("Content-Type: image/jpeg");
  363. }
  364. else
  365. {
  366. buffer.Append("Content-Disposition: form-data; name=\"" + key + "\"\r\n\r\n");
  367. buffer.Append(parameters[key]);
  368. }
  369. }
  370. buffer.Append("\r\n").Append("--").Append(boundary).Append("--");
  371. return buffer.ToString();
  372. }
  373. #endregion
  374. #region 私有类
  375. /// <summary>
  376. /// 表单实体类
  377. /// <summary>
  378. private class ReqFormEntity
  379. {
  380. public string keyValue { get; set; }
  381. public string strEntity { get; set; }
  382. }
  383. /// <summary>
  384. /// 人员详细信息
  385. /// <summary>
  386. private class PersonInfo
  387. {
  388. /// <summary>
  389. /// 错误码(200表示成功,
  390. /// </summary>
  391. public int ErrCode { get; set; }
  392. /// <summary>
  393. /// 错误信息,”success”为正常
  394. /// </summary>
  395. public string ErrMsg { get; set; }
  396. public PersonInfoData data { get; set; }
  397. }
  398. private class PersonInfoData
  399. {
  400. /// <summary>
  401. /// 人员存在数据库的唯一ID
  402. /// </summary>
  403. public int Seqid { get; set; }
  404. /// <summary>
  405. /// 头像存储的地址ur
  406. /// </summary>
  407. public List<PersonDataPictures> Pictures { get; set; }
  408. }
  409. private class PersonDataPictures
  410. {
  411. /// <summary>
  412. /// 登记照片地址
  413. /// </summary>
  414. public string PersonPicturePath { get; set; }
  415. }
  416. #endregion
  417. }
  418. }