public OauthUser GetOUserInfo(string code, string state) { RequestExtension res = new RequestExtension(); //获取AccessToken string getAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code"; string getAccessTokenReponse = res.GetInfo(getAccessTokenUrl); WeiXinResponse modeAccessToken = JsonConvert.DeserializeObject <WeiXinResponse>(getAccessTokenReponse); //获取用户信息 string getUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + modeAccessToken.access_token + "&openid=" + modeAccessToken.openid + "&lang=zh_CN"; string getUserInfoReponse = res.GetInfo(getUserInfoUrl); OauthUser oauthUser = new OauthUser { UserData = getUserInfoReponse, OpenId = modeAccessToken.openid, ExpiresIn = modeAccessToken.expires_in, RefreshToken = modeAccessToken.refresh_token, Scope = modeAccessToken.scope, AccessToken = modeAccessToken.access_token }; return(oauthUser); }
/// <summary> /// 增加一条回复信息 /// </summary> /// <param name="response"></param> /// <returns></returns> public bool AddResponse(WeiXinResponse response) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into WeiXin_Response("); strSql.Append("Text,Type,LoginID,AddDateTime,IsUsed)"); strSql.Append(" values ("); strSql.Append("@Text,@Type,@LoginID,@AddDateTime,@IsUsed)"); SqlParameter[] parameters = { new SqlParameter("@Text", SqlDbType.VarChar, 50), new SqlParameter("@Type", SqlDbType.Int, 4), new SqlParameter("@LoginID", SqlDbType.Int), new SqlParameter("@AddDateTime", SqlDbType.DateTime), new SqlParameter("@IsUsed", SqlDbType.Int, 4) }; parameters[0].Value = response.Text; parameters[1].Value = response.Type; parameters[2].Value = response.LoginID.ID; parameters[3].Value = response.AddDateTime; parameters[4].Value = response.IsUsed; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return(true); } else { return(false); } }
protected void btnBIn_Click(object sender, EventArgs e) { if (ddlType.SelectedValue == "-1") { lbltxt.Text = "请选择一个分类"; return; } if (txttext.Text.Trim() == "") { lbltxt.Text = "输入内容不能为空"; return; } if (Request.QueryString["id"] != null) { AMS_UserInfo userinfo = Session["Login"] as AMS_UserInfo; WeiXinResponse wxr = new WeiXinResponse { AddDateTime = DateTime.Now, IsUsed = 1, LoginID = new AMS_UserInfo { ID = userinfo.ID }, Text = txttext.Text, Type = Convert.ToInt32(ddlType.SelectedValue) }; wxr.ID = Convert.ToInt32(Request.QueryString["id"]); if (WeiXinProxy.UpdateResponse(wxr)) { Response.Write("<script>alert('修改成功!');window.location.href ='TextsConfig.aspx'</script>"); } else { lbltxt.Text = "修改失败"; } } else { AMS_UserInfo userinfo = Session["Login"] as AMS_UserInfo; WeiXinResponse wxr = new WeiXinResponse { AddDateTime = DateTime.Now, IsUsed = 1, LoginID = new AMS_UserInfo { ID = userinfo.ID }, Text = txttext.Text, Type = Convert.ToInt32(ddlType.SelectedValue) }; WeiXinProxy.UpResponstype(wxr.Type); if (WeiXinProxy.AddResponse(wxr)) { Response.Write("<script>window.location.href ='TextsConfig.aspx'</script>"); } else { lbltxt.Text = "添加失败"; } } DataBind(); }
public override void DataBind() { if (Request.QueryString["id"] != null) { WeiXinResponse type = WeiXinProxy.GetResponseById(Convert.ToInt32(Request.QueryString["id"])); txttext.Text = type.Text; this.ddlType.SelectedValue = type.Type.ToString(); } }
/// <summary> /// 根据ID获得一个回复实体 /// </summary> /// <param name="ID"></param> /// <returns></returns> public WeiXinResponse GetResponse(int ID) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 ID,Text,Type,LoginID,AddDateTime,IsUsed from WeiXin_Response "); strSql.Append(" where ID=@ID"); strSql.Append(" and IsUsed=1 "); strSql.Append(" order by AddDateTime desc"); SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int, 4) }; parameters[0].Value = ID; WeiXinResponse model = new WeiXinResponse(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "") { model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString()); } if (ds.Tables[0].Rows[0]["Text"] != null && ds.Tables[0].Rows[0]["Text"].ToString() != "") { model.Text = ds.Tables[0].Rows[0]["Text"].ToString(); } if (ds.Tables[0].Rows[0]["Type"] != null && ds.Tables[0].Rows[0]["Type"].ToString() != "") { model.Type = int.Parse(ds.Tables[0].Rows[0]["Type"].ToString()); } if (ds.Tables[0].Rows[0]["LoginID"] != null && ds.Tables[0].Rows[0]["LoginID"].ToString() != "") { model.LoginID = new Model.AMS_UserInfo { ID = int.Parse(ds.Tables[0].Rows[0]["LoginID"].ToString()) }; } if (ds.Tables[0].Rows[0]["AddDateTime"] != null && ds.Tables[0].Rows[0]["AddDateTime"].ToString() != "") { model.AddDateTime = DateTime.Parse(ds.Tables[0].Rows[0]["AddDateTime"].ToString()); } if (ds.Tables[0].Rows[0]["IsUsed"] != null && ds.Tables[0].Rows[0]["IsUsed"].ToString() != "") { model.IsUsed = int.Parse(ds.Tables[0].Rows[0]["IsUsed"].ToString()); } return(model); } else { return(null); } }
public async Task <string> ExecuteAsync() { var result = ""; if (!string.IsNullOrEmpty(HttpRequest.QueryString["echostr"])) { WeiXinRequest = new ValidRequest(this); } else { try { _logger.LogInformation("后台接收到数据流:{0}", XDoc.ToString()); Type type = RequestTypes.Find(T => T.Name.Equals(string.Format("{0}Request", XDoc.Root.Element("MsgType").Value), StringComparison.CurrentCultureIgnoreCase)); if (type != null) { WeiXinRequest = (WeiXinRequest)Activator.CreateInstance(type, new object[1] { this }); } else { } } catch (Exception ex) { _logger.LogInformation("命令执行出错:{0}", ex.StackTrace); } } ICommand command = Commands.Find(C => C.CanExecute); if (command != null) { try { WeiXinResponse = await command.ExecuteAsync(); result = WeiXinResponse.ToString(); } catch (Exception ex) { _logger.LogInformation("命令执行出错:{0}", ex.StackTrace); } } return(result); }
public override WeiXinResponse GetResponse() { WeiXinResponse resp = null; dynamic data = JsonConvert.DeserializeObject <dynamic>(Command.Data); switch (this.Command.Style) { case 1: TextResponse textResp = new TextResponse(this.WeiXin); textResp.Content = data.content; resp = textResp; break; case 3: default: NewsResponse newsResp = new NewsResponse(WeiXin); foreach (dynamic item in data) { string newsTitle = (string)item.title; if (newsTitle == null) { newsTitle = ""; } string content = (string)item.content; if (content == null) { content = ""; } string image = (string)item.image; if (image == null) { image = ""; } string url = (string)item.url; if (url == null) { url = ""; } newsResp.AddArticle(newsTitle, content, image, url); } newsResp.ArticleCount = newsResp.Articles.Count; resp = newsResp; break; } return(resp); }
/// <summary> /// 更新回复信息 /// </summary> /// <param name="response"></param> /// <returns></returns> public bool UpdateResponse(WeiXinResponse response) { StringBuilder strSql = new StringBuilder(); strSql.Append("update WeiXin_Response set "); strSql.Append("Text=@Text,"); strSql.Append("Type=@Type,"); strSql.Append("LoginID=@LoginID,"); strSql.Append("AddDateTime=@AddDateTime,"); strSql.Append("IsUsed=@IsUsed"); strSql.Append(" where ID=@ID"); SqlParameter[] parameters = { new SqlParameter("@Text", SqlDbType.VarChar, 50), new SqlParameter("@Type", SqlDbType.Int, 4), new SqlParameter("@LoginID", SqlDbType.Int), new SqlParameter("@AddDateTime", SqlDbType.DateTime), new SqlParameter("@IsUsed", SqlDbType.Int, 4), new SqlParameter("@ID", SqlDbType.Int, 4) }; parameters[0].Value = response.Text; parameters[1].Value = response.Type; parameters[2].Value = response.LoginID.ID; parameters[3].Value = response.AddDateTime; parameters[4].Value = response.IsUsed; parameters[5].Value = response.ID; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return(true); } else { return(false); } }