添加后台用户表直接分离
This commit is contained in:
70
server/backend/internal/logic/backenduserloginlogic.go
Normal file
70
server/backend/internal/logic/backenduserloginlogic.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/backend/internal/svc"
|
||||
"fusenapi/server/backend/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type BackendUserLoginLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewBackendUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BackendUserLoginLogic {
|
||||
return &BackendUserLoginLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *BackendUserLoginLogic) BackendUserLogin(req *types.RequestUserLogin) (resp *basic.Response, jwtToken string) {
|
||||
// 创建一个 FsUserModel 对象 m 并实例化之,该对象用于操作 MySQL 数据库中的用户数据表。
|
||||
m := l.svcCtx.AllModels.FsBackendUser
|
||||
|
||||
// 在用户数据表中根据登录名(email)查找用户记录,并返回 UserModel 类型的结构体对象 userModel。
|
||||
user, err := m.FindUserByEmail(l.ctx, req.Name)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatus(basic.CodeEmailNotFoundErr), ""
|
||||
}
|
||||
|
||||
// 如果在用户数据表中找到了登录名匹配的用户记录,则判断密码是否匹配。
|
||||
if *user.PasswordHash != req.Password {
|
||||
logx.Info("密码错误")
|
||||
return resp.SetStatus(basic.CodePasswordErr), jwtToken
|
||||
}
|
||||
|
||||
// 如果密码匹配,则生成 JWT Token。
|
||||
nowSec := time.Now().Unix()
|
||||
jwtToken, err = auth.GenerateBackendJwtToken(&l.svcCtx.Config.Auth.AccessSecret, l.svcCtx.Config.Auth.AccessExpire, nowSec, user.Id, 0)
|
||||
|
||||
// 如果生成 JWT Token 失败,则抛出错误并返回未认证的状态码。
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatus(basic.CodeUnAuth), jwtToken
|
||||
}
|
||||
|
||||
// 如果更新 VerificationToken 字段失败,则返回未认证的状态码。
|
||||
if err != nil {
|
||||
return resp.SetStatus(basic.CodeUnAuth), jwtToken
|
||||
}
|
||||
|
||||
// 构造 DataUserLogin 类型的数据对象 data 并设置其属性值为生成的 JWT Token。
|
||||
data := &types.DataUserLogin{
|
||||
Token: jwtToken,
|
||||
}
|
||||
|
||||
// 返回认证成功的状态码以及数据对象 data 和 JWT Token。
|
||||
return resp.SetStatus(basic.CodeOK, data), jwtToken
|
||||
}
|
||||
34
server/backend/internal/logic/quotationdetaillogic.go
Normal file
34
server/backend/internal/logic/quotationdetaillogic.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/backend/internal/svc"
|
||||
"fusenapi/server/backend/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type QuotationDetailLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewQuotationDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QuotationDetailLogic {
|
||||
return &QuotationDetailLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *QuotationDetailLogic) QuotationDetail(req *types.Request, userinfo *auth.BackendUserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
||||
Reference in New Issue
Block a user