register user add redis

This commit is contained in:
xing 2021-04-25 16:29:30 +08:00
parent d64e4fb905
commit a34e4e3629
4 changed files with 33 additions and 17 deletions

View File

@ -101,6 +101,7 @@ func handleMsg() { //处理
} else { } else {
fmt.Println(c.Msg.Msg) fmt.Println(c.Msg.Msg)
} }
cc <- 1
} }
} }
} }
@ -139,6 +140,7 @@ func main() {
<-cc <-cc
case 2: case 2:
addUser(conn) addUser(conn)
<-cc
case 3: case 3:
//s.Store() //s.Store()
loop = false loop = false

View File

@ -28,10 +28,10 @@ func AddUser(user user.User) error {
return errors.New("user name can't be empty") return errors.New("user name can't be empty")
} }
r := getRdm.HMSet(Ctx, "go_chat_Users", map[string]interface{}{ r := getRdm.HMSet(Ctx, "go_chat_Users:"+strconv.Itoa(user.Id), map[string]interface{}{
"id": strconv.Itoa(user.Id), "id": strconv.Itoa(user.Id),
"name": user.Name, "name": user.Name,
"pw": user.Password, "password": user.Password,
"sex": strconv.Itoa(int(user.Sex)), "sex": strconv.Itoa(int(user.Sex)),
}) })
if e := r.Err(); e != nil { if e := r.Err(); e != nil {

View File

@ -4,12 +4,15 @@ import (
"chat/dao" "chat/dao"
"chat/message" "chat/message"
"chat/process" "chat/process"
"chat/rdm"
"chat/user" "chat/user"
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net" "net"
"strconv"
) )
type Server struct { type Server struct {
@ -41,6 +44,17 @@ func (server *Server) Login(uid int, pw string) (*user.User, error) {
return &u, nil 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") 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) { 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{ re := message.Message{
Type: "login_response", Type: "login_response",
@ -88,14 +102,14 @@ func (server *Server) do(s *message.LoginS, conn net.Conn) (string, error) {
} }
r := message.Correspond{ r := message.Correspond{
Code: 1, Code: 0,
Msg: user.Name, Msg: "login fail",
Error: "", Error: "pw fail or login not exist ",
} }
if err != nil { if err == nil {
r.Code = 0 r.Code = 1
r.Msg = "login fail" r.Msg = login.Name
r.Error = "pw fail or user not exist" r.Error = ""
} }
re.Data = r re.Data = r

View File

@ -3,10 +3,10 @@ package user
import "errors" import "errors"
type User struct { type User struct {
Id int Id int `json:"id"`
Name string Name string `json:"name"`
Sex int8 Sex int8 `json:"sex"`
Password string Password string `json:"password"`
} }
func (u *User) CheckPassword(p string) error { func (u *User) CheckPassword(p string) error {