//添加或更新用户 public void InsertOrUpdateUsers(WeiXinSDK.UserInfo userinfo, string uopenid) { try { //获取当前openid对应的商户及用户信息 MZZ.BLL.tb_user userBLL = new MZZ.BLL.tb_user(); var usersList = userBLL.GetModelList("user_openid='" + userinfo.openid + "'"); MZZ.Model.tb_user usersModel = new MZZ.Model.tb_user(); if (usersList.Count > 0)//更新 { usersModel = usersList[0]; } WeiXinSDK.QRCodeTicket ticket = WeiXinSDK.WeiXin.CreateQRCode2(userinfo.openid); if (!string.IsNullOrEmpty(uopenid) && string.IsNullOrEmpty(usersModel.user_up_openid)) { MZZ.BLL.tb_user waiterBLL = new MZZ.BLL.tb_user(); var waiterList = waiterBLL.GetModelList("user_openid='" + uopenid + "'"); if (waiterList.Count > 0 && usersModel.user_openid != waiterList[0].user_openid) { usersModel.user_uopenid_time = Convert.ToDateTime(DateTime.Now); usersModel.user_up_openid = waiterList[0].user_openid;//设置上线 } } if (userinfo.subscribe == 1) { usersModel.user_nick_name = HttpUtility.UrlEncode(userinfo.nickname, System.Text.Encoding.UTF8); usersModel.user_head_img_url = WebHelp.GB2312ToUTF8(userinfo.headimgurl); usersModel.user_sub_time = CHelper.UnixTimeToTime(userinfo.subscribe_time); usersModel.user_qr_code = ticket.ticket; } usersModel.user_sub_scribe = Convert.ToBoolean(userinfo.subscribe); if (usersList.Count > 0)//更新 { userBLL.Update(usersModel); } else//添加 { usersModel.user_qr_code = ticket.ticket; usersModel.user_phone = userinfo.openid; usersModel.user_openid = userinfo.openid; userBLL.Add(usersModel); } } catch (Exception ex) { CHelper.WriteLog("ex:", ex.ToString()); throw ex; } }
public bool GetUserList() { WeiXinSDK.Followers allFollower = new WeiXinSDK.Followers(); //第一步:获取所有关注者列表 allFollower = WeiXinSDK.WeiXin.GetAllFollowers(); if (allFollower.error == null) { int count = allFollower.data.openid.Count; //第二步:将关注者列表的用户数据更新回数据库 MZZ.BLL.tb_user userBLL = new MZZ.BLL.tb_user(); try { List<string> openids = new List<string>(); for (int i = 0; i < count; i++) { //获取用户数据 string openid = allFollower.data.openid[i]; openids.Add(openid); WeiXinSDK.UserInfo userinfo = WeiXinSDK.WeiXin.GetUserInfo(openid, WeiXinSDK.LangType.zh_CN); if (userinfo.error == null) { //添加或更新到用户表 InsertOrUpdateUsers(userinfo, null); } } var usersList = userBLL.GetModelList("user_status>0"); //更改用户状态为未关注 for (int i = 0; i < usersList.Count; i++) { if (!openids.Contains(usersList[i].user_openid)) { usersList[i].user_sub_scribe = false; userBLL.Update(usersList[i]); } } return true; } catch (Exception ex) { return false; } } else { return false; } }
protected void Page_Load(object sender, EventArgs e) { string url = Request.QueryString["url"]; if (url == null || url == "" || !RegexUtil.IsURL(Server.UrlDecode(url.Replace(":801", "")))) { url = "http://" + HttpContext.Current.Request.Url.Host + "/Default.aspx"; } string redirect_uri = Server.UrlEncode("http://" + HttpContext.Current.Request.Url.Host + "/OAuth.aspx?url=" + Server.UrlEncode(url.Replace(":801", "")));//授权后重定向的回调链接地址 string scope = "snsapi_base";//应用授权作用域,snsapi_base、snsapi_userinfo //string state = "1";//重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值 string code = Request.QueryString["code"]; string state = Request.QueryString["state"]; if (string.IsNullOrEmpty(code)) { string codeurl = WeiXinSDK.WeiXin.BuildWebCodeUrl(redirect_uri, scope, "1"); CHelper.WriteLog("codeurl:", codeurl); //第一步:引导用户打开如下页面授权 Response.Redirect(codeurl); return; } else { CHelper.WriteLog("code:", code); //第二步:通过code换取网页授权access_token WeiXinSDK.WebCredential wc = WeiXinSDK.WeiXin.GetWebAccessToken(code); CHelper.WriteLog("网页授权snsapi_base的access_token:", wc.access_token); //判断access_token是否有效 if (wc.error == null || (wc.error.errcode == 0 && wc.error.errmsg == "ok")) { //有效 } else { CHelper.WriteLog("网页授权access_token无效:", wc.access_token); //无效重新获取access_token wc = WeiXinSDK.WeiXin.GetWebAccessToken(code); if (wc.error == null || (wc.error.errcode == 0 && wc.error.errmsg == "ok")) { //有效 } else { Response.Write("网页授权access_token无效: " + wc.error.ToString()); Response.End(); } } //第三步:刷新access_token(如果需要) //wc = WeiXinSDK.WeiXin.GetWebAccessToken(code); CHelper.WriteLog("Oauth ,OpenId", wc.openid); MZZ.BLL.tb_user blluser = new MZZ.BLL.tb_user(); var dt = blluser.GetModelList("user_openid = '" + wc.openid + "'"); if (dt.Count > 0) //用户已存在 { Session["hyid"] = dt[0].user_id; Session["nickname"] = dt[0].user_nick_name; Session["phone"] = dt[0].user_phone; CHelper.WriteLog("nickname", Session["nickname"].ToString()); CHelper.WriteLog("phone", Session["phone"].ToString()); MZZ.Model.tb_user userModel = dt[0]; Session["UserInfo"] = new UserInfo() { TableName = EnumLoginType.User, Entity = userModel }; CHelper.WriteLog("用户已存在", wc.openid); } else { if (state == "2") { //第四步:拉取用户信息(需scope为 snsapi_userinfo) WeiXinSDK.WebUserInfo useinfo = WeiXinSDK.WeiXin.GetWebUserInfo(wc.access_token, wc.openid, WeiXinSDK.LangType.zh_CN); CHelper.WriteLog("用户不存在并拉取用户信息:", useinfo.openid); new WxUser().InsertUpdateWebUsers(useinfo, null); dt = blluser.GetModelList("user_openid = '" + useinfo.openid + "'"); if (dt.Count > 0) //用户已存在 { MZZ.Model.tb_user userModel = dt[0]; Session["UserInfo"] = new UserInfo() { TableName = EnumLoginType.User, Entity = userModel }; } } else { CHelper.WriteLog("用户不存在并获取网页授权snsapi_userinfo信息:", wc.openid); //第二步:通过code换取网页授权access_token scope = "snsapi_userinfo"; string codeurl = WeiXinSDK.WeiXin.BuildWebCodeUrl(redirect_uri, scope, "2"); Response.Redirect(codeurl); return; } } CHelper.WriteLog("页面跳转", url); Response.Redirect(url); } }
///网页端添加用户表 public void InsertUpdateWebUsers(WeiXinSDK.WebUserInfo userinfo, string uopenid) { try { MZZ.BLL.tb_user userBLL = new MZZ.BLL.tb_user(); //获取用户信息 WeiXinSDK.UserInfo userinfo2 = WeiXinSDK.WeiXin.GetUserInfo(userinfo.openid, WeiXinSDK.LangType.zh_CN); //获取当前openid对应的商户及用户信息 var usersList = userBLL.GetModelList("user_openid='" + userinfo.openid + "'"); MZZ.Model.tb_user usersModel = new MZZ.Model.tb_user(); if (usersList.Count > 0)//更新 { usersModel = usersList[0]; } WeiXinSDK.QRCodeTicket ticket = WeiXinSDK.WeiXin.CreateQRCode2(userinfo.openid); if (!string.IsNullOrEmpty(uopenid) && string.IsNullOrEmpty(usersModel.user_up_openid)) { MZZ.BLL.tb_user waiterBLL = new MZZ.BLL.tb_user(); var waiterList = waiterBLL.GetModelList("user_openid='" + uopenid + "'"); if (waiterList.Count > 0 && usersModel.user_openid != waiterList[0].user_openid) { usersModel.user_uopenid_time = Convert.ToDateTime(DateTime.Now); usersModel.user_up_openid = waiterList[0].user_openid;//设置上线 } } if (userinfo2.subscribe == 1) { usersModel.user_sub_scribe = true; usersModel.user_nick_name = HttpUtility.UrlEncode(userinfo2.nickname, System.Text.Encoding.UTF8); usersModel.user_head_img_url = userinfo2.headimgurl; usersModel.user_sub_time = Convert.ToDateTime(DateTime.Now); } else { usersModel.user_sub_scribe = false; usersModel.user_nick_name = HttpUtility.UrlEncode(userinfo.nickname, System.Text.Encoding.UTF8); usersModel.user_head_img_url = userinfo.headimgurl; usersModel.user_qr_code = ticket.ticket; usersModel.user_sub_time = Convert.ToDateTime(DateTime.Now); } if (usersList.Count > 0)//更新 { userBLL.Update(usersModel); } else { usersModel.user_qr_code = ticket.ticket; usersModel.user_phone = userinfo.openid; usersModel.user_openid = userinfo.openid; userBLL.Add(usersModel); } } catch (Exception ex) { throw ex; } }