Documentation
¶
Index ¶
- Constants
- func Callback(c *gin.Context)
- func GetLoginURL(c *gin.Context)
- func GetUserIDFromContext(c *gin.Context) uint64
- func GetUserIDFromSession(s sessions.Session) uint64
- func HandleUpdateAllBadges(ctx context.Context, t *asynq.Task) error
- func HandleUpdateSingleUserBadgeScore(ctx context.Context, t *asynq.Task) error
- func HandleUpdateUserBadgeScores(ctx context.Context, t *asynq.Task) error
- func LoginRequired() gin.HandlerFunc
- func Logout(c *gin.Context)
- func SetUserToContext(c *gin.Context, user *User)
- func UserInfo(c *gin.Context)
- type Badge
- type BasicUserInfo
- type CallbackRequest
- type CallbackResponse
- type GetLoginURLResponse
- type LogoutResponse
- type OAuthUserInfo
- type TrustLevel
- type User
- func (u *User) CalculateUserScore(badges []Badge, badgeScores map[int]int) int
- func (u *User) CheckActive() error
- func (u *User) EnqueueBadgeScoreTask(ctx context.Context)
- func (u *User) Exact(tx *gorm.DB, id uint64) error
- func (u *User) GetUserBadges(ctx context.Context) (*UserBadgeResponse, error)
- func (u *User) RiskLevel() int8
- func (u *User) SetScore(tx *gorm.DB, newScore int) error
- func (u *User) UpdateFromOAuthInfo(oauthInfo *OAuthUserInfo)
- func (u *User) UpdateUserScore(ctx context.Context, newScore int) error
- type UserBadgeResponse
- type UserInfoResponse
Constants ¶
const ( UserNameKey = "username" UserIDKey = "user_id" UserObjKey = "user_obj" )
const ( OAuthStateCacheKeyFormat = "oauth:state:%s" OAuthStateCacheKeyExpiration = 10 * time.Minute UserAllBadges = "user:badges" )
const ( BaseUserScore = 100 MaxUserScore = 100 MinUserScore = -100 )
const ( UnAuthorized = "未登录" InvalidState = "非法登录请求" BannedAccount = "账号已被封禁" )
Variables ¶
This section is empty.
Functions ¶
func Callback ¶
Callback godoc @Tags oauth @Param request body CallbackRequest true "request body" @Produce json @Success 200 {object} CallbackResponse @Router /api/v1/oauth/callback [post]
func GetLoginURL ¶
GetLoginURL godoc @Tags oauth @Produce json @Success 200 {object} GetLoginURLResponse @Router /api/v1/oauth/login [get]
func GetUserIDFromContext ¶
func GetUserIDFromSession ¶
func HandleUpdateAllBadges ¶
HandleUpdateAllBadges 处理更新所有徽章
func HandleUpdateSingleUserBadgeScore ¶
HandleUpdateSingleUserBadgeScore 处理单个用户徽章分数更新任务
func HandleUpdateUserBadgeScores ¶
HandleUpdateUserBadgeScores 处理所有用户徽章分数更新任务
func LoginRequired ¶
func LoginRequired() gin.HandlerFunc
func Logout ¶
Logout godoc @Tags oauth @Produce json @Success 200 {object} LogoutResponse @Router /api/v1/oauth/logout [get]
func SetUserToContext ¶
SetUserToContext 将User对象存储到Context中
Types ¶
type Badge ¶
type Badge struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Score int `json:"score"`
}
Badge 徽章信息
type BasicUserInfo ¶
type CallbackRequest ¶
type CallbackResponse ¶
type CallbackResponse struct {
ErrorMsg string `json:"error_msg"`
Data interface{} `json:"data"`
}
type GetLoginURLResponse ¶
type LogoutResponse ¶
type LogoutResponse struct {
ErrorMsg string `json:"error_msg"`
Data interface{} `json:"data"`
}
type OAuthUserInfo ¶
type TrustLevel ¶
type TrustLevel int8
const ( TrustLevelNewUser TrustLevel = iota TrustLevelBasicUser TrustLevelUser TrustLevelActiveUser TrustLevelLeader )
type User ¶
type User struct {
ID uint64 `json:"id" gorm:"primaryKey"`
Username string `json:"username" gorm:"size:255;unique"`
Nickname string `json:"nickname" gorm:"size:255"`
AvatarUrl string `json:"avatar_url" gorm:"size:255"`
IsActive bool `json:"is_active" gorm:"default:true"`
TrustLevel TrustLevel `json:"trust_level"`
Score int8 `json:"score"`
ViolationCount uint8 `json:"violation_count" gorm:"default:0"`
IsAdmin bool `json:"is_admin" gorm:"default:false"`
LastLoginAt time.Time `json:"last_login_at" gorm:"index"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;index"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime;index"`
}
func GetUserFromContext ¶
GetUserFromContext 从Context中获取User对象
func (*User) CalculateUserScore ¶
func (*User) EnqueueBadgeScoreTask ¶
EnqueueBadgeScoreTask 为用户下发徽章分数计算任务
func (*User) GetUserBadges ¶
func (u *User) GetUserBadges(ctx context.Context) (*UserBadgeResponse, error)
func (*User) UpdateFromOAuthInfo ¶
func (u *User) UpdateFromOAuthInfo(oauthInfo *OAuthUserInfo)
UpdateFromOAuthInfo 根据 OAuth 信息更新用户数据
type UserBadgeResponse ¶
type UserBadgeResponse struct {
Badges []Badge `json:"badges"`
}
UserBadgeResponse API响应
type UserInfoResponse ¶
type UserInfoResponse struct {
ErrorMsg string `json:"error_msg"`
Data BasicUserInfo `json:"data"`
}