78 lines
2.1 KiB
Go
78 lines
2.1 KiB
Go
package analysisuserlogic
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"lone-services/pkg/redis"
|
|
"lone-services/pkg/utils"
|
|
"lone-services/pkg/validate"
|
|
pkgws "lone-services/pkg/ws"
|
|
analysis "lone-services/rpc/analysis/pb"
|
|
"lone-services/services/analysis/internal/logic/common"
|
|
"lone-services/services/analysis/internal/svc"
|
|
"lone-services/services/analysis/validator"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type UserAuthLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewUserAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserAuthLogic {
|
|
return &UserAuthLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *UserAuthLogic) UserAuth(in *analysis.UserAuthReq) (*analysis.Response, error) {
|
|
var req validator.UserAuthValidator
|
|
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
|
return common.OutResponse(utils.ErrorParams, msg), nil
|
|
}
|
|
|
|
loginUser, fail := common.RequireUserLogin(l.ctx)
|
|
if fail != nil {
|
|
return fail, nil
|
|
}
|
|
|
|
storeKey := pkgws.RedisKeyQRSession + strconv.FormatInt(req.StoreId, utils.NumberTen)
|
|
sessionID, err := redis.Client.Get(l.ctx, storeKey).Result()
|
|
if err != nil && err != redis.Nil {
|
|
l.Errorf("user auth redis get session: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
if err == redis.Nil {
|
|
sessionID = utils.StringEmpty
|
|
}
|
|
|
|
if req.Type == int32(utils.StoreAuthBind) {
|
|
bizId := loginUser.BizId
|
|
if bizId < utils.NumberOne {
|
|
return common.OutResponse(utils.ErrorParams, "业务用户不存在"), nil
|
|
}
|
|
if err := common.BindStoreAuth(loginUser.ID, bizId, req.StoreId, req.ValidityTime); err != nil {
|
|
l.Errorf("user auth bind: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
}
|
|
|
|
if sessionID == utils.StringEmpty {
|
|
return common.OkResponse(nil), nil
|
|
}
|
|
|
|
if pubErr := pkgws.Publish(l.ctx, sessionID, pkgws.Success(map[string]interface{}{
|
|
"ws_type": req.Type,
|
|
})); pubErr != nil {
|
|
l.Errorf("user auth publish: %v", pubErr)
|
|
}
|
|
_ = redis.Client.Del(l.ctx, storeKey).Err()
|
|
|
|
return common.OkResponse(nil), nil
|
|
}
|