79 lines
2.4 KiB
C#
79 lines
2.4 KiB
C#
using System;
|
||
using System.Net.Http;
|
||
using System.Threading.Tasks;
|
||
using Hncore.Infrastructure.Common;
|
||
using Hncore.Infrastructure.Extension;
|
||
using Hncore.Infrastructure.Serializer;
|
||
using Hncore.Infrastructure.WebApi;
|
||
using MsgCenterClient.WechatMpTplMsg;
|
||
|
||
namespace MsgCenterClient
|
||
{
|
||
public class MsgCenterHttpClient
|
||
{
|
||
private IHttpClientFactory _httpClientFactory;
|
||
|
||
internal static string _BaseUrl = "";
|
||
|
||
public string BaseUrl => _BaseUrl;
|
||
|
||
public MsgCenterHttpClient(IHttpClientFactory httpClientFactory)
|
||
{
|
||
_httpClientFactory = httpClientFactory;
|
||
}
|
||
|
||
private HttpClient CreateHttpClient()
|
||
{
|
||
return _httpClientFactory.CreateClient();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 发送微信公众号模板消息
|
||
/// </summary>
|
||
/// <param name="msgBase"></param>
|
||
/// <returns></returns>
|
||
public async Task<ApiResult> SendWechatMpTplMsg(MsgBase msgBase)
|
||
{
|
||
var body = msgBase.ToRequestObject();
|
||
|
||
try
|
||
{
|
||
var res = await CreateHttpClient()
|
||
.PostAsJsonGetString(BaseUrl + "/api/msgcenter/v1/msg/SendMPTplMessage", body);
|
||
|
||
return res.FromJsonTo<ApiResult>();
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.Error("发送微信公众号模板消息失败", e + "\n消息内容:\n" + body.ToJson(true));
|
||
|
||
return new ApiResult(ResultCode.C_UNKNOWN_ERROR);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 发送微信小程序模板消息
|
||
/// </summary>
|
||
/// <param name="msgBase"></param>
|
||
/// <returns></returns>
|
||
public async Task<ApiResult> SendMiniAppTplMsg(MiniAppMsgBase msgBase)
|
||
{
|
||
var body = msgBase.ToRequestObject();
|
||
|
||
try
|
||
{
|
||
var res = await CreateHttpClient()
|
||
.PostAsJsonGetString(BaseUrl + "/api/msgcenter/v1/msg/SendMiniAppTplMessage", body);
|
||
|
||
return res.FromJsonTo<ApiResult>();
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.Error("发送小程序模板消息失败", e + "\n消息内容:\n" + body.ToJson(true));
|
||
|
||
return new ApiResult(ResultCode.C_UNKNOWN_ERROR);
|
||
}
|
||
}
|
||
|
||
}
|
||
} |