using MAX.Core.Commons; using MAX.Core.Cryptography; using MAX.Core.DependencyInjection; using MAX.Core.Helpers; using MAX.Core.Logger; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; using Store.VirtualLogistics.Models; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; namespace Store.VirtualLogistics.Apis; public class VirtualLogisticsApi : ISingletonDependency { private const string Host = "https://yuncang.df8899.top"; private readonly ILoggerFacade _logger; protected string UserName = "18756025201"; protected string Passworkd = "aj123456"; public VirtualLogisticsApi(ILoggerFacade logger) { _logger = logger; } public string GetSign(long unix) { string pwd = MD5Helper.Md5_Lower32(MD5Helper.Md5_Lower32(Passworkd)); string s = $"{UserName}{pwd}{unix}"; string sign = MD5Helper.Md5_Lower32(s); return sign; } public List GetExpressList() { var ret = ServerRequest>(new HttpItem { URL = "/api/lipin/list", Method = MethodType.Post }); return ret.Data; } public UserMoney GetUserMoney() { var ret = ServerRequest(new HttpItem { URL = "/api/user/index", Method = MethodType.Post }); return ret.Data; } /// /// 获取仓库列表及价格 /// public List GetCangList() { var ret = ServerRequest>(new HttpItem { URL = "/api/lipin/cang", Method = MethodType.Post }); return ret.Data; } /// /// 获取礼品列表及价格 /// /// 快递ID,可选 public List GetGoodsList(string kuaidiId = null) { var ret = ServerRequest>(new HttpItem { URL = "/api/lipin/goods", Method = MethodType.Post }); return ret.Data; } /// /// 购买快递 /// public Result> Buy(BuyRequest request) { var res = ServerRequest>(new HttpItem { URL = $"/api/lipin/buy", Postdata = JsonConvert.SerializeObject(request), Method = MethodType.Post, ContentType = "application/json", }); var ret = new Result>() { Code = res.Code, Success = res.Success, Msg = res.Msg, }; if (res.Success) { ret.Data = res.Data.Values.ToList(); } return ret; } /// /// 查询快递订单信息及状态 /// /// 查询参数 /// public Result> QueryOrderStatus(string[] orderIds) { var res = ServerRequest>(new HttpItem { URL = "/api/lipin/status", Postdata = JsonConvert.SerializeObject(new { order_sn = orderIds }), Method = MethodType.Post }); var ret = new Result>() { Code = res.Code, Success = res.Success, Msg = res.Msg, }; if (res.Success) { ret.Data = res.Data.Values.ToList(); } return ret; } /// /// 上传取件码 /// public Result> UploadPickup(List data) { return ServerRequest>(new HttpItem { URL = "/api/lipin/uploadPickup", Postdata = JsonConvert.SerializeObject(new { data = data }), Method = MethodType.Post }); } /// /// 查询快递订单信息及状态 /// public Result> GetOrderStatus(string[] orders) { var res = ServerRequest>(new HttpItem { URL = "/api/lipin/status", Postdata = JsonConvert.SerializeObject(new { show_delivery = 1, order_sn = orders }), Method = MethodType.Post }); var ret = new Result>() { Code = res.Code, Success = res.Success, Msg = res.Msg, }; if (res.Success) { ret.Data = res.Data.Values.ToList(); } return ret; } /// /// 服务器请求 /// /// /// public Result ServerRequest(HttpItem item) { try { if (!item.URL.StartsWith("http")) { item.URL = Host + item.URL; } item.Encoding = Encoding.UTF8; //item.ProxyIp = "127.0.0.1:8080"; long unix = DateHelper.GetCurrentUnixTimestampInSeconds(); string sign = GetSign(unix); HttpHelper http = new HttpHelper(); item.ContentType = "application/json"; var auth = new { username = UserName, sid = unix, sign = sign, }; if (string.IsNullOrWhiteSpace(item.Postdata)) { item.Postdata = JsonConvert.SerializeObject(auth); } else { var obj = JObject.Parse(item.Postdata); obj["username"] = auth.username; obj["sid"] = auth.sid; obj["sign"] = auth.sign; item.Postdata = obj.ToString(); } if (item.ContentType != "application/json") { if (!string.IsNullOrEmpty(item.Postdata)) { if (item.Postdata.StartsWith("&") == false) item.Postdata = "&" + item.Postdata; } item.Postdata = $"username={UserName}&sid={unix}&sign={sign}" + item.Postdata; } item.PostDataType = PostDataType.String; item.PostEncoding = Encoding.UTF8; HttpResult result = http.GetHtml(item); if (result.StatusCode == HttpStatusCode.OK) { var settings = new JsonSerializerSettings { ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() } }; var res = JsonConvert.DeserializeObject>(result.Html, settings); if (res.Code == 0) { return new Result { Success = true, Code = res.Code, Msg = res.Msg, Data = res.Data.ToObject(JsonSerializer.CreateDefault(settings)) }; } else { _logger.Error($"API错误:{result.Html}"); throw new Exception($"{res.Msg}"); } } throw new Exception(result.Html); } catch (Exception ex) { //_logger.Error(ex, $"VirtualLogisticsApi:{item.URL}\n{item.Postdata}"); return new Result { Code = -1, Msg = ex.Message, }; } } }