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 {
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

View File

@ -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

View File

@ -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

View File

@ -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 {