71 lines
2.1 KiB
Go
71 lines
2.1 KiB
Go
package models
|
||
|
||
import (
|
||
"encoding/json"
|
||
"time"
|
||
)
|
||
|
||
// SocialAccount 社交账号绑定模型
|
||
type SocialAccount struct {
|
||
ID uint64 `json:"id" db:"id"`
|
||
UserID uint64 `json:"user_id" db:"user_id"`
|
||
Provider string `json:"provider" db:"provider"` // wechat, qq, weibo, google, facebook, twitter
|
||
ProviderUserID string `json:"provider_user_id" db:"provider_user_id"`
|
||
ProviderUsername string `json:"provider_username" db:"provider_username"`
|
||
AccessToken string `json:"-" db:"access_token"` // 不返回给前端
|
||
RefreshToken string `json:"-" db:"refresh_token"`
|
||
ExpiresAt *time.Time `json:"expires_at" db:"expires_at"`
|
||
RawData JSON `json:"-" db:"raw_data"`
|
||
IsPrimary bool `json:"is_primary" db:"is_primary"`
|
||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||
}
|
||
|
||
// SocialAccountInfo 返回给前端的社交账号信息(不含敏感信息)
|
||
type SocialAccountInfo struct {
|
||
ID uint64 `json:"id"`
|
||
Provider string `json:"provider"`
|
||
ProviderUserID string `json:"provider_user_id"`
|
||
ProviderUsername string `json:"provider_username"`
|
||
IsPrimary bool `json:"is_primary"`
|
||
CreatedAt time.Time `json:"created_at"`
|
||
}
|
||
|
||
// ToInfo 转换为安全信息
|
||
func (sa *SocialAccount) ToInfo() *SocialAccountInfo {
|
||
return &SocialAccountInfo{
|
||
ID: sa.ID,
|
||
Provider: sa.Provider,
|
||
ProviderUserID: sa.ProviderUserID,
|
||
ProviderUsername: sa.ProviderUsername,
|
||
IsPrimary: sa.IsPrimary,
|
||
CreatedAt: sa.CreatedAt,
|
||
}
|
||
}
|
||
|
||
// JSON 自定义JSON类型,用于存储RawData
|
||
type JSON struct {
|
||
Data interface{}
|
||
}
|
||
|
||
// Scan 实现 sql.Scanner 接口
|
||
func (j *JSON) Scan(value interface{}) error {
|
||
if value == nil {
|
||
j.Data = nil
|
||
return nil
|
||
}
|
||
bytes, ok := value.([]byte)
|
||
if !ok {
|
||
return nil
|
||
}
|
||
return json.Unmarshal(bytes, &j.Data)
|
||
}
|
||
|
||
// Value 实现 driver.Valuer 接口
|
||
func (j JSON) Value() (interface{}, error) {
|
||
if j.Data == nil {
|
||
return nil, nil
|
||
}
|
||
return json.Marshal(j.Data)
|
||
}
|