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 (
"chat/message"
"chat/process"
"chat/user"
"fmt"
"net"
)
@ -29,24 +30,52 @@ func login(conn net.Conn) error {
}
func showMenu(name string) {
for {
fmt.Printf("-----------------------------欢迎%s登录---------------------------\n", name)
fmt.Printf("\t\t\t 1.显示在线用户列表\n")
fmt.Printf("\t\t\t 2.发送消息\n")
fmt.Printf("\t\t\t 3.信息列表\n")
fmt.Printf("\t\t\t 4.信息列表\n")
fmt.Printf("\t\t\t 4.退出\n")
fmt.Printf("\t\t\t 请选择(1-4):")
var k int
_, err := fmt.Scanf("%d", &k)
if err != nil {
fmt.Println(err)
return
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 {
fmt.Println(err)
return
}
process.WriteConn(conn, message.Message{
Type: "add_user",
Code: 0,
Msg: "",
Data: user.User{
Id: 0,
Name: name,
Sex: sex,
Password: pw,
},
})
}
func handleMsg() { //处理
@ -58,6 +87,7 @@ func handleMsg() { //处理
if r, ok := c.Msg.Data.(*message.Correspond); ok {
if r.Error == "" {
fmt.Println("登录成功!")
showMenu(r.Msg)
} else {
fmt.Println("登录失败", r.Error)
}
@ -65,14 +95,20 @@ func handleMsg() { //处理
fmt.Println("登录失败")
}
cc <- 1
case "add_user_response":
if c.Msg.Code == 1 {
fmt.Println("添加用户成功")
} else {
fmt.Println(c.Msg.Msg)
}
}
}
}
}
func main() {
conn, err := net.Dial("tcp", "127.0.0.1:8989")
if err != nil {
fmt.Println(err)
return
@ -89,6 +125,7 @@ func main() {
fmt.Printf("\t\t\t 3.退出系统\n")
fmt.Printf("\t\t\t 请选择(1-3):")
_, err := fmt.Scanf("%d", &i)
fmt.Println()
if err != nil {
return
}
@ -101,7 +138,7 @@ func main() {
}
<-cc
case 2:
addUser(conn)
case 3:
//s.Store()
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
import (
"chat/dao"
"chat/message"
"chat/process"
"chat/user"
@ -34,13 +35,13 @@ func NewServer() *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 {
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() {
@ -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++
u := user.User{
server.Id, name, sex, pw,
Id: server.Id, Name: name, Sex: sex, Password: pw,
}
server.Users = append(server.Users, u)
err := dao.AddUser(u)
if err != nil {
return err
}
return nil
}
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) {
err := server.Login(s.Uid, s.Pw)
user, err := server.Login(s.Uid, s.Pw)
re := message.Message{
Type: "login_response",
Data: nil,
}
re.Data = message.Correspond{
r := message.Correspond{
Code: 1,
Msg: "success",
Msg: user.Name,
Error: "",
}
if err != nil {
re.Data = message.Correspond{
Code: 0,
Msg: "login fail",
Error: "pw fail or user not exist",
}
r.Code = 0
r.Msg = "login fail"
r.Error = "pw fail or user not exist"
}
re.Data = r
err = process.WriteConn(conn, re)
return "login_send", err
@ -107,6 +114,24 @@ func (s *Server) processConn() {
if err != nil {
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
import (
"chat/user"
"reflect"
)
var MsgType = map[string]reflect.Type{
"login_send": reflect.TypeOf(&LoginS{}).Elem(),
"login_response": reflect.TypeOf(&Correspond{}).Elem(),
"add_user": reflect.TypeOf(&user.User{}).Elem(),
}
type Message struct {
//Id int
Type string
Code int
Msg string
Data interface{}
}

View File

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

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