public void LoadInit(int customerid, bool tryreg = true) { this.CurrentCustomerID = customerid; //判断来源微信环境 bool isWechatEnvironment = this.ValidateMicroMessenger(); if (!isWechatEnvironment) { Response.Redirect("/WechatPropmt.html"); } string callbakUrl = Server.UrlEncode(HttpContext.Current.Request.RawUrl); string oauthUrl = "/OAuth2/WeixinAuthorize.aspx?userinfo=0&scope=1&uda=0&customerid=" + this.CurrentCustomerID + "&tryreg=" + (tryreg ? 1 : 0) + "&redirecturl=" + callbakUrl; //用户授权 WeixinOAuthUserInfoModel model = UserLogic.Instance.GetUserInfo(this.CurrentCustomerID); if (model != null) { this.OpenId = model.Openid; if (!string.IsNullOrEmpty(model.Nickname)) { this.SetOpenIdVal(this.OpenId); } } else { Response.Redirect(oauthUrl); } }
protected override void OnPreInit(EventArgs e) { this.CurrentCustomerID = 1003; string _openid = this.GetQueryString("openid", ""); string callbakUrl = Server.UrlEncode(HttpContext.Current.Request.Url.AbsoluteUri); string oauthUrl = "http://yilite.huobanmall.com/OAuth2/WeixinAuthorize.aspx?retuinfo=&scope=0&customerid=6457&redirecturl=" + callbakUrl; if (string.IsNullOrEmpty(_openid)) { WeixinOAuthUserInfoModel model = UserService.Instance.GetUserInfo(this.CurrentCustomerID); if (model == null) { Response.Redirect(oauthUrl); } } else { UserService.Instance.SetUserInfo(this.CurrentCustomerID, new WeixinOAuthUserInfoModel() { City = "", Country = "", Headimgurl = "", Nickname = "", Openid = _openid, Privilege = null, Province = "", Sex = 0, UnionID = "" }); } }
/// <summary> /// 获取微信授权用户信息 /// </summary> /// <param name="customerId">商户Id</param> /// <returns></returns> public WeixinOAuthUserInfoModel GetUserInfo(int customerId) { string keyUserinfo = this.GetUserinfoDataKey(customerId); string encryptedUserInfo = CookieHelper.GetCookieVal(keyUserinfo); if (string.IsNullOrEmpty(encryptedUserInfo)) { //尝试从session中读取 if (HttpContext.Current.Session[keyUserinfo] != null) { WeixinOAuthUserInfoModel seModel = HttpContext.Current.Session[keyUserinfo] as WeixinOAuthUserInfoModel; return(seModel); } return(null); } try { WeixinOAuthUserInfoModel model = JsonConvert.DeserializeObject <WeixinOAuthUserInfoModel>(EncryptHelper.Decrypt(encryptedUserInfo, ENCRYPTKEY)); return(model); } catch (Exception ex) { LogHelper.Write(string.Format("WeixinOAuthUserDataProvider->GetUserInfo发生异常:{0}", ex.Message)); } return(null); }
/// <summary> /// 保存微信授权用户信息 /// </summary> /// <param name="customerId">商户Id</param> /// <param name="model"></param> public void SetUserInfo(int customerId, WeixinOAuthUserInfoModel model) { string json = JsonConvert.SerializeObject(model); string jsonEncrypt = EncryptHelper.Encrypt(json, ENCRYPTKEY); CookieHelper.SetCookieValByCurrentDomain(this.GetUserinfoDataKey(customerId), 1, jsonEncrypt); //双保险,session也存储 HttpContext.Current.Session[this.GetUserinfoDataKey(customerId)] = model; }
public int RegisterUser(WeixinOAuthUserInfoModel oauth) { UserModel model = new UserModel() { HeadImg = oauth.Headimgurl, openId = oauth.Openid, NickName = oauth.Nickname }; return(dal.AddUserInfo(model)); }
protected void Page_Load(object sender, EventArgs e) { IsSignUp = 0; WeixinOAuthUserInfoModel userModel = UserService.Instance.GetUserInfo(this.CurrentCustomerID); if (userModel != null) { data = UserSignUp.Instance.GetSignUpUserInfo(CurrentCustomerID, userModel.Openid, 0); if (data != null) { IsSignUp = 1; } } }
private void signUp() { try { string name = this.GetFormValue("name", ""); string mobile = this.GetFormValue("mobile", ""); string province = this.GetFormValue("province", ""); string city = this.GetFormValue("city", ""); string county = this.GetFormValue("county", ""); int customerid = this.GetFormValue("customerid", 0); WeixinOAuthUserInfoModel userInfo = UserService.Instance.GetUserInfo(customerid); UserSignModel model = new UserSignModel() { name = name, Province = province, city = city, address = county, customerid = customerid, mobile = mobile, openid = userInfo.Openid, nickname = userInfo.Nickname, headimgurl = userInfo.Headimgurl, Type = this.GetFormValue("type", 0), content1 = this.GetFormValue("content1", ""), content2 = this.GetFormValue("content2", ""), content3 = this.GetFormValue("content3", ""), content4 = this.GetFormValue("content4", ""), }; if (UserSignUp.Instance.AddSignUpUser(model) > 0) { this.code = 1; } else { this.msg = "预约失败"; } } catch (Exception ex) { this.msg = "预约失败"; LogHelper.Error(string.Format("signUp--->>message:{0} ,StackTrace:{1}", ex.Message, ex.StackTrace)); } }
protected override void OnPreInit(EventArgs e) { string callbakUrl = Server.UrlEncode(HttpContext.Current.Request.RawUrl); string oauthUrl = "/OAuth2/WeixinAuthorize.aspx?userinfo=0&scope=1&uda=0&customerid=" + this.CurrentCustomerID + "&tryreg=" + (tryreg ? 1 : 0) + "&redirecturl=" + callbakUrl; //用户授权 WeixinOAuthUserInfoModel model = UserService.Instance.GetUserInfo(this.CurrentCustomerID); if (model != null) { } else { Response.Redirect(oauthUrl); } this.CurrentCustomerID = 1003; //用户授权 WeixinOAuthUserInfoModel model = UserService.Instance.GetUserInfo(this.CurrentCustomerID); if (model == null) { UserService.Instance.SetUserInfo(this.CurrentCustomerID, new WeixinOAuthUserInfoModel() { City = "", Country = "", Headimgurl = "", Nickname = "", Openid = StringHelper.RandomNo(new Random(Guid.NewGuid().GetHashCode()), 32), Privilege = null, Province = "", Sex = 0, UnionID = "" }); } }
/// <summary> /// 注册用户 /// </summary> /// <param name="customerId"></param> /// <param name="oauth"></param> /// <returns></returns> public int RegisterUser(int customerId, WeixinOAuthUserInfoModel oauth) { AttendUserModel model = new AttendUserModel() { customerid = customerId, headimgurl = oauth.Headimgurl, openid = oauth.Openid, nickname = oauth.Nickname, city = oauth.City, isAttend = 0, unionId = oauth.UnionID, couponCode = StringHelper.RandomNo(new Random(Guid.NewGuid().GetHashCode()), 16) }; string couponValue = ConfigHelper.GetConfigString("couponValue", "0"); if (!string.IsNullOrEmpty(couponValue)) { model.money = Convert.ToDecimal(couponValue); } model.defmoney = model.money; return(dal.AddUserInfo(model)); }
protected void Page_Load(object sender, EventArgs e) { if (this.Code.Equals("authdeny", StringComparison.CurrentCultureIgnoreCase)) { this.WriteLog("当前用户取消了授权。"); return; } //合法的链接都会带上code和state if (this.Code == "" || this.State == "") { this.WriteLog("不合法的请求"); return; } //分析state中的数据 this.DoAnalyzeState(); if (this.NonceStr == "" || this.CustomerId == 0) { this.WriteLog("state参数残缺"); return; } _redirectUrl = UserService.Instance.GetOAuthUrl(this.NonceStr); //获取带openid的信息 OAuthAccessTokenResult atResult; OAuthUserInfoPlus oauthUserInfo = null; try { string errmsg = string.Empty; //获取openid atResult = OAuth.GetAccessToken(this.AppId(this.CustomerId), this.AppSecret(this.CustomerId), this.Code); if (atResult.errcode != ReturnCode.请求成功) { this.WriteLog(string.Format("code:{0},msg:{1}", atResult.errcode, atResult.errmsg)); return; } #region 使用高级授权获取用户信息 try { // oauthUserInfo = OAuth.GetUserInfo(atResult.access_token, atResult.openid); //新方法 string wxApiUrl = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", atResult.access_token, atResult.openid); MPUserInfoResult result = MPHelper.GetWxUserInfo(wxApiUrl, out errmsg); if (result != null) { this.SetUserInfo(this.CustomerId, new WeixinOAuthUserInfoModel() { City = result.city, Country = result.country, Headimgurl = result.headimgurl, Nickname = result.nickname, Openid = result.openid, Privilege = result.privilege, Province = result.province, Sex = result.sex, UnionID = result.unionid }); oauthUserInfo = new OAuthUserInfoPlus() { city = result.city, country = result.country, headimgurl = result.headimgurl, nickname = result.nickname, openid = result.openid, privilege = result.privilege, province = result.province, sex = result.sex, unionid = result.unionid }; } } catch (Exception ex) { LogHelper.Write("OAuth.GetAccessToken:" + ex.Message); this.WriteLog(ex.Message); return; } try { string url = ""; #region 1、openid处理带上URL url = this.AppendUrlParameters(_redirectUrl, "openid", atResult.openid); #endregion #region 2、授权用户的详细信息JSON带上URL if (this.ReturnUserInfo && oauthUserInfo != null) { string encodedUserInfo = HttpContext.Current.Server.UrlEncode(JsonConvert.SerializeObject(oauthUserInfo)); url = this.AppendUrlParameters(url, "retuinfo", encodedUserInfo); } if (TryReg) { WeixinOAuthUserInfoModel wouInfo = null; if (oauthUserInfo != null) { wouInfo = GetUserInfo(this.CustomerId); } int retuid = UserService.Instance.GetTopOAuthedUserId(this.CustomerId, atResult.openid); if (retuid == 0)//注册 { /** * 执行用户注册操作 */ retuid = UserService.Instance.RegisterUser(this.CustomerId, wouInfo); } url = this.AppendUrlParameters(url, "retuid", retuid.ToString()); } this.StoreOpenId(this.CustomerId, atResult.openid); this.SetOpenId(this.CustomerId, atResult.openid); Response.Redirect(url, false); #endregion } catch (Exception) { throw; } #endregion } catch (Exception) { throw; } }