Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(): 修改用户名为不能重复 #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion models/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package models

import "errors"

var(
var (
// ErrMemberNoExist 用户不存在.
ErrMemberNoExist = errors.New("用户不存在")
// ErrMemberNoExist 用户不存在.
ErrMemberIsExist = errors.New("用户已经存在")
// ErrorMemberPasswordError 密码错误.
ErrorMemberPasswordError = errors.New("用户密码错误")
// ErrServerAlreadyExist 指定的服务已存在.
Expand Down
65 changes: 37 additions & 28 deletions models/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ import (

// Member 会员信息.
type Member struct {
MemberId int `orm:"pk;auto;unique;column(member_id)"`
Account string `orm:"size(100);unique;column(account)"`
Password string `orm:"size(1000);column(password)"`
Email string `orm:"size(255);column(email);null;default(null)"`
Phone string `orm:"size(255);column(phone);null;default(null)"`
Avatar string `orm:"size(1000);column(avatar)"`
Role int `orm:"column(role);type(int);default(1)"` //用户角色:0 管理员/1 普通用户
Status int `orm:"column(status);type(int);default(0)"` //用户状态:0 正常/1 禁用
CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add"`
CreateAt int `orm:"type(int);column(create_at)"`
LastLoginTime time.Time `orm:"type(datetime);column(last_login_time);null"`
MemberId int `orm:"pk;auto;unique;column(member_id)"`
Account string `orm:"size(100);unique;column(account)"`
Password string `orm:"size(1000);column(password)"`
Email string `orm:"size(255);column(email);null;default(null)"`
Phone string `orm:"size(255);column(phone);null;default(null)"`
Avatar string `orm:"size(1000);column(avatar)"`
Role int `orm:"column(role);type(int);default(1)"` //用户角色:0 管理员/1 普通用户
Status int `orm:"column(status);type(int);default(0)"` //用户状态:0 正常/1 禁用
CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add"`
CreateAt int `orm:"type(int);column(create_at)"`
LastLoginTime time.Time `orm:"type(datetime);column(last_login_time);null"`
}

// TableName 获取对应数据库表名.
func (m *Member) TableName() string {
return "members"
}

// TableEngine 获取数据使用的引擎.
func (m *Member) TableEngine() string {
return "INNODB"
Expand All @@ -36,7 +38,7 @@ func NewMember() *Member {
}

// Find 根据用户ID查找用户.
func (m *Member) Find() (error) {
func (m *Member) Find() error {
o := orm.NewOrm()

err := o.Read(m)
Expand All @@ -49,52 +51,59 @@ func (m *Member) Find() (error) {
}

// Login 用户登录.
func (m *Member) Login(account string,password string) (*Member,error) {
func (m *Member) Login(account string, password string) (*Member, error) {
o := orm.NewOrm()

member := &Member{}

err := o.QueryTable(m.TableName()).Filter("account",account).Filter("status",0).One(member);
err := o.QueryTable(m.TableName()).Filter("account", account).Filter("status", 0).One(member)

if err != nil {
return member,ErrMemberNoExist
return member, ErrMemberNoExist
}

ok,err := passwords.PasswordVerify(member.Password,password) ;
ok, err := passwords.PasswordVerify(member.Password, password)

if ok && err == nil {
return member,nil
return member, nil
}

return member,ErrorMemberPasswordError
return member, ErrorMemberPasswordError
}


// Add 添加一个用户.
func (member *Member) Add () (error) {
func (member *Member) Add() error {
o := orm.NewOrm()

hash ,err := passwords.PasswordHash(member.Password);
m := &Member{}

err := o.QueryTable(member.TableName()).Filter("account", member.Account).One(m)

if err == nil {
return ErrMemberIsExist
}

hash, err := passwords.PasswordHash(member.Password)

if err != nil {
if err != nil {
return err
}

member.Password = hash

_,err = o.Insert(member)
_, err = o.Insert(member)

if err != nil {
return err
}
return nil
return nil
}

// Update 更新用户信息.
func (m *Member) Update(cols... string) (error) {
func (m *Member) Update(cols ...string) error {
o := orm.NewOrm()

if _,err := o.Update(m,cols...);err != nil {
if _, err := o.Update(m, cols...); err != nil {
return err
}
return nil
Expand All @@ -104,8 +113,8 @@ func (m *Member) Update(cols... string) (error) {
func (m *Member) Delete() error {
o := orm.NewOrm()

if _,err := o.Delete(m);err != nil {
if _, err := o.Delete(m); err != nil {
return err
}
return nil
}
}