u
This commit is contained in:
1 parent
b9f728812d
commit
d9b2522552
12 files changed
+104
-19
No files matched your search
@@ -3,17 +3,16 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
auth "cn/oneao/base-go/biz/model/auth"
|
||||
"cn/oneao/base-go/db"
|
||||
"cn/oneao/base-go/ent/userinfo"
|
||||
"cn/oneao/base-go/logx"
|
||||
"cn/oneao/base-go/utils/jwtx"
|
||||
"cn/oneao/base-go/utils/response"
|
||||
"cn/oneao/base-go/utils/routinex"
|
||||
"context"
|
||||
"github.com/cloudwego/hertz/pkg/common/hlog"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
auth "cn/oneao/base-go/biz/model/auth"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/protocol/consts"
|
||||
)
|
||||
@@ -28,6 +27,7 @@ func LoginDefault(ctx context.Context, c *app.RequestContext) {
|
||||
c.String(consts.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
start := time.Now() // 记录开始时间
|
||||
|
||||
// 查询登录用户
|
||||
loginUser, err := db.Client.UserInfo.
|
||||
@@ -35,23 +35,27 @@ func LoginDefault(ctx context.Context, c *app.RequestContext) {
|
||||
Where(userinfo.AccountEQ(req.Account),
|
||||
userinfo.PasswordEQ(req.Password)).
|
||||
Only(ctx)
|
||||
duration := time.Since(start) // 计算耗时
|
||||
|
||||
fmt.Println("耗时:", duration)
|
||||
if err != nil {
|
||||
response.Fail(c).Message("账号或密码错误").Send()
|
||||
return
|
||||
}
|
||||
|
||||
// 异步更新登录事件
|
||||
routinex.Go(func() {
|
||||
go func() {
|
||||
err := db.Client.UserInfo.
|
||||
Update().
|
||||
SetLastLoginTime(time.Now()).
|
||||
Where(userinfo.IDEQ(loginUser.ID)).
|
||||
Exec(ctx)
|
||||
|
||||
logx.CtxInfo(ctx, "测试测试")
|
||||
if err != nil {
|
||||
hlog.Errorf("更新用户登录时间失败,UserID=%d, err=%v", loginUser.ID, err)
|
||||
logx.CtxError(ctx, "更新用户登录时间失败,UserID=%d, err=%v", loginUser.ID, err)
|
||||
}
|
||||
})
|
||||
}()
|
||||
|
||||
// 包装数据
|
||||
token, err := jwtx.CreateAccessToken(loginUser.ID)
|
||||
|
||||
@@ -12,8 +12,6 @@ func Error() app.HandlerFunc {
|
||||
c.Next(ctx)
|
||||
|
||||
if err := c.Errors.Last(); err != nil {
|
||||
// 未知错误 -> 500
|
||||
//c.JSON(500, ErrorResponse{500, "Internal Server Error"})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,21 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"cn/oneao/base-go/logx"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/common/hlog"
|
||||
"github.com/hertz-contrib/requestid"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func Log() app.HandlerFunc {
|
||||
return func(c context.Context, ctx *app.RequestContext) {
|
||||
reqId := requestid.Get(ctx)
|
||||
|
||||
c = context.WithValue(c, "RequestID", reqId)
|
||||
|
||||
start := time.Now() // 记录开始时间
|
||||
|
||||
// 执行后续处理
|
||||
@@ -20,8 +24,7 @@ func Log() app.HandlerFunc {
|
||||
// 计算耗时
|
||||
duration := time.Since(start).Milliseconds()
|
||||
|
||||
hlog.Info("请求完成",
|
||||
zap.String("RequestID", requestid.Get(ctx)),
|
||||
logx.CtxInfo(c, "请求完成",
|
||||
zap.String("IP", ctx.ClientIP()),
|
||||
zap.String("Method", string(ctx.Method())),
|
||||
zap.String("Path", string(ctx.Path())),
|
||||
|
||||
Reference in new issue
Block a user