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.

200 lines
7.9 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
using DS.Module.UserModule;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using NLog;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DS.Module.PrintModule
{
public class PrintService: IPrintService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly string ip;
private readonly int port;
private readonly string accessKey;
private readonly string accessSecret;
private readonly string moduleUrl;
private readonly string templateUrl;
private readonly string jsonPrintInfoUrl;
private readonly string jsonPrintStreamUrl;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
/// <summary>
/// 构造函数
/// </summary>
/// <param name="serviceProvider"></param>
public PrintService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
ip = AppSetting.app(new string[] { "PrintService", "IP" }).ObjToString();
port = AppSetting.app(new string[] { "PrintService", "Port" }).ToInt();
accessKey = AppSetting.app(new string[] { "PrintService", "AccessKey" }).ObjToString();
accessSecret = AppSetting.app(new string[] { "PrintService", "AccessSecret" }).ObjToString();
moduleUrl = AppSetting.app(new string[] { "PrintService", "GetModuleListUrl" }).ObjToString();
templateUrl = AppSetting.app(new string[] { "PrintService", "GetTemplateListUrl" }).ObjToString();
jsonPrintInfoUrl = AppSetting.app(new string[] { "PrintService", "GetJsonPrintInfoUrl" }).ObjToString();
jsonPrintStreamUrl = AppSetting.app(new string[] { "PrintService", "GetJsonPrintStreamUrl" }).ObjToString();
}
/// <summary>
/// 获取打印模块列表
/// </summary>
/// <returns></returns>
public DataResult GetOpenPrintModuleList()
{
// 只要平台信息参数一致,多个请求只需设置一次参数
HttpUtillib.SetPlatformInfo(accessKey, accessSecret, ip, port, false);
DataResult res = new DataResult();
// 发起POST请求超时时间15秒返回响应字节数组
string result = HttpUtillib.HttpGet(moduleUrl, 15);
if (null == result)
{
res = DataResult.Failed("请求失败,请联系管理员");
// 请求失败
// Console.WriteLine("/artemis/api/resource/v1/cameras/indexCode: POST fail");
}
else
{
res = JsonConvert.DeserializeObject<DataResult>(result);
// Console.WriteLine(System.Text.Encoding.UTF8.GetString(result));
}
return res;
}
/// <summary>
/// 获取打印模板列表
/// </summary>
/// <param name="id">模块id</param>
/// <returns></returns>
public DataResult GetOpenPrintTemplateList(string id)
{
// 只要平台信息参数一致,多个请求只需设置一次参数
HttpUtillib.SetPlatformInfo(accessKey, accessSecret, ip, port, false);
DataResult res = new DataResult();
// 发起POST请求超时时间15秒返回响应字节数组
string result = HttpUtillib.HttpGet(templateUrl+"?id=" + id, 15);
if (null == result)
{
res = DataResult.Failed("请求失败,请联系管理员");
// 请求失败
// Console.WriteLine("/artemis/api/resource/v1/cameras/indexCode: POST fail");
}
else
{
res = JsonConvert.DeserializeObject<DataResult>(result);
// Console.WriteLine(System.Text.Encoding.UTF8.GetString(result));
}
return res;
}
/// <summary>
/// 获取打印模板列表
/// </summary>
/// <param name="id">模块id</param>
/// <returns></returns>
public async Task<DataResult> GetOpenPrintTemplateListAsync(string id)
{
// 只要平台信息参数一致,多个请求只需设置一次参数
HttpUtillib.SetPlatformInfo(accessKey, accessSecret, ip, port, false);
DataResult res = new DataResult();
var url = templateUrl + "?id=" + id;
// 发起POST请求超时时间15秒返回响应字节数组
var result = await HttpUtillib.HttpGetAsync(url,15);
if (null == result)
{
res = DataResult.Failed("请求失败,请联系管理员");
// 请求失败
// Console.WriteLine("/artemis/api/resource/v1/cameras/indexCode: POST fail");
}
else
{
res = JsonConvert.DeserializeObject<DataResult>(result);
// Console.WriteLine(System.Text.Encoding.UTF8.GetString(result));
}
return res;
}
/// <summary>
/// 获取Json打印信息
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public DataResult GetOpenJsonPrintInfo(OpenJsonPrintReq req)
{
// 只要平台信息参数一致,多个请求只需设置一次参数
HttpUtillib.SetPlatformInfo(accessKey, accessSecret, ip, port, false);
DataResult res = new DataResult();
// 发起POST请求超时时间15秒返回响应字节数组
string result = HttpUtillib.HttpPost(jsonPrintInfoUrl, JsonConvert.SerializeObject(req), 30);
if (null == result)
{
res = DataResult.Failed("请求失败,请联系管理员");
// 请求失败
// Console.WriteLine("/artemis/api/resource/v1/cameras/indexCode: POST fail");
}
else
{
res = JsonConvert.DeserializeObject<DataResult>(result);
// Console.WriteLine(System.Text.Encoding.UTF8.GetString(result));
}
return res;
}
/// <summary>
/// 获取Json打印信息
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public DataResult<PrintStreamRes> GetOpenJsonPrintStream(OpenJsonPrintReq req)
{
// 只要平台信息参数一致,多个请求只需设置一次参数
HttpUtillib.SetPlatformInfo(accessKey, accessSecret, ip, port, false);
//DataResult res = new DataResult();
// 发起POST请求超时时间15秒返回响应字节数组
string result = HttpUtillib.HttpPost(jsonPrintStreamUrl, JsonConvert.SerializeObject(req), 30);
if (null == result)
{
throw new Exception("请求失败,请联系管理员");
// 请求失败
// Console.WriteLine("/artemis/api/resource/v1/cameras/indexCode: POST fail");
}
else
{
var res = JsonConvert.DeserializeObject<DataResult>(result);
if (!res.Succeeded)
{
return DataResult<PrintStreamRes>.Failed(res.Message);
}
else
{
var printRes = JsonConvert.DeserializeObject<PrintStreamRes>(res.Data.ToString());
return DataResult<PrintStreamRes>.Success(printRes);
}
}
//return res;
}
}
}