/// <summary> /// //更新access_token值 /// </summary> /// <param name="_id"></param> /// <param name="AppId"></param> /// <param name="AppSecret"></param> private void UpdateAccess_Token(int _id, string AppId, string AppSecret) { try { wx_property_info pBll = new wx_property_info(); if (!pBll.ExistsWid(_id)) { return; } string newToken = ""; try { var result = Senparc.Weixin.MP.CommonAPIs.CommonApi.GetToken(AppId, AppSecret); newToken = result.access_token; } catch (Exception ex) { JscriptMsg("AppId或者AppSecret填写错误!", "", "Error"); } finally { //更新到数据库里 MxWeiXinPF.Model.wx_property_info wxProperty = pBll.GetModelList("iName='access_token' and wid=" + _id)[0]; wxProperty.iContent = newToken; wxProperty.createDate = DateTime.Now; pBll.Update(wxProperty); } } catch (Exception ex) { } }
private void ShowInfo() { Model.wx_userweixin weixin = GetWeiXinCode(); lblWeixinName.Text = weixin.wxName; lblAppid.Text = weixin.AppId; BLL.wx_payment_wxpay wxpayBll = new wx_payment_wxpay(); Model.wx_payment_wxpay model = wxpayBll.GetModelByWid(weixin.id); if (model == null || model.id == 0) { //新增记录 } else { //修改 hidId.Value = model.id.ToString(); this.txtmch_id.Text = model.mch_id; this.txtpaykey.Text = model.paykey; this.txtcertInfoPath.Text = model.certInfoPath; this.txtcerInfoPwd.Text = model.cerInfoPwd; } BLL.wx_property_info propertyBll = new wx_property_info(); Model.wx_property_info propertyEntity = propertyBll.GetModelByIName(weixin.id, MXEnums.WXPropertyKeyName.OpenOauth.ToString()); if (propertyEntity != null) { radOpenOAuth.SelectedValue = propertyEntity.iContent; hidOpenOauthId.ID = propertyEntity.id.ToString(); } }
//保存 protected void btnSubmit_Click(object sender, EventArgs e) { BLL.wx_payment_wxpay wxpayBll = new wx_payment_wxpay(); int id = MyCommFun.Obj2Int(hidId.Value, 0); Model.wx_payment_wxpay wxpayModel = new Model.wx_payment_wxpay(); Model.wx_userweixin weixin = GetWeiXinCode(); if (id == 0) { //新增 wxpayModel.wid = weixin.id; wxpayModel.createDate = DateTime.Now; } else { //修改 wxpayModel = wxpayBll.GetModel(id); } wxpayModel.mch_id = txtmch_id.Text.Trim(); wxpayModel.paykey = txtpaykey.Text.Trim(); wxpayModel.certInfoPath = txtcertInfoPath.Text.Trim(); wxpayModel.cerInfoPwd = txtcerInfoPwd.Text.Trim(); bool ret = false; if (id == 0) { wxpayModel.createDate = DateTime.Now; int retNum = wxpayBll.Add(wxpayModel); if (retNum > 0) { ret = true; } } else { ret = wxpayBll.Update(wxpayModel); } //OpenOAuth开启 BLL.wx_property_info propertyBll = new wx_property_info(); string pValue = radOpenOAuth.SelectedItem.Value; propertyBll.AddProperty(weixin.id, MXEnums.WXPropertyKeyName.OpenOauth.ToString(), pValue); if (ret) { JscriptMsg("修改信息成功!", "baseSetting.aspx", "Success"); } else { JscriptMsg("修改信息成功!", "", "Error"); return; } }
/// <summary> ///【强制刷新】access_token值 /// access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒, /// 重复获取将导致上次获取的access_token失效。 /// 每日限额获取access_token.我们将access_token保存到数据库里,间隔时间为20分钟,从微信公众平台获得一次。 /// </summary> /// <returns></returns> public static string FlushAccessToken(int wid, out string error) { string token = ""; error = ""; try { wx_property_info pBll = new wx_property_info(); BLL.wx_userweixin wBll = new wx_userweixin(); Model.wx_userweixin weixininfo = wBll.GetModel(wid); if (weixininfo.AppId == null || weixininfo.AppSecret == null || weixininfo.AppId.Trim().Length <= 0 || weixininfo.AppSecret.Trim().Length <= 0) { error = "appId或者AppSecret未填写完全,请在[我的公众帐号]里补全信息!"; return(""); } var result = CommonApi.GetToken(weixininfo.AppId, weixininfo.AppSecret); token = result.access_token; //第一次插入微信属性表 if (!pBll.ExistsWid(wid, "access_token")) { //插入 pBll.AddAccess_Token(wid, token, result.expires_in); } else { WeiXinPF.Model.wx_property_info wxProperty = new WeiXinPF.Model.wx_property_info(); wxProperty = pBll.GetModelList("iName='access_token' and wid=" + wid)[0]; //更新到数据库里 wxProperty.iContent = token; wxProperty.createDate = DateTime.Now; wxProperty.expires_in = result.expires_in; pBll.Update(wxProperty); } } catch (Exception ex) { error = "获得access_token出错:" + ex.Message; WXLogs.AddLog(wid, "access_token", "获得access_token(FlushAccessToken)", error); } return(token); }
/// <summary> /// 及时获得access_token值 /// access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒, /// 重复获取将导致上次获取的access_token失效。 /// 每日限额获取access_token.我们将access_token保存到数据库里,间隔时间为20分钟,从微信公众平台获得一次。 /// </summary> /// <returns></returns> public static string getAccessToken(int wid, out string error) { string token = ""; error = ""; try { wx_property_info pBll = new wx_property_info(); BLL.wx_userweixin wBll = new wx_userweixin(); Model.wx_userweixin weixininfo = wBll.GetModel(wid); if (weixininfo.AppId == null || weixininfo.AppSecret == null || weixininfo.AppId.Trim().Length <= 0 || weixininfo.AppSecret.Trim().Length <= 0) { error = "appId或者AppSecret未填写完全,请在[我的公众帐号]里补全信息!"; WXLogs.AddLog(wid, "access_token", "获得access_token", error); return(""); } WeiXinPF.Model.wx_property_info wxProperty = new WeiXinPF.Model.wx_property_info(); //第一次插入微信属性表 if (!pBll.ExistsWid(wid, "access_token")) { AccessTokenResult result = CommonApi.GetToken(weixininfo.AppId, weixininfo.AppSecret); token = result.access_token; pBll.AddAccess_Token(wid, token, result.expires_in); return(token); //WeChatAccountManager manager = WeChatAccountManager.CreateInstance(weixininfo.AppId, weixininfo.AppSecret); //Credential credential = new Credential(manager); //token = credential.AccessToken; //pBll.AddAccess_Token(wid, token, 7000); //return token; } wxProperty = pBll.GetModelList("iName='access_token' and wid=" + wid)[0]; double chajunSecond = (DateTime.Now - wxProperty.createDate.Value).TotalSeconds; if (chajunSecond >= wxProperty.expires_in) { //从微信平台重新获得access_token AccessTokenResult result = CommonApi.GetToken(weixininfo.AppId, weixininfo.AppSecret); token = result.access_token; //更新到数据库里 wxProperty.iContent = token; wxProperty.createDate = DateTime.Now; wxProperty.expires_in = result.expires_in; //WeChatAccountManager manager = WeChatAccountManager.CreateInstance(weixininfo.AppId, weixininfo.AppSecret); //Credential credential = new Credential(manager); //token = credential.AccessToken; ////更新到数据库里 //wxProperty.iContent = token; //wxProperty.createDate = DateTime.Now; //wxProperty.expires_in = 7000; //pBll.Update(wxProperty); } else { token = wxProperty.iContent; } } catch (Exception ex) { error = "获得access_token出错:" + ex.Message; WXLogs.AddLog(wid, "access_token", "获得access_token", error); } return(token); }
/// <summary> /// 获取jssdk 里的临时票据 /// </summary> /// <param name="wid"></param> /// <param name="error"></param> /// <returns></returns> public static string getJsApiTicket(int wid, out string error) { string atErr = ""; string accessToken = getAccessToken(wid, out atErr); if (atErr != "") { accessToken = FlushAccessToken(wid, out atErr); } if (accessToken == "") { error = "取accessToken值出现异常"; WXLogs.AddLog(wid, "getJsApiTicket", "获得getJsApiTicket", "WeiXinPF.WeiXinComm.getJsApiTicket" + error); return(""); } string token = ""; error = ""; try { wx_property_info pBll = new wx_property_info(); BLL.wx_userweixin wBll = new wx_userweixin(); Model.wx_userweixin weixininfo = wBll.GetModel(wid); if (weixininfo.AppId == null || weixininfo.AppSecret == null || weixininfo.AppId.Trim().Length <= 0 || weixininfo.AppSecret.Trim().Length <= 0) { error = "appId或者AppSecret未填写完全,请在[我的公众帐号]里补全信息!"; return(""); } WeiXinPF.Model.wx_property_info wxProperty = new WeiXinPF.Model.wx_property_info(); //第一次插入微信属性表 if (!pBll.ExistsWid(wid, "JsApiTicket")) { string type = "jsapi"; var url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type={1}", accessToken, type); JsApiTicketResult result = OneGulp.WeChat.HttpUtility.Get.GetJson <JsApiTicketResult>(url); token = result.ticket; //存入属性表 wxProperty.wid = wid; wxProperty.iName = "JsApiTicket"; wxProperty.iContent = token; wxProperty.createDate = DateTime.Now; wxProperty.expires_in = result.expires_in; wxProperty.count = 1; pBll.Add(wxProperty); return(token); } wxProperty = pBll.GetModelList("iName='JsApiTicket' and wid=" + wid)[0]; double chajunSecond = (DateTime.Now - wxProperty.createDate.Value).TotalSeconds; if (chajunSecond >= wxProperty.expires_in) { //从微信平台重新获得access_token string type = "jsapi"; var url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type={1}", accessToken, type); JsApiTicketResult result = OneGulp.WeChat.HttpUtility.Get.GetJson <JsApiTicketResult>(url); token = result.ticket; //更新到数据库里 wxProperty.iContent = token; wxProperty.createDate = DateTime.Now; wxProperty.expires_in = result.expires_in; pBll.Update(wxProperty); } else { token = wxProperty.iContent; } } catch (Exception ex) { error = "获得getJsApiTicket出错:" + ex.Message; WXLogs.AddLog(wid, "getJsApiTicket", "获得getJsApiTicket", "WeiXinPF.WeiXinComm.getJsApiTicket" + error); } return(token); }