Files
jinko.yikatong/Src/Dlhis.ScheduledTask/WsProxy/ZHR_OA_O_WORK_CARD_INFO.cs
2024-01-04 14:40:04 +08:00

140 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using Newtonsoft.Json;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
using Hangfire.Logging;
namespace Dlhis.ScheduledTask.WsProxy
{
/// <summary>
/// 获取图片
/// </summary>
public class ZHR_OA_O_WORK_CARD_INFO
{
private static readonly log4net.ILog log = LogManager.GetLogger(typeof(ZHR_OA_O_WORK_CARD_INFO));
/// <summary>
/// 获取员工卡片信息
/// </summary>
public List<string> Get_ZHR_OA_O_WORK_CARD_INFO(string url, string userName, string password, List<string> workcode)
{
List<string> list = new List<string>();
try
{
if (workcode == null || workcode.Count == 0) return list;
List<Dictionary<string, string>> workcodes = new List<Dictionary<string, string>>();
foreach (var item in workcode)
{
var dictitem = new Dictionary<string, string>
{
{ "WORKCODE", item }
};
workcodes.Add(dictitem);
}
Dictionary<string, object> dictitems = new Dictionary<string, object>
{
{ "item", workcodes }
};
Dictionary<string, object> IT_PERSON = new Dictionary<string, object>
{
{ "IT_PERSON", dictitems }
};
string postData = JsonConvert.SerializeObject(IT_PERSON);
var auth = EncodeBase64($"{userName}:{password}");
string readTxt = "";
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Headers.Add("Authorization", $"Basic {auth}");
request.Timeout = 30 * 1000;
request.ContentType = "application/json";
request.Method = "POST";
byte[] data = Encoding.UTF8.GetBytes(postData);
request.ContentLength = data.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(data, 0, data.Length);
postStream.Close();
using (var res = request.GetResponse() as HttpWebResponse)
{
if (res.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
readTxt = reader.ReadToEnd();
}
}
if (!string.IsNullOrEmpty(readTxt))
{
var resutJson = JsonConvert.DeserializeObject<JObject>(readTxt);
if (resutJson.HasValues)
{
var resultItem = resutJson["ET_DATA"]?["item"];
if (resultItem.HasValues && resultItem.Count() > 0)
{
foreach (var _item in resultItem)
{
var _photo = _item["PHOTO_STR"]?.ToString();
if (string.IsNullOrEmpty(_photo))
{
list.Add("");
}
else
{
list.Add(_photo);
}
}
}
}
}
return list;
}
catch (Exception ex)
{
log.Error(ex.ToString());
throw new Exception($"请求{url}失败", ex);
}
}
/// <summary>
/// Base64编码
/// </summary>
/// <param name="code"></param>
/// <param name="code_type">默认UTF8</param>
/// <returns></returns>
public static string EncodeBase64(string code, string code_type = "")
{
string encode = "";
byte[] bytes = null;
if (string.IsNullOrEmpty(code_type))
{
bytes = System.Text.Encoding.UTF8.GetBytes(code);
}
else
{
bytes = Encoding.GetEncoding(code_type).GetBytes(code);
}
try
{
encode = Convert.ToBase64String(bytes);
}
catch
{
encode = code;
}
return encode;
}
}
}