123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using System;
- using System.Collections.Concurrent;
- using System.Net;
- using Newtonsoft.Json;
- using Common;
- using System.Threading.Tasks;
- namespace ZcPeng.weixin.PublicAccount
- {
-
-
-
- public class AccessToken
- {
-
-
-
- private static ConcurrentDictionary<string, Tuple<AccessToken, DateTime>> accessTokens = new ConcurrentDictionary<string, Tuple<AccessToken, DateTime>>();
-
-
-
- private const string urlForGettingAccessToken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";
-
-
-
- private const string httpMethodForGettingAccessToken = WebRequestMethods.Http.Get;
-
-
-
- private const int accessTokenSavingSeconds = 7000;
-
-
-
- public string access_token { get; set; }
-
-
-
- public int expires_in { get; set; }
-
-
-
-
-
- internal AccessToken(string _access_token, int _expires_in)
- {
- access_token = _access_token;
- expires_in = _expires_in;
- }
-
-
-
-
- public override string ToString()
- {
- return string.Format("AccessToken:{0}\r\n有效时间:{1}秒", access_token, expires_in);
- }
-
-
-
-
-
- internal static AccessToken ParseFromJson(string json)
- {
- var at = JsonConvert.DeserializeAnonymousType(json, new { access_token = "", expires_in = 1 });
- return new AccessToken(at.access_token, at.expires_in);
- }
-
-
-
-
-
-
- internal static bool TryParseFromJson(string json, out AccessToken token)
- {
- bool success = false;
- token = null;
- try
- {
- token = ParseFromJson(json);
- success = true;
- }
- catch { }
- return success;
- }
-
-
-
-
-
- public static AccessToken Get(string userName)
- {
- Tuple<AccessToken, DateTime> lastToken = accessTokens.ContainsKey(userName) ? accessTokens[userName] : null;
- AccessToken token = lastToken == null ? null : lastToken.Item1;
- DateTime refreshTime = lastToken == null ? DateTime.MinValue : lastToken.Item2;
- if ((DateTime.Now - refreshTime).TotalSeconds > accessTokenSavingSeconds || token == null)
- {
-
- ErrorMessage msg;
- AccessToken newToken = GetFromWeixinServer(userName, out msg);
- if (newToken == null)
- newToken = GetFromWeixinServer(userName, out msg);
-
- if (newToken != null)
- {
- lastToken = new Tuple<AccessToken, DateTime>(newToken, DateTime.Now);
- accessTokens[userName] = lastToken;
- token = newToken;
- Message msg1 = new Message(Guid.NewGuid(), MessageType.Request, token.ToString(),
- DateTime.Now, "GetToken", msg.errmsg);
- Message.Insert(msg1);
- }
- else
- {
- Message msg1 = new Message(Guid.NewGuid(), MessageType.Request, msg.errcode == 0 ? "" : msg.errcode.ToString(),
- DateTime.Now, "GetToken", msg.errcode+":"+msg.errmsg);
- Message.Insert(msg1);
- return null;
- }
- }
- return token;
- }
-
-
-
-
-
-
- private static AccessToken GetFromWeixinServer(string userName, out ErrorMessage msg)
- {
- AccessToken token = null;
- msg = new ErrorMessage(ErrorMessage.ExceptionCode, "");
- AccountInfo account = AccountInfoCollection.GetAccountInfo(userName);
- if (account == null)
- {
- msg.errmsg = "获取公众号参数失败。";
- return token;
- }
- string url = string.Format(urlForGettingAccessToken, account.AppId, account.AppSecret);
- string result = HttpHelper.HttpPost(url, httpMethodForGettingAccessToken, "application/text").Result;
- if(result =="")
- {
- msg.errmsg = "从微信服务器获取响应失败。";
- return token;
- }
- if (ErrorMessage.IsErrorMessage(result))
- msg = ErrorMessage.Parse(result);
- else
- {
- try
- {
- token = AccessToken.ParseFromJson(result);
- }
- catch (Exception e)
- {
- msg = new ErrorMessage(e);
- }
- }
- return token;
- }
- }
- }
|