//检测用户名密码正确 func CheckLogin(username, password string) (err error) { o := orm.NewOrm() user := &User{Username: username} if err = o.Read(user, "Username"); err == nil { if util.Md5(password) != user.Password { err = errors.New("用户密码错误!") return } } else if err == orm.ErrNoRows { err = errors.New("用户名不存在!") } return }
//修改用户 func EditUser(id int64, username, password string) (err error) { user := &User{Id: id} o := orm.NewOrm() if err = o.Read(user); err == nil { if user.Id != id { err = errors.New("用户名已经存在!") return } user.Username = username user.Password = util.Md5(password) _, err = o.Update(user) } return }
//添加初始用户 func AddAdmin() (err error) { user := &User{ Username: "******", Password: util.Md5("admin"), Login: time.Now(), } o := orm.NewOrm() if created, _, err := o.ReadOrCreate(user, "Username"); err == nil { if created { return err } else { err = errors.New("用户名已经存在!") } } return }