强制手机绑定
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m1s

This commit is contained in:
2026-04-24 10:26:32 +08:00
parent 3f2cf31543
commit 15cd26c81a
6 changed files with 41 additions and 4 deletions

View File

@@ -14,12 +14,12 @@ import (
wechatConfigSvc "github.com/break/junhong_cmp_fiber/internal/service/wechat_config"
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
"github.com/break/junhong_cmp_fiber/pkg/auth"
"github.com/break/junhong_cmp_fiber/pkg/config"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/break/junhong_cmp_fiber/pkg/errors"
"github.com/break/junhong_cmp_fiber/pkg/wechat"
"github.com/golang-jwt/jwt/v5"
"github.com/redis/go-redis/v9"
"github.com/spf13/viper"
"go.uber.org/zap"
"gorm.io/gorm"
)
@@ -439,19 +439,27 @@ func (s *Service) signAssetToken(assetType string, assetID uint) (string, error)
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString([]byte(viper.GetString("jwt.secret_key") + ":asset"))
cfg := config.Get()
if cfg == nil || cfg.JWT.SecretKey == "" {
return "", errors.New(errors.CodeInternalError, "JWT 密钥未配置")
}
return token.SignedString([]byte(cfg.JWT.SecretKey + ":asset"))
}
func (s *Service) verifyAssetToken(assetToken string) (*assetTokenClaims, error) {
if assetToken == "" {
return nil, errors.New(errors.CodeInvalidParam)
}
cfg := config.Get()
if cfg == nil || cfg.JWT.SecretKey == "" {
return nil, errors.New(errors.CodeInternalError, "JWT 密钥未配置")
}
parsed, err := jwt.ParseWithClaims(assetToken, &assetTokenClaims{}, func(token *jwt.Token) (any, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, errors.New(errors.CodeInvalidToken)
}
return []byte(viper.GetString("jwt.secret_key") + ":asset"), nil
return []byte(cfg.JWT.SecretKey + ":asset"), nil
})
if err != nil {
return nil, errors.New(errors.CodeInvalidToken)
@@ -719,12 +727,17 @@ func (s *Service) issueLoginToken(ctx context.Context, customerID uint, assetTyp
// 查询用户已绑定的主手机号,写入 JWT供后续接口直接从 context 取用
var boundPhone string
needBindPhone := false
cfg := config.Get()
requirePhoneBinding := true
if cfg != nil {
requirePhoneBinding = cfg.Client.RequirePhoneBinding
}
primaryPhone, phoneErr := s.phoneStore.GetPrimaryPhone(ctx, customerID)
if phoneErr == nil {
boundPhone = primaryPhone.Phone
} else if phoneErr != gorm.ErrRecordNotFound {
return "", false, errors.Wrap(errors.CodeInternalError, phoneErr, "查询手机号绑定关系失败")
} else if viper.GetBool("client.require_phone_binding") {
} else if requirePhoneBinding {
needBindPhone = true
}