AuthorizedAPPIDHandle.cs 15 KB

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