sunpengfei
2025-11-21 6e9ec1c080f1a2d8ea3f2e923acd807487ada271
ApiTools.Core/Utils/WxmpUtils/WxmpUtils.cs
@@ -1,8 +1,10 @@
using Aliyun.OSS;
using Azure.Core;
using Aop.Api.Domain;
using ApiTools.Core.Utils.WxmpUtils;
using Azure.Core;
using Furion.FriendlyException;
using Furion.HttpRemote;
using Furion.JsonSerialization;
using Mapster;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Options;
@@ -14,8 +16,10 @@
using System.Net.Http.Json;
using System.Text;
using System.Text.Json.Nodes;
using System.Text.Unicode;
using System.Threading;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace ApiTools.Core
{
@@ -124,5 +128,61 @@
            var result = AliyunOSSUtils.Upload(command.OssScene, stream, command.OssFileName);
            return result.Url;
        }
        /// <summary>
        /// 查询订阅消息模板
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public async Task<GetWxmpSubscribMessageTemplatesResponse> GetWxmpSubscribMessageTemplate(GetWxmpSubscribMessageTemplatesRequest command)
        {
            var option = options.Value.Items.FirstOrDefault(it => it.Code == command.WxmpCode);
            if (option == null || option.AppId.IsNull() || option.AppSecret.IsNull())
                throw Oops.Oh(EnumErrorCodeType.s400, "发送订阅消息失败,缺失配置:WxmpOptions");
            var accessToken = await GetAccessToken(command.WxmpCode);
            var jsonContent = JsonConvert.SerializeObject(command, new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            });
            var response = await httpRemoteService.GetAsAsync<GetWxmpSubscribMessageTemplatesResponse>(
                "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate",
                builder => builder.WithQueryParameter("access_token", accessToken));
            return response;
        }
        /// <summary>
        /// 发送订阅消息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public async Task WxmpSendSubscribMessage(WxmpSendSubscribMessageRequest command)
        {
            var option = options.Value.Items.FirstOrDefault(it => it.Code == command.WxmpCode);
            if (option == null || option.AppId.IsNull() || option.AppSecret.IsNull())
                throw Oops.Oh(EnumErrorCodeType.s400, "发送订阅消息失败,缺失配置:WxmpOptions");
            command.EnvVersion = option.EnvVersion;
            //if (!options.Value.SubscribMessageTemplates.ContainsKey(command.TemplateId))
            //    throw Oops.Oh(EnumErrorCodeType.s400, "发送订阅消息失败,模板不存在");
            //var template = options.Value.SubscribMessageTemplates[command.TemplateId];
            //command.TemplateId = template.TemplateId;
            //if (command.Page.IsNull()) command.Page = template.Page;
            var accessToken = await GetAccessToken(command.WxmpCode);
            var jsonContent = JsonConvert.SerializeObject(command, new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            });
            var response = await httpRemoteService.PostAsync("https://api.weixin.qq.com/cgi-bin/message/subscribe/send",
                builder => builder
                .WithQueryParameter("access_token", accessToken)
                .SetJsonContent(jsonContent));
            response.EnsureSuccessStatusCode();
            if (response.Content.Headers.ContentType.ToString() == "application/json; charset=UTF-8")
            {
                var jsonResult = await response.Content.ReadAsStringAsync();
                var callback = jsonResult.JsonTo<WxmpSendSubscribMessageResponse>();
                if (callback == null || callback.ErrorCode != 0)
                    throw Oops.Oh(EnumErrorCodeType.s510, $"发送订阅消息失败:{callback.ErrorMessage},请联系管理员");
            }
        }
    }
}