添加对应 php注册登录的token 验证逻辑

This commit is contained in:
2024-04-15 17:01:42 +08:00
parent de7cd23deb
commit 62db070f61
6 changed files with 109 additions and 177 deletions

View File

@@ -34,6 +34,7 @@ func (m *KillaraCustomerTokenModel) InsertToken(data *KillaraCustomerToken) erro
return m.db.Model(&KillaraCustomerToken{}).Transaction(func(tx *gorm.DB) error {
// 查找是否存在相同客户ID、平台和Token的记录
var existingToken KillaraCustomerToken
err := tx.Where("customer_id = ? AND platform = ? AND token = ?", data.CustomerId, data.Platform, *data.Token).First(&existingToken).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
@@ -44,7 +45,7 @@ func (m *KillaraCustomerTokenModel) InsertToken(data *KillaraCustomerToken) erro
if !errors.Is(err, gorm.ErrRecordNotFound) {
// 存在记录,先删除
err = tx.Delete(&existingToken).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
if err != nil {
tx.Rollback() // 删除错误,回滚事务
return err
}
@@ -57,7 +58,7 @@ func (m *KillaraCustomerTokenModel) InsertToken(data *KillaraCustomerToken) erro
return err
}
return tx.Commit().Error // 提交事务
return err // 提交事务
})
}
@@ -113,12 +114,12 @@ func (m *KillaraCustomerTokenModel) GetToken(token string) (*KillaraCustomerToke
// return nil, nil
// }
func (m *KillaraCustomerTokenModel) CheckToken(token string) (*KillaraCustomerToken, error) {
func (m *KillaraCustomerTokenModel) CheckToken(tokenstr string) (*KillaraCustomerToken, error) {
var resultToken *KillaraCustomerToken
err := m.db.Model(&KillaraCustomerToken{}).Transaction(func(tx *gorm.DB) error {
// 查找 Token 记录
var token KillaraCustomerToken
err := tx.Where("token = ?", token).First(&token).Error
err := tx.Where("token = ?", tokenstr).First(&token).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil
@@ -138,7 +139,7 @@ func (m *KillaraCustomerTokenModel) CheckToken(token string) (*KillaraCustomerTo
}
resultToken = &token
return tx.Commit().Error // 提交事务
return err // 提交事务
})
if err != nil {
@@ -150,7 +151,7 @@ func (m *KillaraCustomerTokenModel) CheckToken(token string) (*KillaraCustomerTo
func (m *KillaraCustomerTokenModel) ClearDuplicateToken(customerID uint64, currentToken string, platform int) error {
return m.db.Model(&KillaraCustomerToken{}).Transaction(func(tx *gorm.DB) error {
var tokens []KillaraCustomerToken
var tokens []*KillaraCustomerToken
err := tx.Where("customer_id = ? AND platform = ?", customerID, platform).Find(&tokens).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -164,7 +165,7 @@ func (m *KillaraCustomerTokenModel) ClearDuplicateToken(customerID uint64, curre
for _, token := range tokens {
if *token.Token != currentToken {
// err := tx.Delete(token).Error todo: 不太明白php为什么不删除token, 难道用来链路跟踪? 但是客户端id也被更新了, 没有存在的意义了
err := tx.Where("token = ?", token).Update("customer_id", 0).Error
err := tx.Where("token = ?", *token.Token).Update("customer_id", 0).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fmt.Errorf("tokens 查询出来, 自身都不存在, 疑似出了什么错误2")