Ejemplo n.º 1
0
        /// <summary>
        /// 获得用户信息
        /// </summary>
        /// <param name="qqOauthInfo"></param>
        /// <param name="openID"></param>
        /// <returns></returns>
        public static UserInfo GetUserInfo(QQOauthInfo qqOauthInfo, string openID)
        {
            string urlGetInfo  = string.Format(@"https://graph.qq.com/user/get_user_info?access_token={0}&oauth_consumer_key={1}&openid={2}", qqOauthInfo.AccessToken, appID, openID);
            string resUserInfo = GetUrlResponse(urlGetInfo, Encoding.UTF8);

            InfraStructure.Log.InfoLog.Log("SYSTEM", "GetUserInfo", "GetUserInfo", resUserInfo);
            dynamic   obj     = JsonConvert.DeserializeObject(resUserInfo);
            QQAccount qqlogin = new QQAccount()
            {
                nickname = obj.nickname,
                //使用40*40的QQ头像图片作为标准头像
                figureurl  = obj.figureurl_qq_1,
                gender     = obj.gender,
                OpenID     = openID,
                LastAccess = DateTime.Now
            };
            var qqAccount = GetQQAccountByOpenId(qqlogin.OpenID);

            if (qqAccount == null)
            {
                //没有该用户,添加
                var      qqId     = InsertQQAccount(qqlogin);
                UserInfo userinfo = new UserInfo()
                {
                    RegisterAccountID = qqId,
                    TopicList         = new List <string>(),
                    TagList           = new List <string>(),
                    RegisterMethod    = QQ,
                    NickName          = qqlogin.nickname,
                    Avatar_url        = qqlogin.figureurl,
                    Privilege         = UserType.Normal,
                    ContainTag        = string.Empty,
                    AntiTag           = string.Empty,
                    Catalog           = new List <string>(),
                    Level             = new List <ArticleLevel>()
                };
                var userinfoId = UserInfo.InsertUserInfo(userinfo);
                if (userinfoId == 1.ToString(SnFormat))
                {
                    //第一个用户是管理员(该代码仅仅被执行一次)
                    userinfo.Privilege = UserType.Admin;
                    UserInfo.UpdateUserInfo(userinfo);
                }
                //更新UserInfoID
                qqlogin.UserInfoID = userinfoId;
                UpdateQQAccount(qqlogin);

                //Welcome
                var articleurl = "<a href = '/Article/Index?ArticleId=00000006'>网站使用方法</a>";
                SiteMessage.CreateNotify(userinfoId, "欢迎加入CodeSnippet.info,请阅读[" + articleurl + "]");
                return(userinfo);
            }
            else
            {
                //用新的信息替换旧的信息
                qqAccount.figureurl = qqlogin.figureurl;
                qqAccount.nickname  = qqlogin.nickname;
                UpdateQQAccount(qqAccount);

                var userinfo = GetUserInfoByRegMethodInnerId(qqAccount.Sn);
                userinfo.Avatar_url = qqlogin.figureurl;
                userinfo.NickName   = qqlogin.nickname;

                //防御性代码
                if (userinfo.TagList == null)
                {
                    userinfo.TagList = new List <string>();
                }
                if (userinfo.TopicList == null)
                {
                    userinfo.TopicList = new List <string>();
                }
                if (userinfo.Catalog == null)
                {
                    userinfo.Catalog = new List <string>();
                }
                if (userinfo.Level == null)
                {
                    userinfo.Level = new List <ArticleLevel>();
                }
                if (userinfo.ContainTag == null)
                {
                    userinfo.ContainTag = string.Empty;
                }
                if (userinfo.AntiTag == null)
                {
                    userinfo.AntiTag = string.Empty;
                }

                UserInfo.UpdateUserInfo(userinfo);
                return(userinfo);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获得用户
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public static UserInfo GetUserInfo(string Code)
        {
            var gitlogin = GetGithubAccount(Code);
            //获得GitAccount
            GithubAccount gitAccount = GetGitAccountByLogin(gitlogin.Login);

            if (gitAccount == null)
            {
                //没有该用户,添加
                var      GitId    = InsertGithubAccount(gitlogin);
                UserInfo userinfo = new UserInfo()
                {
                    RegisterAccountID = GitId,
                    TopicList         = new List <string>(),
                    TagList           = new List <string>(),
                    //具有50个追随者的普通是特约作者
                    Privilege      = gitlogin.Followers >= 50 ? UserType.Author : UserType.Normal,
                    RegisterMethod = Github,
                    NickName       = gitlogin.Name,
                    Avatar_url     = gitlogin.Avatar_url,
                    ContainTag     = string.Empty,
                    AntiTag        = string.Empty,
                    Catalog        = new List <string>(),
                    Level          = new List <ArticleLevel>()
                };
                var userinfoId = UserInfo.InsertUserInfo(userinfo);
                if (userinfoId == 1.ToString(SnFormat))
                {
                    //第一个用户是管理员(该代码仅仅被执行一次)
                    userinfo.Privilege = UserType.Admin;
                    UserInfo.UpdateUserInfo(userinfo);
                }
                //更新UserInfoID
                gitlogin.UserInfoID = userinfoId;
                UpdateGithubAccount(gitlogin);

                //Welcome
                var articleurl = "<a href = '/Article/Index?ArticleId=00000006'>网站使用方法</a>";
                SiteMessage.CreateNotify(userinfoId, "欢迎加入CodeSnippet.info,请阅读[" + articleurl + "]");
                return(userinfo);
            }
            else
            {
                //用新的信息替换旧的信息
                gitAccount.Avatar_url = gitlogin.Avatar_url;
                gitAccount.Blog       = gitlogin.Blog;
                gitAccount.Company    = gitlogin.Company;
                gitAccount.Email      = gitlogin.Email;
                gitAccount.Followers  = gitlogin.Followers;
                gitAccount.Following  = gitlogin.Following;
                gitAccount.Html_url   = gitlogin.Html_url;
                gitAccount.Location   = gitlogin.Location;
                gitAccount.Name       = gitlogin.Name;
                UpdateGithubAccount(gitAccount);
                var userinfo = GetUserInfoByRegMethodInnerId(gitAccount.Sn);
                //这里必须返回gitAccount,因为gitlogin没有UserInfoID
                userinfo.Avatar_url = gitlogin.Avatar_url;
                userinfo.NickName   = gitlogin.Name;

                //防御性代码
                if (userinfo.TagList == null)
                {
                    userinfo.TagList = new List <string>();
                }
                if (userinfo.TopicList == null)
                {
                    userinfo.TopicList = new List <string>();
                }
                if (userinfo.Catalog == null)
                {
                    userinfo.Catalog = new List <string>();
                }
                if (userinfo.Level == null)
                {
                    userinfo.Level = new List <ArticleLevel>();
                }
                if (userinfo.ContainTag == null)
                {
                    userinfo.ContainTag = string.Empty;
                }
                if (userinfo.AntiTag == null)
                {
                    userinfo.AntiTag = string.Empty;
                }

                //具有50个追随者的普通人是特约作者
                if (gitlogin.Followers >= 50)
                {
                    if (userinfo.Privilege == UserType.Normal)
                    {
                        userinfo.Privilege = UserType.Author;
                    }
                }
                UserInfo.UpdateUserInfo(userinfo);
                return(userinfo);
            }
        }