public Model_9H.RESTfulModel Get(string authorizerAppID) { try { AuthorizationInfoModel authorizationInfoModel = authorizationInfoDAL.GetModel(authorizerAppID); if (authorizationInfoModel != null) { int timestamp = (int)((DateTime.Now - authorizationInfoModel.UpdateTime).TotalMinutes); if (timestamp >= 110) { ConfigModel configModel = configDAL.GetModel("component_access_token"); string url_5 = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=" + configModel.Value; LogHelper.Info("5、获取(刷新)授权公众号的接口调用凭据(令牌) url_5", url_5); AuthorizationInfoModel authorizationInfoModel2 = Refresh(authorizationInfoModel.AuthorizerAppID, authorizationInfoModel.AuthorizerAccessToken, authorizationInfoModel.AuthorizerRefreshToken, configModel.Value); authorizationInfoModel2.AuthorizerAccessTokenOld = authorizationInfoModel.AuthorizerAccessToken; return(new RESTfulModel() { Code = (int)CodeEnum.成功, Msg = string.Format(codeMsgDAL.GetByCode((int)CodeEnum.成功), "成功"), Data = authorizationInfoModel2 }); } else { return(new RESTfulModel() { Code = (int)CodeEnum.成功, Msg = string.Format(codeMsgDAL.GetByCode((int)CodeEnum.成功), "成功"), Data = authorizationInfoModel }); } } else { return(new RESTfulModel() { Code = (int)CodeEnum.用户未授权, Msg = codeMsgDAL.GetByCode((int)CodeEnum.用户未授权) }); } } catch (Exception ex) { LogHelper.Error(ex); return(new RESTfulModel() { Code = (int)CodeEnum.系统异常, Msg = codeMsgDAL.GetByCode((int)CodeEnum.系统异常) }); } }
public RESTfulModel Get() { try { ConfigModel configModel = configDAL.GetModel("component_access_token"); return(new RESTfulModel() { Code = (int)CodeEnum.成功, Msg = string.Format(codeMsgDAL.GetByCode((int)CodeEnum.成功), "成功"), Data = configModel.Value }); } catch (Exception ex) { LogHelper.Error(ex); return(new RESTfulModel() { Code = (int)CodeEnum.系统异常, Msg = codeMsgDAL.GetByCode((int)CodeEnum.系统异常) }); } }
public string Receive(string requestBody_Cipher) { try { // AppID和EncodingAESKey登录微信开放平台可见,通过中控器保证AppSecret安全 string componentAppID = ConfigHelper.ComponentAppID; string componentAppSecret = ConfigHelper.ComponentAppSecret; string encodingAESKey = ConfigHelper.EncodingAESKey; #region 1、推送component_verify_ticket协议 // 1、推送component_verify_ticket协议 //Stream requestStream = Stream.Null; //HttpContent content = request.Content; //Task readTask = content.ReadAsStreamAsync().ContinueWith((task) => { requestStream = task.Result; }); //readTask.Wait(); //string requestBody_Cipher = string.Empty; //using (StreamReader reader = new StreamReader(requestStream)) //{ // requestBody_Cipher = reader.ReadToEnd(); //} // 记录requestBody_Cipher(XML格式) LogHelper.Info("1、推送component_verify_ticket协议 requestBody_Cipher", requestBody_Cipher); XmlNode root_Cipher = XmlHelper.Deserialize(requestBody_Cipher); string encrypt_Cipher = root_Cipher["Encrypt"].InnerText; string requestBody = Tencent.Cryptography.AES_decrypt(encrypt_Cipher, encodingAESKey, ref componentAppID); // 记录requestBody(XML格式) LogHelper.Info("1、推送component_verify_ticket协议 requestBody", requestBody); XmlNode root = XmlHelper.Deserialize(requestBody); string createTime = root["CreateTime"].InnerText; string infoType = root["InfoType"].InnerText; #endregion if (infoType == "component_verify_ticket") { #region component_verify_ticket string componentVerifyTicket = root["ComponentVerifyTicket"].InnerText; // component_verify_ticket 每次更新, component_verify_ticket、component_access_token 的 update_time 设置为同一个值,可以减少数据库查询两次 DateTime dt = new DateTime(1970, 1, 1, 8, 0, 0, DateTimeKind.Local); DateTime updateTime = dt.AddSeconds(createTime.ToLong()); configDAL.Update("component_verify_ticket", componentVerifyTicket, updateTime); ConfigModel configModel = configDAL.GetModel("component_access_token"); int timestamp = (int)((DateTime.Now - configModel.UpdateTime).TotalMinutes); if (timestamp >= 110) { #region 2、获取第三方平台component_access_token // 2、获取第三方平台component_access_token string url_2 = "https://api.weixin.qq.com/cgi-bin/component/api_component_token"; LogHelper.Info("2、获取第三方平台component_access_token url_2", url_2); ComponentAccessTokenGetReq req_2 = new ComponentAccessTokenGetReq(); req_2.ComponentAppID = componentAppID; req_2.ComponentAppSecret = componentAppSecret; req_2.ComponentVerifyTicket = componentVerifyTicket; string requestBody_2 = JsonConvert.SerializeObject(req_2); LogHelper.Info("2、获取第三方平台component_access_token requestBody_2", requestBody_2); string responseBody_2 = HttpHelper.Post(url_2, requestBody_2); LogHelper.Info("2、获取第三方平台component_access_token responseBody_2", responseBody_2); ComponentAccessTokenGetResp resp_2 = JsonConvert.DeserializeObject <ComponentAccessTokenGetResp>(responseBody_2); // component_access_token每隔1小时50分钟更新一次 configDAL.Update("component_access_token", resp_2.ComponentAccessToken, updateTime); #endregion } #endregion } else if (infoType == "authorized") { string authorizerAppid = root["AuthorizerAppid"].InnerText; string authorizationCode = root["AuthorizationCode"].InnerText; string authorizationCodeExpiredTime = root["AuthorizationCodeExpiredTime"].InnerText; } else if (infoType == "updateauthorized") { string authorizerAppid = root["AuthorizerAppid"].InnerText; string authorizationCode = root["AuthorizationCode"].InnerText; string authorizationCodeExpiredTime = root["AuthorizationCodeExpiredTime"].InnerText; } else if (infoType == "unauthorized") { string authorizerAppid = root["AuthorizerAppid"].InnerText; } return("success"); } catch (Exception ex) { LogHelper.Error(ex); return("exception"); } }