AuthorizedAPPIDHandle.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace LYFZ.WXLibrary
  6. {
  7. /// <summary>
  8. /// 公众号授权处理
  9. /// </summary>
  10. public class AuthorizedAPPIDHandle
  11. {
  12. public AuthorizedAPPIDHandle()
  13. {
  14. }
  15. public AuthorizedAPPIDHandle(
  16. string authorization_info_json,
  17. string companyName,
  18. int authorizeStatus,
  19. DateTime refreshTokenTime)
  20. {
  21. this.SetAuthorizedInfo(authorization_info_json, companyName, authorizeStatus, refreshTokenTime);
  22. }
  23. public void SetAuthorizedInfo(
  24. string authorization_info_json,
  25. string companyName,
  26. int authorizeStatus,
  27. DateTime refreshTokenTime)
  28. {
  29. this._CompanyName = companyName;
  30. this._authorization_info = authorization_info_json;
  31. this._AuthorizeStatus = authorizeStatus;
  32. var authorizationObj = LYFZ.Weixin.SDK.BasicAPI.JsonToDynamic(authorization_info_json);
  33. this._authorizer_appid = authorizationObj.authorization_info.authorizer_appid;
  34. this._authorizer_access_token = authorizationObj.authorization_info.authorizer_access_token;
  35. this._expires_in = Convert.ToInt32(authorizationObj.authorization_info.expires_in);
  36. this._authorizer_refresh_token = authorizationObj.authorization_info.authorizer_refresh_token;
  37. this._RefreshTokenTime = refreshTokenTime;
  38. }
  39. /// <summary>
  40. /// 刷新授权公众号令牌时的事件委托
  41. /// </summary>
  42. /// <param name="e"></param>
  43. public delegate void EventEventAuthorizerRefreshTokenHandler(EventAuthorizerRefreshToken e);
  44. #region 字段
  45. string _JMGDomainName = "";
  46. /// <summary>
  47. /// 授权公众号绑定的加密锁域名
  48. /// </summary>
  49. public string JMGDomainName
  50. {
  51. get { return _JMGDomainName; }
  52. set { _JMGDomainName = value; }
  53. }
  54. string _CompanyName = "";
  55. /// <summary>
  56. /// 授权公众号绑定的企业名称
  57. /// </summary>
  58. public string CompanyName
  59. {
  60. get { return _CompanyName; }
  61. set { _CompanyName = value; }
  62. }
  63. string _authorizer_appid = "";
  64. /// <summary>
  65. /// 授权公众号AppID
  66. /// </summary>
  67. public string Authorizer_appid
  68. {
  69. get { return _authorizer_appid; }
  70. set { _authorizer_appid = value; }
  71. }
  72. string _authorization_info = "";
  73. /// <summary>
  74. /// 授权信息(json)
  75. /// </summary>
  76. public string Authorization_info
  77. {
  78. get { return _authorization_info; }
  79. set { _authorization_info = value; }
  80. }
  81. int _AuthorizeStatus = 0;
  82. /// <summary>
  83. /// 授权状态
  84. /// </summary>
  85. public int AuthorizeStatus
  86. {
  87. get { return _AuthorizeStatus; }
  88. set { _AuthorizeStatus = value; }
  89. }
  90. /// <summary>
  91. /// 刷新授权公众号的令牌时的事件
  92. /// </summary>
  93. public event EventEventAuthorizerRefreshTokenHandler EventEvent_AuthorizerRefreshToken;
  94. string _authorizer_access_token = "";
  95. /// <summary>
  96. /// 授权令牌
  97. /// </summary>
  98. public string Authorizer_access_token
  99. {
  100. get
  101. {
  102. //如果为空,或者过期,需要重新获取
  103. if (string.IsNullOrEmpty(_authorizer_access_token) || HasExpired())
  104. {
  105. //获取
  106. LYFZ.WXLibrary.CommonHandleClass.WriteLog("开始请求获取新的有效Authorizer_access_token令牌", "-2");
  107. _authorizer_access_token = GetAuthorizer_AccessToken(OpenPlatformConfig.OpenAppID, OpenPlatformConfig.Component_Access_Token, Authorizer_appid, Authorizer_refresh_token);
  108. }
  109. return _authorizer_access_token;
  110. }
  111. set { _authorizer_access_token = value; }
  112. }
  113. int _expires_in = 7200;
  114. /// <summary>
  115. /// 令牌有效时长
  116. /// </summary>
  117. public int Expires_in
  118. {
  119. get { return _expires_in; }
  120. set { _expires_in = value; }
  121. }
  122. DateTime _RefreshTokenTime = DateTime.MinValue;
  123. /// <summary>
  124. /// 获取(刷新)令牌时的时间
  125. /// </summary>
  126. public DateTime RefreshTokenTime
  127. {
  128. get { return _RefreshTokenTime; }
  129. set { _RefreshTokenTime = value; }
  130. }
  131. string _authorizer_refresh_token = "";
  132. /// <summary>
  133. /// 刷新Authorizer_access_token 过期令牌时要用的刷新令牌
  134. /// </summary>
  135. public string Authorizer_refresh_token
  136. {
  137. get { return _authorizer_refresh_token; }
  138. set { _authorizer_refresh_token = value; }
  139. }
  140. #endregion
  141. /// <summary>
  142. /// 清除Authorizer_access_token
  143. /// </summary>
  144. public void ClearAccessToken()
  145. {
  146. _authorizer_access_token = "";
  147. }
  148. /// <summary>
  149. /// 获取授权公众号的令牌
  150. /// </summary>
  151. /// <param name="appId"></param>
  152. /// <param name="appSecret"></param>
  153. /// <returns></returns>
  154. private string GetAuthorizer_AccessToken(
  155. string appId,
  156. string component_access_token,
  157. string authorizer_appid,
  158. string authorizer_refresh_token
  159. )
  160. {
  161. try
  162. {
  163. Dictionary<string, string> dic = new Dictionary<string, string>
  164. {
  165. { "component_appid",appId},
  166. { "authorizer_appid",authorizer_appid},
  167. { "authorizer_refresh_token",authorizer_refresh_token}
  168. };
  169. string json = (new System.Web.Script.Serialization.JavaScriptSerializer()).Serialize(dic);
  170. //获取access_token
  171. string retmsg = "";
  172. var retObj = LYFZ.Weixin.SDK.BasicAPI.GetAuthorizer_Refresh_Token(component_access_token, json, out retmsg);
  173. LYFZ.WXLibrary.CommonHandleClass.WriteLog("请求获取新的授权公众号的令牌返回信息:" + retmsg, "-2");
  174. if (retObj != null)
  175. {
  176. string retAuthorizer_AccessToken = retObj.authorizer_access_token.Trim();
  177. if (retAuthorizer_AccessToken.Trim().Length > 0)
  178. {
  179. _RefreshTokenTime = DateTime.Now;
  180. Expires_in = Convert.ToInt32(retObj.expires_in);
  181. _authorizer_refresh_token = retObj.authorizer_refresh_token.Trim();
  182. if (EventEvent_AuthorizerRefreshToken != null)
  183. {
  184. EventEvent_AuthorizerRefreshToken(new EventAuthorizerRefreshToken(appId, retAuthorizer_AccessToken, Expires_in, _authorizer_refresh_token, _RefreshTokenTime));
  185. }
  186. return retAuthorizer_AccessToken;
  187. }
  188. else
  189. {
  190. _RefreshTokenTime = DateTime.MinValue;
  191. }
  192. }
  193. else
  194. {
  195. _RefreshTokenTime = DateTime.MinValue;
  196. LYFZ.WXLibrary.CommonHandleClass.WriteLog("获取授权Appid:" + authorizer_appid + "的Access_Token令牌时出错:" + retmsg, "-2");
  197. }
  198. }
  199. catch (Exception ex)
  200. {
  201. _RefreshTokenTime = DateTime.MinValue;
  202. LYFZ.WXLibrary.CommonHandleClass.WriteLog("获取授权Appid:" + authorizer_appid + "的Access_Token令牌时出错(Exception):" + ex.Message, "-2");
  203. }
  204. return "";
  205. }
  206. /// <summary>
  207. /// 判断Access_token是否过期
  208. /// </summary>
  209. /// <returns>bool</returns>
  210. private bool HasExpired()
  211. {
  212. if (_RefreshTokenTime != null)
  213. {
  214. //过期时间,允许有一定的误差,五分钟。获取时间消耗
  215. if (DateTime.Now > _RefreshTokenTime.AddSeconds(Expires_in).AddSeconds(-300))
  216. {
  217. return true;
  218. }
  219. }
  220. return false;
  221. }
  222. AccountBasicInfo _Authorizer_AccountBasicInfo = new AccountBasicInfo();
  223. /// <summary>
  224. /// 当前授权公众号的帐号基本信息
  225. /// </summary>
  226. public AccountBasicInfo Authorizer_AccountBasicInfo
  227. {
  228. get { return _Authorizer_AccountBasicInfo; }
  229. set { _Authorizer_AccountBasicInfo = value; }
  230. }
  231. int _AccountBasicInfo_expires_in = 7200;
  232. /// <summary>
  233. /// 帐号基本信息有效时长
  234. /// </summary>
  235. public int AccountBasicInfo_expires_in
  236. {
  237. get { return _AccountBasicInfo_expires_in; }
  238. set { _AccountBasicInfo_expires_in = value; }
  239. }
  240. DateTime _RefreshAccountBasicInfoTime = DateTime.Now.AddDays(-2);
  241. /// <summary>
  242. /// 获取(刷新)帐号基本信息时的时间
  243. /// </summary>
  244. public DateTime RefreshAccountBasicInfoTime
  245. {
  246. get { return _RefreshAccountBasicInfoTime; }
  247. set { _RefreshAccountBasicInfoTime = value; }
  248. }
  249. }
  250. /// <summary>
  251. ///
  252. /// </summary>
  253. public class EventAuthorizerRefreshToken : EventArgs
  254. {
  255. public EventAuthorizerRefreshToken(string appid, string authorizer_access_token, int expires_in, string authorizer_refresh_token, DateTime refreshTokenTime)
  256. {
  257. this._appid = appid;
  258. this._authorizer_access_token = authorizer_access_token;
  259. this._authorizer_refresh_token = authorizer_refresh_token;
  260. this._expires_in = expires_in;
  261. this._RefreshTokenTime = refreshTokenTime;
  262. }
  263. string _appid = "";
  264. public string Appid
  265. {
  266. get { return _appid; }
  267. set { _appid = value; }
  268. }
  269. string _authorizer_access_token = "";
  270. public string Authorizer_access_token
  271. {
  272. get { return _authorizer_access_token; }
  273. set { _authorizer_access_token = value; }
  274. }
  275. int _expires_in = 0;
  276. public int Expires_in
  277. {
  278. get { return _expires_in; }
  279. set { _expires_in = value; }
  280. }
  281. string _authorizer_refresh_token = "";
  282. public string Authorizer_refresh_token
  283. {
  284. get { return _authorizer_refresh_token; }
  285. set { _authorizer_refresh_token = value; }
  286. }
  287. bool isRefreshSuccess = false;
  288. /// <summary>
  289. /// 是否刷新成功
  290. /// </summary>
  291. public bool IsRefreshSuccess
  292. {
  293. get { return isRefreshSuccess; }
  294. set { isRefreshSuccess = value; }
  295. }
  296. DateTime _RefreshTokenTime = DateTime.MinValue;
  297. /// <summary>
  298. ///
  299. /// </summary>
  300. public DateTime RefreshTokenTime
  301. {
  302. get { return _RefreshTokenTime; }
  303. set { _RefreshTokenTime = value; }
  304. }
  305. }
  306. /// <summary>
  307. /// 帐号基本信息
  308. /// </summary>
  309. public class AccountBasicInfo
  310. {
  311. public AccountBasicInfo(string jsonData = "")
  312. {
  313. }
  314. string _nick_name = "";
  315. /// <summary>
  316. /// 授权方昵称
  317. /// </summary>
  318. public string Nick_name
  319. {
  320. get { return _nick_name; }
  321. set { _nick_name = value; }
  322. }
  323. string _head_img = "";
  324. /// <summary>
  325. /// 授权方头像
  326. /// </summary>
  327. public string Head_img
  328. {
  329. get { return _head_img; }
  330. set { _head_img = value; }
  331. }
  332. int _service_type_info = 0;
  333. /// <summary>
  334. /// 授权方公众号类型,0代表订阅号,1代表由历史老帐号升级后的订阅号,2代表服务号
  335. /// </summary>
  336. public int Service_type_info
  337. {
  338. get { return _service_type_info; }
  339. set { _service_type_info = value; }
  340. }
  341. int _verify_type_inf = -1;
  342. /// <summary>
  343. /// 授权方认证类型,
  344. /// -1代表未认证,
  345. /// 0代表微信认证,
  346. /// 1代表新浪微博认证,
  347. /// 2代表腾讯微博认证,
  348. /// 3代表已资质认证通过但还未通过名称认证,
  349. /// 4代表已资质认证通过、还未通过名称认证,但通过了新浪微博认证,
  350. /// 5代表已资质认证通过、还未通过名称认证,但通过了腾讯微博认证
  351. /// </summary>
  352. public int Verify_type_inf
  353. {
  354. get { return _verify_type_inf; }
  355. set { _verify_type_inf = value; }
  356. }
  357. string _user_name = "";
  358. /// <summary>
  359. /// 授权方公众号的原始ID
  360. /// </summary>
  361. public string User_name
  362. {
  363. get { return _user_name; }
  364. set { _user_name = value; }
  365. }
  366. string _alias = "";
  367. /// <summary>
  368. /// 授权方公众号所设置的微信号,可能为空
  369. /// </summary>
  370. public string Alias
  371. {
  372. get { return _alias; }
  373. set { _alias = value; }
  374. }
  375. //以下信息暂不使用
  376. //string _business_info=
  377. // 用以了解以下功能的开通状况(0代表未开通,1代表已开通):
  378. // open_store:是否开通微信门店功能
  379. // open_scan:是否开通微信扫商品功能
  380. // open_pay:是否开通微信支付功能
  381. // open_card:是否开通微信卡券功能
  382. // open_shake:是否开通微信摇一摇功能
  383. // qrcode_url 二维码图片的URL,开发者最好自行也进行保存
  384. //authorization_info 授权信息
  385. //appid 授权方appid
  386. //func_info 公众号授权给开发者的权限集列表,ID为1到15时分别代表:
  387. //消息管理权限
  388. //用户管理权限
  389. //帐号服务权限
  390. //网页服务权限
  391. //微信小店权限
  392. //微信多客服权限
  393. //群发与通知权限
  394. //微信卡券权限
  395. //微信扫一扫权限
  396. //微信连WIFI权限
  397. //素材管理权限
  398. //微信摇周边权限
  399. //微信门店权限
  400. //微信支付权限
  401. //自定义菜单权限
  402. }
  403. }