chore: init bj_power monorepo (dashboard/mes/wms/wms_client/workstation), clear subproject git metadata and align naming to folder names
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package login
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"bj_power_mes/common/errorx"
|
||||
xhttp "bj_power_mes/common/httpx"
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/user"
|
||||
"bj_power_mes/internal/auth"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type LoginLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 登录
|
||||
func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
|
||||
return &LoginLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.TokenReply, err error) {
|
||||
u, err := l.svcCtx.EntClient.User.Query().Where(user.UsernameEQ(req.Username)).First(l.ctx)
|
||||
if ent.IsNotFound(err) {
|
||||
return nil, errorx.New(errorx.BadUsernameOrPassword, "not found user")
|
||||
} else if err != nil {
|
||||
l.Error(err)
|
||||
return nil, errorx.New(errorx.DbError)
|
||||
}
|
||||
if !auth.CheckPassword(req.Password, u.Password) {
|
||||
return nil, errorx.New(errorx.BadUsernameOrPassword, "wrong password")
|
||||
}
|
||||
token, _ := GenToken(l.svcCtx.Config.Auth.AccessSecret, time.Now().Unix(), int64(l.svcCtx.Config.Auth.AccessExpire), map[string]any{
|
||||
xhttp.CtxKeyJwtUserId: u.ID,
|
||||
})
|
||||
refreshToken := GenRefreshToken()
|
||||
l.svcCtx.TokenStore.Setex(l.ctx, fmt.Sprintf("user:%d:refesh_token", u.ID), refreshToken, l.svcCtx.Config.Auth.RefreshExpire)
|
||||
resp = &types.TokenReply{
|
||||
AccessToken: token,
|
||||
AccessExpire: l.svcCtx.Config.Auth.AccessExpire,
|
||||
RefreshToken: refreshToken,
|
||||
RefreshExpire: l.svcCtx.Config.Auth.RefreshExpire,
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user