49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
|
|
package captcha
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
|
|||
|
|
"ycc-server/app/main/api/internal/svc"
|
|||
|
|
"ycc-server/app/main/api/internal/types"
|
|||
|
|
"ycc-server/pkg/captcha"
|
|||
|
|
|
|||
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
type GetEncryptedSceneIdLogic struct {
|
|||
|
|
logx.Logger
|
|||
|
|
ctx context.Context
|
|||
|
|
svcCtx *svc.ServiceContext
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func NewGetEncryptedSceneIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetEncryptedSceneIdLogic {
|
|||
|
|
return &GetEncryptedSceneIdLogic{
|
|||
|
|
Logger: logx.WithContext(ctx),
|
|||
|
|
ctx: ctx,
|
|||
|
|
svcCtx: svcCtx,
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (l *GetEncryptedSceneIdLogic) GetEncryptedSceneId() (*types.GetEncryptedSceneIdResp, error) {
|
|||
|
|
cfg := l.svcCtx.Config.Captcha
|
|||
|
|
|
|||
|
|
// 如果没有配置 ekey,返回空(使用非加密模式)
|
|||
|
|
if cfg.EKey == "" {
|
|||
|
|
l.Logger.Info("[Captcha] 未配置 EKey,使用非加密模式")
|
|||
|
|
return &types.GetEncryptedSceneIdResp{
|
|||
|
|
EncryptedSceneId: "",
|
|||
|
|
}, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 生成加密场景ID,有效期1小时
|
|||
|
|
encrypted, err := captcha.GenerateEncryptedSceneID(cfg.SceneID, cfg.EKey, 3600)
|
|||
|
|
if err != nil {
|
|||
|
|
l.Logger.Errorf("[Captcha] 生成加密场景ID失败: %+v", err)
|
|||
|
|
return nil, err
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return &types.GetEncryptedSceneIdResp{
|
|||
|
|
EncryptedSceneId: encrypted,
|
|||
|
|
}, nil
|
|||
|
|
}
|