香堤湾 更新消息模板

This commit is contained in:
Yuan
2024-09-12 19:00:52 +08:00
parent d2996857b1
commit dedf55aca4
7 changed files with 503 additions and 119 deletions

View File

@@ -32,7 +32,7 @@
<ItemGroup>
<None Update="Appointment.Entity.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>

View File

@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Google.Protobuf.WellKnownTypes;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
@@ -6,6 +7,7 @@ using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Xml.Linq;
namespace Appointment.Entity
{
@@ -441,7 +443,7 @@ namespace Appointment.Entity
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string retString = reader.ReadToEnd();
_logger.Info("-->" + itemMsg);
_logger.Info("-->" + itemMsg + " response-->" + retString);
if (!string.IsNullOrWhiteSpace(retString))
{
@@ -464,5 +466,76 @@ namespace Appointment.Entity
if (msg.Length > 0) _logger.Error(msg.ToString());
}
public static ApiResult SendTempMsg(string msg)
{
try
{
string accessTokenMode = BaseConfig.App(new string[] { "AccessTokenMode" });
if (string.IsNullOrWhiteSpace(accessTokenMode))
{
_logger.Error("配置文件中未获取有效的AccessTokenMode");
return ApiResult.Failed("配置文件中未获取有效的AccessTokenMode");
}
// 获取 AccessToken
string access_token = string.Empty;
for (int i = 0; i < 3; i++)
{
ApiResult<string> tokenResult = null;
if (i == 0)
{
tokenResult = accessTokenMode == "1" ? AizhuhotelGetAccesstoken() : GetAccessToken();
}
else
{
tokenResult = accessTokenMode == "1" ? AizhuhotelGetAccesstoken() : GetAccessToken(true);
}
if (tokenResult.IsSuccess && !string.IsNullOrEmpty(tokenResult.Info))
{
access_token = tokenResult.Info;
break;
}
Thread.Sleep(500);
}
if (string.IsNullOrWhiteSpace(access_token)) return ApiResult.Failed("未获取有效的AccessToken");
// 推送消息
var postUrl = $"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={access_token}";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl);
request.Proxy = null;
request.Method = "POST";
request.ContentType = "application/json";
using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
{
dataStream.Write(msg);
dataStream.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding == null || encoding.Length < 1)
{
encoding = "UTF-8"; //默认编码
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string retString = reader.ReadToEnd();
_logger.Info("-->" + msg + " response-->" + retString);
if (!string.IsNullOrWhiteSpace(retString))
{
var sendTemplateMsgRes = JsonConvert.DeserializeObject<SendTemplateMsgRes>(retString);
if (sendTemplateMsgRes.Errmsg.ToLower() == "ok")
{
return ApiResult.Success(string.Empty);
}
}
return ApiResult.Failed(msg: "", info: retString);
}
catch (Exception ex)
{
_logger.Debug(ex.ToString());
return ApiResult.Failed(msg: "", info: ex.Message);
}
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using Google.Protobuf.WellKnownTypes;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
@@ -9,6 +10,7 @@ namespace Appointment.Entity
public class WechatHelper
{
private static HttpClient _HttpClient = null;
static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(WechatHelper));
static WechatHelper()
{
@@ -25,7 +27,7 @@ namespace Appointment.Entity
public ApiResult<WeChatLoginRes> Code2Session(string code)
{
string appId = BaseConfig.App(new string[] { "WeChat", "AppId" });
if(string.IsNullOrWhiteSpace(appId)) ApiResult<WeChatLoginRes>.Failed("缺少配置参数 [AppId]!");
if (string.IsNullOrWhiteSpace(appId)) ApiResult<WeChatLoginRes>.Failed("缺少配置参数 [AppId]!");
string appSecret = BaseConfig.App(new string[] { "WeChat", "AppSecret" });
if (string.IsNullOrWhiteSpace(appSecret)) ApiResult<WeChatLoginRes>.Failed("缺少配置参数 [AppSecret]!");
@@ -40,6 +42,7 @@ namespace Appointment.Entity
if (res.StatusCode == HttpStatusCode.OK)
{
var result = Encoding.Default.GetString(res.Content.ReadAsByteArrayAsync().Result);
log.Info($"jscode2session:{result}");
if (string.IsNullOrWhiteSpace(result) || result.IndexOf("session_key") == -1)
{
return ApiResult<WeChatLoginRes>.Failed("调用微信接口失败,可能code已失效");
@@ -58,7 +61,7 @@ namespace Appointment.Entity
//
public class LoveInnGetAccessTokenReq
{
{
public string AppId { get; set; }
public string AppName { get; set; }