47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
|
package whitelistr
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
"tianyuan-api/apps/gateway/internal/validator"
|
||
|
"tianyuan-api/apps/sentinel/client/whitelist"
|
||
|
|
||
|
"tianyuan-api/apps/gateway/internal/svc"
|
||
|
"tianyuan-api/apps/gateway/internal/types"
|
||
|
|
||
|
"github.com/zeromicro/go-zero/core/logx"
|
||
|
)
|
||
|
|
||
|
type AddWhitelistLogic struct {
|
||
|
logx.Logger
|
||
|
ctx context.Context
|
||
|
svcCtx *svc.ServiceContext
|
||
|
}
|
||
|
|
||
|
func NewAddWhitelistLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddWhitelistLogic {
|
||
|
return &AddWhitelistLogic{
|
||
|
Logger: logx.WithContext(ctx),
|
||
|
ctx: ctx,
|
||
|
svcCtx: svcCtx,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (l *AddWhitelistLogic) AddWhitelist(req *types.AddWhitelistReq) error {
|
||
|
userId, ok := l.ctx.Value("userId").(int64)
|
||
|
if !ok {
|
||
|
return errors.New("无法获取 userId")
|
||
|
}
|
||
|
isIp := validator.IsValidIPAddress(req.Ip)
|
||
|
if !isIp {
|
||
|
return errors.New("请输入正确的IP地址")
|
||
|
}
|
||
|
_, err := l.svcCtx.WhitelistRpc.CreateWhitelist(l.ctx, &whitelist.CreateWhitelistRequest{
|
||
|
UserId: userId,
|
||
|
WhitelistIp: req.Ip,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|