/// <summary> /// 绑定微信 /// </summary> /// <returns></returns> public ActionResult Auth() { ConfigInfo config = ConfigInfo.GetInfo(); OAuthManage _oauth = new OAuthManage(config.AppID, config.AppSecret, new LinkManage().GetUserOAthHandle()); string wxUrl = _oauth.GetCodeUrl(); string redirecturl = Url_Shop.GetRecirectUrl(); wxUrl += "?redirecturl=" + redirecturl; if (Url_Mobile.IsWechat()) { return(Redirect(wxUrl)); } ViewBag.wxUrl = wxUrl; return(View()); }
public ActionResult OAuthHandle() { ConfigInfo config = ConfigInfo.GetInfo(); OAuthManage _oauth = new OAuthManage(config.AppID, config.AppSecret, new LinkManage().GetUserOAthHandle()); string recirecturl = Url_Shop.GetRecirectUrl(); LogHelper.Debug("OAuthHandle-redirecturl:" + recirecturl); string result = ""; try { //注册事件处理 _oauth.OnError = (e) => { string msg = ""; Exception inner = e; while (inner != null) { msg += inner.Message; inner = inner.InnerException; } result = msg; LogOperate.Write(msg); }; _oauth.OnGetTokenSuccess = (token) => { result += "<br/>"; result += token.ToJsonString(); LogOperate.Write("获取token成功:" + result); }; //特别处理获取用户信息成功 _oauth.OnGetUserInfoSuccess = (user) => { result += "<br/>"; result += user.ToJsonString(); CookieHelper.SetCookie("openid", user.openid, 1); CookieHelper.SetCookie("nickname", user.nickname, 1); CookieHelper.SetCookie("headimgurl", user.headimgurl, 1); //判断openid的用户是否存在 Member_Info model = DB.Member_Info.GetModelByOpenID(user.openid); if (model != null) { //保存信息到客户端同步登录 User_Shop.SetUser(model); } else { if (model == null) { //如果是游客,返回登录 if (string.IsNullOrEmpty(recirecturl)) { recirecturl = $"http://{Request.Url.Host}/mobile/login"; } } else { model.OpenID = user.openid; if (string.IsNullOrEmpty(model.NickName)) { model.NickName = user.nickname; } model.Photo = user.headimgurl; DB.Member_Info.Update(model); //保存信息到客户端同步登录 User_Shop.SetUser(model); recirecturl = Url_Mobile.GetUserCenter(); } } }; //第二步 _oauth.GetAccess_Token(); //第三步 _oauth.GetUserInfo(); //显示结果 ViewBag.msg = result; if (string.IsNullOrEmpty(recirecturl)) { recirecturl = $"http://{Request.Url.Host}/mobile"; } return(Redirect(recirecturl)); } catch (Exception ex) { string msg = ""; Exception inner = ex; while (inner != null) { msg += inner.Message; inner = inner.InnerException; } return(Content(result + "----->" + msg + "<br />" + ex.StackTrace)); } }