u
This commit is contained in:
1 parent
69d03709ba
commit
44fb48ef6a
9 files changed
+625
-25
No files matched your search
@@ -1,13 +1,17 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"base-go-v2/internal/db"
|
||||
response "base-go-v2/internal/reponse"
|
||||
"base-go-v2/internal/router"
|
||||
"base-go-v2/internal/utils/mapx"
|
||||
"base-go-v2/internal/utils/uid"
|
||||
)
|
||||
|
||||
func InitAuthRouter(r *router.Router) {
|
||||
group := r.Group("auth")
|
||||
group.POST("/login/default", loginDefault)
|
||||
group.POST("/register/default", registerDefault)
|
||||
}
|
||||
|
||||
// 默认登录
|
||||
@@ -15,13 +19,41 @@ func loginDefault(c *router.Context) error {
|
||||
return response.Success(c).Send()
|
||||
}
|
||||
|
||||
// 注册
|
||||
func registerDefault(c *router.Context) error {
|
||||
var req struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
params, err := c.GetBodyWithRequired("account", "password")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
account := params.GetString("account")
|
||||
password := params.GetString("password")
|
||||
|
||||
// 构造查询条件,检查账号是否已存在
|
||||
accountQuery := mapx.New().SetKV("account", account)
|
||||
|
||||
existingUsers, err := db.Find("user_info", accountQuery)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := c.BindJSON(&req); err != nil {
|
||||
if len(existingUsers) > 0 {
|
||||
return response.Error(c).Message("该账号已被注册").Send()
|
||||
}
|
||||
|
||||
// 构造新用户数据
|
||||
newUserData := mapx.New().SetKV(
|
||||
"id", uid.NextID(),
|
||||
"account", account,
|
||||
"password", password,
|
||||
)
|
||||
|
||||
// 插入新用户
|
||||
insertedRows, err := db.InsertOne("user_info", newUserData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if insertedRows != 1 {
|
||||
return response.Error(c).Message("注册失败").Send()
|
||||
}
|
||||
|
||||
return response.Success(c).Message("注册成功").Send()
|
||||
}
|
||||
@@ -1,29 +1,45 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"base-go-v2/internal/errs"
|
||||
"base-go-v2/internal/logx"
|
||||
"base-go-v2/internal/reponse"
|
||||
"base-go-v2/internal/router"
|
||||
"errors"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
func ErrorMiddleware() router.HandlerFunc {
|
||||
// 可扩展的业务错误类型列表
|
||||
businessErrors := []interface{}{
|
||||
(*errs.ValidationError)(nil),
|
||||
// (*errs.BusinessError)(nil),
|
||||
// (*errs.AuthError)(nil),
|
||||
}
|
||||
|
||||
return func(c *router.Context) error {
|
||||
err := c.Next()
|
||||
|
||||
if err != nil {
|
||||
stack := string(debug.Stack())
|
||||
|
||||
logx.Logger.Error("请求错误",
|
||||
logx.String("path", string(c.RequestCtx.Path())),
|
||||
logx.String("method", string(c.RequestCtx.Method())),
|
||||
logx.String("error", err.Error()),
|
||||
logx.String("stack", stack),
|
||||
)
|
||||
|
||||
return response.Error(c).Message(err.Error()).Send()
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
// 判断是否属于业务错误
|
||||
for _, be := range businessErrors {
|
||||
// be 是 nil 指针类型
|
||||
if errors.As(err, &be) {
|
||||
// 业务错误,不记录日志
|
||||
return response.Error(c).Message(err.Error()).Send()
|
||||
}
|
||||
}
|
||||
|
||||
// 系统错误,记录日志
|
||||
stack := string(debug.Stack())
|
||||
logx.Logger.Error("请求错误",
|
||||
logx.String("path", string(c.RequestCtx.Path())),
|
||||
logx.String("method", string(c.RequestCtx.Method())),
|
||||
logx.String("error", err.Error()),
|
||||
logx.String("stack", stack),
|
||||
)
|
||||
return response.Error(c).Message("服务器内部错误").Send()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user