feat(P1): Account 模型扩展 + 头像上传 + 个人简介 + Refresh Token 机制

This commit is contained in:
Sisyphus
2026-04-25 18:49:16 +08:00
parent 2e0c5dd632
commit 9b20df4bf3
6 changed files with 392 additions and 146 deletions

View File

@@ -5,6 +5,8 @@ import (
"errors"
"feedsystem_video_go/internal/auth"
"log"
"strconv"
"strings"
"time"
rediscache "feedsystem_video_go/internal/middleware/redis"
@@ -108,31 +110,40 @@ func (as *AccountService) FindByUsername(ctx context.Context, username string) (
}
}
func (as *AccountService) Login(ctx context.Context, username, password string) (string, error) {
func (as *AccountService) Login(ctx context.Context, username, password string) (string, string, error) {
account, err := as.FindByUsername(ctx, username)
if err != nil {
return "", err
return "", "", err
}
if err := bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(password)); err != nil {
return "", err
return "", "", err
}
// generate token
token, err := auth.GenerateToken(account.ID, account.Username)
accessToken, err := auth.GenerateToken(account.ID, account.Username)
if err != nil {
return "", err
return "", "", err
}
if err := as.accountRepository.Login(ctx, account.ID, token); err != nil {
return "", err
refreshToken, err := auth.GenerateRefreshToken(account.ID)
if err != nil {
return "", "", err
}
if err := as.accountRepository.Login(ctx, account.ID, accessToken, refreshToken); err != nil {
return "", "", err
}
if as.cache != nil {
cacheCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
defer cancel()
if err := as.cache.SetBytes(cacheCtx, as.cache.Key("account:%d", account.ID), []byte(token), 24*time.Hour); err != nil {
if err := as.cache.SetBytes(cacheCtx, as.cache.Key("account:%d", account.ID), []byte(accessToken), 24*time.Hour); err != nil {
log.Printf("failed to set cache: %v", err)
}
if err := as.cache.SetBytes(cacheCtx, as.cache.Key("account:%d:refresh", account.ID), []byte(refreshToken), 7*24*time.Hour); err != nil {
log.Printf("failed to set refresh cache: %v", err)
}
if err := as.cache.SetBytes(cacheCtx, as.cache.Key("refresh:%s", refreshToken), []byte(strconv.FormatUint(uint64(account.ID), 10)), 7*24*time.Hour); err != nil {
log.Printf("failed to set refresh lookup: %v", err)
}
}
return token, nil
return accessToken, refreshToken, nil
}
func (as *AccountService) Logout(ctx context.Context, accountID uint) error {
@@ -150,6 +161,76 @@ func (as *AccountService) Logout(ctx context.Context, accountID uint) error {
if err := as.cache.Del(cacheCtx, as.cache.Key("account:%d", account.ID)); err != nil {
log.Printf("failed to del cache: %v", err)
}
if err := as.cache.Del(cacheCtx, as.cache.Key("account:%d:refresh", account.ID)); err != nil {
log.Printf("failed to del refresh cache: %v", err)
}
if account.RefreshToken != "" {
as.cache.Del(cacheCtx, as.cache.Key("refresh:%s", account.RefreshToken))
}
}
return as.accountRepository.Logout(ctx, account.ID)
}
func (as *AccountService) UpdateAvatar(ctx context.Context, accountID uint, avatarURL string) error {
return as.accountRepository.UpdateAvatar(ctx, accountID, avatarURL)
}
func (as *AccountService) FindAll(ctx context.Context) ([]*Account, error) {
return as.accountRepository.FindAll(ctx)
}
func (as *AccountService) UpdateProfile(ctx context.Context, accountID uint, req *UpdateProfileRequest) error {
updates := map[string]interface{}{}
if req.Bio != "" {
updates["bio"] = strings.TrimSpace(req.Bio)
}
if req.AvatarURL != "" {
updates["avatar_url"] = strings.TrimSpace(req.AvatarURL)
}
if len(updates) == 0 {
return errors.New("nothing to update")
}
return as.accountRepository.UpdateFields(ctx, accountID, updates)
}
func (as *AccountService) RefreshAccessToken(ctx context.Context, refreshToken string) (string, uint, string, error) {
if refreshToken == "" {
return "", 0, "", errors.New("refresh token is empty")
}
if as.cache != nil {
cacheCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
defer cancel()
b, err := as.cache.GetBytes(cacheCtx, as.cache.Key("refresh:%s", refreshToken))
if err == nil {
idStr := string(b)
id, parseErr := strconv.ParseUint(idStr, 10, 64)
if parseErr == nil {
account, err := as.FindByID(ctx, uint(id))
if err == nil && account != nil && account.RefreshToken == refreshToken {
newToken, err := auth.GenerateToken(account.ID, account.Username)
if err != nil {
return "", 0, "", err
}
as.accountRepository.UpdateToken(ctx, account.ID, newToken)
as.cache.SetBytes(cacheCtx, as.cache.Key("account:%d", account.ID), []byte(newToken), 24*time.Hour)
return newToken, account.ID, account.Username, nil
}
}
}
}
accounts, err := as.FindAll(ctx)
if err != nil {
return "", 0, "", err
}
for _, acc := range accounts {
if acc.RefreshToken == refreshToken {
newToken, err := auth.GenerateToken(acc.ID, acc.Username)
if err != nil {
return "", 0, "", err
}
as.accountRepository.UpdateToken(ctx, acc.ID, newToken)
return newToken, acc.ID, acc.Username, nil
}
}
return "", 0, "", errors.New("invalid refresh token")
}