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 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;
}
///
/// 刷新授权公众号令牌时的事件委托
///
///
public delegate void EventEventAuthorizerRefreshTokenHandler(EventAuthorizerRefreshToken e);
#region 字段
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 = "";
///
/// 授权公众号AppID
///
public string Authorizer_appid
{
get { return _authorizer_appid; }
set { _authorizer_appid = value; }
}
string _authorization_info = "";
///
/// 授权信息(json)
///
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 = "";
///
/// 刷新Authorizer_access_token 过期令牌时要用的刷新令牌
///
public string Authorizer_refresh_token
{
get { return _authorizer_refresh_token; }
set { _authorizer_refresh_token = value; }
}
#endregion
///
/// 清除Authorizer_access_token
///
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 dic = new Dictionary
{
{ "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 "";
}
///
/// 判断Access_token是否过期
///
/// bool
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;
///
/// 授权方公众号类型,0代表订阅号,1代表由历史老帐号升级后的订阅号,2代表服务号
///
public int Service_type_info
{
get { return _service_type_info; }
set { _service_type_info = value; }
}
int _verify_type_inf = -1;
///
/// 授权方认证类型,
/// -1代表未认证,
/// 0代表微信认证,
/// 1代表新浪微博认证,
/// 2代表腾讯微博认证,
/// 3代表已资质认证通过但还未通过名称认证,
/// 4代表已资质认证通过、还未通过名称认证,但通过了新浪微博认证,
/// 5代表已资质认证通过、还未通过名称认证,但通过了腾讯微博认证
///
public int Verify_type_inf
{
get { return _verify_type_inf; }
set { _verify_type_inf = value; }
}
string _user_name = "";
///
/// 授权方公众号的原始ID
///
public string User_name
{
get { return _user_name; }
set { _user_name = value; }
}
string _alias = "";
///
/// 授权方公众号所设置的微信号,可能为空
///
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权限
//素材管理权限
//微信摇周边权限
//微信门店权限
//微信支付权限
//自定义菜单权限
}
}