123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace LYFZ.WXLibrary
- {
- /// <summary>
- /// 公众号授权处理
- /// </summary>
- public class AuthorizedAPPIDHandle
- {
- public AuthorizedAPPIDHandle()
- {
- }
- public AuthorizedAPPIDHandle(
- string authorization_info_json,
- string companyName,
- int authorizeStatus,
- DateTime refreshTokenTime)
- {
- this.SetAuthorizedInfo(authorization_info_json, companyName, authorizeStatus, refreshTokenTime);
- }
- public void SetAuthorizedInfo(
- string authorization_info_json,
- string companyName,
- int authorizeStatus,
- DateTime refreshTokenTime)
- {
- 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;
- }
- /// <summary>
- /// 刷新授权公众号令牌时的事件委托
- /// </summary>
- /// <param name="e"></param>
- public delegate void EventEventAuthorizerRefreshTokenHandler(EventAuthorizerRefreshToken e);
- #region 字段
- string _JMGDomainName = "";
- /// <summary>
- /// 授权公众号绑定的加密锁域名
- /// </summary>
- public string JMGDomainName
- {
- get { return _JMGDomainName; }
- set { _JMGDomainName = value; }
- }
- string _CompanyName = "";
- /// <summary>
- /// 授权公众号绑定的企业名称
- /// </summary>
- public string CompanyName
- {
- get { return _CompanyName; }
- set { _CompanyName = value; }
- }
- string _authorizer_appid = "";
- /// <summary>
- /// 授权公众号AppID
- /// </summary>
- public string Authorizer_appid
- {
- get { return _authorizer_appid; }
- set { _authorizer_appid = value; }
- }
- string _authorization_info = "";
- /// <summary>
- /// 授权信息(json)
- /// </summary>
- public string Authorization_info
- {
- get { return _authorization_info; }
- set { _authorization_info = value; }
- }
- int _AuthorizeStatus = 0;
- /// <summary>
- /// 授权状态
- /// </summary>
- public int AuthorizeStatus
- {
- get { return _AuthorizeStatus; }
- set { _AuthorizeStatus = value; }
- }
- /// <summary>
- /// 刷新授权公众号的令牌时的事件
- /// </summary>
- public event EventEventAuthorizerRefreshTokenHandler EventEvent_AuthorizerRefreshToken;
- string _authorizer_access_token = "";
- /// <summary>
- /// 授权令牌
- /// </summary>
- 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;
- /// <summary>
- /// 令牌有效时长
- /// </summary>
- public int Expires_in
- {
- get { return _expires_in; }
- set { _expires_in = value; }
- }
- DateTime _RefreshTokenTime = DateTime.MinValue;
- /// <summary>
- /// 获取(刷新)令牌时的时间
- /// </summary>
- public DateTime RefreshTokenTime
- {
- get { return _RefreshTokenTime; }
- set { _RefreshTokenTime = value; }
- }
- string _authorizer_refresh_token = "";
- /// <summary>
- /// 刷新Authorizer_access_token 过期令牌时要用的刷新令牌
- /// </summary>
- public string Authorizer_refresh_token
- {
- get { return _authorizer_refresh_token; }
- set { _authorizer_refresh_token = value; }
- }
- #endregion
- /// <summary>
- /// 清除Authorizer_access_token
- /// </summary>
- public void ClearAccessToken()
- {
- _authorizer_access_token = "";
- }
- /// <summary>
- /// 获取授权公众号的令牌
- /// </summary>
- /// <param name="appId"></param>
- /// <param name="appSecret"></param>
- /// <returns></returns>
- 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);
- //获取access_token
- 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 "";
- }
- /// <summary>
- /// 判断Access_token是否过期
- /// </summary>
- /// <returns>bool</returns>
- private bool HasExpired()
- {
- if (_RefreshTokenTime != null)
- {
- //过期时间,允许有一定的误差,五分钟。获取时间消耗
- if (DateTime.Now > _RefreshTokenTime.AddSeconds(Expires_in).AddSeconds(-300))
- {
- return true;
- }
- }
- return false;
- }
- AccountBasicInfo _Authorizer_AccountBasicInfo = new AccountBasicInfo();
- /// <summary>
- /// 当前授权公众号的帐号基本信息
- /// </summary>
- public AccountBasicInfo Authorizer_AccountBasicInfo
- {
- get { return _Authorizer_AccountBasicInfo; }
- set { _Authorizer_AccountBasicInfo = value; }
- }
- int _AccountBasicInfo_expires_in = 7200;
- /// <summary>
- /// 帐号基本信息有效时长
- /// </summary>
- public int AccountBasicInfo_expires_in
- {
- get { return _AccountBasicInfo_expires_in; }
- set { _AccountBasicInfo_expires_in = value; }
- }
- DateTime _RefreshAccountBasicInfoTime = DateTime.Now.AddDays(-2);
- /// <summary>
- /// 获取(刷新)帐号基本信息时的时间
- /// </summary>
- public DateTime RefreshAccountBasicInfoTime
- {
- get { return _RefreshAccountBasicInfoTime; }
- set { _RefreshAccountBasicInfoTime = value; }
- }
- }
- /// <summary>
- ///
- /// </summary>
- 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;
- /// <summary>
- /// 是否刷新成功
- /// </summary>
- public bool IsRefreshSuccess
- {
- get { return isRefreshSuccess; }
- set { isRefreshSuccess = value; }
- }
- DateTime _RefreshTokenTime = DateTime.MinValue;
- /// <summary>
- ///
- /// </summary>
- public DateTime RefreshTokenTime
- {
- get { return _RefreshTokenTime; }
- set { _RefreshTokenTime = value; }
- }
- }
- /// <summary>
- /// 帐号基本信息
- /// </summary>
- public class AccountBasicInfo
- {
- public AccountBasicInfo(string jsonData = "")
- {
- }
- string _nick_name = "";
- /// <summary>
- /// 授权方昵称
- /// </summary>
- public string Nick_name
- {
- get { return _nick_name; }
- set { _nick_name = value; }
- }
- string _head_img = "";
- /// <summary>
- /// 授权方头像
- /// </summary>
- public string Head_img
- {
- get { return _head_img; }
- set { _head_img = value; }
- }
- int _service_type_info = 0;
- /// <summary>
- /// 授权方公众号类型,0代表订阅号,1代表由历史老帐号升级后的订阅号,2代表服务号
- /// </summary>
- public int Service_type_info
- {
- get { return _service_type_info; }
- set { _service_type_info = value; }
- }
- int _verify_type_inf = -1;
- /// <summary>
- /// 授权方认证类型,
- /// -1代表未认证,
- /// 0代表微信认证,
- /// 1代表新浪微博认证,
- /// 2代表腾讯微博认证,
- /// 3代表已资质认证通过但还未通过名称认证,
- /// 4代表已资质认证通过、还未通过名称认证,但通过了新浪微博认证,
- /// 5代表已资质认证通过、还未通过名称认证,但通过了腾讯微博认证
- /// </summary>
- public int Verify_type_inf
- {
- get { return _verify_type_inf; }
- set { _verify_type_inf = value; }
- }
- string _user_name = "";
- /// <summary>
- /// 授权方公众号的原始ID
- /// </summary>
- public string User_name
- {
- get { return _user_name; }
- set { _user_name = value; }
- }
- string _alias = "";
- /// <summary>
- /// 授权方公众号所设置的微信号,可能为空
- /// </summary>
- public string Alias
- {
- get { return _alias; }
- set { _alias = value; }
- }
- //以下信息暂不使用
- //string _business_info=
- // 用以了解以下功能的开通状况(0代表未开通,1代表已开通):
- // open_store:是否开通微信门店功能
- // open_scan:是否开通微信扫商品功能
- // open_pay:是否开通微信支付功能
- // open_card:是否开通微信卡券功能
- // open_shake:是否开通微信摇一摇功能
- // qrcode_url 二维码图片的URL,开发者最好自行也进行保存
- //authorization_info 授权信息
- //appid 授权方appid
- //func_info 公众号授权给开发者的权限集列表,ID为1到15时分别代表:
- //消息管理权限
- //用户管理权限
- //帐号服务权限
- //网页服务权限
- //微信小店权限
- //微信多客服权限
- //群发与通知权限
- //微信卡券权限
- //微信扫一扫权限
- //微信连WIFI权限
- //素材管理权限
- //微信摇周边权限
- //微信门店权限
- //微信支付权限
- //自定义菜单权限
- }
- }
|