Ejemplo n.º 1
0
        public ActionResult AutoLogin()
        {
            string code   = RQuery["code"];
            string state  = RQuery["state"];
            string refUrl = RQuery["refUrl"];
            string openid = RQuery["openid"];

            if (string.IsNullOrEmpty(openid))
            {
                OAuthAccessTokenResult authResult = OAuth.GetAccessToken(MpCenterCache.AppID, MpCenterCache.AppSecret, code);
                openid = authResult.openid;
            }
            if (!string.IsNullOrEmpty(openid))
            {
                MpUser currUser = MpUserService.GetByOpenID(openid);
                if (currUser == null)
                {
                    currUser               = new MpUser();
                    currUser.MpID          = MpCenterCache.Id;
                    currUser.CreateDate    = DateTime.Now;
                    currUser.MpGroupID     = MpUserGroupService.GetSystemGroup("默认分组").Id;
                    currUser.IsSubscribe   = true;
                    currUser.LastVisitDate = DateTime.Now;
                    currUser.OpenID        = openid;
                    currUser.OrginID       = MpCenterCache.OriginID;
                    currUser.UserState     = 0;
                    MpUserService.Insert(currUser);
                }
                if (currUser != null)
                {
                    Log4NetImpl.Write("oAuth:" + currUser.OpenID);
                    WriteMpUserCookie(currUser);
                }
                if (string.IsNullOrEmpty(OpenID))
                {
                    return(Content("用户未识别"));
                }
                if (!string.IsNullOrEmpty(refUrl))
                {
                    Response.Redirect(refUrl);
                }
            }
            return(Content("请联系管理员"));
        }
Ejemplo n.º 2
0
        public MpUser AysnUser(string OpenId)
        {
            MpUser currUser = MpUserService.GetByOpenID(OpenId);

            if (currUser == null && !string.IsNullOrEmpty(OpenId))
            {
                currUser               = new MpUser();
                currUser.MpID          = MpCenterCache.Id;
                currUser.CreateDate    = DateTime.Now;
                currUser.MpGroupID     = MpUserGroupService.GetSystemGroup("默认分组").Id;
                currUser.IsSubscribe   = true;
                currUser.LastVisitDate = DateTime.Now;
                currUser.OpenID        = OpenId;
                currUser.OrginID       = MpCenterCache.OriginID;
                currUser.UserState     = 0;
                currUser.SceneId       = 0;
                string token = GetAccessToken();
                if (!string.IsNullOrEmpty(token))
                {
                    try
                    {
                        UserInfoJson info = Senparc.Weixin.MP.AdvancedAPIs.User.Info(token, OpenId);
                        if (info != null)
                        {
                            currUser.City          = info.city;
                            currUser.Country       = info.country;
                            currUser.HeadImgUrl    = info.headimgurl;
                            currUser.Language      = info.language;
                            currUser.NickName      = info.nickname;
                            currUser.Province      = info.province;
                            currUser.SubscribeDate = DateTimeHelper.GetDateTimeFromXml(info.subscribe_time);
                            currUser.Sex           = info.sex;
                        }
                    }
                    catch (Exception e)
                    {
                        Log4NetImpl.Write("AysnUser失败:" + e.Message);
                    }
                }
                MpUser currUserCopy = MpUserService.GetByOpenID(OpenId);
                if (currUserCopy == null || currUserCopy.Id == Guid.Empty)
                {
                    MpUserService.Insert(currUser);
                    Log4NetImpl.Write("AysnUserInsert结束");
                }
                else
                {
                    currUser.Id = currUserCopy.Id;
                    MpUserService.Update(currUser);
                    Log4NetImpl.Write("AysnUserUpdate结束");
                }
            }
            else
            {
                if (!currUser.IsSubscribe)
                {
                    currUser.IsSubscribe   = true;
                    currUser.SubscribeDate = DateTime.Now;
                    MpUserService.Update(currUser);
                }
            }
            return(currUser);
        }