<th id="v9g6b"><track id="v9g6b"></track></th>

  • <em id="v9g6b"><acronym id="v9g6b"></acronym></em>
    <progress id="v9g6b"><pre id="v9g6b"></pre></progress>
    <tbody id="v9g6b"></tbody>
    <button id="v9g6b"><acronym id="v9g6b"></acronym></button>
    <rp id="v9g6b"><ruby id="v9g6b"></ruby></rp>

        <dd id="v9g6b"></dd>

        <em id="v9g6b"></em>

          Web开发框架微平台公众号一个登陆页面实现代码

          Web开发框架微平台公众号一个登陆页面实现代码,微信公众号在第三方微平台登陆怎么做?

          Web开发框架微平台登陆公众号

          直接贴上代码:

           /// <summary>
              ///Web开发框架 WXLogin1 的摘要说明
              /// </summary>
              public partial class WXLogin1 : AjaxBase, IRequiresSessionState
              {
                  string code = ""; //微信返回的Code
                  //页面初始化
                  protected override void PageLoad()
                  {
                      base.PageLoad();
                      code = Parse.String("code", "");
                  }
                  //Web开发框架发起微信登录请求
                  [QD.Web.Ajax.AjaxMethod]
                  public void Login()
                  {
                      //第一步: 获取code
                      Response.Redirect(string.Format(WXLoginConfig.Url_Sep1, WXLoginConfig.APPID, WXLoginConfig.Redirect_Uri));
                  }
                  //微信授权后,回调函数
                  [QD.Web.Ajax.AjaxMethod]
                  public void CallBack()
                  {
                      if (string.IsNullOrEmpty(code))
                          JsonResult(500, "授权失败,code参数无效!");
                      mShop model = Oauth();                    //微信授权
                      if (model != null)
                          UserLogin(model, model.open_id);                //验证成功后,注册用户、登录
                  }
                  mShop Oauth()
                  {
                      //第二步:根据code获取token失败
                      string url2 = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", WXLoginConfig.APPID, WXLoginConfig.APPSECRET, code);
                      string html2 = GetHtml(url2);
                      MWXLogin json = JsonParse<MWXLogin>(html2);
                      if (json == null || string.IsNullOrEmpty(json.Access_Token))
                          JsonResult(500, "'授权失败!无法获取access_token,返回值:' " + html2);
                      //第三步: 根据openid和access_token查询用户信息
                      string url3 = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", json.Access_Token, json.OpenID);
                      string html3 = GetHtml(url3);
                      json = JsonParse<MWXLogin>(html3);
                      //web开发框架获取昵称、性别、头像、国家、省份、城市,openid等参数
                      if (string.IsNullOrEmpty(json.OpenID))
                          JsonResult(500, "授权失败!无法获取昵称、头像等信息,返回值:" + html3);
                      return GetModel(json);
                  }
                  void UserLogin(mShop model, string unionid)
                  {
                      //登录成功后的信息
                      int userid = 0;
                      int code = DaoUser.Login(model, out userid);
                      model.Id = userid;
                      //获取会员版本
                      if (model.Id > 0)
                      {
                          model.GroupId = GetGroupId(model.Id);
                      }
                      if (code == 4007)
                          ShowLoginError(4007);   //账户被禁用
                      if (code == 4005)
                      {
                          //用户名不存在,注册新账号
                          code = Register(model);
                          //添加积分
                          ShopPointBLL.AddByOption(model.Id, PointOption.MyRegister);
                      }
                      if (code == 0 || code == 4005)
                      {
                          //通过二维码方式邀请的, 加入亲友圈
                          string msg = string.Empty;
                          if (Request.Cookies["fromid"] != null)
                              msg = AddFriend(model.Id);
                          ShowLoginSuccess(model, msg);
                      }
                      else
                          ShowLoginError(code);
                  }
                  int Register(mShop model)
                  {
                      int user_id = 0;
                      int code = new ShopBLL().Regist(RegisterType.WeChart, model.NickName, model.Logo, model.open_id, customId, out user_id);
                      model.Id = user_id;
                      return code;
                  }
                  mShop GetModel(MWXLogin json)
                  {
                      Random rand = new Random();
                      mShop model = new mShop();
                      model.Name = "wx" + rand.Next(10000000, 99999999);
                      model.NickName = json.NickName;
                      model.Logo = json.HeadImgUrl;   //头像,完整URL
                      model.login_type = 3;           //1 账号密码登录,2 QQ登录,3微信登录
                      model.Phone = "";
                      model.Password = "";
                      model.QQ = "";
                      model.Email = "";
                      model.open_id = json.UnionID;    //必须用unionID,这样才能保证app和网页登录是同一个微信账号
                      return model;
                  }
                  /// <summary>
                  /// 获取会员版本
                  /// </summary>
                  int GetGroupId(int userid)
                  {
                      Dictionary<string, object> dic = new ShopDAO().Get(userid, "group_id,expiretime");
                      if (dic != null && dic["expiretime"] != DBNull.Value)
                      {
                          DateTime expiretime = Convert.ToDateTime(dic["expiretime"]);
                          if (expiretime.Subtract(DateTime.Now).TotalDays >= 0)
                              return Convert.ToInt32(dic["group_id"]);
                      }
                      return 0;
                  }
                  /// <summary>
                  /// 通过二维码邀请方式,添加亲友
                  /// </summary>
                  /// <param name="userid"></param>
                  string AddFriend(int login_userid)
                  {
                      MoFriend modelFriend = new MoFriend()
                         {
                             userid = Convert.ToInt32(Request.Cookies["fromid"].Value),
                             union_id = login_userid,
                             circle_id = Convert.ToInt32(Request.Cookies["from_circleid"].Value),
                             state = 1,
                             date = DateTime.Now
                         };
                      return FriendBLL.AddFriendByCode(modelFriend, login_userid);
                      //DaoShopGroup dalGroup = new DaoShopGroup();
                      //try
                      //{
                      //    int add_shop_id = Convert.ToInt32(Request.Cookies["fromid"].Value);
                      //    //获取会员的版本
                      //    Dictionary<string, object> group = dalGroup.GetMyUsed(add_shop_id);
                      //    //更新会员VIP版本
                      //    if (group != null)
                      //        UserBLL.UpdateVersion(userid, Convert.ToInt32(group["groupid"]), Convert.ToDateTime(group["endtime"]), add_shop_id);
                      //}
                      //catch (Exception ex)
                      //{
                      //    new DaoLog().add("wxreg", string.Format("注册后自动升级VIP失败,{0}", ex.Message));
                      //}
                  }
                  void ShowLoginError(int code)
                  {
                      Response.Redirect(string.Format("{0}/wap/error.html?code={1}&msg=", ZYBConfig.Path.Domain, code));
                  }
                  void ShowLoginSuccess(mShop model, string msg)
                  {
                      //记录登录状态
                      HttpCookie c1 = new HttpCookie("logintype", "3");
                      HttpCookie c2 = new HttpCookie("nick_name", QD.WX.Utill.Util.UrlEncode(model.NickName));
                      HttpCookie c3 = new HttpCookie("userid", model.Id.ToString());
                      HttpCookie c4 = new HttpCookie("groupid", model.GroupId.ToString());
                      c1.Expires = c2.Expires = c3.Expires = c4.Expires = DateTime.Now.AddDays(30);
                      Response.Cookies.Add(c1);
                      Response.Cookies.Add(c2);
                      Response.Cookies.Add(c3);
                      Response.Cookies.Add(c4);
                      //跳转到亲友页面
                      if (Request.Cookies["fromid"] != null)
                      {
                          string circle_id = Request.Cookies["from_circleid"].Value;
                          HttpCookie c5 = new HttpCookie("fromid", "0");
                          HttpCookie c6 = new HttpCookie("from_circleid", "0");
                          c5.Expires = c6.Expires = DateTime.Now.AddDays(-1);
                          Response.Cookies.Add(c5);        //清空from_userid
                          Response.Cookies.Add(c6);       //清空from_circleid
                          Response.Redirect(string.Format("/wap/qy_friend.html?circle_id={0}&addmsg={1}", circle_id, msg));
                      }
                      else if (Request.Cookies["backurl"] != null)
                      {
                          //跳转到Web开发框架Web开发框架微云登陆指定页面,如:扫码绑定血压、血糖设备
                          Response.Redirect(HttpContext.Current.Server.UrlDecode(Request.Cookies["backurl"].Value));    //登录后跳转,在扫描二维码绑定设备中用到
                      }
                      else
                      {
                          //跳转到会员中心
                          Response.Redirect(string.Format("{0}/wap/my.html?code={1}&userid={2}&groupid={3}&logintype={4}&nick_name={5}", ZYBConfig.Path.Domain, code, model.Id, model.GroupId, 3, model.NickName));
                      }
                  }
                  string GetHtml(String url)
                  {
                      return WebHttpHelper.Request(url);
                  }
              }
          标签: Web开发框架微信公众号开发技术系列标签

          网站&系统开发技术学习交流群:463167176

          本站文章除注明转载外,均为本站原创或翻译,欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,共创和谐网络环境。
          转载请注明:文章转载自:软件开发框架 ? Web开发框架微平台公众号一个登陆页面实现代码
          本文标题:Web开发框架微平台公众号一个登陆页面实现代码
          本文地址:http://www.sajuice.com/OrgTec/Back/0029.html

          相关文章: 多系统单点登录概念和原理_B/S开发框架

          电话
          电话 18718672256

          扫一扫
          二维码
          本港台开奖 <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <文本链> <文本链> <文本链> <文本链> <文本链> <文本链>