Files
goinim/user/userregister.go

134 lines
3.3 KiB
Go
Raw Normal View History

2025-11-24 11:20:33 +08:00
package user
import (
"context"
"encoding/json"
"io"
"log"
"net/http"
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Userreg struct {
Username string `json:"username"`
Password string `json:"password"`
Email string `json:"email"`
Verifycode string `json:"verifycode"`
}
func Userregister(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8080")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Allow-Credentials", "true")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
body, error := io.ReadAll(r.Body)
if error != nil {
log.Println("数据读取失败")
return
}
var userinfo Userreg
error = json.Unmarshal(body, &userinfo)
if error != nil {
log.Println("数据读取失败")
return
}
var username = userinfo.Username
var password = userinfo.Password
var useremail = userinfo.Email
var verifycode = userinfo.Verifycode
mongodbcli := options.Client().ApplyURI("mongodb://localhost:27017").SetMaxPoolSize(100)
mongodbclient, error := mongo.Connect(context.TODO(), mongodbcli)
if error != nil {
log.Println("数据库连接失败")
return
}
findtheoneuser := bson.M{"username": username}
finduser := mongodbclient.Database("users").Collection("userlist")
_, error = finduser.Find(context.Background(), findtheoneuser)
if error == nil {
log.Println("该用户名已存在")
loginres := Loginresponse{
Success: "no",
Message: "注册失败,用户已经存在",
Token: "2",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(loginres)
return
}
findtheoneemail := bson.M{"email": useremail}
_, error = finduser.Find(context.Background(), findtheoneemail)
if error == nil {
log.Println("该邮箱已存在")
loginres := Loginresponse{
Success: "no",
Message: "注册失败",
Token: "3",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(loginres)
return
}
redisdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
findveri, error := redisdb.HGet(context.Background(), "userverify", useremail).Result()
if error != nil {
log.Println("验证码验证失败")
loginres := Loginresponse{
Success: "no",
Message: "注册失败,验证码错误",
Token: "4",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(loginres)
return
}
if findveri != verifycode {
log.Println("验证码验证失败")
loginres := Loginresponse{
Success: "no",
Message: "注册失败,验证码错误",
Token: "4",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(loginres)
return
}
_, error = finduser.InsertOne(context.Background(), bson.M{"username": username, "password": password, "email": useremail})
if error != nil {
log.Println("数据插入失败")
loginres := Loginresponse{
Success: "no",
Message: "用户数据错误",
Token: "5",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(loginres)
return
}
}