From a34e4e3629356ed4dee201fbd1be59289439bd31 Mon Sep 17 00:00:00 2001 From: xing Date: Sun, 25 Apr 2021 16:29:30 +0800 Subject: [PATCH] register user add redis --- cmd/client/main.go | 2 ++ dao/dao.go | 10 +++++----- lib/server.go | 30 ++++++++++++++++++++++-------- user/user.go | 8 ++++---- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index 66db6c7..4c8c6f7 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -101,6 +101,7 @@ func handleMsg() { //处理 } else { fmt.Println(c.Msg.Msg) } + cc <- 1 } } } @@ -139,6 +140,7 @@ func main() { <-cc case 2: addUser(conn) + <-cc case 3: //s.Store() loop = false diff --git a/dao/dao.go b/dao/dao.go index c4ca439..39cd603 100644 --- a/dao/dao.go +++ b/dao/dao.go @@ -28,11 +28,11 @@ func AddUser(user user.User) error { return errors.New("user name can't be empty") } - r := getRdm.HMSet(Ctx, "go_chat_Users", map[string]interface{}{ - "id": strconv.Itoa(user.Id), - "name": user.Name, - "pw": user.Password, - "sex": strconv.Itoa(int(user.Sex)), + r := getRdm.HMSet(Ctx, "go_chat_Users:"+strconv.Itoa(user.Id), map[string]interface{}{ + "id": strconv.Itoa(user.Id), + "name": user.Name, + "password": user.Password, + "sex": strconv.Itoa(int(user.Sex)), }) if e := r.Err(); e != nil { return e diff --git a/lib/server.go b/lib/server.go index c7babf6..0311d13 100644 --- a/lib/server.go +++ b/lib/server.go @@ -4,12 +4,15 @@ import ( "chat/dao" "chat/message" "chat/process" + "chat/rdm" "chat/user" + "context" "encoding/json" "errors" "fmt" "io/ioutil" "net" + "strconv" ) type Server struct { @@ -41,6 +44,17 @@ func (server *Server) Login(uid int, pw string) (*user.User, error) { return &u, nil } } + u := rdm.GetRdm().HGetAll(context.Background(), "go_chat_Users:"+strconv.Itoa(uid)) + if u != nil && u.Val()["password"] == pw { + t := u.Val() + tt, _ := json.Marshal(t) + uu := &user.User{} + x := json.Unmarshal(tt, uu) + if nil != x { + return nil, x + } + return uu, nil + } return nil, errors.New("not found this u") } @@ -80,7 +94,7 @@ func (server *Server) Process(conn net.Conn) { } func (server *Server) do(s *message.LoginS, conn net.Conn) (string, error) { - user, err := server.Login(s.Uid, s.Pw) + login, err := server.Login(s.Uid, s.Pw) re := message.Message{ Type: "login_response", @@ -88,14 +102,14 @@ func (server *Server) do(s *message.LoginS, conn net.Conn) (string, error) { } r := message.Correspond{ - Code: 1, - Msg: user.Name, - Error: "", + Code: 0, + Msg: "login fail", + Error: "pw fail or login not exist ", } - if err != nil { - r.Code = 0 - r.Msg = "login fail" - r.Error = "pw fail or user not exist" + if err == nil { + r.Code = 1 + r.Msg = login.Name + r.Error = "" } re.Data = r diff --git a/user/user.go b/user/user.go index 796aaf8..ba03520 100644 --- a/user/user.go +++ b/user/user.go @@ -3,10 +3,10 @@ package user import "errors" type User struct { - Id int - Name string - Sex int8 - Password string + Id int `json:"id"` + Name string `json:"name"` + Sex int8 `json:"sex"` + Password string `json:"password"` } func (u *User) CheckPassword(p string) error {