You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/Myshipping.Application/Helper/WebDataHelper.cs

155 lines
6.5 KiB
C#

using Furion;
using Furion.Logging;
using Furion.RemoteRequest.Extensions;
using Myshipping.Core;
using Myshipping.Core.Service;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Myshipping.Application.Helper
{
/// <summary>
/// 获取指定网站运踪数据的辅助类
/// </summary>
public static class WebDataHelper
{
/// <summary>
/// 异步调取网站HTML数据
/// </summary>
/// <param name="userId">用户名ID</param>
/// <param name="tenantId">租户ID</param>
/// <param name="tenantName">租户名称</param>
/// <param name="mblno">提单号</param>
/// <param name="webCodeEnum">网站类型</param>
/// <param name="yardid">场站Id当<see cref="WebCodeEnum"/>为<see cref="WebCodeEnum.YARD"/>时,此参数必须</param>
/// <returns>返回网站HTML数据</returns>
public static async Task<KeyValuePair<bool, string>> GetWebData(long userId, long tenantId, string tenantName, string mblno, WebCodeEnum webCodeEnum, string yardid = null)
{
var cacheService = App.GetService<ISysCacheService>();
var webAccService = App.GetService<IDjyWebsiteAccountConfigService>();
var sysConfig = await cacheService.GetAllSysConfig();
var userKey = sysConfig.FirstOrDefault(x => x.Code == "spiderUserKeyBilltrace");
var userSecret = sysConfig.FirstOrDefault(x => x.Code == "spiderUserSecretBilltrace");
var spiderServerUrl = sysConfig.FirstOrDefault(x => x.Code == "spiderServerUrlBillTraceNew");
var webAcc = "";
var webPwd = "";
var newWebCode = "";
if (webCodeEnum == WebCodeEnum.YARD)
{
if (string.IsNullOrEmpty(yardid))
{
return new KeyValuePair<bool, string>(false, "查询场站的网页数据时需要传入场站Id");
}
var needYgtAccountList = cacheService.GetAllDictData().Result.Where(x => x.TypeCode == "YardListNeedYgtAccount").ToList();//需要云港通账号的场站
var needLhtAccountList = cacheService.GetAllDictData().Result.Where(x => x.TypeCode == "YardListNeedLhtAccount").ToList();//需要陆海通账号的场站
if (needYgtAccountList.Count(x => x.Code == yardid) > 0)
{
//从租户参数中获取网站云港通账号密码
var tenantParam = await cacheService.GetAllTenantParam();
var ygtAcc = await webAccService.GetAccountConfig("YunGangTong", userId);
if (ygtAcc == null || string.IsNullOrEmpty(ygtAcc.Account) || string.IsNullOrEmpty(ygtAcc.Password))
{
return new KeyValuePair<bool, string>(false, "未找到云港通账号、密码配置,请到账号维护模块中添加");
}
webAcc = ygtAcc.Account;
webPwd = ygtAcc.Password;
}
if (needLhtAccountList.Count(x => x.Code == yardid) > 0)
{
//从租户参数中获取网站云港通账号密码
var tenantParam = await cacheService.GetAllTenantParam();
var ygtAcc = await webAccService.GetAccountConfig("LuHaiTong", userId);
if (ygtAcc == null || string.IsNullOrEmpty(ygtAcc.Account) || string.IsNullOrEmpty(ygtAcc.Password))
{
return new KeyValuePair<bool, string>(false, "未找到陆海通账号、密码配置,请到账号维护模块中添加");
}
webAcc = ygtAcc.Account;
webPwd = ygtAcc.Password;
}
var yardMappings = await cacheService.GetAllMappingYard();
var ym = yardMappings.FirstOrDefault(x => x.Code.ToLower() == yardid.ToLower() && x.Module == "BillTrace");
if (ym == null)
{
return new KeyValuePair<bool, string>(false, $"场站代号配置未找到:{yardid}");
}
12 months ago
newWebCode = ym.MapCode;
}
else
{
newWebCode = webCodeEnum.ToString();
}
var objSend = new
{
user_key = userKey.Value,
user_secret = userSecret.Value,
customer_id = tenantId.ToString(),
customer_name = tenantName,
web_code = newWebCode,
bno = mblno,
req_type = "1",
web_user = webAcc,
web_psw = webPwd,
};
var urlYard = spiderServerUrl.Value;
if (!urlYard.EndsWith("/"))
{
urlYard += "/";
}
urlYard += "real/query";
var strJson = objSend.ToJsonString();
Log.Information($"查询实时网站HTML数据发送数据url{urlYard}json{strJson}");
string rtn = await urlYard.SetBody(objSend).PostAsStringAsync();
Log.Information($"查询实时网站HTML数据返回:{rtn}");
var rtnObj = JObject.Parse(rtn);
if (rtnObj.GetIntValue("code") != 200)
{
return new KeyValuePair<bool, string>(false, $"获取网站HTML数据失败{rtnObj.GetStringValue("msg")}");
}
else
{
var rtnData = rtnObj.GetStringValue("data");
var viewstatehtml = GetDataHtmlList(rtnData, "__VIEWSTATE", "/>");
if (viewstatehtml != "")
{
rtnData = rtnData.Replace(viewstatehtml, "");
}
return new KeyValuePair<bool, string>(true, rtnData);
}
}
private static string GetDataHtmlList(string html, string startstr, string endstr)
{
var subhtml = html;
var htmllength = subhtml.Length;
var startindex = subhtml.IndexOf(startstr);
//if (startindex == -1 || startindex == 0) return "";
if (startindex == -1) return "";
subhtml = subhtml.Substring(startindex + startstr.Length, htmllength - startindex - startstr.Length);
var endindex = subhtml.IndexOf(endstr);
if (endindex != -1 && endindex != 0)
subhtml = subhtml.Substring(0, endindex);
return subhtml;
}
}
}