From 8d9adc809cee12d16caf9854ada2e7d030a5f4b4 Mon Sep 17 00:00:00 2001
From: sunpengfei <i@angelzzz.com>
Date: 星期五, 21 十一月 2025 15:41:42 +0800
Subject: [PATCH] feat:开发

---
 ApiTools.Core/Utils/WxmpUtils/WxmpUtils.cs |   62 ++++++++++++++++++++++++++++++
 1 files changed, 61 insertions(+), 1 deletions(-)

diff --git a/ApiTools.Core/Utils/WxmpUtils/WxmpUtils.cs b/ApiTools.Core/Utils/WxmpUtils/WxmpUtils.cs
index 44f09c0..33c360c 100644
--- a/ApiTools.Core/Utils/WxmpUtils/WxmpUtils.cs
+++ b/ApiTools.Core/Utils/WxmpUtils/WxmpUtils.cs
@@ -1,8 +1,10 @@
 锘縰sing 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, "鍙戦�佽闃呮秷鎭け璐ワ紝缂哄け閰嶇疆锛歐xmpOptions");
+            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, "鍙戦�佽闃呮秷鎭け璐ワ紝缂哄け閰嶇疆锛歐xmpOptions");
+            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}锛岃鑱旂郴绠$悊鍛�");
+            }
+        }
     }
 }

--
Gitblit v1.9.1