コード例 #1
0
        public JsonResult UploadPicture()
        {
            ViewModel.BaseJsonData json = new ViewModel.BaseJsonData();
            var file = Request.Files["data"];

            if (file == null)
            {
                json.state    = 0;
                json.msg_text = "没有文件,请重新上传。";
            }
            if (Path.GetExtension(file.FileName).ToLower() != ".jpg")
            {
                json.state    = 0;
                json.msg_text = "请上传jpg格式文件。";
            }
            string photoTempDir = MyConfiguration.GetTempPhotoPath();

            if (!Directory.Exists(photoTempDir))
            {
                Directory.CreateDirectory(photoTempDir);
            }
            string guid           = Guid.NewGuid().ToString("N");
            string file_name      = string.Format("{0}{1}.jpg", photoTempDir, guid);
            string file_name_temp = string.Format("{0}{1}_temp.jpg", photoTempDir, guid);

            file.SaveAs(file_name);
            ImageFun.MakeThumbnail(file_name, file_name_temp, 106, 0, "W");
            json.state = 1;
            json.data  = Path.GetFileName(file_name_temp);
            return(Json(json));
        }
コード例 #2
0
        public ActionResult Edit([Bind(Include = "user_id,user_name,real_name,gender,user_phone,user_info,user_email,user_password,user_password2,user_home_address,user_photo_path,role_id,state")] TeacherEditModel model)
        {
            setSelect();
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToRoute(new { controller = "Login", action = "LogOut" }));
            }
            if (ModelState.IsValid)
            {
                //if (Session["token"] == null || Session["token"].ToString() != model.token)
                //{
                //    ViewBag.msg = "异常操作,请退出当前页面后重新进入操作。";
                //    return View(model);
                //}
                int userid = PageValidate.FilterParam(User.Identity.Name);
                if (!RoleCheck.CheckHasAuthority(userid, db, "用户管理") && model.user_id != userid)
                {
                    return(RedirectToRoute(new { controller = "Error", action = "Index", err = "没有权限。" }));
                }
                User_Info user_Info = db.User_Infos.Find(model.user_id);
                if (user_Info == null)
                {
                    ViewBag.msg = "没有找到相关信息,资料可能被删除。";
                    return(View(model));
                }

                if (db.User_Infos.Where(x => x.user_id != model.user_id && x.user_phone == model.user_phone).Count() > 0)
                {
                    ViewBag.msg = "该手机号码已存在。";
                    return(View(model));
                }
                if (!string.IsNullOrEmpty(model.user_password))
                {
                    if (model.user_password != model.user_password2)
                    {
                        ViewBag.msg = "两次输入的密码不匹配。";
                        return(View(model));
                    }
                    var salt = Guid.NewGuid().ToString("N").Substring(0, 10).ToUpper();
                    user_Info.user_password = AESEncrypt.Encrypt(PasswordUnit.getPassword(model.user_password.ToUpper(), salt));
                    user_Info.user_salt     = salt;
                }
                string err = "";
                if (!string.IsNullOrEmpty(model.user_photo_path) && model.user_photo_path != user_Info.user_photo_path)
                {
                    string photoDir = MyConfiguration.GetPhotoPath();
                    if (!Directory.Exists(photoDir))
                    {
                        Directory.CreateDirectory(photoDir);
                    }
                    string photoTempDir   = MyConfiguration.GetTempPhotoPath();
                    string file_name      = string.Format("{0}{1}", photoDir, model.user_photo_path).Replace("_temp", "");
                    string temp_file_name = string.Format("{0}{1}", photoTempDir, model.user_photo_path);
                    if (System.IO.File.Exists(temp_file_name))
                    {
                        FileInfo fi = new FileInfo(temp_file_name);
                        fi.CopyTo(file_name, true);
                        model.user_photo_path     = Path.GetFileName(file_name);
                        user_Info.user_photo_path = model.user_photo_path;
                    }
                    else
                    {
                        err = "图片保存失败。";
                    }
                }
                user_Info.user_name         = model.user_name;
                user_Info.user_phone        = model.user_phone;
                user_Info.user_info         = model.user_info;
                user_Info.user_email        = model.user_email;
                user_Info.user_home_address = model.user_home_address;
                user_Info.user_update_time  = DateTime.Now;
                user_Info.user_update_user  = userid;
                user_Info.user_gender       = model.gender;
                user_Info.real_name         = model.real_name;
                user_Info.user_is_teacher   = true;
                if (string.IsNullOrEmpty(user_Info.user_bindCode))
                {
                    user_Info.user_bindCode = Guid.NewGuid().ToString("N").Substring(0, 8);
                }
                db.Entry(user_Info).State = EntityState.Modified;
                try
                {
                    db.SaveChanges();
                }catch (Exception e)
                {
                    err = "资料保存失败。";
                    ErrorUnit.WriteErrorLog(e.ToString(), this.GetType().ToString());
                }
                //权限设置
                if (RoleCheck.CheckIsSuperAdmin(model.user_id, db))
                {
                    if (model.role_id != 1)
                    {
                        err = "系统管理员权限不允许更改。";
                    }
                    goto next;
                }
                if (model.role_id == 1 && !RoleCheck.CheckIsSuperAdmin(userid, db))//添加系统管理员权限
                {
                    err = "只有系统管理员才可以添加系统管理员权限。";
                }
                else
                {
                    var uvr = db.User_vs_Roles.Where(x => x.uvr_user_id == model.user_id);
                    db.User_vs_Roles.RemoveRange(uvr);
                    User_vs_Role Nuvr = new User_vs_Role
                    {
                        uvr_user_id = model.user_id,
                        uvr_role_id = model.role_id
                    };
                    db.User_vs_Roles.Add(Nuvr);
                    try
                    {
                        db.SaveChanges();
                    }catch (Exception e)
                    {
                        err = "角色添加失败。";
                    }
                }
next:
                if (err == "")
                {
                    ViewBag.msg = "修改成功。";
                }
                else
                {
                    ViewBag.msg = err;
                }
            }
            return(View(model));
        }