// 使用ID作为登陆标识 func (this *Server) SignIn(con net.Conn, accountID int, password string) (*Connect, bool, string, error) { manager, err := db.NewDBManager() if err != nil { log.Println(err) return nil, false, "", err } defer manager.Close() //acc, _ := strconv.Atoi(account) //var u *db.User u := db.User{ID: accountID, Name: "", AvatarUrl: "", Password: []byte(password)} var user *db.User user, err = manager.LoginUser(&u) if err != nil { log.Println(err) this.SendMSG(con, constant.SignInFail+" "+user.Name) return nil, false, "", err } var connect *Connect connect = NewConnect(accountID, "lobby", con) this.connects = append(this.connects, connect) log.Println("登陆成功") log.Println("用户昵称: " + strconv.Itoa(accountID)) log.Println("用户账号: " + user.Name) this.SendMSG(con, constant.SignInSuccess+" "+user.Name) return connect, true, user.Name, nil }
// 使用ID作为登陆标识 func (this *Server) GetUserInfo(con net.Conn, msgs []string) (*db.User, bool, string, error) { _, account := msgs[0], msgs[1] manager, err := db.NewDBManager() if err != nil { log.Println(err) return nil, false, "", err } defer manager.Close() acc, _ := strconv.Atoi(account) //var u *db.User u := db.User{ID: acc, Name: "", AvatarUrl: "", Password: []byte("")} var user *db.User user, err = manager.FindUserByID(&u) if err != nil { log.Println(err) return nil, false, "", err } log.Println(u) log.Println(user) response := "" for _, f := range user.Friends { response += f + " " } this.SendMSG(con, constant.FriendsListSuccess+" "+response) return user, true, user.Name, nil }
// 创建数字ID作为用户唯一标识 func (this *Server) SignUp(con net.Conn, name, password string) int { id := rand.New(rand.NewSource(time.Now().UnixNano())).Intn(10000) strID := strconv.Itoa(id) this.SendMSG(con, strID) log.Println("注册成功") log.Println("用户昵称: " + name) log.Println("用户账号: " + strID) con.Close() manager, err := db.NewDBManager() if err != nil { log.Println(err) } defer manager.Close() p, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) var fs []string fs = make([]string, 0) fs = append(fs, "000") fs = append(fs, "111") fs = append(fs, "222") fs = append(fs, "333") u := db.User{ID: id, Name: name, AvatarUrl: "", Password: p, Friends: fs} err = manager.RegisterUser(u) if err != nil { log.Println(err) } return id }
// 使用ID作为登陆标识 func (this *Server) GetAllUsers(con net.Conn) ([]db.User, error) { manager, err := db.NewDBManager() if err != nil { log.Println(err) return nil, err } defer manager.Close() var users []db.User users, err = manager.GetAllUsers() if err != nil { log.Println(err) return nil, err } response := "" for _, user := range users { response += strconv.Itoa(user.ID) + " " } this.SendMSG(con, constant.GetAllUsersSuccess+" "+response) return users, nil }