271 lines
6.1 KiB
C#
271 lines
6.1 KiB
C#
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<ExpressItem> GetExpressList()
|
|
{
|
|
var ret = ServerRequest<List<ExpressItem>>(new HttpItem
|
|
{
|
|
URL = "/api/lipin/list",
|
|
Method = MethodType.Post
|
|
});
|
|
return ret.Data;
|
|
}
|
|
public UserMoney GetUserMoney()
|
|
{
|
|
var ret = ServerRequest<UserMoney>(new HttpItem
|
|
{
|
|
URL = "/api/user/index",
|
|
Method = MethodType.Post
|
|
});
|
|
return ret.Data;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取仓库列表及价格
|
|
/// </summary>
|
|
public List<CangItem> GetCangList()
|
|
{
|
|
var ret = ServerRequest<List<CangItem>>(new HttpItem
|
|
{
|
|
URL = "/api/lipin/cang",
|
|
Method = MethodType.Post
|
|
});
|
|
return ret.Data;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取礼品列表及价格
|
|
/// </summary>
|
|
/// <param name="kuaidiId">快递ID,可选</param>
|
|
public List<GoodsItem> GetGoodsList(string kuaidiId = null)
|
|
{
|
|
var ret = ServerRequest<List<GoodsItem>>(new HttpItem
|
|
{
|
|
URL = "/api/lipin/goods",
|
|
Method = MethodType.Post
|
|
});
|
|
return ret.Data;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 购买快递
|
|
/// </summary>
|
|
public Result<List<BuyResultItem>> Buy(BuyRequest request)
|
|
{
|
|
var res = ServerRequest<Dictionary<string, BuyResultItem>>(new HttpItem
|
|
{
|
|
URL = $"/api/lipin/buy",
|
|
Postdata = JsonConvert.SerializeObject(request),
|
|
Method = MethodType.Post,
|
|
ContentType = "application/json",
|
|
});
|
|
var ret = new Result<List<BuyResultItem>>()
|
|
{
|
|
Code = res.Code,
|
|
Success = res.Success,
|
|
Msg = res.Msg,
|
|
};
|
|
if (res.Success)
|
|
{
|
|
ret.Data = res.Data.Values.ToList();
|
|
}
|
|
return ret;
|
|
}
|
|
/// <summary>
|
|
/// 查询快递订单信息及状态
|
|
/// </summary>
|
|
/// <param name="query">查询参数</param>
|
|
/// <returns></returns>
|
|
public Result<List<OrderStatusResultItem>> QueryOrderStatus(string[] orderIds)
|
|
{
|
|
var res = ServerRequest<Dictionary<string, OrderStatusResultItem>>(new HttpItem
|
|
{
|
|
URL = "/api/lipin/status",
|
|
Postdata = JsonConvert.SerializeObject(new
|
|
{
|
|
order_sn = orderIds
|
|
}),
|
|
Method = MethodType.Post
|
|
});
|
|
var ret = new Result<List<OrderStatusResultItem>>()
|
|
{
|
|
Code = res.Code,
|
|
Success = res.Success,
|
|
Msg = res.Msg,
|
|
};
|
|
if (res.Success)
|
|
{
|
|
ret.Data = res.Data.Values.ToList();
|
|
}
|
|
return ret;
|
|
}
|
|
/// <summary>
|
|
/// 上传取件码
|
|
/// </summary>
|
|
public Result<List<PickupResultItem>> UploadPickup(List<PickupItem> data)
|
|
{
|
|
return ServerRequest<List<PickupResultItem>>(new HttpItem
|
|
{
|
|
URL = "/api/lipin/uploadPickup",
|
|
Postdata = JsonConvert.SerializeObject(new
|
|
{
|
|
data = data
|
|
}),
|
|
Method = MethodType.Post
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询快递订单信息及状态
|
|
/// </summary>
|
|
public Result<List<OrderStatusResultItem>> GetOrderStatus(string[] orders)
|
|
{
|
|
var res = ServerRequest<Dictionary<string, OrderStatusResultItem>>(new HttpItem
|
|
{
|
|
URL = "/api/lipin/status",
|
|
Postdata = JsonConvert.SerializeObject(new
|
|
{
|
|
show_delivery = 1,
|
|
order_sn = orders
|
|
}),
|
|
Method = MethodType.Post
|
|
});
|
|
|
|
var ret = new Result<List<OrderStatusResultItem>>()
|
|
{
|
|
Code = res.Code,
|
|
Success = res.Success,
|
|
Msg = res.Msg,
|
|
};
|
|
if (res.Success)
|
|
{
|
|
ret.Data = res.Data.Values.ToList();
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 服务器请求
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <returns></returns>
|
|
public Result<T> ServerRequest<T>(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<JToken>>(result.Html, settings);
|
|
if (res.Code == 0)
|
|
{
|
|
return new Result<T>
|
|
{
|
|
Success = true,
|
|
Code = res.Code,
|
|
Msg = res.Msg,
|
|
Data = res.Data.ToObject<T>(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<T>
|
|
{
|
|
Code = -1,
|
|
Msg = ex.Message,
|
|
};
|
|
}
|
|
}
|
|
}
|