/// <summary> /// 验证微信是否已经授权-登陆账号 /// </summary> public void wechatlogin() { string cid = CookieTool.GetCookieString("qrcodeid"); int id = 0; if (cid != "") { int.TryParse(cid, out id); Lebi_weixin_qrcode model = B_Lebi_weixin_qrcode.GetModel(id); if (model != null) { if (model.User_id > 0) { //CookieTool.SetCookieString("qrcodeid", "0", -1); CookieTool.DeleteCookie("qrcodeid"); Lebi_User user = B_Lebi_User.GetModel(model.User_id); EX_User.LoginOK(user); Response.Write("{\"msg\":\"OK\"}"); return; } } } Response.Write("{\"msg\":\"NO\"}"); }
/// <summary> /// 生成登录二维码 /// </summary> public void GetQrCode() { string cid = CookieTool.GetCookieString("qrcodeid"); int id = 0; string url = ""; int DT_id = ShopPage.GetDT(); Lebi_weixin_qrcode model; int.TryParse(cid, out id); string where = "id='" + id + "'"; if (DT_id > 0) { where += " and DT_id =" + DT_id + ""; } model = B_Lebi_weixin_qrcode.GetModel(where); if (model == null) { model = new Lebi_weixin_qrcode(); model.User_id = CurrentUser.id; model.DT_id = DT_id; B_Lebi_weixin_qrcode.Add(model); id = B_Lebi_weixin_qrcode.GetMaxId(); CookieTool.SetCookieString("qrcodeid", id.ToString(), 60); } string token = RequestTool.RequestString("token"); string backurl = RequestTool.RequestString("url"); backurl = HttpUtility.UrlDecode(backurl); if (EX_User.MD5(SYS.InstallCode + backurl) != token) { backurl = URL("P_Index", ""); } if (backurl.Contains("?")) { backurl += "&qrcodeid=" + id; } else { backurl += "?qrcodeid=" + id; } backurl = HttpUtility.UrlEncode(backurl); url = Shop.Platform.weixin.GetInstance(DT_id, CurrentSite).LoginURL(backurl); //QRCode.Instance.CreateImage(url, id.ToString()); Bitmap image = QRCode.Instance.CreateImage(url); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); //删除60分钟前的二维码 //string where = "datediff(n,Time_add,'" + System.DateTime.Now + "')>60"; //List<Lebi_weixin_qrcode> qs = B_Lebi_weixin_qrcode.GetList(where, ""); //foreach (Lebi_weixin_qrcode q in qs) //{ // ImageHelper.DeleteImage("/qrcode/" + q.id + ".png"); //} //B_Lebi_weixin_qrcode.Delete(where); ////================ //Response.Write("{\"msg\":\"OK\",\"img\":\"" + WebPath + "/qrcode/" + id + ".png\"}"); }
/// <summary> /// 微信登录 /// https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code /// </summary> /// <param name="back"></param> /// <returns></returns> public string Login(string back, int IsLogin = 1) { string code = RequestTool.RequestString("code"); if (code != "") { try { string uri = reurnurl + "?backurl=" + ENBackuri(back); int userid = 0; uri = System.Web.HttpUtility.UrlEncode(uri); string res = Post("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + appkey + "&code=" + code + "&grant_type=authorization_code"); JavaScriptSerializer jss = new JavaScriptSerializer(); Model.weixin.tokeninfo tokeninfo = jss.Deserialize <Model.weixin.tokeninfo>(res); string uid = tokeninfo.openid; //SystemLog.Add(uid); //获取用户资料 //https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN res = Post("https://api.weixin.qq.com/sns/userinfo?access_token=" + tokeninfo.access_token + "&openid=" + uid + "&lang=zh_CN"); Model.weixin.userinfo model = jss.Deserialize <Model.weixin.userinfo>(res); string where = "bind_weixin_id='" + uid + "' and bind_weixin_id!=''"; //if (DT_id > 0) //{ // where += " and DT_id =" + DT_id + ""; //} Lebi_User user = B_Lebi_User.GetModel(where); Lebi_User CurrentUser = null; string qrcodeid_ = RegexTool.GetRegValue((back + "&"), @"qrcodeid=(\d*)&"); int qrid = 0; int.TryParse(qrcodeid_, out qrid); if (qrid > 0) { //扫描二维码授权绑定当前账号 Lebi_weixin_qrcode qcode = B_Lebi_weixin_qrcode.GetModel(qrid); CurrentUser = B_Lebi_User.GetModel(qcode.User_id); if (IsLogin == 0) { if (user != null && CurrentUser != null) { return("已绑定其它帐号"); } } } if (user == null) { Lebi_UserLevel defaultlevel = B_Lebi_UserLevel.GetModel("Grade>0 order by Grade asc"); if (defaultlevel == null) { defaultlevel = new Lebi_UserLevel(); } if (defaultlevel.RegisterType == 0) //关闭注册 { return("会员注册已关闭"); } if (CurrentUser == null) { user = new Lebi_User(); user.bind_weixin_id = uid; user.bind_weixin_nickname = model.nickname; user.bind_weixin_token = tokeninfo.access_token; try { user.Face = DownLoadImage(model.headimgurl); } catch { user.Face = model.headimgurl; } user.NickName = model.nickname; user.UserName = "******" + uid; user.Password = EX_User.MD5(uid); user.Language = Language.CurrentLanguage().Code; user.Sex = model.sex == "2" ? "女" : "男"; user.UserLevel_id = B_Lebi_UserLevel.GetList("Grade>0", "Grade asc").FirstOrDefault().id; user.IsPlatformAccount = 1; //B_Lebi_User.Add(user); //user.id = B_Lebi_User.GetMaxId(); //userid = user.id; user.DT_id = DT_id; EX_User.UserReg(user); } else { CurrentUser.bind_weixin_id = uid; CurrentUser.bind_weixin_nickname = model.nickname; CurrentUser.bind_weixin_token = tokeninfo.access_token; try { CurrentUser.Face = DownLoadImage(model.headimgurl); } catch { CurrentUser.Face = model.headimgurl; } //CurrentUser.Face = model.headimgurl;//头像 50*50 if (CurrentUser.NickName == "") { CurrentUser.NickName = model.nickname; } CurrentUser.DT_id = DT_id; //B_Lebi_User.Update(CurrentUser); EX_User.LoginOK(CurrentUser, 0); userid = CurrentUser.id; } } else { user.bind_weixin_id = uid; user.bind_weixin_nickname = model.nickname; user.bind_weixin_token = tokeninfo.access_token;; try { user.Face = DownLoadImage(model.headimgurl); } catch (Exception ex) { user.Face = ""; LB.Tools.TxtLog.Add(ex.Message); } if (user.NickName == "") { user.NickName = model.nickname; } user.DT_id = DT_id; //user.Sex = model.gender == "f" ? "女" : "男"; //B_Lebi_User.Update(user); EX_User.LoginOK(user, 0); userid = user.id; } if (qrid > 0) { //扫描二维码登录 Lebi_weixin_qrcode qcode = B_Lebi_weixin_qrcode.GetModel(qrid); qcode.User_id = userid; B_Lebi_weixin_qrcode.Update(qcode); //Log.Add(back); } return("OK"); } catch (Exception ex) { LB.Tools.TxtLog.Add(ex.Message); return("授权失败" + ex.Message); } } return("授权失败"); }