提交
This commit is contained in:
123
api_v1/Appointment.Entity/Utility/WechatHelper.cs
Normal file
123
api_v1/Appointment.Entity/Utility/WechatHelper.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
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 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);
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user