sunpengfei
2025-11-21 8d9adc809cee12d16caf9854ada2e7d030a5f4b4
ApiTools.Core/Utils/WxmpUtils/WxmpUtils.cs
@@ -130,19 +130,44 @@
        }
        /// <summary>
        /// 该接口用于发送订阅消息
        /// 查询订阅消息模板
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public async Task<string> WxSend(SendRequest command)
        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");
            command.EnvVersion = option.EnvVersion;
                throw Oops.Oh(EnumErrorCodeType.s400, "发送订阅消息失败,缺失配置:WxmpOptions");
            var accessToken = await GetAccessToken(command.WxmpCode);
            var request = command.Adapt<SendRequest>();
            var jsonContent = JsonConvert.SerializeObject(request, new JsonSerializerSettings
            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
            });
@@ -151,22 +176,13 @@
                .WithQueryParameter("access_token", accessToken)
                .SetJsonContent(jsonContent));
            response.EnsureSuccessStatusCode();
            //var a = response.Content.Headers.ContentType.ToString();
            //application/json; encoding=utf-8
            //if (response.Content.Headers.ContentType.ToString() == "application/json; charset=UTF-8")
            if (response.Content.Headers.ContentType.ToString() == "application/json; encoding=utf-8")
            if (response.Content.Headers.ContentType.ToString() == "application/json; charset=UTF-8")
            {
                var jsonResult = await response.Content.ReadAsStringAsync();
                //var callback = jsonResult.JsonTo<SendResponse>();
                //if (callback == null || callback.ErrorCode != 0)
                    //throw Oops.Oh(EnumErrorCodeType.s510, $"获取小程序码失败:{callback.errmsg},请联系管理员");
                return jsonResult;
                var callback = jsonResult.JsonTo<WxmpSendSubscribMessageResponse>();
                if (callback == null || callback.ErrorCode != 0)
                    throw Oops.Oh(EnumErrorCodeType.s510, $"发送订阅消息失败:{callback.ErrorMessage},请联系管理员");
            }
            //var stream = await response.Content.ReadAsStreamAsync();
            //var result = AliyunOSSUtils.Upload(command, stream, command.OssFileName);
            return "";
        }
    }
}