using Lskj.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
namespace Lskj.Util
{
public static class UploadFileUtil
{
///
/// 登录获取token
///
///
///
public static string Login(string oaUrl)
{
string token = "";
try
{
string loginUrl = $"{oaUrl}/Api/SysUserAjaxApi.ashx";
HttpHelper.Setting("application/x-www-form-urlencoded", null, null, HttpHelper.Encode.UTF8);
Dictionary loginPmsDic = new Dictionary();
loginPmsDic.Add("method", "Login");
loginPmsDic.Add("username", HttpUtility.UrlEncode(ERPInfo.Instance.UserName));
loginPmsDic.Add("password", HttpUtility.UrlEncode(ERPInfo.Instance.InPassWord));
HttpWebResponse loginResponse = HttpHelper.Post(loginUrl, "", loginPmsDic, null, out string loginResult);
if (loginResponse != null && !string.IsNullOrEmpty(loginResult))
{
JObject jObject = JsonConvert.DeserializeObject(loginResult);
if (jObject.ContainsKey("success"))
{
if ((jObject["success"] + "").Equals("True"))
{
if (jObject.ContainsKey("token"))
{
token = jObject["token"] + "";
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return token;
}
///
/// 上传文件
///
///
///
///
///
///
///
public static bool UploadFile(string token, string url, string fileStream, out string fileId, out string returnMsg)
{
bool isUpload = false;
fileId = "-1";
returnMsg = "";
if (!string.IsNullOrEmpty(token))
{
HttpHelper.Setting("application/x-www-form-urlencoded", null, null);
string result = "";
Dictionary headerDic = new Dictionary();
headerDic.Add("Authorization", $"Bearer {token}");
HttpWebResponse webResponse = HttpHelper.Post(url, fileStream, null, headerDic, out result);
if (webResponse != null && !string.IsNullOrEmpty(result))
{
JObject jsonObject = (JObject)JsonConvert.DeserializeObject(result);
string isSuccess = jsonObject["success"] + "";
if (isSuccess.Equals("True", StringComparison.CurrentCultureIgnoreCase))
{
if (jsonObject.ContainsKey("other"))
{
fileId = jsonObject["other"] + "";
}
isUpload = true;
}
else
{
if (jsonObject.ContainsKey("msg"))
{
returnMsg = jsonObject["msg"] + "";
}
isUpload = false;
}
}
else
{
returnMsg = "上传请求失败";
isUpload = false;
}
}
else
{
returnMsg = "登录失败";
isUpload = false;
}
return isUpload;
}
///
/// 上传文件
///
///
///
///
///
public static bool DeleteFile(string token, string url, out string returnMsg)
{
bool isDelete = false;
returnMsg = "";
if (!string.IsNullOrEmpty(token))
{
HttpHelper.Setting("application/x-www-form-urlencoded", null, null);
Dictionary headerDic = new Dictionary();
headerDic.Add("Authorization", $"Bearer {token}");
HttpWebResponse webResponse = HttpHelper.Post(url, "", null, headerDic, out string result);
if (webResponse != null && !string.IsNullOrEmpty(result))
{
JObject jsonObject = (JObject)JsonConvert.DeserializeObject(result);
string isSuccess = jsonObject["success"] + "";
if (isSuccess.Equals("True", StringComparison.CurrentCultureIgnoreCase))
{
isDelete = true;
}
else
{
if (jsonObject.ContainsKey("msg"))
{
returnMsg = jsonObject["msg"] + "";
}
isDelete = false;
}
}
else
{
returnMsg = "删除请求失败";
isDelete = false;
}
}
else
{
returnMsg