feat: 完成微信小程序消息推送
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
using Yi.Framework.WeChat.MiniProgram.Abstract;
|
||||
|
||||
namespace Yi.Framework.WeChat.MiniProgram.HttpModels;
|
||||
|
||||
public class SubscribeNoticeRequest
|
||||
{
|
||||
/// <summary>
|
||||
///用户openid,可以是小程序的openid,也可以是mp_template_msg.appid对应的公众号的openid
|
||||
/// </summary>
|
||||
public string touser { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 小程序模板id
|
||||
/// </summary>
|
||||
public string template_id { get; set; }
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 小程序模板消息的数据
|
||||
/// </summary>
|
||||
public Dictionary<string, keyValueItem> data { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 默认为正式版
|
||||
/// </summary>
|
||||
public string miniprogram_state { get; set; } = "formal";
|
||||
|
||||
/// <summary>
|
||||
/// 默认为中文
|
||||
/// </summary>
|
||||
public string lang { get; set; } = "zh_CN";
|
||||
}
|
||||
|
||||
|
||||
public class SubscribeNoticeInput
|
||||
{
|
||||
/// <summary>
|
||||
///用户openid,可以是小程序的openid,也可以是mp_template_msg.appid对应的公众号的openid
|
||||
/// </summary>
|
||||
public string touser { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 小程序模板id
|
||||
/// </summary>
|
||||
public string template_id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 公众号模板消息的数据
|
||||
/// </summary>
|
||||
public Dictionary<string, keyValueItem> data { get; set; }
|
||||
}
|
||||
|
||||
public class SubscribeNoticeResponse : IErrorObjct
|
||||
{
|
||||
public int errcode { get; set; }
|
||||
public string errmsg { get; set; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public class keyValueItem
|
||||
{
|
||||
public keyValueItem(string value)
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public string value { get; set; }
|
||||
}
|
||||
@@ -10,4 +10,11 @@ public interface IWeChatMiniProgramManager
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
Task<Code2SessionResponse> Code2SessionAsync(Code2SessionInput input);
|
||||
|
||||
/// <summary>
|
||||
/// 向用户发送订阅消息,要openid
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
Task SendSubscribeNoticeAsync(SubscribeNoticeInput input);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Net.Http.Json;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Yi.Framework.Core.Extensions;
|
||||
using Yi.Framework.WeChat.MiniProgram.HttpModels;
|
||||
@@ -44,4 +45,32 @@ public class WeChatMiniProgramManager : IWeChatMiniProgramManager, ISingletonDep
|
||||
return responseBody;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 发送模板订阅消息
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
public async Task SendSubscribeNoticeAsync(SubscribeNoticeInput input)
|
||||
{
|
||||
var token = await _weChatToken.GetTokenAsync();
|
||||
string url = $"https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={token}";
|
||||
var req = new SubscribeNoticeRequest
|
||||
{
|
||||
touser = input.touser,
|
||||
template_id = input.template_id,
|
||||
data = input.data,
|
||||
miniprogram_state = _options.Notice.State??"formal"
|
||||
};
|
||||
req.template_id=req.template_id?? _options.Notice.TemplateId;
|
||||
|
||||
using (HttpClient httpClient = new HttpClient())
|
||||
{
|
||||
var body =new StringContent(JsonConvert.SerializeObject(req));
|
||||
HttpResponseMessage response = await httpClient.PostAsync(url, body);
|
||||
var responseBody = await response.Content.ReadFromJsonAsync<SubscribeNoticeResponse>();
|
||||
responseBody.ValidateSuccess();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,4 +12,19 @@ public class WeChatMiniProgramOptions
|
||||
/// </summary>
|
||||
public string AppSecret { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 消息
|
||||
/// </summary>
|
||||
public WeChatMiniProgramNoticeItem Notice { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public class WeChatMiniProgramNoticeItem
|
||||
{
|
||||
/// <summary>
|
||||
/// 模板id
|
||||
/// </summary>
|
||||
public string TemplateId { get; set; }
|
||||
|
||||
public string State { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user