Files
AppointmentOA/api_v1/Appointment.Entity/Utility/WechatHelper.cs
2024-09-12 19:00:52 +08:00

127 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Google.Protobuf.WellKnownTypes;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
namespace Appointment.Entity
{
public class WechatHelper
{
private static HttpClient _HttpClient = null;
static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(WechatHelper));
static WechatHelper()
{
_HttpClient = new HttpClient(new HttpClientHandler());
}
public object JsonConvert { get; private set; }
/// <summary>
/// 根据小程序登录code获取用户信息
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public ApiResult<WeChatLoginRes> Code2Session(string code)
{
string appId = BaseConfig.App(new string[] { "WeChat", "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]!");
if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(appId) || string.IsNullOrWhiteSpace(appSecret))
{
return ApiResult<WeChatLoginRes>.Failed("获取小程序配置信息出错");
}
string url = $"https://api.weixin.qq.com/sns/jscode2session?appid={appId}&secret={appSecret}&js_code={code}&grant_type=authorization_code";
var res = _HttpClient.GetAsync(url).Result;
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已失效");
}
var weChatDto = Newtonsoft.Json.JsonConvert.DeserializeObject<WeChatLoginRes>(result);
return ApiResult<WeChatLoginRes>.Success("", weChatDto);
}
else
{
return ApiResult<WeChatLoginRes>.Failed("调用微信接口失败");
}
}
//
public class LoveInnGetAccessTokenReq
{
public string AppId { get; set; }
public string AppName { get; set; }
public string AppSecret { get; set; }
}
/// <summary>
/// 模版信息
/// </summary>
/// <returns></returns>
public static string BuildTemlMsg(string toUser, string tmplId, string url, string first, string remark, params object[] args)
{
string appId = BaseConfig.App(new string[] { "WeChat", "AppId" });
if (string.IsNullOrWhiteSpace(appId)) ApiResult<WeChatLoginRes>.Failed("缺少配置参数 [AppId]!");
StringBuilder sbMsg = new StringBuilder();
sbMsg.Append("{");
sbMsg.AppendFormat("\"touser\":\"{0}\",", toUser);
sbMsg.AppendFormat("\"template_id\":\"{0}\",", tmplId);
sbMsg.Append("\"miniprogram\":{");
sbMsg.AppendFormat("\"appid\":\"{0}\",", appId);
sbMsg.AppendFormat("\"pagepath\":\"{0}\"", url);
sbMsg.Append("},");
sbMsg.Append("\"topcolor\":\"#FF0000\",");
sbMsg.Append("\"data\":");
sbMsg.Append("{");
sbMsg.Append("\"first\":");
sbMsg.Append("{");
sbMsg.AppendFormat("\"value\":\"{0}\",", first);
sbMsg.AppendFormat("\"color\":\"{0}\"", "#333333");
sbMsg.Append("},");
for (int i = 0; i < args.Length; i++)
{
sbMsg.AppendFormat("\"keyword{0}\":", i + 1);
sbMsg.Append("{");
sbMsg.AppendFormat("\"value\":\"{0}\",", args[i]);
sbMsg.AppendFormat("\"color\":\"{0}\"", "#333333");
sbMsg.Append("},");
}
sbMsg.Append("\"remark\":");
sbMsg.Append("{");
sbMsg.AppendFormat("\"value\":\"{0}\",", remark);
sbMsg.AppendFormat("\"color\":\"{0}\"", "#333333");
sbMsg.Append("}");
sbMsg.Append("}");
sbMsg.Append("}");
return sbMsg.ToString();
}
}
}