2023-06-01 10:35:09 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-06 12:08:32 +00:00
|
|
|
"time"
|
2023-06-01 10:35:09 +00:00
|
|
|
|
|
|
|
"fusenapi/home-user-auth/internal/svc"
|
|
|
|
"fusenapi/home-user-auth/internal/types"
|
|
|
|
"fusenapi/model"
|
2023-06-05 09:56:55 +00:00
|
|
|
"fusenapi/utils/basic"
|
2023-06-01 10:35:09 +00:00
|
|
|
|
2023-06-06 09:25:49 +00:00
|
|
|
"github.com/golang-jwt/jwt"
|
2023-06-01 10:35:09 +00:00
|
|
|
"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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-06 12:08:32 +00:00
|
|
|
func (l *UserLoginLogic) genJwtToken(accessSecret string, accessExpire, nowSec, userid int64) (string, error) {
|
|
|
|
|
2023-06-06 09:25:49 +00:00
|
|
|
claims := make(jwt.MapClaims)
|
2023-06-06 12:08:32 +00:00
|
|
|
claims["exp"] = nowSec + accessExpire
|
|
|
|
claims["iat"] = nowSec
|
|
|
|
claims["userid"] = userid
|
2023-06-06 09:25:49 +00:00
|
|
|
token := jwt.New(jwt.SigningMethodHS256)
|
|
|
|
token.Claims = claims
|
2023-06-06 12:08:32 +00:00
|
|
|
return token.SignedString([]byte(accessSecret))
|
2023-06-06 09:25:49 +00:00
|
|
|
}
|
|
|
|
|
2023-06-06 12:08:32 +00:00
|
|
|
func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin) (resp *types.Response, jwtToken string) {
|
2023-06-05 09:56:55 +00:00
|
|
|
// 必须返回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)
|
2023-06-06 12:08:32 +00:00
|
|
|
|
2023-06-01 10:35:09 +00:00
|
|
|
if err == model.ErrNotFound {
|
2023-06-05 09:56:55 +00:00
|
|
|
logx.Error(err)
|
2023-06-06 09:36:10 +00:00
|
|
|
resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
|
2023-06-06 12:08:32 +00:00
|
|
|
return resp, jwtToken
|
2023-06-01 10:35:09 +00:00
|
|
|
}
|
2023-06-06 12:08:32 +00:00
|
|
|
|
|
|
|
// jwt 生成
|
|
|
|
nowSec := time.Now().Unix()
|
|
|
|
jwtToken, err = l.genJwtToken(l.svcCtx.Config.Auth.AccessSecret, l.svcCtx.Config.Auth.AccessExpire, nowSec, userModel.Id)
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
resp.SetStatus(basic.CodeUnAuth)
|
|
|
|
return resp, jwtToken
|
|
|
|
}
|
|
|
|
|
2023-06-05 09:56:55 +00:00
|
|
|
data := &types.DataUserLogin{
|
2023-06-06 12:08:32 +00:00
|
|
|
Token: userModel.PasswordResetToken.String,
|
|
|
|
JwtToken: jwtToken,
|
2023-06-01 10:35:09 +00:00
|
|
|
}
|
2023-06-06 12:08:32 +00:00
|
|
|
|
2023-06-06 09:36:10 +00:00
|
|
|
resp.SetStatus(basic.CodeOK, data)
|
2023-06-06 12:08:32 +00:00
|
|
|
return resp, jwtToken
|
2023-06-01 10:35:09 +00:00
|
|
|
}
|