register user

This commit is contained in:
xing 2021-04-25 09:53:49 +08:00
parent cd56fd39f1
commit d64e4fb905
8 changed files with 175 additions and 48 deletions

View File

@ -1,13 +0,0 @@
package Redis
import (
"github.com/go-redis/redis/v8"
)
func GetRedis() *redis.Client {
rdb := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
DB: 0,
})
return rdb
}

View File

@ -3,6 +3,7 @@ package main
import ( import (
"chat/message" "chat/message"
"chat/process" "chat/process"
"chat/user"
"fmt" "fmt"
"net" "net"
) )
@ -29,24 +30,52 @@ func login(conn net.Conn) error {
} }
func showMenu(name string) { func showMenu(name string) {
fmt.Printf("-----------------------------欢迎%s登录---------------------------\n", name)
fmt.Printf("\t\t\t 1.显示在线用户列表\n") for {
fmt.Printf("\t\t\t 2.发送消息\n") fmt.Printf("-----------------------------欢迎%s登录---------------------------\n", name)
fmt.Printf("\t\t\t 3.信息列表\n") fmt.Printf("\t\t\t 1.显示在线用户列表\n")
fmt.Printf("\t\t\t 4.信息列表\n") fmt.Printf("\t\t\t 2.发送消息\n")
fmt.Printf("\t\t\t 请选择(1-4):") fmt.Printf("\t\t\t 3.信息列表\n")
var k int fmt.Printf("\t\t\t 4.退出\n")
_, err := fmt.Scanf("%d", &k) fmt.Printf("\t\t\t 请选择(1-4):")
var k int
_, err := fmt.Scanf("%d", &k)
if err != nil {
fmt.Println(err)
continue
}
switch k {
case 1:
fmt.Println("在线用户列表")
case 2:
fmt.Println("发送信息")
case 4:
return
}
}
}
func addUser(conn net.Conn) {
fmt.Print("请输入姓名、密码、性别(1男2女)空格隔开:")
var name, pw string
var sex int8
_, err := fmt.Scanf("%s %s %d", &name, &pw, &sex)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
switch k { process.WriteConn(conn, message.Message{
case 1: Type: "add_user",
fmt.Println("在线用户列表") Code: 0,
case 2: Msg: "",
fmt.Println("发送信息") Data: user.User{
} Id: 0,
Name: name,
Sex: sex,
Password: pw,
},
})
} }
func handleMsg() { //处理 func handleMsg() { //处理
@ -58,6 +87,7 @@ func handleMsg() { //处理
if r, ok := c.Msg.Data.(*message.Correspond); ok { if r, ok := c.Msg.Data.(*message.Correspond); ok {
if r.Error == "" { if r.Error == "" {
fmt.Println("登录成功!") fmt.Println("登录成功!")
showMenu(r.Msg)
} else { } else {
fmt.Println("登录失败", r.Error) fmt.Println("登录失败", r.Error)
} }
@ -65,14 +95,20 @@ func handleMsg() { //处理
fmt.Println("登录失败") fmt.Println("登录失败")
} }
cc <- 1 cc <- 1
case "add_user_response":
if c.Msg.Code == 1 {
fmt.Println("添加用户成功")
} else {
fmt.Println(c.Msg.Msg)
}
} }
} }
} }
} }
func main() { func main() {
conn, err := net.Dial("tcp", "127.0.0.1:8989") conn, err := net.Dial("tcp", "127.0.0.1:8989")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
@ -89,6 +125,7 @@ func main() {
fmt.Printf("\t\t\t 3.退出系统\n") fmt.Printf("\t\t\t 3.退出系统\n")
fmt.Printf("\t\t\t 请选择(1-3):") fmt.Printf("\t\t\t 请选择(1-3):")
_, err := fmt.Scanf("%d", &i) _, err := fmt.Scanf("%d", &i)
fmt.Println()
if err != nil { if err != nil {
return return
} }
@ -101,7 +138,7 @@ func main() {
} }
<-cc <-cc
case 2: case 2:
addUser(conn)
case 3: case 3:
//s.Store() //s.Store()
loop = false loop = false

41
dao/dao.go Normal file
View File

@ -0,0 +1,41 @@
package dao
import (
"chat/rdm"
"chat/user"
"chat/utils"
"context"
"errors"
"strconv"
)
var Ctx context.Context
func init() {
Ctx = context.Background()
}
func AddUser(user user.User) error {
getRdm := rdm.GetRdm()
if user.Name == "" {
return errors.New("user name can't be empty")
}
if user.Password == "" {
return errors.New("user password can't be empty")
}
if utils.IsContain(user.Sex, []int8{1, 2}) < 0 {
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)),
})
if e := r.Err(); e != nil {
return e
}
return nil
}

View File

@ -1,6 +1,7 @@
package lib package lib
import ( import (
"chat/dao"
"chat/message" "chat/message"
"chat/process" "chat/process"
"chat/user" "chat/user"
@ -34,13 +35,13 @@ func NewServer() *Server {
return &Server{} return &Server{}
} }
func (server *Server) Login(uid int, pw string) error { func (server *Server) Login(uid int, pw string) (*user.User, error) {
for _, u := range server.Users { for _, u := range server.Users {
if uid == u.Id && pw == u.Password { if uid == u.Id && pw == u.Password {
return nil return &u, nil
} }
} }
return errors.New("not found this u") return nil, errors.New("not found this u")
} }
func (server *Server) Store() { func (server *Server) Store() {
@ -56,12 +57,17 @@ func (server *Server) Store() {
} }
} }
func (server *Server) AddUser(name, pw string, sex int8) { func (server *Server) AddUser(name, pw string, sex int8) error {
server.Id++ server.Id++
u := user.User{ u := user.User{
server.Id, name, sex, pw, Id: server.Id, Name: name, Sex: sex, Password: pw,
} }
server.Users = append(server.Users, u) server.Users = append(server.Users, u)
err := dao.AddUser(u)
if err != nil {
return err
}
return nil
} }
func (server *Server) Process(conn net.Conn) { func (server *Server) Process(conn net.Conn) {
@ -74,23 +80,24 @@ 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) {
err := server.Login(s.Uid, s.Pw) user, err := server.Login(s.Uid, s.Pw)
re := message.Message{ re := message.Message{
Type: "login_response", Type: "login_response",
Data: nil, Data: nil,
} }
re.Data = message.Correspond{
r := message.Correspond{
Code: 1, Code: 1,
Msg: "success", Msg: user.Name,
Error: "", Error: "",
} }
if err != nil { if err != nil {
re.Data = message.Correspond{ r.Code = 0
Code: 0, r.Msg = "login fail"
Msg: "login fail", r.Error = "pw fail or user not exist"
Error: "pw fail or user not exist",
}
} }
re.Data = r
err = process.WriteConn(conn, re) err = process.WriteConn(conn, re)
return "login_send", err return "login_send", err
@ -107,6 +114,24 @@ func (s *Server) processConn() {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
case "add_user":
u := c.Msg.Data.(*user.User)
err := s.AddUser(u.Name, u.Password, u.Sex)
r := message.Message{
Type: "add_user_response",
Code: 1,
Msg: "",
Data: nil,
}
if err != nil {
fmt.Println(err)
r.Msg = err.Error()
r.Code = 0
}
err = process.WriteConn(c.Conn, r)
if err != nil {
fmt.Println(err)
}
} }
} }
} }

View File

@ -1,17 +1,21 @@
package message package message
import ( import (
"chat/user"
"reflect" "reflect"
) )
var MsgType = map[string]reflect.Type{ var MsgType = map[string]reflect.Type{
"login_send": reflect.TypeOf(&LoginS{}).Elem(), "login_send": reflect.TypeOf(&LoginS{}).Elem(),
"login_response": reflect.TypeOf(&Correspond{}).Elem(), "login_response": reflect.TypeOf(&Correspond{}).Elem(),
"add_user": reflect.TypeOf(&user.User{}).Elem(),
} }
type Message struct { type Message struct {
//Id int //Id int
Type string Type string
Code int
Msg string
Data interface{} Data interface{}
} }

View File

@ -49,12 +49,14 @@ func (t *Transfer) ReadConn(c chan Ms) {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
tt := message.MsgType[msg.Type] tt, ok := message.MsgType[msg.Type]
mm := message.Message{} var mm = msg
mm.Data = reflect.New(tt).Interface() if ok {
err = json.Unmarshal(info[:i], &mm) mm.Data = reflect.New(tt).Interface()
if err != nil { err = json.Unmarshal(info[:i], &mm)
fmt.Println(err) if err != nil {
fmt.Println(err)
}
} }
w := Ms{ w := Ms{
Msg: mm, Msg: mm,

15
rdm/rdm.go Normal file
View File

@ -0,0 +1,15 @@
package rdm
import "github.com/go-redis/redis/v8"
var RDM *redis.Client
func GetRdm() *redis.Client {
if RDM == nil {
RDM = redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
DB: 0,
})
}
return RDM
}

View File

@ -1 +1,17 @@
package utils package utils
import "reflect"
func IsContain(elem interface{}, arr interface{}) int {
r := reflect.ValueOf(arr)
k := r.Kind()
if k == reflect.Slice || k == reflect.Array {
l := r.Len()
for i := 0; i < l; i++ {
if r.Index(i).Interface() == elem {
return i
}
}
}
return -1
}