fix: 第1批 F1、M12 一资产一号与换货按手机号关联校验
- F1 一个资产同时只能有一个有效手机号关联,他号绑定返回 1192(不含号码),同号幂等 - 登录 need_bind_phone 改看资产是否已有有效关联,新增 asset_phone_bound、asset_phone_is_own;H5 绑定按客户/资产两侧状态分别写入 - 客户换号不再迁移资产关联 - M12 换货新资产有有效手机号关联即拒绝,号码只在 data.phone;换货完成清空旧资产客户绑定并失效旧资产关联(exchange_completed),均不迁移 - OpenSpec Change enforce-single-phone-per-asset:11/16,H5 短信与换货端到端验证待测试部署后完成
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
|
||||
accessauditapp "github.com/break/junhong_cmp_fiber/internal/application/accessaudit"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/config"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
@@ -88,15 +87,14 @@ func (s *Service) establishAssociation(
|
||||
return s.associationWriter.Establish(ctx, tx, customer, phone, assetType, assetID)
|
||||
}
|
||||
|
||||
// migrateAssociations 委托关联写入规则完成换绑原子迁移与上限/冲突判定。
|
||||
// 开关关闭时完全短路,不迁移任何关联。
|
||||
func (s *Service) migrateAssociations(
|
||||
ctx context.Context,
|
||||
tx *gorm.DB,
|
||||
oldPhone, newPhone string,
|
||||
) ([]accessauditapp.PhoneAssetAssociationChange, error) {
|
||||
if !requirePhoneBinding() {
|
||||
return nil, nil
|
||||
// assetHasValidAssociation 判断当前访问资产是否已有任一有效手机号关联;开关关闭或无资产身份时返回 false。
|
||||
func (s *Service) assetHasValidAssociation(ctx context.Context, tx *gorm.DB, assetType string, assetID uint) (bool, error) {
|
||||
if !requirePhoneBinding() || assetType == "" || assetID == 0 {
|
||||
return false, nil
|
||||
}
|
||||
return s.associationWriter.Migrate(ctx, tx, oldPhone, newPhone)
|
||||
phones, err := s.associationStore.WithTx(tx).ListValidByAsset(ctx, assetType, assetID)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(errors.CodeInternalError, err, "查询资产手机号关联失败")
|
||||
}
|
||||
return len(phones) > 0, nil
|
||||
}
|
||||
|
||||
@@ -197,7 +197,7 @@ func (s *Service) WechatLogin(ctx context.Context, req *dto.WechatLoginRequest,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
token, needBindPhone, err := s.issueLoginToken(ctx, customerID, assetClaims.AssetType, assetClaims.AssetID)
|
||||
token, bindState, err := s.issueLoginToken(ctx, customerID, assetClaims.AssetType, assetClaims.AssetID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -208,9 +208,11 @@ func (s *Service) WechatLogin(ctx context.Context, req *dto.WechatLoginRequest,
|
||||
)
|
||||
|
||||
return &dto.WechatLoginResponse{
|
||||
Token: token,
|
||||
NeedBindPhone: needBindPhone,
|
||||
IsNewUser: isNewUser,
|
||||
Token: token,
|
||||
NeedBindPhone: bindState.NeedBindPhone,
|
||||
AssetPhoneBound: bindState.AssetPhoneBound,
|
||||
AssetPhoneIsOwn: bindState.AssetPhoneIsOwn,
|
||||
IsNewUser: isNewUser,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -256,7 +258,7 @@ func (s *Service) MiniappLogin(ctx context.Context, req *dto.MiniappLoginRequest
|
||||
return nil, err
|
||||
}
|
||||
|
||||
token, needBindPhone, err := s.issueLoginToken(ctx, customerID, assetClaims.AssetType, assetClaims.AssetID)
|
||||
token, bindState, err := s.issueLoginToken(ctx, customerID, assetClaims.AssetType, assetClaims.AssetID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -267,9 +269,11 @@ func (s *Service) MiniappLogin(ctx context.Context, req *dto.MiniappLoginRequest
|
||||
)
|
||||
|
||||
return &dto.WechatLoginResponse{
|
||||
Token: token,
|
||||
NeedBindPhone: needBindPhone,
|
||||
IsNewUser: isNewUser,
|
||||
Token: token,
|
||||
NeedBindPhone: bindState.NeedBindPhone,
|
||||
AssetPhoneBound: bindState.AssetPhoneBound,
|
||||
AssetPhoneIsOwn: bindState.AssetPhoneIsOwn,
|
||||
IsNewUser: isNewUser,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -299,8 +303,12 @@ func (s *Service) SendCode(ctx context.Context, req *dto.ClientSendCodeRequest,
|
||||
|
||||
// BindPhone A5 绑定手机号
|
||||
// POST /api/c/v1/auth/bind-phone
|
||||
// 无主手机号时建立账号手机号;已有主手机号且提交号码与主号一致、验证码有效时,
|
||||
// 幂等建立当前访问资产与该手机号的关联且不修改账号手机号;提交号码与主号不一致仍拒绝。
|
||||
// 客户手机号与资产手机号相互独立,按两侧状态分别写入:
|
||||
// - 无主手机号、资产无有效关联:同时写客户手机号与资产关联;
|
||||
// - 无主手机号、资产已有有效关联:只写客户手机号,不动资产关联;
|
||||
// - 已有主手机号、资产无有效关联:只为资产建立关联,号码可与主号不同,不改客户手机号;
|
||||
// - 已有主手机号、资产已有有效关联:同号幂等成功,其他号码按一资产一号拒绝。
|
||||
//
|
||||
// 请求不含当前访问资产身份时只完成账号手机号绑定,不建立关联。
|
||||
func (s *Service) BindPhone(ctx context.Context, customerID uint, assetType string, assetID uint, req *dto.BindPhoneRequest) (*dto.BindPhoneResponse, error) {
|
||||
if req == nil {
|
||||
@@ -380,6 +388,11 @@ func (s *Service) BindPhone(ctx context.Context, customerID uint, assetType stri
|
||||
)); err != nil {
|
||||
return err
|
||||
}
|
||||
// 资产已有有效关联(无论号码属于谁)时只写客户手机号,不动资产关联。
|
||||
assetBound, err := s.assetHasValidAssociation(ctx, tx, assetType, assetID)
|
||||
if err != nil || assetBound {
|
||||
return err
|
||||
}
|
||||
// 建联与账号手机号同事务:超限或写入失败时账号手机号一并回滚。
|
||||
if _, err := s.establishAssociation(ctx, tx, customer, record.Phone, assetType, assetID); err != nil {
|
||||
return err
|
||||
@@ -397,8 +410,8 @@ func (s *Service) BindPhone(ctx context.Context, customerID uint, assetType stri
|
||||
}, nil
|
||||
}
|
||||
|
||||
// bindExistingPrimaryPhone 处理已有主手机号的绑定请求。
|
||||
// 提交号码等于主号且验证码有效时幂等建立关联且不改账号手机号;号码不一致仍拒绝换号。
|
||||
// bindExistingPrimaryPhone 处理已有主手机号的绑定请求:只为当前访问资产建立关联,不改账号手机号。
|
||||
// 强制绑定开启且带资产身份时,资产侧号码可与主号不同;否则提交号码必须等于主号(不支持经绑定入口换号)。
|
||||
func (s *Service) bindExistingPrimaryPhone(
|
||||
ctx context.Context,
|
||||
customer *model.PersonalCustomer,
|
||||
@@ -407,7 +420,8 @@ func (s *Service) bindExistingPrimaryPhone(
|
||||
assetID uint,
|
||||
req *dto.BindPhoneRequest,
|
||||
) (*dto.BindPhoneResponse, error) {
|
||||
if primary.Phone != req.Phone {
|
||||
assetBinding := requirePhoneBinding() && assetType != "" && assetID != 0
|
||||
if primary.Phone != req.Phone && !assetBinding {
|
||||
appErr := errors.New(errors.CodeAlreadyBoundPhone)
|
||||
s.recordPersonalFailure(ctx, constants.AuditActionPersonalCustomerPhoneBound, "绑定个人手机号被拒绝", customer, primary, appErr)
|
||||
return nil, appErr
|
||||
@@ -421,7 +435,7 @@ func (s *Service) bindExistingPrimaryPhone(
|
||||
now := time.Now()
|
||||
err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
// 固定加锁次序:先取手机号串行化点(advisory),再按 id ASC 锁行。
|
||||
if err := s.lockPhoneScopesInOrder(ctx, tx, primary.Phone); err != nil {
|
||||
if err := s.lockPhoneScopesInOrder(ctx, tx, req.Phone); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.lockPhoneRowsInIDOrder(ctx, tx, []uint{primary.ID}); err != nil {
|
||||
@@ -436,11 +450,11 @@ func (s *Service) bindExistingPrimaryPhone(
|
||||
}
|
||||
return errors.Wrap(errors.CodeInternalError, err, "查询主手机号失败")
|
||||
}
|
||||
if locked.Phone != req.Phone {
|
||||
if !assetBinding && locked.Phone != req.Phone {
|
||||
// 并发换绑已把主号改成其他号码:提交号码不再等于账号手机号,按既有语义拒绝。
|
||||
return errors.New(errors.CodeAlreadyBoundPhone)
|
||||
}
|
||||
_, err := s.establishAssociation(ctx, tx, customer, locked.Phone, assetType, assetID)
|
||||
_, err := s.establishAssociation(ctx, tx, customer, req.Phone, assetType, assetID)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
@@ -498,9 +512,8 @@ func (s *Service) ChangePhone(ctx context.Context, customerID uint, req *dto.Cha
|
||||
now := time.Now()
|
||||
var beforeData map[string]any
|
||||
var failurePhone *model.PersonalCustomerPhone
|
||||
var migrationChanges []accessauditapp.PhoneAssetAssociationChange
|
||||
err = s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
// 统一加锁顺序:先按手机号行 id ASC 锁定旧、新手机号行,再由关联迁移按 id ASC 锁定两侧有效关系行。
|
||||
// 统一加锁顺序:先取旧、新手机号串行化点,再按手机号行 id ASC 锁定旧、新手机号行。
|
||||
// 换绑两行的加锁顺序与 bind-phone 的单行加锁一致,两条路径不会形成 A→B / B→A 死锁环。
|
||||
if idErr := s.lockPhoneScopesInOrder(ctx, tx, primary.Phone, req.NewPhone); idErr != nil {
|
||||
return idErr
|
||||
@@ -533,12 +546,7 @@ func (s *Service) ChangePhone(ctx context.Context, customerID uint, req *dto.Cha
|
||||
} else if err != nil && err != gorm.ErrRecordNotFound {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "查询新手机号绑定关系失败")
|
||||
}
|
||||
// 关联迁移先于账号手机号改写:超限或与既有关系冲突时整次失败,旧、新关系均保持原状。
|
||||
changes, migrateErr := s.migrateAssociations(ctx, tx, primary.Phone, req.NewPhone)
|
||||
if migrateErr != nil {
|
||||
return migrateErr
|
||||
}
|
||||
migrationChanges = changes
|
||||
// 客户手机号与资产手机号相互独立:换号只改客户手机号,不迁移、复制或失效任何资产关联。
|
||||
if err := tx.Model(primary).Updates(map[string]any{
|
||||
"phone": req.NewPhone,
|
||||
"verified_at": now,
|
||||
@@ -548,22 +556,10 @@ func (s *Service) ChangePhone(ctx context.Context, customerID uint, req *dto.Cha
|
||||
}
|
||||
primary.Phone = req.NewPhone
|
||||
primary.VerifiedAt = &now
|
||||
if err := s.accessAudit.WriteAccessChange(ctx, tx, personalPhoneAudit(
|
||||
return s.accessAudit.WriteAccessChange(ctx, tx, personalPhoneAudit(
|
||||
constants.AuditActionPersonalCustomerPhoneChanged, "更换个人手机号", customer, primary, beforeData,
|
||||
map[string]any{"phone": sanitizer.MaskPhone(primary.Phone)}, "手机号已更换", constants.AuditResultSuccess,
|
||||
)); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(migrationChanges) == 0 {
|
||||
return nil
|
||||
}
|
||||
return s.accessAudit.WriteAccessChange(ctx, tx, accessauditapp.ChangeAudit{
|
||||
ActionCode: constants.AuditActionPhoneAssetAssociationMigrated,
|
||||
Summary: "换绑手机号并迁移资产关联",
|
||||
OperatorID: customer.ID, ActorKind: constants.AuditActorPersonalCustomer, ActorName: customer.Nickname,
|
||||
Source: constants.AuditSourcePersonalAPI, ScopeType: constants.AuditScopePersonalCustomer,
|
||||
PhoneAssociations: migrationChanges,
|
||||
})
|
||||
))
|
||||
})
|
||||
if err != nil {
|
||||
if failurePhone == nil {
|
||||
@@ -1014,10 +1010,10 @@ func (s *Service) bindAsset(ctx context.Context, tx *gorm.DB, customerID uint, a
|
||||
// 已存在有效关系 → false。全局开关关闭时恒为 false,且不查询、不创建、不删除任何关系。
|
||||
// 关联查询使用当前访问资产的 asset_type/asset_id,不使用 phone claim:phone 是登录时快照,
|
||||
// 换绑后到下次登录前仍是旧号。该字段只作前端提示,不改变任何资源授权。
|
||||
func (s *Service) issueLoginToken(ctx context.Context, customerID uint, assetType string, assetID uint) (string, bool, error) {
|
||||
func (s *Service) issueLoginToken(ctx context.Context, customerID uint, assetType string, assetID uint) (string, loginBindState, error) {
|
||||
// 查询用户已绑定的主手机号,写入 JWT,供后续接口直接从 context 取用
|
||||
var boundPhone string
|
||||
needBindPhone := false
|
||||
state := loginBindState{}
|
||||
cfg := config.Get()
|
||||
requirePhoneBinding := true
|
||||
if cfg != nil {
|
||||
@@ -1027,30 +1023,26 @@ func (s *Service) issueLoginToken(ctx context.Context, customerID uint, assetTyp
|
||||
if phoneErr == nil {
|
||||
boundPhone = primaryPhone.Phone
|
||||
} else if phoneErr != gorm.ErrRecordNotFound {
|
||||
return "", false, errors.Wrap(errors.CodeInternalError, phoneErr, "查询手机号绑定关系失败")
|
||||
return "", state, errors.Wrap(errors.CodeInternalError, phoneErr, "查询手机号绑定关系失败")
|
||||
}
|
||||
|
||||
// 开关关闭必须完全短路:既不查询也不写任何关系,否则会出现「关闭开关却写库」的越权写入。
|
||||
if requirePhoneBinding {
|
||||
if boundPhone == "" {
|
||||
needBindPhone = true
|
||||
} else {
|
||||
associated, err := s.associationStore.ExistsValid(ctx, boundPhone, assetType, assetID)
|
||||
if err != nil {
|
||||
return "", false, errors.Wrap(errors.CodeInternalError, err, "查询手机号资产关联失败")
|
||||
}
|
||||
needBindPhone = !associated
|
||||
resolved, err := s.resolveLoginBindState(ctx, boundPhone, assetType, assetID)
|
||||
if err != nil {
|
||||
return "", state, err
|
||||
}
|
||||
state = resolved
|
||||
}
|
||||
|
||||
token, err := s.jwtManager.GeneratePersonalCustomerToken(customerID, boundPhone, assetType, assetID)
|
||||
if err != nil {
|
||||
return "", false, errors.Wrap(errors.CodeInternalError, err, "生成登录令牌失败")
|
||||
return "", state, errors.Wrap(errors.CodeInternalError, err, "生成登录令牌失败")
|
||||
}
|
||||
|
||||
claims, err := s.jwtManager.VerifyPersonalCustomerToken(token)
|
||||
if err != nil {
|
||||
return "", false, errors.Wrap(errors.CodeInternalError, err, "解析登录令牌失败")
|
||||
return "", state, errors.Wrap(errors.CodeInternalError, err, "解析登录令牌失败")
|
||||
}
|
||||
|
||||
ttl := time.Until(claims.ExpiresAt.Time)
|
||||
@@ -1060,10 +1052,38 @@ func (s *Service) issueLoginToken(ctx context.Context, customerID uint, assetTyp
|
||||
|
||||
redisKey := constants.RedisPersonalCustomerTokenKey(customerID)
|
||||
if err := s.redis.Set(ctx, redisKey, token, ttl).Err(); err != nil {
|
||||
return "", false, errors.Wrap(errors.CodeRedisError, err, "保存登录状态失败")
|
||||
return "", state, errors.Wrap(errors.CodeRedisError, err, "保存登录状态失败")
|
||||
}
|
||||
|
||||
return token, needBindPhone, nil
|
||||
return token, state, nil
|
||||
}
|
||||
|
||||
// loginBindState 是登录响应的手机号绑定提示,只含布尔值,不含号码。
|
||||
type loginBindState struct {
|
||||
NeedBindPhone bool
|
||||
AssetPhoneBound bool
|
||||
AssetPhoneIsOwn bool
|
||||
}
|
||||
|
||||
// resolveLoginBindState 按客户主手机号与当前资产有效关联判定绑定提示:
|
||||
// 客户无主手机号仍需绑定;资产尚无有效关联需为资产绑定;资产已有有效关联(无论号码属于谁)且客户有主手机号时不需绑定。
|
||||
func (s *Service) resolveLoginBindState(ctx context.Context, boundPhone, assetType string, assetID uint) (loginBindState, error) {
|
||||
state := loginBindState{}
|
||||
if assetType != "" && assetID != 0 {
|
||||
assetPhones, err := s.associationStore.ListValidByAsset(ctx, assetType, assetID)
|
||||
if err != nil {
|
||||
return state, errors.Wrap(errors.CodeInternalError, err, "查询资产手机号关联失败")
|
||||
}
|
||||
state.AssetPhoneBound = len(assetPhones) > 0
|
||||
for _, phone := range assetPhones {
|
||||
if boundPhone != "" && phone == boundPhone {
|
||||
state.AssetPhoneIsOwn = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
state.NeedBindPhone = boundPhone == "" || !state.AssetPhoneBound
|
||||
return state, nil
|
||||
}
|
||||
|
||||
// DevLogin 开发环境测试登录
|
||||
|
||||
Reference in New Issue
Block a user