public WXFansInfo GetUserInfo(string wxappid, string wxopenid) { int retrycount = 0; string access_token = GetAccessToken(wxappid); WXFansInfo userinfo = new WXFansInfo(); string url; while (retrycount < 3) { url = string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN", access_token, wxopenid); userinfo = ObjectSeriallizeHelper.JsonToObject <WXFansInfo>(WXInterfaceHttpRequest(url, "", "GET")); if (userinfo.errcode == 0) { break; } else { if (userinfo.errcode == 40001) { access_token = GetAccessToken(wxappid, true); } retrycount++; } } return(userinfo); }
public WXFansInfo GetUserInfo_OAuth(string wxappid, string access_token, string openid) { int retrycount = 0; WXFansInfo userinfo = new WXFansInfo(); string url; while (retrycount < 3) { url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN"; userinfo = ObjectSeriallizeHelper.JsonToObject <WXFansInfo>(WXInterfaceHttpRequest(url, "", "GET")); if (userinfo.errcode == 0) { break; } else { if (userinfo.errcode == 40001) { access_token = GetAccessToken(wxappid, true); } retrycount++; } } return(userinfo); }
/// <summary> /// 二维码扫描关注 /// </summary> /// <param name="wxopenid">微信粉丝ID</param> /// <param name="scene_id">场景ID</param> /// <param name="wxuser">粉丝信息</param> /// <param name="errmsg">出错时返回的错误提示信息</param> /// <returns>是否成功执行</returns> public bool QRCodeApplication(string wxopenid, string scene_id, WXFansInfo wxuser, ref string errmsg) { bool result = true; DALWXFansManage mydal = new DALWXFansManage(); result = mydal.QRCodeApplication(wxopenid, scene_id, wxuser, ref errmsg); return(result); }
/// <summary> /// 关注微信公众号 /// </summary> /// <param name="wxopenid">微信粉丝ID</param> /// <param name="wxuser">粉丝信息</param> /// <param name="errmsg">出错时返回的错误提示信息</param> /// <returns>是否成功执行</returns> public bool Subscribe(string wxopenid, WXFansInfo wxuser, ref string errmsg) { bool result = true; DALWXFansManage mydal = new DALWXFansManage(); result = mydal.Subscribe(wxopenid, wxuser, ref errmsg); return(result); }
/// <summary> /// 更新微信 /// </summary> /// <param name="wxfansinfo">微信粉丝信息</param> /// <param name="errmsg">出错时返回的错误提示信息</param> /// <returns>是否成功执行</returns> public bool UpdateWXFansInfo(WXFansInfo wxfansinfo, ref string errmsg) { bool result = true; DALUserManage mydal = new DALUserManage(); result = mydal.UpdateWXFansInfo(wxfansinfo, ref errmsg); return(result); }
/// <summary> /// 发送文本型的客服消息(目前只是临时用作新客户注册通知内部使用,未正式) /// </summary> /// <param name="wxappid"></param> /// <param name="wxopenid"></param> /// <param name="msg"></param> /// <param name="?"></param> public void SendCSMsg_Text(string wxappid, string wxopenid, string msg) { string access_token = GetAccessToken(wxappid); WXFansInfo userinfo = new WXFansInfo(); string url = string.Format("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}", access_token); string sendcontent = @"{ ""touser"":""" + wxopenid + @""", ""msgtype"":""text"", ""text"": { ""content"":""" + msg.Replace("\r\n", "\\n") + @""" } }"; string result = WXInterfaceHttpRequest(url, sendcontent, "POST"); }
/// <summary> /// 获取微信粉丝信息 /// </summary> /// <param name="wxopenid">微信OpenID</param> /// <param name="access_token">用户级access_token</param> /// <param name="wxfansinfo">粉丝信息</param> /// <param name="errmsg">出错时返回的错误提示信息</param> /// <returns>是否成功执行</returns> private bool GetWXFansInfo(string wxopenid, string access_token, ref WXFansInfo wxfansinfo, ref string errmsg) { bool result = true; string url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + wxopenid + "&lang=zh_CN"; //string url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + access_token + "&openid=" + wxopenid + "&lang=zh_CN"; try { string json = HttpRequests.WXInterfaceHttpRequest(url, "", "GET"); wxfansinfo = ObjectSeriallizeHelper.JsonToObject <WXFansInfo>(json); } catch (Exception ex) { errmsg = ex.Message.ToString(); result = false; LogWriter.WriteLog(ex); } return(result); }
/// <summary> /// 更新微信 /// </summary> /// <param name="wxfansinfo">微信粉丝信息</param> /// <param name="errmsg">出错时返回的错误提示信息</param> /// <returns>是否成功执行</returns> public bool UpdateWXFansInfo(WXFansInfo wxfansinfo, ref string errmsg) { bool result = true; OracleDBO mydbo = new OracleDBO(); mydbo.IsByParamName = true; mydbo.IsThrowException = true; try { string sql1 = @"select count(*) from sys_s_wxfansinfo t where t.wxopenid = :wxopenid"; OracleDBO.OracleCmdParam[] oparam1 = new OracleDBO.OracleCmdParam[1]; oparam1[0].Name = ":wxopenid"; oparam1[0].Type = OracleDbType.Varchar2; oparam1[0].Value = wxfansinfo.openid; int num = Convert.ToInt32(mydbo.ExecuteScalar(sql1, oparam1)); if (num == 0) { #region 添加粉丝 string sql2 = @"insert into sys_s_wxfansinfo (wxopenid, nickname, sex, province, city, country, headimgurl, subscribed) values (:wxopenid, :nickname, :sex, :province, :city, :country, :headimgurl, :subscribed)"; OracleDBO.OracleCmdParam[] oparam2 = new OracleDBO.OracleCmdParam[8]; oparam2[0].Name = ":wxopenid"; oparam2[0].Type = OracleDbType.Varchar2; oparam2[0].Value = wxfansinfo.openid; oparam2[1].Name = ":nickname"; oparam2[1].Type = OracleDbType.Varchar2; oparam2[1].Value = wxfansinfo.nickname; oparam2[2].Name = ":sex"; oparam2[2].Type = OracleDbType.Varchar2; oparam2[2].Value = wxfansinfo.sex == "1" ? "男" : "女"; oparam2[3].Name = ":province"; oparam2[3].Type = OracleDbType.Varchar2; oparam2[3].Value = wxfansinfo.province; oparam2[4].Name = ":city"; oparam2[4].Type = OracleDbType.Varchar2; oparam2[4].Value = wxfansinfo.city; oparam2[5].Name = ":country"; oparam2[5].Type = OracleDbType.Varchar2; oparam2[5].Value = wxfansinfo.country; oparam2[6].Name = ":headimgurl"; oparam2[6].Type = OracleDbType.Varchar2; oparam2[6].Value = wxfansinfo.headimgurl; oparam2[7].Name = ":subscribed"; oparam2[7].Type = OracleDbType.Int32; oparam2[7].Value = 1; int n = mydbo.ExecuteNonQuery(sql2, oparam2); #endregion } else { #region 更新粉丝数据 string sql2 = @"update sys_s_wxfansinfo t set t.nickname = :nickname, t.sex = :sex, t.province = :province, t.city = :city, t.country = :country, t.headimgurl = :headimgurl where t.wxopenid = :wxopenid"; OracleDBO.OracleCmdParam[] oparam2 = new OracleDBO.OracleCmdParam[7]; oparam2[0].Name = ":wxopenid"; oparam2[0].Type = OracleDbType.Varchar2; oparam2[0].Value = wxfansinfo.openid; oparam2[1].Name = ":nickname"; oparam2[1].Type = OracleDbType.Varchar2; oparam2[1].Value = wxfansinfo.nickname; oparam2[2].Name = ":sex"; oparam2[2].Type = OracleDbType.Varchar2; oparam2[2].Value = wxfansinfo.sex == "1" ? "男" : "女"; oparam2[3].Name = ":province"; oparam2[3].Type = OracleDbType.Varchar2; oparam2[3].Value = wxfansinfo.province; oparam2[4].Name = ":city"; oparam2[4].Type = OracleDbType.Varchar2; oparam2[4].Value = wxfansinfo.city; oparam2[5].Name = ":country"; oparam2[5].Type = OracleDbType.Varchar2; oparam2[5].Value = wxfansinfo.country; oparam2[6].Name = ":headimgurl"; oparam2[6].Type = OracleDbType.Varchar2; oparam2[6].Value = wxfansinfo.headimgurl; int n = mydbo.ExecuteNonQuery(sql2, oparam2); #endregion } } catch (Exception ex) { errmsg = ex.Message.ToString(); result = false; LogWriter.WriteLog(ex); } finally { mydbo.Close(); } return(result); }
/// <summary> /// 处理二维码扫描事件 /// </summary> /// <param name="xmlModel"></param> /// <returns></returns> public string ScanHandle(ref Dictionary <string, string> xmlModel) { string result = ""; //返回欢迎语内容 string wxappusername = xmlModel["ToUserName"]; //事件发给哪个开发者微信号(注意这里不是公众号的AppID,而是公众号的微信号) string wxopenid = xmlModel["FromUserName"]; //事件是哪个粉丝发起的(粉丝的openid) string strCreateTime = xmlModel["CreateTime"]; if (string.IsNullOrEmpty(strCreateTime)) { //无消息创建时间,无法排重,放弃处理 return(""); } double wxcreatetime; if (!double.TryParse(strCreateTime, out wxcreatetime)) { //无法将createtime转换成功,无法排重,放弃处理 return(""); } string wxappid = ""; DALWXMessageHandleBasic mydal = new DALWXMessageHandleBasic(); //事件消息排重 bool success = true; success = mydal.EventMsgPreventDuplicates(wxopenid, wxcreatetime); if (!success) { //事件消息排重失败,放弃处理 return(""); } wxappid = WXApiInfo.wxappid; CallWXInterface wxinterface = new CallWXInterface(); //获取粉丝信息,这一步是必须的,也要在明文模式下,要防止虚构粉丝数据 WXFansInfo wxuser = wxinterface.GetUserInfo(wxappid, wxopenid); if (wxuser == null || wxuser.errcode != 0) { return(""); } //推送过来的二维码场景值 string EventKey = ""; if (xmlModel.ContainsKey("EventKey")) { EventKey = xmlModel["EventKey"]; } //result = QRCodeApplication(wxopenid, EventKey.Replace("qrscene_", ""), wxuser); string errmsg = ""; BLLWXFansManage mybll = new BLLWXFansManage(); success = mybll.QRCodeApplication(wxopenid, EventKey.Replace("qrscene_", ""), wxuser, ref errmsg); if (success) { result = errmsg; } else { result = errmsg; } //返回文本消息 string rspxml = ""; if (!string.IsNullOrEmpty(result)) { rspxml = "<xml><ToUserName><![CDATA[" + xmlModel["FromUserName"] + "]]></ToUserName><FromUserName><![CDATA[" + xmlModel["ToUserName"] + "]]></FromUserName><CreateTime>" + xmlModel["CreateTime"] + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + result + "]]></Content><FuncFlag>0</FuncFlag></xml>"; } return(rspxml); }
/// <summary> /// 处理关注事件 /// </summary> /// <param name="xmlModel"></param> /// <returns></returns> public string SubscribeHandle(ref Dictionary <string, string> xmlModel) { string result = ""; //返回欢迎语内容 string wxappusername = xmlModel["ToUserName"]; //事件发给哪个开发者微信号(注意这里不是公众号的AppID,而是公众号的微信号) string wxopenid = xmlModel["FromUserName"]; //事件是哪个粉丝发起的(粉丝的openid) string strCreateTime = xmlModel["CreateTime"]; if (string.IsNullOrEmpty(strCreateTime)) { //无消息创建时间,无法排重,放弃处理 return(""); } double wxcreatetime; if (!double.TryParse(strCreateTime, out wxcreatetime)) { //无法将createtime转换成功,无法排重,放弃处理 return(""); } string wxappid = ""; DALWXMessageHandleBasic mydal = new DALWXMessageHandleBasic(); //事件消息排重 bool success = true; success = mydal.EventMsgPreventDuplicates(wxopenid, wxcreatetime); if (!success) { //事件消息排重失败,放弃处理 return(""); } wxappid = WXApiInfo.wxappid; CallWXInterface wxinterface = new CallWXInterface(); //获取粉丝信息,这一步是必须的,因为在明文模式下,要防止虚构粉丝数据 WXFansInfo wxuser = wxinterface.GetUserInfo(wxappid, wxopenid); if (wxuser == null || wxuser.errcode != 0) { return(""); } //根据微信POST过来的事件数据,判断是普通关注还是带参数的二维码关注,如果是带参数二维码关注,需要读取相应的二维码应用信息 string EventKey = ""; if (xmlModel.ContainsKey("EventKey")) { EventKey = xmlModel["EventKey"]; } //注意微信的坑:普通关注的事件是有EventKey的(而文档上面没有)。带参数二维码扫描关注的EventKey是qrscene_开头的。 #region 根据二维码应用信息来处理 //WXUser bllWXUser = new WXUser(); if (string.IsNullOrEmpty(EventKey) || !TextValidation.CheckStringByRegexp(EventKey, "qrscene_.+")) { //普通关注 string errmsg = ""; BLLWXFansManage mybll = new BLLWXFansManage(); mybll.SubscribeLog(wxopenid, 1, ref errmsg);//关注日志 success = mybll.Subscribe(wxopenid, wxuser, ref errmsg); if (success) { result = "欢迎关注每通系统!"; } else { result = errmsg; } } else { //二维码扫描 //result = QRCodeApplication(wxopenid, EventKey.Replace("qrscene_", ""), wxuser); string errmsg = ""; BLLWXFansManage mybll = new BLLWXFansManage(); mybll.SubscribeLog(wxopenid, 1, ref errmsg);//关注日志 success = mybll.QRCodeApplication(wxopenid, EventKey.Replace("qrscene_", ""), wxuser, ref errmsg); if (success) { result = errmsg; } else { result = errmsg; } } #endregion //因为对于关注粉丝,可以自动发送欢迎语或其他内容,所以这里可返回内容 string rspxml = ""; if (!string.IsNullOrEmpty(result)) { rspxml = "<xml><ToUserName><![CDATA[" + xmlModel["FromUserName"] + "]]></ToUserName><FromUserName><![CDATA[" + xmlModel["ToUserName"] + "]]></FromUserName><CreateTime>" + xmlModel["CreateTime"] + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + result + "]]></Content><FuncFlag>0</FuncFlag></xml>"; } return(rspxml); }
/// <summary> /// 初始化(初始化state),用于检查是否已经登录和微信网页授权,并把state信息存入session /// 情况一:用户是否从微信跳转进入,如果是就进行微信Web授权。 /// 情况一:另外,用户可能在会话期重新在微信中跳转进来,所以要检查session中的state判断是用户是否在会话期内,如果是则无需重新获取微信资料 /// 情况二:一个微信关注多商户的情况,继情况一,如果用户在会话期内重新进来,有可能会进入另一个商户,此时还要判断session中的state记录的AppID是否与querystring的state一致,如果不一致就要重新获取微信资料(openid ) /// 情况三:用户正常浏览(即request的code和state为空),此要检查是否已经登录(检查state是否为空) /// </summary> /// <param name="e"></param> protected override void OnInit(EventArgs e) { if (Request.QueryString["code"] != null && code != Request.QueryString["code"]) { code = Request.QueryString["code"]; string[] arr_code = code.Split(','); code = arr_code[arr_code.Length - 1]; state = Request.QueryString["state"]; string errmsg = ""; bool result = true; if (Session["openid"] == null || Session["access_token"] == null) { #region 获取access_token及openid if (Session["access_token"] == null) { string scope = ""; result = GetWXAccreditOpenID(code, ref access_token, ref openid, ref scope, ref errmsg); if (result && !string.IsNullOrWhiteSpace(access_token)) { Session["access_token"] = access_token; Session["openid"] = openid; } } #endregion } else { openid = Session["openid"].ToString(); if (string.IsNullOrWhiteSpace(openid)) { Session["openid"] = null; Session["access_token"] = null; Session["wxfansinfo"] = null; #region 让用户授权并获取用户授权code Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wxappid + "&redirect_uri=http%3a%2f%2f240344pb55.qicp.vip%2fWX%2fre_login.aspx&response_type=code&scope=snsapi_userinfo&state=wxoauthz0z#wechat_redirect"); // Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wxappid + "&redirect_uri=http%3a%2f%2fo24034e466.qicp.vip%2fWebForm1.aspx&response_type=code&scope=snsapi_userinfo&state=wxoauthz0z#wechat_redirect"); #endregion } else { access_token = Session["access_token"].ToString(); result = CheckUserAccessToken(wxappid, access_token, ref errmsg); if (result == false) { #region 让用户授权并获取用户授权code //Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wxappid + "&redirect_uri="+WXApiInfo.uri_befor+"WXAccredit.aspx&response_type=code&scope=snsapi_userinfo&state=wxoauthz0z#wechat_redirect"); #endregion #region 获取access_token及openid if (Session["access_token"] == null) { string scope = ""; result = GetWXAccreditOpenID(code, ref access_token, ref openid, ref scope, ref errmsg); if (result) { Session["access_token"] = access_token; Session["openid"] = openid; } } #endregion } } } if (Session["wxfansinfo"] == null) { result = GetWXFansInfo(openid, access_token, ref wxinfo, ref errmsg); if (result) { Session["wxfansinfo"] = wxinfo; BLLUserManage mybll = new BLLUserManage(); result = mybll.UpdateWXFansInfo(wxinfo, ref errmsg); } } else { wxinfo = (WXFansInfo)Session["wxfansinfo"]; if (wxinfo.openid == null) { Session["wxfansinfo"] = null; wxinfo = null; } } } else { wxinfo = (WXFansInfo)Session["wxfansinfo"]; if (wxinfo == null || wxinfo.openid == null || wxinfo.openid.Length == 0) { #region 请求授权后回调到请求页面 wxinfo = null; string user_url = HttpContext.Current.Request.RawUrl; string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wxappid + "&redirect_uri=" + WXApiInfo.uri_befor + user_url + "&response_type=code&scope=snsapi_userinfo&state=wxoauthz0z#wechat_redirect"; Response.Redirect(url); #endregion } } if (wxinfo != null) { string errmsg = ""; DataTable dt = new DataTable(); DataTable dt1 = new DataTable(); string wxopenid = wxinfo.openid; BLLUserManage myuser = new BLLUserManage(); if (string.IsNullOrWhiteSpace(wxopenid)) { #region 请求授权后回调到请求页面 wxinfo = null; string user_url = HttpContext.Current.Request.RawUrl; string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wxappid + "&redirect_uri=" + WXApiInfo.uri_befor + user_url + "&response_type=code&scope=snsapi_userinfo&state=wxoauthz0z#wechat_redirect"; Response.Redirect(url); #endregion } else { bool result = myuser.GetWXFansBindUserInfo(wxopenid, ref dt, ref dt1, ref errmsg); if (result) { if (dt == null || dt.Rows.Count == 0) { string url = "http://www.mtworld.cn/";//绑定接口 Response.Redirect(url); } else { wxuserinfo.usercode = dt.Rows[0]["usercode"].ToString(); wxuserinfo.username = dt.Rows[0]["username"].ToString(); wxuserinfo.usertype = dt.Rows[0]["usertype"].ToString(); wxuserinfo.wxopenid = dt.Rows[0]["wxopenid"].ToString(); //wxuserinfo.phonenumber = dt.Rows[0]["phonenumber"].ToString(); //wxuserinfo.departmentname = dt.Rows[0]["department"].ToString(); //wxuserinfo.departmentcode = dt.Rows[0]["departmentcode"].ToString(); wxuserinfo.account = dt.Rows[0]["account"].ToString(); } } else { string url = "http://www.mtworld.cn/"; Response.Redirect(url); } } } }
/// <summary> /// 二维码扫描关注 /// </summary> /// <param name="wxopenid">微信粉丝ID</param> /// <param name="scene_id">场景ID</param> /// <param name="wxuser">粉丝信息</param> /// <param name="errmsg">出错时返回的错误提示信息</param> /// <returns>是否成功执行</returns> public bool QRCodeApplication(string wxopenid, string scene_id, WXFansInfo wxuser, ref string errmsg) { bool result = true; OracleDBO mydbo = new OracleDBO(); mydbo.IsByParamName = true; mydbo.IsThrowException = true; mydbo.AutoClose = false; try { #region 查询是否存在该粉丝 string sql1 = @"select count(*) from sys_s_wxfansinfo t where t.wxopenid = :wxopenid"; OracleDBO.OracleCmdParam[] oparam1 = new OracleDBO.OracleCmdParam[1]; oparam1[0].Name = ":wxopenid"; oparam1[0].Type = OracleDbType.Varchar2; oparam1[0].Value = wxopenid; int num = Convert.ToInt32(mydbo.ExecuteScalar(sql1, oparam1)); #endregion mydbo.BeginTransaction(); if (num == 0) { #region 添加微信粉丝信息 string sql2 = @"insert into sys_s_wxfansinfo (wxopenid, nickname, sex, province, city, country, headimgurl, subscribetime, subscribed) values (:wxopenid, :nickname, :sex, :province, :city, :country, :headimgurl, :subscribetime, :subscribed)"; OracleDBO.OracleCmdParam[] oparam2 = new OracleDBO.OracleCmdParam[9]; oparam2[0].Name = ":wxopenid"; oparam2[0].Type = OracleDbType.Varchar2; oparam2[0].Value = wxopenid; oparam2[1].Name = ":nickname"; oparam2[1].Type = OracleDbType.Varchar2; oparam2[1].Value = wxuser.nickname; oparam2[2].Name = ":sex"; oparam2[2].Type = OracleDbType.Varchar2; oparam2[2].Value = wxuser.sex; oparam2[3].Name = ":province"; oparam2[3].Type = OracleDbType.Varchar2; oparam2[3].Value = wxuser.province; oparam2[4].Name = ":city"; oparam2[4].Type = OracleDbType.Varchar2; oparam2[4].Value = wxuser.city; oparam2[5].Name = ":country"; oparam2[5].Type = OracleDbType.Varchar2; oparam2[5].Value = wxuser.country; oparam2[6].Name = ":headimgurl"; oparam2[6].Type = OracleDbType.Varchar2; oparam2[6].Value = wxuser.headimgurl; oparam2[7].Name = ":subscribetime"; oparam2[7].Type = OracleDbType.Date; oparam2[7].Value = DateTime.Now; oparam2[8].Name = ":subscribed"; oparam2[8].Type = OracleDbType.Varchar2; oparam2[8].Value = 1; int n = mydbo.ExecuteNonQuery(sql2, oparam2); #endregion } else { #region 更新微信粉丝信息 string sql2 = @"update sys_s_wxfansinfo t set t.nickname = :nickname, t.sex = :sex, t.province = :province, t.city = :city, t.country = :country, t.headimgurl = :headimgurl, t.subscribetime = :subscribetime, t.subscribed = :subscribed where t.wxopenid = :wxopenid"; OracleDBO.OracleCmdParam[] oparam2 = new OracleDBO.OracleCmdParam[9]; oparam2[0].Name = ":wxopenid"; oparam2[0].Type = OracleDbType.Varchar2; oparam2[0].Value = wxopenid; oparam2[1].Name = ":nickname"; oparam2[1].Type = OracleDbType.Varchar2; oparam2[1].Value = wxuser.nickname; oparam2[2].Name = ":sex"; oparam2[2].Type = OracleDbType.Varchar2; oparam2[2].Value = wxuser.sex; oparam2[3].Name = ":province"; oparam2[3].Type = OracleDbType.Varchar2; oparam2[3].Value = wxuser.province; oparam2[4].Name = ":city"; oparam2[4].Type = OracleDbType.Varchar2; oparam2[4].Value = wxuser.city; oparam2[5].Name = ":country"; oparam2[5].Type = OracleDbType.Varchar2; oparam2[5].Value = wxuser.country; oparam2[6].Name = ":headimgurl"; oparam2[6].Type = OracleDbType.Varchar2; oparam2[6].Value = wxuser.headimgurl; oparam2[7].Name = ":subscribetime"; oparam2[7].Type = OracleDbType.Date; oparam2[7].Value = DateTime.Now; oparam2[8].Name = ":subscribed"; oparam2[8].Type = OracleDbType.Varchar2; oparam2[8].Value = 1; int n = mydbo.ExecuteNonQuery(sql2, oparam2); #endregion } string sql3 = @"select t.val1 from sys_p_wxqrmanage t where t.scene_id = :scene_id and t.intentions = '绑定用户' and t.valid = 1"; OracleDBO.OracleCmdParam[] oparam3 = new OracleDBO.OracleCmdParam[1]; oparam3[0].Name = ":scene_id"; oparam3[0].Type = OracleDbType.Int32; oparam3[0].Value = Convert.ToInt32(scene_id); DataTable dt = mydbo.GetDataTable(sql3, oparam3); if (dt != null && dt.Rows.Count > 0) { string usercode = dt.Rows[0]["val1"].ToString(); #region 更新二维码为已使用 string sql4 = @"update sys_p_wxqrmanage t set t.valid = 0 where t.scene_id = :scene_id and t.intentions = '绑定用户' and t.valid = 1"; OracleDBO.OracleCmdParam[] oparam4 = new OracleDBO.OracleCmdParam[1]; oparam4[0].Name = ":scene_id"; oparam4[0].Type = OracleDbType.Int32; oparam4[0].Value = scene_id; int n = mydbo.ExecuteNonQuery(sql4, oparam4); #endregion #region 更新用户绑定微信信息 string sql5 = @"update sys_s_user t set t.wxopenid = :wxopenid where t.usercode = :usercode"; OracleDBO.OracleCmdParam[] oparam5 = new OracleDBO.OracleCmdParam[2]; oparam5[0].Name = ":wxopenid"; oparam5[0].Type = OracleDbType.Varchar2; oparam5[0].Value = wxopenid; oparam5[1].Name = ":usercode"; oparam5[1].Type = OracleDbType.Varchar2; oparam5[1].Value = usercode; n = mydbo.ExecuteNonQuery(sql5, oparam5); #endregion string sql6 = @"select t.username from sys_s_userinfo t where t.usercode=:usercode"; OracleDBO.OracleCmdParam[] oparam6 = new OracleDBO.OracleCmdParam[1]; oparam6[0].Name = ":usercode"; oparam6[0].Type = OracleDbType.Varchar2; oparam6[0].Value = usercode; DataTable dt2 = mydbo.GetDataTable(sql6, oparam6); if (dt2 != null && dt2.Rows.Count > 0) { errmsg = "成功绑定用户:" + dt2.Rows[0]["username"].ToString(); } mydbo.Commit(); } else { result = false; errmsg = "该二维码已失效"; mydbo.Commit(); } } catch (Exception ex) { mydbo.Rollback(); errmsg = ex.Message.ToString(); result = false; LogWriter.WriteLog(ex); } finally { mydbo.Close(); } return(result); }
/// <summary> /// 初始化(初始化state),用于检查是否已经登录和微信网页授权,并把state信息存入session /// 情况一:用户是否从微信跳转进入,如果是就进行微信Web授权。 /// 情况一:另外,用户可能在会话期重新在微信中跳转进来,所以要检查session中的state判断是用户是否在会话期内,如果是则无需重新获取微信资料 /// 情况二:一个微信关注多商户的情况,继情况一,如果用户在会话期内重新进来,有可能会进入另一个商户,此时还要判断session中的state记录的AppID是否与querystring的state一致,如果不一致就要重新获取微信资料(openid ) /// 情况三:用户正常浏览(即request的code和state为空),此要检查是否已经登录(检查state是否为空) /// </summary> /// <param name="e"></param> protected override void OnInit(EventArgs e) { if (Request.QueryString["code"] != null && code != Request.QueryString["code"]) { code = Request.QueryString["code"]; string[] arr_code = code.Split(','); code = arr_code[arr_code.Length - 1]; state = Request.QueryString["state"]; string errmsg = ""; bool result = true; if (Session["openid"] == null || Session["access_token"] == null) { #region 获取access_token及openid if (Session["access_token"] == null) { string scope = ""; result = GetWXAccreditOpenID(code, ref access_token, ref openid, ref scope, ref errmsg); if (result && !string.IsNullOrWhiteSpace(access_token)) { Session["access_token"] = access_token; Session["openid"] = openid; } } #endregion } else { openid = Session["openid"].ToString(); if (string.IsNullOrWhiteSpace(openid)) { Session["openid"] = null; Session["access_token"] = null; Session["wxfansinfo"] = null; #region 让用户授权并获取用户授权code Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wxappid + "&redirect_uri=http%3a%2f%2fwx.mtworld.cn%2fWXAccredit.aspx&response_type=code&scope=snsapi_userinfo&state=wxoauthz0z#wechat_redirect"); #endregion } else { access_token = Session["access_token"].ToString(); result = CheckUserAccessToken(wxappid, access_token, ref errmsg); if (result == false) { #region 让用户授权并获取用户授权code //Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wxappid + "&redirect_uri="+WXApiInfo.uri_befor+"WXAccredit.aspx&response_type=code&scope=snsapi_userinfo&state=wxoauthz0z#wechat_redirect"); #endregion #region 获取access_token及openid if (Session["access_token"] == null) { string scope = ""; result = GetWXAccreditOpenID(code, ref access_token, ref openid, ref scope, ref errmsg); if (result) { Session["access_token"] = access_token; Session["openid"] = openid; } } #endregion } } } if (Session["wxfansinfo"] == null) { result = GetWXFansInfo(openid, access_token, ref wxinfo, ref errmsg); if (result) { Session["wxfansinfo"] = wxinfo; BLLUserManage mybll = new BLLUserManage(); result = mybll.UpdateWXFansInfo(wxinfo, ref errmsg); } } else { wxinfo = (WXFansInfo)Session["wxfansinfo"]; if (wxinfo.openid == null) { Session["wxfansinfo"] = null; wxinfo = null; } } } else { wxinfo = (WXFansInfo)Session["wxfansinfo"]; if (wxinfo == null || wxinfo.openid == null || wxinfo.openid.Length == 0) { #region 请求授权后回调到请求页面 wxinfo = null; string user_url = HttpContext.Current.Request.RawUrl; string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wxappid + "&redirect_uri=" + WXApiInfo.uri_befor + user_url + "&response_type=code&scope=snsapi_userinfo&state=wxoauthz0z#wechat_redirect"; Response.Redirect(url); #endregion } } if (wxinfo != null) { string errmsg = ""; DataTable dt = new DataTable(); DataTable dt_page = new DataTable(); string wxopenid = wxinfo.openid; BLLUserManage myuser = new BLLUserManage(); if (string.IsNullOrWhiteSpace(wxopenid)) { #region 请求授权后回调到请求页面 wxinfo = null; string user_url = HttpContext.Current.Request.RawUrl; string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wxappid + "&redirect_uri=" + WXApiInfo.uri_befor + user_url + "&response_type=code&scope=snsapi_userinfo&state=wxoauthz0z#wechat_redirect"; Response.Redirect(url); #endregion } else { bool result = myuser.GetWXFansBindUserInfo(wxopenid, ref dt, ref dt_page, ref errmsg); if (result) { if (dt == null || dt.Rows.Count == 0) { string url = "http://www.mtworld.cn/"; Response.Redirect(url); } else { /* * usercode, * password, * usertype, * createdate, * wxopenid, * account, * username, * position, * sex, * birthday, * address, * phonenumber, * hiredate, * nickname, * headimgurl, * departmentcode, * departmentname department * idnumber, * idkey */ wxuserinfo.usercode = dt.Rows[0]["usercode"].ToString(); wxuserinfo.username = dt.Rows[0]["username"].ToString(); wxuserinfo.position = dt.Rows[0]["position"].ToString(); wxuserinfo.phonenumber = dt.Rows[0]["phonenumber"].ToString(); wxuserinfo.departmentname = dt.Rows[0]["department"].ToString(); wxuserinfo.departmentcode = dt.Rows[0]["departmentcode"].ToString(); wxuserinfo.account = dt.Rows[0]["account"].ToString(); for (int i = 0; i < dt_page.Rows.Count; i++) { PageInfo pageinfo = new PageInfo(); string PageCode = dt_page.Rows[i]["PageCode"].ToString(); //string PageName = dt_page.Rows[i]["PageName"].ToString(); //string ico = dt_page.Rows[i]["ico"].ToString(); //string url = dt_page.Rows[i]["url"].ToString(); //string superior = dt_page.Rows[i]["superior"].ToString(); pageinfo.PageCode = PageCode; //pageinfo.PageName = PageName; //pageinfo.ico = ico; //pageinfo.url = url; //pageinfo.superior = superior; arraylist.Add(PageCode); // wxuserinfo.pagelist.Add(pageinfo); } } } else { string url = "http://www.mtworld.cn/"; Response.Redirect(url); } } } }