u
This commit is contained in:
1 parent
a57070f860
commit
b9f728812d
22 files changed
+2892
-391
No files matched your search
@@ -7,8 +7,9 @@ import (
|
||||
"cn/oneao/base-go/ent/userinfo"
|
||||
"cn/oneao/base-go/utils/jwtx"
|
||||
"cn/oneao/base-go/utils/response"
|
||||
"cn/oneao/base-go/utils/routinex"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/cloudwego/hertz/pkg/common/hlog"
|
||||
"time"
|
||||
|
||||
auth "cn/oneao/base-go/biz/model/auth"
|
||||
@@ -27,23 +28,32 @@ func LoginDefault(ctx context.Context, c *app.RequestContext) {
|
||||
c.String(consts.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
// 假设 ctx 已有,req.Account / req.Password 已有
|
||||
start := time.Now()
|
||||
|
||||
// 查询登录用户
|
||||
loginUser, err := db.Client.UserInfo.
|
||||
Query().
|
||||
Where(userinfo.AccountEQ(req.Account),
|
||||
userinfo.PasswordEQ(req.Password)).
|
||||
Only(ctx)
|
||||
|
||||
duration := time.Since(start) // 耗时
|
||||
fmt.Printf("登录查询: (耗时: %v)\n", duration)
|
||||
|
||||
if err != nil {
|
||||
c.String(consts.StatusInternalServerError, "账号或密码错误")
|
||||
response.Fail(c).Message("账号或密码错误").Send()
|
||||
return
|
||||
}
|
||||
|
||||
// 异步更新登录事件
|
||||
routinex.Go(func() {
|
||||
err := db.Client.UserInfo.
|
||||
Update().
|
||||
SetLastLoginTime(time.Now()).
|
||||
Where(userinfo.IDEQ(loginUser.ID)).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
hlog.Errorf("更新用户登录时间失败,UserID=%d, err=%v", loginUser.ID, err)
|
||||
}
|
||||
})
|
||||
|
||||
// 包装数据
|
||||
token, err := jwtx.CreateAccessToken(loginUser.ID)
|
||||
refreshToken, err := jwtx.CreateRefreshToken(loginUser.ID)
|
||||
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
// Code generated by hertz generator.
|
||||
|
||||
package hello
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
hello "cn/oneao/base-go/biz/model/hertz/hello"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/protocol/consts"
|
||||
)
|
||||
|
||||
// Method1 .
|
||||
// @router /hello [GET]
|
||||
func Method1(ctx context.Context, c *app.RequestContext) {
|
||||
var err error
|
||||
var req hello.HelloReq
|
||||
err = c.BindAndValidate(&req)
|
||||
if err != nil {
|
||||
c.String(consts.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
resp := new(hello.HelloResp)
|
||||
|
||||
c.JSON(consts.StatusOK, resp)
|
||||
}
|
||||
Reference in new issue
Block a user