// 登录 func (this *Login) Login() { //AJAX 请求执行登录 if this.IsAjax() { username := this.GetString("username") password := this.GetString("password") if username == "" || password == "" { this.JsonOut(nil, "1", "用户名或密码不能为空!") } else { m := models.NewAdmin() admin, err := m.FindOneBy(bson.M{"username": username}) if err == nil { // 判断密码 if admin.PassWord == utils.Encrypt(password) { if admin.Status { data := make(map[string]interface{}) data["id"] = admin.Id.Hex() data["name"] = admin.UserName data["goto"] = "/admin/dashboard" // 保存SESSION this.SetSession("ADMIN_ID", admin.Id.Hex()) this.JsonOut(data, "0") } else { this.JsonOut(nil, "1", "用户"+username+"已被注销,无法登录!") } } else { this.JsonOut(nil, "1", "用户"+username+"密码错误!") } } else if beego.AppConfig.String("RunMode") == "dev" && beego.AppConfig.String("Developer") == username { // 新增超级管理员 m2 := models.NewAdmin() m2.UserName = username m2.PassWord = utils.Encrypt(password) m2.Status = true m2.Persist() this.JsonOut(nil, "1", "新增用户"+username+"成功!") } else { this.JsonOut(nil, "", "服务器繁忙,请稍后再访问", "503") this.JsonOut(nil, "1", "用户"+username+"不存在!") } } } else { this.display("", "login.tpl") } }
// 编辑管理员信息(新增或修改) func (this *Admins) Edit() { id := this.GetString(":id") o := models.NewAdmin() if id == this.CurrentAdmin.Id.Hex() { o = this.CurrentAdmin } else { if id != "" { if o1, err := o.Find(id); nil == err && nil != o1 { o = o1 } } } fm := models.AdminForm{} if err := this.ParseForm(&fm); err != nil { this.JsonOut(nil, "1", err.Error()) } valid := validation.Validation{} valid.Required(fm.UserName, "userName").Message("账号不能为空") valid.Required(fm.Email, "email").Message("邮箱不能为空") valid.Required(fm.RoleId, "roleId").Message("角色不能为空") valid.Required(fm.Status, "status").Message("状态不能为空") // 非新增 if o.Id.Valid() { if fm.PassWord != "" { valid.MinSize(fm.PassWord, 6, "passWord").Message("密码至少需要6位") } } else { valid.Required(fm.PassWord, "passWord").Message("密码不能为空") valid.MinSize(fm.PassWord, 6, "passWord").Message("密码至少需要6位") } if errmsg, ok := this.HasErrMsgs(valid); ok { this.JsonOut(nil, "1", errmsg) } o.UserName = fm.UserName o.Email = fm.Email o.RoleId = fm.RoleId o.Status = fm.Status if fm.PassWord != "" { o.PassWord = utils.Encrypt(fm.PassWord) } err := o.Persist() if nil == err { this.JsonOut(nil, "0") } else if o.Id.Valid() { this.JsonOut(nil, "1", "修改管理员失败") } else { this.JsonOut(nil, "1", "添加管理员失败") } }