|
|
@@ -0,0 +1,447 @@ |
|
|
|
using Nancy; |
|
|
|
using Learun.Util; |
|
|
|
using System.Collections.Generic; |
|
|
|
using Learun.Application.TwoDevelopment.EducationalAdministration; |
|
|
|
using System; |
|
|
|
using System.IO; |
|
|
|
using Learun.Application.Base.SystemModule; |
|
|
|
using System.Net; |
|
|
|
using System.Linq; |
|
|
|
using System.Text; |
|
|
|
using Newtonsoft.Json; |
|
|
|
using System.Collections; |
|
|
|
using System.Web; |
|
|
|
using Learun.Application.Organization; |
|
|
|
|
|
|
|
namespace Learun.Application.WebApi |
|
|
|
{ |
|
|
|
/// <summary> |
|
|
|
/// 版 本 Learun-ADMS-Ultimate V7.0.0 力软敏捷开发框架 |
|
|
|
/// Copyright (c) 2013-2018 上海力软信息技术有限公司 |
|
|
|
/// 创 建:超级管理员 |
|
|
|
/// 日 期:2019-08-19 17:50 |
|
|
|
/// 描 述:传送接口 |
|
|
|
/// </summary> |
|
|
|
|
|
|
|
public class TransferApi : BaseNoAuthentication |
|
|
|
{ |
|
|
|
private EmpInfoIBLL empInfoIBLL = new EmpInfoBLL(); |
|
|
|
private StuInfoBasicIBLL stuInfoBasicIBLL = new StuInfoBasicBLL(); |
|
|
|
private CdDeptIBLL cdDeptIBLL = new CdDeptBLL(); |
|
|
|
private UserIBLL userIBLL = new UserBLL(); |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// 注册接口 |
|
|
|
/// <summary> |
|
|
|
public TransferApi() |
|
|
|
: base("/TransferApi/BasicInfo") |
|
|
|
{ |
|
|
|
//发送部门基础信息 |
|
|
|
Get["/sendDeptInfo"] = SendDeptInfo; |
|
|
|
//发送教师基础信息 |
|
|
|
Get["/sendTeacherInfo"] = SendTeacherInfo; |
|
|
|
} |
|
|
|
/// <summary> |
|
|
|
/// cookie |
|
|
|
/// </summary> |
|
|
|
public static CookieCollection cookie { get; set; } |
|
|
|
public static string cookiestr { get; set; } |
|
|
|
public static DateTime cookiestrTime { get; set; } |
|
|
|
|
|
|
|
#region 获取数据 |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// 获取cookie接口 |
|
|
|
/// </summary> |
|
|
|
/// <returns></returns> |
|
|
|
public static bool GetCookie() |
|
|
|
{ |
|
|
|
var loginParam = new |
|
|
|
{ |
|
|
|
Id = "admin", |
|
|
|
Pwd = Md5Helper.Encrypt("Admin_123456", 32) |
|
|
|
}; |
|
|
|
var responseLogin = SendRequest("POST", @"http://172.70.0.4:8088/fastgate/user/login", Encoding.UTF8, JsonConvert.SerializeObject(loginParam)); |
|
|
|
var responseLoginObj = JsonConvert.DeserializeObject<dynamic>(responseLogin); |
|
|
|
if (responseLoginObj.ErrCode == 200) |
|
|
|
{ |
|
|
|
return true; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
return GetCookie(); |
|
|
|
} |
|
|
|
} |
|
|
|
/// <summary> |
|
|
|
/// 发送部门基础信息 |
|
|
|
/// <summary> |
|
|
|
/// <param name="_"></param> |
|
|
|
/// <returns></returns> |
|
|
|
public Response SendDeptInfo(dynamic _) |
|
|
|
{ |
|
|
|
var result = new List<string>(); |
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(cookiestr) || ((DateTime.Now - cookiestrTime).Minutes > 10)) |
|
|
|
{ |
|
|
|
//调登录接口拿到cookie |
|
|
|
GetCookie(); |
|
|
|
} |
|
|
|
var deptList = cdDeptIBLL.GetAllList(); |
|
|
|
foreach (var item in deptList) |
|
|
|
{ |
|
|
|
var param = new |
|
|
|
{ |
|
|
|
ParentCode = "iccsid", |
|
|
|
pageSize = 10, |
|
|
|
Name = "部门5", |
|
|
|
Code = "5", |
|
|
|
}; |
|
|
|
|
|
|
|
var responseSend = SendRequestWithCookie("POST", "application/json;charset=utf-8", @"http://172.70.0.4:8088/fastgate/department", Encoding.UTF8, JsonConvert.SerializeObject(param)); |
|
|
|
var responseSendObj = JsonConvert.DeserializeObject<dynamic>(responseSend); |
|
|
|
|
|
|
|
result.Add("部门编号" + item.DeptNo + ",新增接口结果:" + responseSendObj.ErrCode + responseSendObj.ErrMsg); |
|
|
|
|
|
|
|
if (responseSendObj.ErrCode == 200) |
|
|
|
{ |
|
|
|
//第三方新增成功,修改表-发送标识为true |
|
|
|
//TODO: |
|
|
|
} |
|
|
|
else if (responseSendObj.ErrCode == 1010) //{ "ErrCode":1010,"ErrMsg":"登录状态已失效"} |
|
|
|
{ |
|
|
|
//获取cookie |
|
|
|
GetCookie(); |
|
|
|
} |
|
|
|
else if (responseSendObj.ErrCode == 10000) //10000 code repetition |
|
|
|
{ |
|
|
|
//调修改接口 |
|
|
|
var param2 = new |
|
|
|
{ |
|
|
|
Code = "5", |
|
|
|
Name = "部门bm5", |
|
|
|
pageSize = 10, |
|
|
|
}; |
|
|
|
var responseSend2 = SendRequestWithCookie("PUT", "application/json;charset=utf-8", @"http://172.70.0.4:8088/fastgate/department", Encoding.UTF8, JsonConvert.SerializeObject(param2)); |
|
|
|
var responseSendObj2 = JsonConvert.DeserializeObject<dynamic>(responseSend2); |
|
|
|
|
|
|
|
result.Add("部门编号" + item.DeptNo + ",修改接口结果:" + responseSendObj2.ErrCode + responseSendObj2.ErrMsg); |
|
|
|
|
|
|
|
if (responseSendObj2.ErrCode == 200) |
|
|
|
{ |
|
|
|
//第三方修改成功,修改表-发送标识为true |
|
|
|
//TODO: |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
//其他错误 |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
//其他错误 |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return Success(result); |
|
|
|
} |
|
|
|
/// <summary> |
|
|
|
/// 发送教师基础信息 |
|
|
|
/// <summary> |
|
|
|
/// <param name="_"></param> |
|
|
|
/// <returns></returns> |
|
|
|
public Response SendTeacherInfo(dynamic _) |
|
|
|
{ |
|
|
|
var result = new List<string>(); |
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(cookiestr) || ((DateTime.Now - cookiestrTime).Minutes > 10)) |
|
|
|
{ |
|
|
|
//调登录接口拿到cookie |
|
|
|
GetCookie(); |
|
|
|
} |
|
|
|
//var teacherList = empInfoIBLL.GetAllList(); |
|
|
|
//foreach (var item in teacherList) |
|
|
|
{ |
|
|
|
Dictionary<string, string> parameters = new Dictionary<string, string>(); //参数列表 |
|
|
|
parameters.Add("Sex", "1"); |
|
|
|
parameters.Add("Depart", "5"); |
|
|
|
parameters.Add("Name", "测试1"); |
|
|
|
parameters.Add("Code", "ceshi1"); |
|
|
|
var param = BuildQuery2(parameters, "----WebKitFormBoundaryAOT8GpQYYAP9jgGf"); |
|
|
|
var contentType = "multipart/form-data; boundary=----WebKitFormBoundaryAOT8GpQYYAP9jgGf"; |
|
|
|
|
|
|
|
var responseSend = SendRequestWithCookie("POST",contentType, @"http://172.70.0.4:8088/fastgate/person", Encoding.UTF8, param); |
|
|
|
var responseSendObj = JsonConvert.DeserializeObject<dynamic>(responseSend); |
|
|
|
|
|
|
|
result.Add("教师编号,新增接口结果:" + responseSendObj.ErrCode + responseSendObj.ErrMsg); |
|
|
|
|
|
|
|
if (responseSendObj.ErrCode == 200) |
|
|
|
{ |
|
|
|
//第三方新增成功,修改表-发送标识为true |
|
|
|
//TODO: |
|
|
|
} |
|
|
|
else if (responseSendObj.ErrCode == 1010) //{ "ErrCode":1010,"ErrMsg":"登录状态已失效"} |
|
|
|
{ |
|
|
|
//获取cookie |
|
|
|
GetCookie(); |
|
|
|
} |
|
|
|
else if (responseSendObj.ErrCode == 202) //202 code repeat |
|
|
|
{ |
|
|
|
//调查询接口 |
|
|
|
var responseSend3 = SendRequestWithCookie("GET", "application/json;charset=utf-8", @"http://172.70.0.4:8088/fastgate/personCode/ceshi1", Encoding.UTF8, null); |
|
|
|
var responseSendObj3 = JsonConvert.DeserializeObject<PersonInfo>(responseSend3); |
|
|
|
|
|
|
|
result.Add("教师编号,查询接口结果:" + responseSendObj3.ErrCode + responseSendObj3.ErrMsg + JsonConvert.SerializeObject(responseSendObj3.data)); |
|
|
|
|
|
|
|
if (responseSendObj3.ErrCode == 200) |
|
|
|
{ |
|
|
|
//第三方查询成功 |
|
|
|
//调修改接口 |
|
|
|
Dictionary<string, string> parameters2 = new Dictionary<string, string>(); //参数列表 |
|
|
|
parameters2.Add("Sex", "1"); |
|
|
|
parameters2.Add("Depart", "5"); |
|
|
|
parameters2.Add("Name", "测试ceshi1"); |
|
|
|
parameters2.Add("Code", "ceshi1"); |
|
|
|
parameters2.Add("Seqid", responseSendObj3.data.Seqid.ToString()); |
|
|
|
parameters2.Add("ImageList", string.Join(",", responseSendObj3.data.Pictures.Select(x => x.PersonPicturePath))); |
|
|
|
var param2 = BuildQuery2(parameters2, "----WebKitFormBoundaryMAj7ABMLt3aJpmGH"); |
|
|
|
var contentType2 = "multipart/form-data; boundary=----WebKitFormBoundaryMAj7ABMLt3aJpmGH"; |
|
|
|
|
|
|
|
var responseSend2 = SendRequestWithCookie("PUT", contentType2, @"http://172.70.0.4:8088/fastgate/person", Encoding.UTF8, param2); |
|
|
|
var responseSendObj2 = JsonConvert.DeserializeObject<dynamic>(responseSend2); |
|
|
|
|
|
|
|
result.Add("教师编号,修改接口结果:" + responseSendObj2.ErrCode + responseSendObj2.ErrMsg); |
|
|
|
|
|
|
|
if (responseSendObj2.ErrCode == 200) |
|
|
|
{ |
|
|
|
//第三方修改成功,修改表-发送标识为true |
|
|
|
//TODO: |
|
|
|
} |
|
|
|
else if (responseSendObj2.ErrCode == 1010) //{ "ErrCode":1010,"ErrMsg":"登录状态已失效"} |
|
|
|
{ |
|
|
|
//获取cookie |
|
|
|
GetCookie(); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
//其他错误 |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
else if (responseSendObj3.ErrCode == 1010) //{ "ErrCode":1010,"ErrMsg":"登录状态已失效"} |
|
|
|
{ |
|
|
|
//获取cookie |
|
|
|
GetCookie(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
//其他错误 |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return Success(result); |
|
|
|
} |
|
|
|
|
|
|
|
#endregion |
|
|
|
|
|
|
|
#region 公共方法 |
|
|
|
/// <summary> |
|
|
|
/// 请求获取cookie: |
|
|
|
/// </summary> |
|
|
|
/// <param name="type">GET,POST</param> |
|
|
|
/// <param name="url"></param> |
|
|
|
/// <param name="encoding">UTF-8</param> |
|
|
|
/// <param name="data">传送需要的参数</param> |
|
|
|
/// <returns></returns> |
|
|
|
public static string SendRequest(string type, string url, Encoding encoding, string data) |
|
|
|
{ |
|
|
|
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); |
|
|
|
webRequest.Method = type; |
|
|
|
webRequest.ContentType = "application/json"; |
|
|
|
if (type == "POST") |
|
|
|
{ |
|
|
|
byte[] postdata = Encoding.UTF8.GetBytes(data); |
|
|
|
webRequest.ContentLength = postdata.Length; |
|
|
|
Stream newStream = webRequest.GetRequestStream(); |
|
|
|
newStream.Write(postdata, 0, postdata.Length); |
|
|
|
newStream.Close(); |
|
|
|
} |
|
|
|
|
|
|
|
//使用HttpWebRequest的CookieContainer属性进行设置 |
|
|
|
CookieContainer ckCtnr = new CookieContainer(); |
|
|
|
if (cookie != null) |
|
|
|
{ |
|
|
|
ckCtnr.Add(cookie); //此处的cookie是HttpWebResponse对象的Cookies属性 |
|
|
|
} |
|
|
|
//获取应答网页 |
|
|
|
webRequest.CookieContainer = ckCtnr; |
|
|
|
|
|
|
|
//webRequest.Headers.Add("Cookie", "JSESSIONID=518211FE9243E875A3C570D510E7262F; Path=/fastgate; HttpOnly"); |
|
|
|
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); |
|
|
|
|
|
|
|
cookie = webResponse.Cookies;//获取Cookie |
|
|
|
cookiestr = webResponse.Headers.Get("Set-Cookie");//获取Cookie字符串 |
|
|
|
if (!string.IsNullOrEmpty(cookiestr)) |
|
|
|
{ |
|
|
|
cookiestrTime = DateTime.Now;//获取Cookie的时间 |
|
|
|
} |
|
|
|
|
|
|
|
StreamReader sr = new StreamReader(webResponse.GetResponseStream(), encoding); |
|
|
|
string str = sr.ReadToEnd(); |
|
|
|
sr.Close(); |
|
|
|
webResponse.Close(); |
|
|
|
return str; |
|
|
|
} |
|
|
|
/// <summary> |
|
|
|
/// 请求携带cookie: |
|
|
|
/// </summary> |
|
|
|
/// <param name="type">GET,POST,PUT</param> |
|
|
|
/// <param name="contentType">请求头类型(json是application/json;charset=utf-8,form-data是multipart/form-data; boundary=----WebKitFormBoundaryAOT8GpQYYAP9jgGf,)</param> |
|
|
|
/// <param name="url"></param> |
|
|
|
/// <param name="encoding">UTF-8</param> |
|
|
|
/// <param name="data">传送需要的参数</param> |
|
|
|
/// <returns></returns> |
|
|
|
public static string SendRequestWithCookie(string type, string contentType, string url, Encoding encoding, string data) |
|
|
|
{ |
|
|
|
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); |
|
|
|
webRequest.Method = type; |
|
|
|
webRequest.ContentType = contentType; |
|
|
|
webRequest.Headers.Add("Cookie", cookiestr);//携带Cookie |
|
|
|
if (type == "POST" || type == "PUT") |
|
|
|
{ |
|
|
|
byte[] postdata = Encoding.UTF8.GetBytes(data); |
|
|
|
webRequest.ContentLength = postdata.Length; |
|
|
|
Stream newStream = webRequest.GetRequestStream(); |
|
|
|
newStream.Write(postdata, 0, postdata.Length); |
|
|
|
newStream.Close(); |
|
|
|
} |
|
|
|
|
|
|
|
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); |
|
|
|
StreamReader sr = new StreamReader(webResponse.GetResponseStream(), encoding); |
|
|
|
string str = sr.ReadToEnd(); |
|
|
|
sr.Close(); |
|
|
|
webResponse.Close(); |
|
|
|
return str; |
|
|
|
} |
|
|
|
/// <summary> |
|
|
|
/// 组装请求参数 |
|
|
|
/// </summary> |
|
|
|
/// <param name="parameters"></param> |
|
|
|
/// <param name="encode"></param> |
|
|
|
/// <returns></returns> |
|
|
|
private static string BuildQuery(IDictionary<string, string> parameters, string encode) |
|
|
|
{ |
|
|
|
StringBuilder postData = new StringBuilder(); |
|
|
|
bool hasParam = false; |
|
|
|
IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator(); |
|
|
|
while (dem.MoveNext()) |
|
|
|
{ |
|
|
|
string name = dem.Current.Key; |
|
|
|
string value = dem.Current.Value; |
|
|
|
// 忽略参数名或参数值为空的参数 |
|
|
|
if (!string.IsNullOrEmpty(name)) |
|
|
|
{ |
|
|
|
if (hasParam) |
|
|
|
{ |
|
|
|
postData.Append("&"); |
|
|
|
} |
|
|
|
postData.Append(name); |
|
|
|
postData.Append("="); |
|
|
|
if (encode == "gb2312") |
|
|
|
{ |
|
|
|
postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding("gb2312"))); |
|
|
|
} |
|
|
|
else if (encode == "utf8") |
|
|
|
{ |
|
|
|
postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8)); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
postData.Append(value); |
|
|
|
} |
|
|
|
hasParam = true; |
|
|
|
} |
|
|
|
} |
|
|
|
return postData.ToString(); |
|
|
|
} |
|
|
|
/// <summary> |
|
|
|
/// 组装请求参数 |
|
|
|
/// </summary> |
|
|
|
/// <param name="parameters">key-value</param> |
|
|
|
/// <param name="boundary">分隔符</param> |
|
|
|
/// <returns></returns> |
|
|
|
private static string BuildQuery2(IDictionary<string, string> parameters, string boundary) |
|
|
|
{ |
|
|
|
StringBuilder buffer = new StringBuilder(); |
|
|
|
foreach (string key in parameters.Keys) |
|
|
|
{ |
|
|
|
buffer.Append("\r\n").Append("--").Append(boundary).Append("\r\n"); |
|
|
|
if (key == "file") |
|
|
|
{ |
|
|
|
buffer.Append("Content-Disposition: form-data; name=\"" + key + "\";filename=\"920_complete_1.jpg\"\r\n"); |
|
|
|
buffer.Append("Content-Type: image/jpeg"); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
buffer.Append("Content-Disposition: form-data; name=\"" + key + "\"\r\n\r\n"); |
|
|
|
buffer.Append(parameters[key]); |
|
|
|
} |
|
|
|
} |
|
|
|
buffer.Append("\r\n").Append("--").Append(boundary).Append("--"); |
|
|
|
return buffer.ToString(); |
|
|
|
} |
|
|
|
#endregion |
|
|
|
|
|
|
|
#region 私有类 |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// 表单实体类 |
|
|
|
/// <summary> |
|
|
|
private class ReqFormEntity |
|
|
|
{ |
|
|
|
public string keyValue { get; set; } |
|
|
|
public string strEntity { get; set; } |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// 人员详细信息 |
|
|
|
/// <summary> |
|
|
|
private class PersonInfo |
|
|
|
{ |
|
|
|
/// <summary> |
|
|
|
/// 错误码(200表示成功, |
|
|
|
/// </summary> |
|
|
|
public int ErrCode { get; set; } |
|
|
|
/// <summary> |
|
|
|
/// 错误信息,”success”为正常 |
|
|
|
/// </summary> |
|
|
|
public string ErrMsg { get; set; } |
|
|
|
public PersonInfoData data { get; set; } |
|
|
|
} |
|
|
|
private class PersonInfoData |
|
|
|
{ |
|
|
|
/// <summary> |
|
|
|
/// 人员存在数据库的唯一ID |
|
|
|
/// </summary> |
|
|
|
public int Seqid { get; set; } |
|
|
|
/// <summary> |
|
|
|
/// 头像存储的地址ur |
|
|
|
/// </summary> |
|
|
|
public List<PersonDataPictures> Pictures { get; set; } |
|
|
|
} |
|
|
|
private class PersonDataPictures |
|
|
|
{ |
|
|
|
/// <summary> |
|
|
|
/// 登记照片地址 |
|
|
|
/// </summary> |
|
|
|
public string PersonPicturePath { get; set; } |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#endregion |
|
|
|
|
|
|
|
} |
|
|
|
} |