示例#1
0
        public ActionResultVM SaveUserInfo(Domain.UserInfo mo)
        {
            var vm = new ActionResultVM();

            if (string.IsNullOrWhiteSpace(mo.Nickname))
            {
                vm.Set(ARTag.refuse);
                vm.msg = "昵称不能为空";

                return(vm);
            }

            int uid = new Func.UserAuthAid(HttpContext).Get().UserId;

            using (var db = new ContextBase())
            {
                var usermo = db.UserInfo.Find(uid);

                //变更账号
                if (!string.IsNullOrWhiteSpace(mo.UserName) && usermo.UserNameChange != 1 && usermo.UserName != mo.UserName)
                {
                    //账号重复
                    if (db.UserInfo.Any(x => x.UserName == mo.UserName))
                    {
                        vm.Set(ARTag.exist);
                        vm.msg = "账号已经存在";

                        return(vm);
                    }
                    else
                    {
                        usermo.UserName       = mo.UserName;
                        usermo.UserNameChange = 1;
                    }
                }

                //变更邮箱
                if (mo.UserMail != usermo.UserMail)
                {
                    usermo.UserMailValid = 0;

                    //邮箱正则验证
                    if (!string.IsNullOrWhiteSpace(mo.UserMail))
                    {
                        if (!Fast.ParsingTo.IsMail(mo.UserMail))
                        {
                            vm.Set(ARTag.invalid);
                            vm.msg = "邮箱格式有误";

                            return(vm);
                        }
                        else
                        {
                            if (db.UserInfo.Any(x => x.UserMail == mo.UserMail))
                            {
                                vm.Set(ARTag.exist);
                                vm.msg = "邮箱已经存在";

                                return(vm);
                            }
                        }
                    }
                }

                usermo.UserMail  = mo.UserMail;
                usermo.Nickname  = mo.Nickname;
                usermo.UserPhone = mo.UserPhone;
                usermo.UserUrl   = mo.UserUrl;

                db.UserInfo.Update(usermo);
                var num = db.SaveChanges();

                //更新授权信息
                using (var ac = new AccountController())
                {
                    ac.SetAuth(HttpContext, usermo, true);
                }

                vm.Set(num > 0);
            };

            return(vm);
        }
示例#2
0
        public ActionResultVM UpdateUserPhoto(string type, string source)
        {
            var vm = new ActionResultVM();

            var uinfo = new Func.UserAuthAid(HttpContext).Get();

            try
            {
                var rootdir  = GlobalTo.WebRootPath + "/" + (GlobalTo.GetValue("StaticResource:RootDir").TrimStart('/').TrimEnd('/') + "/");
                var path     = GlobalTo.GetValue("StaticResource:AvatarPath").TrimEnd('/').TrimStart('/') + '/';
                var fullpath = rootdir + path;

                if (!Directory.Exists(fullpath))
                {
                    Directory.CreateDirectory(fullpath);
                }

                if (string.IsNullOrWhiteSpace(uinfo.UserPhoto))
                {
                    uinfo.UserPhoto = Core.UniqueTo.LongId() + ".jpg";
                }
                var upname = uinfo.UserPhoto.Split('?')[0];
                var npnew  = upname + "?" + DateTime.Now.ToTimestamp();

                switch (type)
                {
                case "file":
                {
                    source = source.Substring(source.LastIndexOf(",") + 1);
                    byte[] bytes = Convert.FromBase64String(source);
                    using var ms  = new MemoryStream(bytes);
                    using var bmp = new System.Drawing.Bitmap(ms);
                    var hp = fullpath + upname.Replace(".", "_lg.");
                    bmp.Save(hp, ImageFormat.Jpeg);
                    Fast.ImageTo.MinImg(hp, fullpath, upname, 40, 40, "wh");

                    using (var db = new ContextBase())
                    {
                        var usermo = db.UserInfo.Find(uinfo.UserId);
                        usermo.UserPhoto = npnew;
                        db.UserInfo.Update(usermo);
                        int num = db.SaveChanges();
                        if (num > 0)
                        {
                            using var ac = new AccountController();
                            ac.SetAuth(HttpContext, usermo);
                        }
                    }

                    vm.Set(ARTag.success);
                }
                break;

                case "link":
                {
                    using var wc = new System.Net.WebClient();
                    var hp = fullpath + upname.Replace(".", "_lg.");
                    wc.DownloadFile(source, hp);
                    Fast.ImageTo.MinImg(hp, fullpath, upname, 40, 40, "wh");

                    using (var db = new ContextBase())
                    {
                        var usermo = db.UserInfo.Find(uinfo.UserId);
                        usermo.UserPhoto = npnew;
                        db.UserInfo.Update(usermo);
                        int num = db.SaveChanges();
                        if (num > 0)
                        {
                            using var ac = new AccountController();
                            ac.SetAuth(HttpContext, usermo);
                        }
                    }

                    vm.Set(ARTag.success);
                }
                break;
                }
            }
            catch (Exception ex)
            {
                vm.Set(ex);
            }

            return(vm);
        }