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/YardDataHelper.cs

120 lines
4.6 KiB
C#

using Furion;
using Furion.Logging;
using Furion.RemoteRequest.Extensions;
using Myshipping.Core;
using Myshipping.Core.Service;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application.Helper
{
/// <summary>
/// 获取场站数据辅助类(调用爬虫新运踪接口)
/// </summary>
public static class YardDataHelper
{
public static async Task<KeyValuePair<bool, string>> GetYardData(long tenantId, string tenantName, string mblno, string yardid, bool isWeb = true)
{
var cacheService = App.GetService<ISysCacheService>();
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");
if (yardid == "YIHANG" || yardid == "YZH") yardid = "GLJ";
var qduname = "";
var qdpwd = "";
if (yardid == "YGT" || yardid == "CHANGRONG" || yardid == "GLR" || yardid == "JZLG")
{
//从租户参数中获取网站青岛港账号密码
var tenantParam = await cacheService.GetAllTenantParam();
var tenantUserName = tenantParam.FirstOrDefault(x => x.TenantId == tenantId && x.ParaCode == "QDPORT_USERNAME");
var tenantPwd = tenantParam.FirstOrDefault(x => x.TenantId == tenantId && x.ParaCode == "QDPORT_PWD");
if (tenantUserName == null || tenantPwd == null)
{
return new KeyValuePair<bool, string>(false, "未找到青岛港用户名、密码配置");
}
qduname = tenantUserName.ItemCode;
qdpwd = tenantPwd.ItemCode;
}
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}");
}
var objSend = new
{
user_key = userKey.Value,
user_secret = userSecret.Value,
customer_id = tenantId.ToString(),
customer_name = tenantName,
web_code = ym.MapCode,
bno = mblno,
req_type = isWeb ? "1" : "0",
web_user = qduname,
web_psw = qdpwd,
};
var urlYard = spiderServerUrl.Value;
if (!urlYard.EndsWith("/"))
{
urlYard += "/";
}
2 years ago
urlYard += "real/query";
2 years ago
var strJson = objSend.ToJsonString();
Log.Information($"查询实时场站数据发送数据url{urlYard}json{strJson}");
string rtn = await urlYard.SetBody(objSend).PostAsStringAsync();
2 years ago
Log.Information($"查询实时场站数据,返回:{rtn}");
var rtnObj = JObject.Parse(rtn);
if (rtnObj.GetIntValue("code") != 200)
{
return new KeyValuePair<bool, string>(false, $"获取场站数失败:{rtnObj.GetStringValue("msg")}");
}
else
{
var rtnData = rtnObj.GetStringValue("data");
if (isWeb)
{
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;
}
}
}