2024-04-07 16:03:37 +00:00
|
|
|
package model
|
|
|
|
|
2024-04-08 10:13:01 +00:00
|
|
|
import (
|
2024-04-11 06:57:47 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2024-04-11 09:21:44 +00:00
|
|
|
"fmt"
|
2024-04-11 06:57:47 +00:00
|
|
|
"time"
|
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/log"
|
2024-04-10 06:58:19 +00:00
|
|
|
"gorm.io/gorm"
|
2024-04-08 10:13:01 +00:00
|
|
|
)
|
|
|
|
|
2024-04-07 16:03:37 +00:00
|
|
|
type KillaraCustomerTokenModel struct {
|
|
|
|
// fields ...
|
2024-04-10 06:58:19 +00:00
|
|
|
db *gorm.DB
|
2024-04-08 10:13:01 +00:00
|
|
|
TableName string // 表名
|
2024-04-07 16:03:37 +00:00
|
|
|
}
|
2024-04-11 06:57:47 +00:00
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
// 旧的php逻辑 不是一致操作, 存在bug的可能性.
|
|
|
|
// func (m *KillaraCustomerTokenModel) InsertToken(data *KillaraCustomerToken) error {
|
|
|
|
// // 删除重复的 token
|
2024-04-11 06:57:47 +00:00
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
// m.DeleteToken(*data.Token)
|
2024-04-11 06:57:47 +00:00
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
// // 删除相同客户ID和平台的旧 token
|
|
|
|
// if *data.CustomerId != 0 && *data.Platform != 0 {
|
|
|
|
// m.db.Where("customer_id = ? AND platform = ?", *data.CustomerId, *data.Platform).Delete(&KillaraCustomerToken{})
|
2024-04-11 06:57:47 +00:00
|
|
|
// }
|
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
// return m.db.Create(data).Error
|
2024-04-11 06:57:47 +00:00
|
|
|
// }
|
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
func (m *KillaraCustomerTokenModel) InsertToken(data *KillaraCustomerToken) error {
|
|
|
|
return m.db.Model(&KillaraCustomerToken{}).Transaction(func(tx *gorm.DB) error {
|
|
|
|
// 查找是否存在相同客户ID、平台和Token的记录
|
|
|
|
var existingToken KillaraCustomerToken
|
2024-04-15 09:01:42 +00:00
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
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) {
|
|
|
|
tx.Rollback() // 发生错误,回滚事务
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
// 存在记录,先删除
|
|
|
|
err = tx.Delete(&existingToken).Error
|
2024-04-15 09:01:42 +00:00
|
|
|
if err != nil {
|
2024-04-11 09:21:44 +00:00
|
|
|
tx.Rollback() // 删除错误,回滚事务
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 插入新记录
|
|
|
|
err = tx.Create(data).Error
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback() // 插入错误,回滚事务
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-15 09:01:42 +00:00
|
|
|
return err // 提交事务
|
2024-04-11 09:21:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-11 06:57:47 +00:00
|
|
|
func (m *KillaraCustomerTokenModel) UpdateTokenData(token string, data map[string]interface{}) error {
|
2024-04-11 09:21:44 +00:00
|
|
|
dataJSON, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return m.db.Model(&KillaraCustomerToken{}).Where("token = ?", token).Update("data", string(dataJSON)).Error
|
2024-04-11 06:57:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *KillaraCustomerTokenModel) UpdateTokenExpiry(token string, expiry int64) error {
|
2024-04-11 09:21:44 +00:00
|
|
|
return m.db.Model(&KillaraCustomerToken{}).Where("token = ?", token).Update("expiry", expiry).Error
|
2024-04-11 06:57:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *KillaraCustomerTokenModel) UpdateTokenCustomerID(token string, customerID uint64) error {
|
2024-04-11 09:21:44 +00:00
|
|
|
return m.db.Model(&KillaraCustomerToken{}).Where("token = ?", token).Update("customer_id", customerID).Error
|
2024-04-11 06:57:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *KillaraCustomerTokenModel) DeleteToken(token string) error {
|
|
|
|
err := m.db.Where("token = ?", token).Delete(&KillaraCustomerToken{}).Error
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-16 10:00:05 +00:00
|
|
|
func (m *KillaraCustomerTokenModel) GetToken(tx *gorm.DB, token string) (*KillaraCustomerToken, error) {
|
|
|
|
var db *gorm.DB
|
|
|
|
if tx != nil {
|
|
|
|
db = tx
|
|
|
|
} else {
|
|
|
|
db = m.db
|
|
|
|
}
|
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
var tokenData KillaraCustomerToken
|
2024-04-16 10:00:05 +00:00
|
|
|
err := db.Model(&tokenData).Where("token = ?", token).First(&tokenData).Error
|
2024-04-11 06:57:47 +00:00
|
|
|
if err == gorm.ErrRecordNotFound {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2024-04-11 09:21:44 +00:00
|
|
|
return &tokenData, err
|
2024-04-11 06:57:47 +00:00
|
|
|
}
|
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
// PHP 旧的代码
|
2024-04-11 06:57:47 +00:00
|
|
|
// func (m *KillaraCustomerTokenModel) CheckToken(token string) (*KillaraCustomerToken, error) {
|
2024-04-11 09:21:44 +00:00
|
|
|
// tokenData, err := m.GetToken(token)
|
2024-04-11 06:57:47 +00:00
|
|
|
// if err != nil {
|
2024-04-11 09:21:44 +00:00
|
|
|
// if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
// return nil, nil
|
|
|
|
// }
|
2024-04-11 06:57:47 +00:00
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
// if *tokenData.Expiry > time.Now().Unix() {
|
|
|
|
// return tokenData, nil
|
2024-04-11 06:57:47 +00:00
|
|
|
// }
|
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
// // 过期则删除 token
|
|
|
|
// m.DeleteToken(token)
|
|
|
|
// return nil, nil
|
2024-04-11 06:57:47 +00:00
|
|
|
// }
|
|
|
|
|
2024-04-15 09:01:42 +00:00
|
|
|
func (m *KillaraCustomerTokenModel) CheckToken(tokenstr string) (*KillaraCustomerToken, error) {
|
2024-04-11 09:21:44 +00:00
|
|
|
var resultToken *KillaraCustomerToken
|
|
|
|
err := m.db.Model(&KillaraCustomerToken{}).Transaction(func(tx *gorm.DB) error {
|
|
|
|
// 查找 Token 记录
|
|
|
|
var token KillaraCustomerToken
|
2024-04-15 09:01:42 +00:00
|
|
|
err := tx.Where("token = ?", tokenstr).First(&token).Error
|
2024-04-11 09:21:44 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
tx.Rollback() // 发生错误,回滚事务
|
|
|
|
return err
|
2024-04-11 06:57:47 +00:00
|
|
|
}
|
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
// 判断 Token 是否过期
|
|
|
|
if *token.Expiry <= time.Now().Unix() {
|
|
|
|
// 过期则删除 Token
|
|
|
|
err = tx.Delete(&token).Error
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback() // 删除错误,回滚事务
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-04-11 06:57:47 +00:00
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
resultToken = &token
|
2024-04-15 09:01:42 +00:00
|
|
|
return err // 提交事务
|
2024-04-11 09:21:44 +00:00
|
|
|
})
|
2024-04-11 06:57:47 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-11 09:21:44 +00:00
|
|
|
return resultToken, nil
|
|
|
|
}
|
2024-04-11 06:57:47 +00:00
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
func (m *KillaraCustomerTokenModel) ClearDuplicateToken(customerID uint64, currentToken string, platform int) error {
|
|
|
|
|
|
|
|
return m.db.Model(&KillaraCustomerToken{}).Transaction(func(tx *gorm.DB) error {
|
2024-04-15 09:01:42 +00:00
|
|
|
var tokens []*KillaraCustomerToken
|
2024-04-11 09:21:44 +00:00
|
|
|
err := tx.Where("customer_id = ? AND platform = ?", customerID, platform).Find(&tokens).Error
|
2024-04-11 06:57:47 +00:00
|
|
|
if err != nil {
|
2024-04-11 09:21:44 +00:00
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
log.Println("当前的token, 自身都不存在, 疑似出了什么错误1")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
tx.Rollback() // 发生错误,回滚事务
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, token := range tokens {
|
|
|
|
if *token.Token != currentToken {
|
|
|
|
// err := tx.Delete(token).Error todo: 不太明白php为什么不删除token, 难道用来链路跟踪? 但是客户端id也被更新了, 没有存在的意义了
|
2024-04-15 09:01:42 +00:00
|
|
|
err := tx.Where("token = ?", *token.Token).Update("customer_id", 0).Error
|
2024-04-11 09:21:44 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return fmt.Errorf("tokens 查询出来, 自身都不存在, 疑似出了什么错误2")
|
|
|
|
}
|
|
|
|
tx.Rollback() // 发生错误,回滚事务
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-04-11 06:57:47 +00:00
|
|
|
}
|
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
return tx.Commit().Error
|
|
|
|
})
|
2024-04-11 06:57:47 +00:00
|
|
|
}
|
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
func (m *KillaraCustomerTokenModel) GetTokenByCustomerID(customerID uint64) (*string, error) {
|
|
|
|
var token KillaraCustomerToken
|
|
|
|
err := m.db.Select("token").Where("customer_id = ?", customerID).First(&token).Error
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil, nil
|
2024-04-11 06:57:47 +00:00
|
|
|
}
|
2024-04-11 09:21:44 +00:00
|
|
|
return token.Token, err
|
2024-04-11 06:57:47 +00:00
|
|
|
}
|
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
func (m *KillaraCustomerTokenModel) GetTokenDataByCustomerID(customerID uint64) (map[string]interface{}, error) {
|
|
|
|
var token KillaraCustomerToken
|
|
|
|
err := m.db.Select("data").Where("customer_id = ?", customerID).First(&token).Error
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
2024-04-11 06:57:47 +00:00
|
|
|
}
|
|
|
|
|
2024-04-11 09:21:44 +00:00
|
|
|
var data map[string]interface{}
|
|
|
|
err = json.Unmarshal([]byte(*token.Data), &data)
|
|
|
|
return data, err
|
|
|
|
}
|