复现双验签

This commit is contained in:
2024-04-11 14:57:47 +08:00
parent b7239af0e0
commit 43c9890f06
4 changed files with 411 additions and 0 deletions

View File

@@ -1,6 +1,10 @@
package model
import (
"encoding/json"
"errors"
"time"
"gorm.io/gorm"
)
@@ -9,3 +13,144 @@ type KillaraCustomerTokenModel struct {
db *gorm.DB
TableName string // 表名
}
// InsertToken inserts a new token record
// func (m *KillaraCustomerTokenModel) InsertToken(data map[string]interface{}) error {
// token, ok := data["token"].(string)
// if !ok {
// return ErrInvalidTokenValue
// }
// m.DeleteToken(token)
// customerID, _ := data["customer_id"].(uint64)
// platform, _ := data["platform"].(int64)
// if customerID != 0 && platform != 0 {
// m.db.Where("customer_id = ? AND platform = ?", customerID, platform).Delete(&KillaraCustomerToken{})
// }
// return m.db.Create(&KillaraCustomerToken{
// Token: &token,
// CustomerId: &customerID,
// Data: nilStringPtr(data["data"].(string)),
// Expiry: nilTimePtr(time.Unix(int64(data["expiry"].(float64)), 0)),
// Platform: platform,
// }).Error
// }
// UpdateTokenData updates the data field of a token record
func (m *KillaraCustomerTokenModel) UpdateTokenData(token string, data map[string]interface{}) error {
dataStr, _ := json.Marshal(data)
return m.db.Model(&KillaraCustomerToken{}).Where("token = ?", token).Update("data", string(dataStr)).Error
}
// UpdateTokenExpiry updates the expiry field of a token record
func (m *KillaraCustomerTokenModel) UpdateTokenExpiry(token string, expiry int64) error {
return m.db.Model(&KillaraCustomerToken{}).Where("token = ?", token).Update("expiry", time.Unix(expiry, 0)).Error
}
// UpdateTokenCustomerID updates the customer_id field of a token record
func (m *KillaraCustomerTokenModel) UpdateTokenCustomerID(token string, customerID uint64) error {
err := m.db.Model(&KillaraCustomerToken{}).Where("token = ?", token).Update("customer_id", customerID).Error
if err == gorm.ErrRecordNotFound {
return nil
}
return err
}
// DeleteToken deletes a token record
func (m *KillaraCustomerTokenModel) DeleteToken(token string) error {
err := m.db.Where("token = ?", token).Delete(&KillaraCustomerToken{}).Error
if err == gorm.ErrRecordNotFound {
return nil
}
return err
}
// GetToken retrieves a token record by token value
func (m *KillaraCustomerTokenModel) GetToken(token string) (*KillaraCustomerToken, error) {
var record KillaraCustomerToken
err := m.db.Where("token = ?", token).First(&record).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return &record, err
}
// CheckToken checks if a token is valid
// func (m *KillaraCustomerTokenModel) CheckToken(token string) (*KillaraCustomerToken, error) {
// var record KillaraCustomerToken
// err := m.db.Where("token = ?", token).First(&record).Error
// if err != nil {
// return nil, err
// }
// if record.Expiry == nil || record.Expiry.Before(time.Now()) {
// m.DeleteToken(token)
// return nil, ErrTokenExpired
// }
// return &record, nil
// }
// ClearDuplicateToken clears duplicate tokens for a customer_id
func (m *KillaraCustomerTokenModel) ClearDuplicateToken(customerID uint64, currentToken string, platform int64) error {
tokens := []KillaraCustomerToken{}
err := m.db.Where("customer_id = ? AND platform = ?", customerID, platform).Find(&tokens).Error
if err != nil {
return err
}
for _, token := range tokens {
if *token.Token != currentToken {
m.UpdateTokenCustomerID(*token.Token, 0)
}
}
return nil
}
// GetTokenByCustomerID retrieves the token value for a customer_id
func (m *KillaraCustomerTokenModel) GetTokenByCustomerID(customerID uint64) (string, error) {
var record KillaraCustomerToken
err := m.db.Select("token").Where("customer_id = ?", customerID).First(&record).Error
return *record.Token, err
}
// GetTokenDataByCustomerID retrieves the token data for a customer_id
func (m *KillaraCustomerTokenModel) GetTokenDataByCustomerID(customerID uint64) (map[string]interface{}, error) {
var record KillaraCustomerToken
err := m.db.Select("data").Where("customer_id = ?", customerID).First(&record).Error
if err != nil {
return nil, err
}
var data map[string]interface{}
if record.Data != nil {
err = json.Unmarshal([]byte(*record.Data), &data)
if err != nil {
return nil, err
}
}
return data, nil
}
func nilStringPtr(s string) *string {
if s == "" {
return nil
}
return &s
}
func nilTimePtr(t time.Time) *time.Time {
if t.IsZero() {
return nil
}
return &t
}
var (
ErrInvalidTokenValue = errors.New("invalid token value")
ErrTokenExpired = errors.New("token has expired")
)