123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace LYFZ.WXLibrary
- {
-
-
-
- public class AuthorizedAPPIDHandle
- {
- public AuthorizedAPPIDHandle()
- {
- }
- public AuthorizedAPPIDHandle(string authorization_info_json, string jmgdomain, string companyName, int authorizeStatus, DateTime refreshTokenTime)
- {
- this.SetAuthorizedInfo(authorization_info_json, jmgdomain, companyName, authorizeStatus, refreshTokenTime);
- }
- public void SetAuthorizedInfo(string authorization_info_json, string jmgdomain, string companyName, int authorizeStatus, DateTime refreshTokenTime)
- {
- this._JMGDomainName = jmgdomain;
- this._CompanyName = companyName;
- this._authorization_info = authorization_info_json;
- this._AuthorizeStatus = authorizeStatus;
- var authorizationObj = LYFZ.Weixin.SDK.BasicAPI.JsonToDynamic(authorization_info_json);
- this._authorizer_appid = authorizationObj.authorization_info.authorizer_appid;
- this._authorizer_access_token = authorizationObj.authorization_info.authorizer_access_token;
- this._expires_in = Convert.ToInt32(authorizationObj.authorization_info.expires_in);
- this._authorizer_refresh_token = authorizationObj.authorization_info.authorizer_refresh_token;
- this._RefreshTokenTime = refreshTokenTime;
-
-
-
-
- }
-
-
-
-
- public delegate void EventEventAuthorizerRefreshTokenHandler(EventAuthorizerRefreshToken e);
- string _JMGDomainName = "";
-
-
-
- public string JMGDomainName
- {
- get { return _JMGDomainName; }
- set { _JMGDomainName = value; }
- }
- string _CompanyName = "";
-
-
-
- public string CompanyName
- {
- get { return _CompanyName; }
- set { _CompanyName = value; }
- }
- string _authorizer_appid = "";
-
-
-
- public string Authorizer_appid
- {
- get { return _authorizer_appid; }
- set { _authorizer_appid = value; }
- }
- string _authorization_info = "";
-
-
-
- public string Authorization_info
- {
- get { return _authorization_info; }
- set { _authorization_info = value; }
- }
- int _AuthorizeStatus = 0;
-
-
-
- public int AuthorizeStatus
- {
- get { return _AuthorizeStatus; }
- set { _AuthorizeStatus = value; }
- }
-
-
-
- public event EventEventAuthorizerRefreshTokenHandler EventEvent_AuthorizerRefreshToken;
- string _authorizer_access_token = "";
-
-
-
- public string Authorizer_access_token
- {
- get {
-
- if (string.IsNullOrEmpty(_authorizer_access_token) || HasExpired())
- {
-
- LYFZ.WXLibrary.CommonHandleClass.WriteLog("开始请求获取新的有效Authorizer_access_token令牌", "-2");
- _authorizer_access_token = GetAuthorizer_AccessToken(OpenPlatformConfig.OpenAppID, OpenPlatformConfig.Component_Access_Token, Authorizer_appid, Authorizer_refresh_token);
- }
- return _authorizer_access_token; }
- set { _authorizer_access_token = value; }
- }
- int _expires_in = 7200;
-
-
-
- public int Expires_in
- {
- get { return _expires_in; }
- set { _expires_in = value; }
- }
- DateTime _RefreshTokenTime = DateTime.MinValue;
-
-
-
- public DateTime RefreshTokenTime
- {
- get { return _RefreshTokenTime; }
- set { _RefreshTokenTime = value; }
- }
- string _authorizer_refresh_token = "";
-
-
-
- public string Authorizer_refresh_token
- {
- get { return _authorizer_refresh_token; }
- set { _authorizer_refresh_token = value; }
- }
-
-
-
- public void ClearAccessToken()
- {
- _authorizer_access_token = "";
- }
-
-
-
-
-
-
- private string GetAuthorizer_AccessToken(string appId, string component_access_token, string authorizer_appid, string authorizer_refresh_token)
- {
- try
- {
- Dictionary<string, string> dic = new Dictionary<string, string>
- {
- {"component_appid",appId},
- {"authorizer_appid",authorizer_appid},
- {"authorizer_refresh_token",authorizer_refresh_token}
- };
- string json = (new System.Web.Script.Serialization.JavaScriptSerializer()).Serialize(dic);
-
- string retmsg = "";
- var retObj = LYFZ.Weixin.SDK.BasicAPI.GetAuthorizer_Refresh_Token(component_access_token, json, out retmsg);
- LYFZ.WXLibrary.CommonHandleClass.WriteLog("请求获取新的授权公众号的令牌返回信息:" + retmsg, "-2");
- if (retObj != null)
- {
- string retAuthorizer_AccessToken = retObj.authorizer_access_token.Trim();
- if (retAuthorizer_AccessToken.Trim().Length > 0)
- {
- _RefreshTokenTime = DateTime.Now;
- Expires_in = Convert.ToInt32(retObj.expires_in);
- _authorizer_refresh_token = retObj.authorizer_refresh_token.Trim();
- if (EventEvent_AuthorizerRefreshToken != null)
- {
- EventEvent_AuthorizerRefreshToken(new EventAuthorizerRefreshToken(appId, retAuthorizer_AccessToken, Expires_in, _authorizer_refresh_token, _RefreshTokenTime));
- }
- return retAuthorizer_AccessToken;
- }
- else
- {
- _RefreshTokenTime = DateTime.MinValue;
- }
- }
- else
- {
- _RefreshTokenTime = DateTime.MinValue;
- LYFZ.WXLibrary.CommonHandleClass.WriteLog("获取授权Appid:" + authorizer_appid + "的Access_Token令牌时出错:" + retmsg, "-2");
- }
- }
- catch (Exception ex)
- {
- _RefreshTokenTime = DateTime.MinValue;
- LYFZ.WXLibrary.CommonHandleClass.WriteLog("获取授权Appid:" + authorizer_appid + "的Access_Token令牌时出错(Exception):" + ex.Message, "-2");
- }
- return "";
- }
-
-
-
-
- private bool HasExpired()
- {
- if (_RefreshTokenTime != null)
- {
-
- if (DateTime.Now > _RefreshTokenTime.AddSeconds(Expires_in).AddSeconds(-300))
- {
- return true;
- }
- }
- return false;
- }
- AccountBasicInfo _Authorizer_AccountBasicInfo = new AccountBasicInfo();
-
-
-
- public AccountBasicInfo Authorizer_AccountBasicInfo
- {
- get { return _Authorizer_AccountBasicInfo; }
- set { _Authorizer_AccountBasicInfo = value; }
- }
- int _AccountBasicInfo_expires_in = 7200;
-
-
-
- public int AccountBasicInfo_expires_in
- {
- get { return _AccountBasicInfo_expires_in; }
- set { _AccountBasicInfo_expires_in = value; }
- }
- DateTime _RefreshAccountBasicInfoTime = DateTime.Now.AddDays(-2);
-
-
-
- public DateTime RefreshAccountBasicInfoTime
- {
- get { return _RefreshAccountBasicInfoTime; }
- set { _RefreshAccountBasicInfoTime = value; }
- }
- }
- public class EventAuthorizerRefreshToken : EventArgs
- {
- public EventAuthorizerRefreshToken(string appid, string authorizer_access_token, int expires_in, string authorizer_refresh_token,DateTime refreshTokenTime)
- {
- this._appid = appid;
- this._authorizer_access_token = authorizer_access_token;
- this._authorizer_refresh_token = authorizer_refresh_token;
- this._expires_in = expires_in;
- this._RefreshTokenTime = refreshTokenTime;
- }
- string _appid = "";
- public string Appid
- {
- get { return _appid; }
- set { _appid = value; }
- }
- string _authorizer_access_token = "";
- public string Authorizer_access_token
- {
- get { return _authorizer_access_token; }
- set { _authorizer_access_token = value; }
- }
- int _expires_in = 0;
- public int Expires_in
- {
- get { return _expires_in; }
- set { _expires_in = value; }
- }
- string _authorizer_refresh_token = "";
- public string Authorizer_refresh_token
- {
- get { return _authorizer_refresh_token; }
- set { _authorizer_refresh_token = value; }
- }
- bool isRefreshSuccess = false;
-
-
-
- public bool IsRefreshSuccess
- {
- get { return isRefreshSuccess; }
- set { isRefreshSuccess = value; }
- }
- DateTime _RefreshTokenTime = DateTime.MinValue;
-
-
-
- public DateTime RefreshTokenTime
- {
- get { return _RefreshTokenTime; }
- set { _RefreshTokenTime = value; }
- }
- }
-
-
-
- public class AccountBasicInfo
- {
- public AccountBasicInfo(string jsonData = "")
- {
- }
- string _nick_name = "";
-
-
-
- public string Nick_name
- {
- get { return _nick_name; }
- set { _nick_name = value; }
- }
- string _head_img = "";
-
-
-
- public string Head_img
- {
- get { return _head_img; }
- set { _head_img = value; }
- }
- int _service_type_info = 0;
-
-
-
- public int Service_type_info
- {
- get { return _service_type_info; }
- set { _service_type_info = value; }
- }
- int _verify_type_inf = -1;
-
-
-
-
-
-
-
-
-
-
- public int Verify_type_inf
- {
- get { return _verify_type_inf; }
- set { _verify_type_inf = value; }
- }
- string _user_name = "";
-
-
-
- public string User_name
- {
- get { return _user_name; }
- set { _user_name = value; }
- }
- string _alias = "";
-
-
-
- public string Alias
- {
- get { return _alias; }
- set { _alias = value; }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- }
|