/// <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) { } }
/// <summary> /// 得到一个对象实体 /// </summary> public MxWeiXinPF.Model.wx_property_info DataRowToModel(DataRow row) { MxWeiXinPF.Model.wx_property_info model=new MxWeiXinPF.Model.wx_property_info(); if (row != null) { if(row["id"]!=null && row["id"].ToString()!="") { model.id=int.Parse(row["id"].ToString()); } if(row["wid"]!=null && row["wid"].ToString()!="") { model.wid=int.Parse(row["wid"].ToString()); } if(row["typeId"]!=null && row["typeId"].ToString()!="") { model.typeId=int.Parse(row["typeId"].ToString()); } if(row["typeName"]!=null) { model.typeName=row["typeName"].ToString(); } if(row["iName"]!=null) { model.iName=row["iName"].ToString(); } if(row["iContent"]!=null) { model.iContent=row["iContent"].ToString(); } if(row["expires_in"]!=null && row["expires_in"].ToString()!="") { model.expires_in=int.Parse(row["expires_in"].ToString()); } if(row["createDate"]!=null && row["createDate"].ToString()!="") { model.createDate=DateTime.Parse(row["createDate"].ToString()); } if(row["count"]!=null && row["count"].ToString()!="") { model.count=int.Parse(row["count"].ToString()); } if(row["categoryId"]!=null && row["categoryId"].ToString()!="") { model.categoryId=int.Parse(row["categoryId"].ToString()); } if(row["categoryName"]!=null) { model.categoryName=row["categoryName"].ToString(); } if(row["remark"]!=null) { model.remark=row["remark"].ToString(); } } return model; }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(MxWeiXinPF.Model.wx_property_info model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update wx_property_info set "); strSql.Append("wid=@wid,"); strSql.Append("typeId=@typeId,"); strSql.Append("typeName=@typeName,"); strSql.Append("iName=@iName,"); strSql.Append("iContent=@iContent,"); strSql.Append("expires_in=@expires_in,"); strSql.Append("createDate=@createDate,"); strSql.Append("count=@count,"); strSql.Append("categoryId=@categoryId,"); strSql.Append("categoryName=@categoryName,"); strSql.Append("remark=@remark"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@wid", SqlDbType.Int, 4), new SqlParameter("@typeId", SqlDbType.Int, 4), new SqlParameter("@typeName", SqlDbType.VarChar, 100), new SqlParameter("@iName", SqlDbType.VarChar, 200), new SqlParameter("@iContent", SqlDbType.VarChar, -1), new SqlParameter("@expires_in", SqlDbType.Int, 4), new SqlParameter("@createDate", SqlDbType.DateTime), new SqlParameter("@count", SqlDbType.Int, 4), new SqlParameter("@categoryId", SqlDbType.Int, 4), new SqlParameter("@categoryName", SqlDbType.VarChar, 50), new SqlParameter("@remark", SqlDbType.VarChar, 1000), new SqlParameter("@id", SqlDbType.Int, 4) }; parameters[0].Value = model.wid; parameters[1].Value = model.typeId; parameters[2].Value = model.typeName; parameters[3].Value = model.iName; parameters[4].Value = model.iContent; parameters[5].Value = model.expires_in; parameters[6].Value = model.createDate; parameters[7].Value = model.count; parameters[8].Value = model.categoryId; parameters[9].Value = model.categoryName; parameters[10].Value = model.remark; parameters[11].Value = model.id; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return(true); } else { return(false); } }
/// <summary> /// 得到一个对象实体 /// </summary> public MxWeiXinPF.Model.wx_property_info DataRowToModel(DataRow row) { MxWeiXinPF.Model.wx_property_info model = new MxWeiXinPF.Model.wx_property_info(); if (row != null) { if (row["id"] != null && row["id"].ToString() != "") { model.id = int.Parse(row["id"].ToString()); } if (row["wid"] != null && row["wid"].ToString() != "") { model.wid = int.Parse(row["wid"].ToString()); } if (row["typeId"] != null && row["typeId"].ToString() != "") { model.typeId = int.Parse(row["typeId"].ToString()); } if (row["typeName"] != null) { model.typeName = row["typeName"].ToString(); } if (row["iName"] != null) { model.iName = row["iName"].ToString(); } if (row["iContent"] != null) { model.iContent = row["iContent"].ToString(); } if (row["expires_in"] != null && row["expires_in"].ToString() != "") { model.expires_in = int.Parse(row["expires_in"].ToString()); } if (row["createDate"] != null && row["createDate"].ToString() != "") { model.createDate = DateTime.Parse(row["createDate"].ToString()); } if (row["count"] != null && row["count"].ToString() != "") { model.count = int.Parse(row["count"].ToString()); } if (row["categoryId"] != null && row["categoryId"].ToString() != "") { model.categoryId = int.Parse(row["categoryId"].ToString()); } if (row["categoryName"] != null) { model.categoryName = row["categoryName"].ToString(); } if (row["remark"] != null) { model.remark = row["remark"].ToString(); } } return(model); }
/// <summary> /// 及时获得access_token值 /// access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒, /// 重复获取将导致上次获取的access_token失效。 /// 每日限额获取access_token.我们将access_token保存到数据库里,间隔时间为20分钟,从微信公众平台获得一次。 /// </summary> /// <returns></returns> public string getAccessToken(int wid, out string error) { string token = ""; error = ""; try { 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(""); } MxWeiXinPF.Model.wx_property_info wxProperty = new MxWeiXinPF.Model.wx_property_info(); //第一次插入微信属性表 if (!pBll.ExistsWid(wid)) { var result = Senparc.Weixin.MP.CommonAPIs.CommonApi.GetToken(weixininfo.AppId, weixininfo.AppSecret); token = result.access_token; pBll.AddAccess_Token(wid, token); return(token); } wxProperty = pBll.GetModelList("iName='access_token' and wid=" + wid)[0]; TimeSpan chajun = DateTime.Now - wxProperty.createDate.Value; double chajunSecond = chajun.TotalSeconds; if (wxProperty.iContent == null || wxProperty.iContent.Trim() == "" || chajunSecond >= wxProperty.expires_in) { //从微信平台重新获得access_token var result = Senparc.Weixin.MP.CommonAPIs.CommonApi.GetToken(weixininfo.AppId, weixininfo.AppSecret); token = result.access_token; //更新到数据库里 wxProperty.iContent = token; wxProperty.createDate = DateTime.Now; pBll.Update(wxProperty); } else { token = wxProperty.iContent; } } catch (Exception ex) { error = "获得access_token出错:" + ex.Message; } return(token); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(MxWeiXinPF.Model.wx_property_info model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into wx_property_info("); strSql.Append("wid,typeId,typeName,iName,iContent,expires_in,createDate,count,categoryId,categoryName,remark)"); strSql.Append(" values ("); strSql.Append("@wid,@typeId,@typeName,@iName,@iContent,@expires_in,@createDate,@count,@categoryId,@categoryName,@remark)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@wid", SqlDbType.Int, 4), new SqlParameter("@typeId", SqlDbType.Int, 4), new SqlParameter("@typeName", SqlDbType.VarChar, 100), new SqlParameter("@iName", SqlDbType.VarChar, 200), new SqlParameter("@iContent", SqlDbType.VarChar, -1), new SqlParameter("@expires_in", SqlDbType.Int, 4), new SqlParameter("@createDate", SqlDbType.DateTime), new SqlParameter("@count", SqlDbType.Int, 4), new SqlParameter("@categoryId", SqlDbType.Int, 4), new SqlParameter("@categoryName", SqlDbType.VarChar, 50), new SqlParameter("@remark", SqlDbType.VarChar, 1000) }; parameters[0].Value = model.wid; parameters[1].Value = model.typeId; parameters[2].Value = model.typeName; parameters[3].Value = model.iName; parameters[4].Value = model.iContent; parameters[5].Value = model.expires_in; parameters[6].Value = model.createDate; parameters[7].Value = model.count; parameters[8].Value = model.categoryId; parameters[9].Value = model.categoryName; parameters[10].Value = model.remark; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }
/// <summary> ///【强制刷新】access_token值 /// access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒, /// 重复获取将导致上次获取的access_token失效。 /// 每日限额获取access_token.我们将access_token保存到数据库里,间隔时间为20分钟,从微信公众平台获得一次。 /// </summary> /// <returns></returns> public string FlushAccessToken(int wid,out string error) { string token = ""; error = ""; try { 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 = Senparc.Weixin.MP.CommonAPIs.CommonApi.GetToken(weixininfo.AppId, weixininfo.AppSecret); token = result.access_token; //第一次插入微信属性表 if (!pBll.ExistsWid(wid)) { //插入 pBll.AddAccess_Token(wid, token); } else { MxWeiXinPF.Model.wx_property_info wxProperty = new MxWeiXinPF.Model.wx_property_info(); wxProperty = pBll.GetModelList("iName='access_token' and wid=" + wid)[0]; //更新到数据库里 wxProperty.iContent = token; wxProperty.createDate = DateTime.Now; pBll.Update(wxProperty); } } catch (Exception ex) { error = "获得access_token出错:" + ex.Message; } return token; }
/// <summary> ///【强制刷新】access_token值 /// access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒, /// 重复获取将导致上次获取的access_token失效。 /// 每日限额获取access_token.我们将access_token保存到数据库里,间隔时间为20分钟,从微信公众平台获得一次。 /// </summary> /// <returns></returns> public string FlushAccessToken(int wid, out string error) { string token = ""; error = ""; try { 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 = Senparc.Weixin.MP.CommonAPIs.CommonApi.GetToken(weixininfo.AppId, weixininfo.AppSecret); token = result.access_token; //第一次插入微信属性表 if (!pBll.ExistsWid(wid)) { //插入 pBll.AddAccess_Token(wid, token); } else { MxWeiXinPF.Model.wx_property_info wxProperty = new MxWeiXinPF.Model.wx_property_info(); wxProperty = pBll.GetModelList("iName='access_token' and wid=" + wid)[0]; //更新到数据库里 wxProperty.iContent = token; wxProperty.createDate = DateTime.Now; pBll.Update(wxProperty); } } catch (Exception ex) { error = "获得access_token出错:" + ex.Message; } return(token); }
/// <summary> /// 得到一个对象实体 /// </summary> public MxWeiXinPF.Model.wx_property_info GetModel(int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 id,wid,typeId,typeName,iName,iContent,expires_in,createDate,count,categoryId,categoryName,remark from wx_property_info "); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int, 4) }; parameters[0].Value = id; MxWeiXinPF.Model.wx_property_info model = new MxWeiXinPF.Model.wx_property_info(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }
/// <summary> /// 得到一个对象实体 /// </summary> public MxWeiXinPF.Model.wx_property_info GetModel(int id) { StringBuilder strSql=new StringBuilder(); strSql.Append("select top 1 id,wid,typeId,typeName,iName,iContent,expires_in,createDate,count,categoryId,categoryName,remark from wx_property_info "); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int,4) }; parameters[0].Value = id; MxWeiXinPF.Model.wx_property_info model=new MxWeiXinPF.Model.wx_property_info(); DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters); if(ds.Tables[0].Rows.Count>0) { return DataRowToModel(ds.Tables[0].Rows[0]); } else { return null; } }
/// <summary> /// 及时获得access_token值 /// access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒, /// 重复获取将导致上次获取的access_token失效。 /// 每日限额获取access_token.我们将access_token保存到数据库里,间隔时间为20分钟,从微信公众平台获得一次。 /// </summary> /// <returns></returns> public string getAccessToken(int wid,out string error) { string token = ""; error=""; try { 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 ""; } MxWeiXinPF.Model.wx_property_info wxProperty = new MxWeiXinPF.Model.wx_property_info(); //第一次插入微信属性表 if (!pBll.ExistsWid(wid)) { var result = Senparc.Weixin.MP.CommonAPIs.CommonApi.GetToken(weixininfo.AppId, weixininfo.AppSecret); token = result.access_token; pBll.AddAccess_Token(wid, token); return token; } wxProperty = pBll.GetModelList("iName='access_token' and wid="+wid)[0]; TimeSpan chajun = DateTime.Now - wxProperty.createDate.Value; double chajunSecond = chajun.TotalSeconds; if (wxProperty.iContent == null || wxProperty.iContent.Trim() == "" || chajunSecond >= wxProperty.expires_in) { //从微信平台重新获得access_token var result = Senparc.Weixin.MP.CommonAPIs.CommonApi.GetToken(weixininfo.AppId, weixininfo.AppSecret); token = result.access_token; //更新到数据库里 wxProperty.iContent = token; wxProperty.createDate = DateTime.Now; pBll.Update(wxProperty); } else { token = wxProperty.iContent; } } catch (Exception ex) { error = "获得access_token出错:" + ex.Message; } return token; }