From 46bd392afb19fe7640245c6480c71bf519ca3023 Mon Sep 17 00:00:00 2001 From: yashen Date: Thu, 17 Aug 2017 16:39:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BE=97=E5=88=B0JS=E7=AD=BE?= =?UTF-8?q?=E5=90=8D=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- B3WeChat/B3WeChat.csproj | 1 + B3WeChat/Entities/GetTicketResponse.cs | 14 +++++++++++ B3WeChat/Rpcs/WeChatUserRpc.cs | 6 +++++ B3WeChat/Utils/InOutMessageUtil.cs | 32 ++++++++++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 B3WeChat/Entities/GetTicketResponse.cs diff --git a/B3WeChat/B3WeChat.csproj b/B3WeChat/B3WeChat.csproj index a7a2bad..b9126cc 100644 --- a/B3WeChat/B3WeChat.csproj +++ b/B3WeChat/B3WeChat.csproj @@ -122,6 +122,7 @@ + diff --git a/B3WeChat/Entities/GetTicketResponse.cs b/B3WeChat/Entities/GetTicketResponse.cs new file mode 100644 index 0000000..9677638 --- /dev/null +++ b/B3WeChat/Entities/GetTicketResponse.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BWP.B3WeChat.Entities +{ + public class GetTicketResponse:WeChatResponseBase + { + public string ticket { get; set; } + + public int expires_in { get; set; } + } +} diff --git a/B3WeChat/Rpcs/WeChatUserRpc.cs b/B3WeChat/Rpcs/WeChatUserRpc.cs index d30e4d1..95db329 100644 --- a/B3WeChat/Rpcs/WeChatUserRpc.cs +++ b/B3WeChat/Rpcs/WeChatUserRpc.cs @@ -1,6 +1,7 @@ using BWP.B3WeChat.BL; using BWP.B3WeChat.BO; using BWP.B3WeChat.BO.NamedValueTemplate; +using BWP.B3WeChat.Utils; using Forks.EnterpriseServices.DomainObjects2; using Forks.EnterpriseServices.DomainObjects2.DQuery; using Forks.EnterpriseServices.JsonRpc; @@ -87,5 +88,10 @@ namespace BWP.B3WeChat.Rpcs } + [Rpc] + public static string GetJsApiSignature(string noncestr, string timestamp, string url) + { + return InOutMessageUtil.GetJsApiSignature(noncestr, timestamp, url); + } } } diff --git a/B3WeChat/Utils/InOutMessageUtil.cs b/B3WeChat/Utils/InOutMessageUtil.cs index 4483f1f..0680e80 100644 --- a/B3WeChat/Utils/InOutMessageUtil.cs +++ b/B3WeChat/Utils/InOutMessageUtil.cs @@ -2,6 +2,7 @@ using BWP.B3WeChat.BO; using BWP.B3WeChat.Entities; using Forks.Utils; +using Forks.Utils.Caching; using System; using System.Collections.Generic; using System.IO; @@ -140,6 +141,37 @@ namespace BWP.B3WeChat.Utils GetRequest(url); } + + static DateTime? ticketExpiredTime; + + static string GetJsApiTicket() + { + var time = DateTime.Now; + if (ticketExpiredTime == null || ticketExpiredTime.Value > time) + { + //过期时间是2小时,不过这里小一些,避免边界问题 + ticketExpiredTime = time.Add(new TimeSpan(1, 30, 30)); + } + + var key = string.Format("{0}_GetJsApiTicket_{1}", TOKEN, ticketExpiredTime.Value.Ticks); + + return CacheManager.GetOrSet(key, (args) => + { + var url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", TOKEN); + var res = GetRequest(url); + return res.ticket; + }); + + } + + public static string GetJsApiSignature(string noncestr, string timestamp, string url) + { + var origin = string.Format("jsapi_ticket={0}&=noncestr{1}×tamp={2}&url={3}", GetJsApiTicket(),noncestr, timestamp, url); + return FormsAuthentication.HashPasswordForStoringInConfigFile(origin, "SHA1").ToLower(); + } + + + public static List GetOpenIDList() {