fusenapi/home-user-auth/internal/logic/userloginlogic.go
2023-06-06 17:36:10 +08:00

56 lines
1.5 KiB
Go

package logic
import (
"context"
"fusenapi/home-user-auth/internal/svc"
"fusenapi/home-user-auth/internal/types"
"fusenapi/model"
"fusenapi/utils/basic"
"github.com/golang-jwt/jwt"
"github.com/zeromicro/go-zero/core/logx"
)
type UserLoginLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
return &UserLoginLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UserLoginLogic) getJwtToken(secretKey string, iat, seconds, userId int64) (string, error) {
claims := make(jwt.MapClaims)
claims["exp"] = iat + seconds
claims["iat"] = iat
claims["userId"] = userId
token := jwt.New(jwt.SigningMethodHS256)
token.Claims = claims
return token.SignedString([]byte(secretKey))
}
func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin) (resp *types.Response) {
// 必须返回response, 前端需要的是内部约定的Code码, 处理相关的逻辑. 例子(eg): resp.Set(501, "error")
resp = &types.Response{}
userModel, err := model.NewFsUserModel(l.svcCtx.MysqlConn).FindOneByEmail(l.ctx, req.Name)
// log.Printf("%t %t %v", err, model.ErrNotFound, err == model.ErrNotFound)
if err == model.ErrNotFound {
logx.Error(err)
resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
return resp
}
data := &types.DataUserLogin{
Token: userModel.PasswordResetToken.String,
}
resp.SetStatus(basic.CodeOK, data)
return resp
}