You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
210 lines
8.2 KiB
210 lines
8.2 KiB
using com.sun.org.apache.bcel.@internal.generic; |
|
using com.sun.xml.@internal.xsom; |
|
using Elight.Utility; |
|
using Elight.Utility.Code; |
|
using Elight.Utility.Extensions; |
|
using java.lang.annotation; |
|
using Microsoft.IdentityModel.Logging; |
|
using Newtonsoft.Json; |
|
using Newtonsoft.Json.Linq; |
|
using System.Runtime.Caching; |
|
|
|
namespace _24Hour |
|
{ |
|
|
|
public class WechatMessagerClient |
|
{ |
|
private readonly HttpClient httpCliet; |
|
// 实例化MemoryCache对象 |
|
MemoryCache cache = MemoryCache.Default; |
|
private readonly IConfiguration _configuration; |
|
/// <summary> |
|
/// 小程序AppID |
|
/// </summary> |
|
private string appid = "wx48108a0c98a3dab0"; |
|
/// <summary> |
|
/// 小程序AppID |
|
/// </summary> |
|
private string secret = "wx48108a0c98a3dab0"; |
|
/// <summary> |
|
/// 小程序secretID |
|
/// </summary> |
|
private string secid = "e752e4fba67526eca21313a18c96a58f"; |
|
public WechatMessagerClient(HttpClient _httpCliet, IConfiguration configuration) |
|
{ |
|
this.httpCliet = _httpCliet; |
|
|
|
httpCliet.DefaultRequestHeaders.Add("Accept", "application/json"); |
|
httpCliet.DefaultRequestHeaders.Add("User-Agent", "HttpClientFactory-Sample"); |
|
httpCliet.DefaultRequestHeaders.ConnectionClose = true; |
|
_configuration = configuration; |
|
|
|
appid = $"{configuration.GetSection("Wechat:appid").Value}"; |
|
secret = $"{configuration.GetSection("Wechat:secret").Value}"; |
|
secid = $"{configuration.GetSection("Wechat:secid").Value}"; |
|
} |
|
public async Task<Result> GetOpenId(string code) |
|
{ |
|
var ret = new Result(); |
|
var reponse = await httpCliet.GetAsync($"/sns/jscode2session?appid={appid}&secret={secid}&js_code={code}&grant_type=authorization_code"); |
|
var opendata = await reponse.EnsureSuccessStatusCode().Content.ReadAsStringAsync(); |
|
var data = JsonConvert.DeserializeAnonymousType(opendata, new |
|
{ |
|
openid = default(string), |
|
session_key = default(string), |
|
unionid = default(string), |
|
errcode = default(int), |
|
errmsg = default(string), |
|
}); |
|
if (data.errcode == 0) |
|
{ |
|
ret.IsSucceed = true; |
|
ret.result = data.openid; |
|
} |
|
else |
|
{ |
|
ret.IsSucceed = false; |
|
ret.Message = data.errmsg; |
|
} |
|
return ret; |
|
} |
|
|
|
/// <summary> |
|
/// 律师预约微信消息提醒,成功 |
|
/// </summary> |
|
/// <returns></returns> |
|
public bool wxts(string? useropenId, string? name, string? unit, DateTime? datetime, string? matter,string? attention) |
|
{ |
|
try |
|
{ |
|
var token = ""; |
|
|
|
// 获取缓存 |
|
token = cache.Get("token")?.ToString(); |
|
|
|
if (string.IsNullOrEmpty(token)) |
|
{ |
|
string geturl = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}"; |
|
var re1 = HttpHelper.Get(geturl); |
|
if (re1.Jsonstr("errcode") == null) |
|
{ |
|
token = re1.Jsonstr("access_token"); |
|
|
|
// 移除缓存 |
|
cache.Remove("token"); |
|
// 添加缓存 |
|
cache.Add("token", token, new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddMinutes(120) }); |
|
} |
|
} |
|
string url = $"https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=";//发送地址 |
|
try |
|
{ |
|
object msg = null; |
|
string posturl = $"{url}{token}";//发送地址 |
|
var thing4 = matter; |
|
var thing5 = attention; |
|
if (matter.Length>20) |
|
thing4 = matter.Substring(0, 17)+"..."; |
|
if (attention.Length>20) |
|
thing5 = attention.Substring(0, 17)+"..."; |
|
msg = new |
|
{ |
|
touser = useropenId, |
|
template_id = "mRfzPnc_3JuV9cPhjKrRL3jX1C_JJoBGYtqEnbfadRM", |
|
page= "pages/selfService/reception/reception", |
|
miniprogram_state= $"{_configuration.GetSection("Wechat:miniprogram_state").Value}", |
|
lang ="zh_CN", |
|
data = new |
|
{ |
|
//name1 = new { value = $"{name}"},// |
|
thing2 = new { value = $"{unit}"},// |
|
date3 = new { value = $"{datetime:yyyy年MM月dd日 HH:mm}"},// |
|
thing4 = new { value = $"{thing4}"},// |
|
thing5 = new { value = $"{thing5}", color = "#173177" },// |
|
}, |
|
}; |
|
var re = HttpHelper.Post(posturl, msg); |
|
if (re.Jsonstr("errcode") == "0") |
|
return true; |
|
return false; |
|
} |
|
catch (Exception ex) |
|
{ |
|
} |
|
return true; |
|
} |
|
catch (Exception ex) |
|
{ |
|
} |
|
return false; |
|
} |
|
|
|
|
|
/// <summary> |
|
/// 律师预约微信消息提醒,失败 |
|
/// </summary> |
|
/// <returns></returns> |
|
public bool sbwxts(string? useropenId,string? name,string? unit,DateTime? datetime,string? matter) |
|
{ |
|
try |
|
{ |
|
var token = ""; |
|
|
|
// 获取缓存 |
|
var tokenss = cache.Get("token"); |
|
|
|
if (string.IsNullOrEmpty(token)) |
|
{ |
|
string geturl = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx48108a0c98a3dab0&secret=e752e4fba67526eca21313a18c96a58f"; |
|
var re1 = HttpHelper.Get(geturl); |
|
if (re1.Jsonstr("errcode") == null) |
|
{ |
|
token = re1.Jsonstr("access_token"); |
|
|
|
// 移除缓存 |
|
cache.Remove("token"); |
|
// 添加缓存 |
|
cache.Add("token", token, new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddMinutes(120) }); |
|
} |
|
} |
|
string url = $"https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=";//发送地址 |
|
try |
|
{ |
|
object msg = null; |
|
string posturl = $"{url}{token}";//发送地址 |
|
var thing4 = matter; |
|
if (matter.Length > 20) |
|
thing4 = matter.Substring(0, 17) + "..."; |
|
msg = new |
|
{ |
|
touser = useropenId, |
|
template_id = "sPbZd6ro14L4uCR_TyDlB4g-sR20zzqcvb-c6QkV3U4", |
|
page = "pages/selfService/reception/reception", |
|
miniprogram_state = $"{_configuration.GetSection("Wechat:miniprogram_state").Value}", |
|
lang = "zh_CN", |
|
data = new |
|
{ |
|
thing1 = new { value = $"{name}" },//访问人 |
|
thing2 = new { value = $"{unit}" },//访问单位 |
|
time3 = new { value = $"{datetime:yyyy年MM月dd日 HH:mm}" },//预约时间 |
|
thing4 = new { value = $"{thing4}" },//事由 |
|
}, |
|
}; |
|
var re = HttpHelper.Post(posturl, msg); |
|
if (re.Jsonstr("errcode") == "0") |
|
return true; |
|
return false; |
|
} |
|
catch (Exception ex) |
|
{ |
|
} |
|
return true; |
|
} |
|
catch (Exception ex) |
|
{ |
|
} |
|
return false; |
|
} |
|
|
|
} |
|
}
|
|
|