feat: 实现积分与等级系统 + 签到功能 + 下拉菜单优化
- User 模型增加 exp 字段,等级规则 Lv0-Lv6 - 新增签到记录(UserCheckIn)与任务记录(UserTask)模型,唯一索引防重 - 自动签到:AuthMiddleware 验证会话后,基于 Asia/Shanghai 自然日自动签到 +5 经验 - 手动签到:下拉菜单"签到"按钮兜底,已签到显示"今日已签到"并禁用 - 首次任务经验:上传头像/设置用户名/设置个性签名各 +10,审核通过与直接更新均触发 - 经验值变动后自动比对等级,升级时通过通知系统发送"恭喜提升至 LvX" - 下拉菜单重构:顶部头像+用户名+LvX 等级标签,菜单项增加左侧图标,优化布局样式 - 新增 API:POST /api/level/checkin、GET /api/level/info
This commit is contained in:
@ -33,29 +33,36 @@ type siteSettingsChecker interface {
|
||||
IsAuditTypeEnabled(auditType string, defaultVal bool) bool
|
||||
}
|
||||
|
||||
// auditNotifier 审核服务所需的通知接口(ISP:审核通过/拒绝时发送通知)
|
||||
// auditNotifier AuditService 所需的通知接口(ISP:审核通过/拒绝时发送通知)
|
||||
type auditNotifier interface {
|
||||
NotifyAuditApproved(userID uint, auditType string, relatedID uint)
|
||||
NotifyAuditRejected(userID uint, auditType, reason string, relatedID uint)
|
||||
}
|
||||
|
||||
// auditTaskCompleter 审核通过时触发首次任务奖励的接口
|
||||
type auditTaskCompleter interface {
|
||||
CompleteTask(userID uint, taskType string) (newExp int, levelUp bool, err error)
|
||||
}
|
||||
|
||||
// AuditService 审核业务逻辑
|
||||
type AuditService struct {
|
||||
auditRepo auditRepo
|
||||
userRepo userProfileStore
|
||||
settings siteSettingsChecker
|
||||
defaultCfg config.AuditConfig
|
||||
notifier auditNotifier
|
||||
auditRepo auditRepo
|
||||
userRepo userProfileStore
|
||||
settings siteSettingsChecker
|
||||
defaultCfg config.AuditConfig
|
||||
notifier auditNotifier
|
||||
levelSvc auditTaskCompleter
|
||||
}
|
||||
|
||||
// NewAuditService 构造函数
|
||||
func NewAuditService(auditRepo auditRepo, userRepo userProfileStore, settings siteSettingsChecker, cfg config.AuditConfig, notifier auditNotifier) *AuditService {
|
||||
func NewAuditService(auditRepo auditRepo, userRepo userProfileStore, settings siteSettingsChecker, cfg config.AuditConfig, notifier auditNotifier, levelSvc auditTaskCompleter) *AuditService {
|
||||
return &AuditService{
|
||||
auditRepo: auditRepo,
|
||||
userRepo: userRepo,
|
||||
settings: settings,
|
||||
defaultCfg: cfg,
|
||||
notifier: notifier,
|
||||
levelSvc: levelSvc,
|
||||
}
|
||||
}
|
||||
|
||||
@ -220,7 +227,23 @@ func (s *AuditService) applyApproval(submission *model.AuditSubmission) error {
|
||||
case model.AuditTypeBio:
|
||||
user.Bio = submission.NewValue
|
||||
}
|
||||
return s.userRepo.Update(user)
|
||||
if err := s.userRepo.Update(user); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 触发首次任务奖励(静默失败不影响主流程)
|
||||
if s.levelSvc != nil {
|
||||
switch submission.AuditType {
|
||||
case model.AuditTypeUsername:
|
||||
_, _, _ = s.levelSvc.CompleteTask(submission.UserID, "username")
|
||||
case model.AuditTypeAvatar:
|
||||
_, _, _ = s.levelSvc.CompleteTask(submission.UserID, "avatar")
|
||||
case model.AuditTypeBio:
|
||||
_, _, _ = s.levelSvc.CompleteTask(submission.UserID, "bio")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// List 分页查询审核列表
|
||||
|
||||
149
internal/service/level_service.go
Normal file
149
internal/service/level_service.go
Normal file
@ -0,0 +1,149 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"metazone.cc/metalab/internal/model"
|
||||
)
|
||||
|
||||
// LevelService 积分与等级业务逻辑
|
||||
type LevelService struct {
|
||||
userRepo userLevelStore
|
||||
checkRepo checkInStore
|
||||
taskRepo taskStore
|
||||
notifSvc levelNotifier
|
||||
}
|
||||
|
||||
// NewLevelService 构造函数
|
||||
func NewLevelService(userRepo userLevelStore, checkRepo checkInStore, taskRepo taskStore, notifSvc levelNotifier) *LevelService {
|
||||
return &LevelService{userRepo: userRepo, checkRepo: checkRepo, taskRepo: taskRepo, notifSvc: notifSvc}
|
||||
}
|
||||
|
||||
// GetLevel 获取用户当前等级、经验值和今日签到状态
|
||||
func (s *LevelService) GetLevel(userID uint) (level int, exp int, checkedIn bool, err error) {
|
||||
user, err := s.userRepo.FindByID(userID)
|
||||
if err != nil {
|
||||
return 0, 0, false, err
|
||||
}
|
||||
|
||||
loc, _ := time.LoadLocation("Asia/Shanghai")
|
||||
now := time.Now().In(loc)
|
||||
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc)
|
||||
_, err = s.checkRepo.FindCheckIn(userID, today)
|
||||
checkedIn = err == nil
|
||||
|
||||
return model.GetLevelByExp(user.Exp), user.Exp, checkedIn, nil
|
||||
}
|
||||
|
||||
// AddExp 增加经验值,自动检查升级并发送通知
|
||||
// 返回:新经验值、是否升级、错误
|
||||
func (s *LevelService) AddExp(userID uint, delta int) (newExp int, levelUp bool, err error) {
|
||||
if delta <= 0 {
|
||||
user, err := s.userRepo.FindByID(userID)
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
return user.Exp, false, nil
|
||||
}
|
||||
|
||||
// 先查旧等级
|
||||
user, err := s.userRepo.FindByID(userID)
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
oldLevel := model.GetLevelByExp(user.Exp)
|
||||
|
||||
// 原子增加经验值
|
||||
if err := s.userRepo.AddExp(userID, delta); err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
|
||||
// 查新等级
|
||||
user, err = s.userRepo.FindByID(userID)
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
newLevel := model.GetLevelByExp(user.Exp)
|
||||
|
||||
if newLevel > oldLevel {
|
||||
levelUp = true
|
||||
if s.notifSvc != nil {
|
||||
_ = s.notifSvc.Create(userID, model.NotifyLevelUp,
|
||||
"等级提升",
|
||||
fmt.Sprintf("恭喜您!您的等级已提升至 %s", model.GetLevelName(newLevel)),
|
||||
nil)
|
||||
}
|
||||
}
|
||||
|
||||
return user.Exp, levelUp, nil
|
||||
}
|
||||
|
||||
// CheckIn 执行签到(基于 Asia/Shanghai 自然日严格防重)
|
||||
// 返回:是否成功签到、新经验值、是否升级、错误
|
||||
func (s *LevelService) CheckIn(userID uint) (checkedIn bool, newExp int, levelUp bool, err error) {
|
||||
loc, _ := time.LoadLocation("Asia/Shanghai")
|
||||
now := time.Now().In(loc)
|
||||
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc)
|
||||
|
||||
// 检查今日是否已签到
|
||||
_, err = s.checkRepo.FindCheckIn(userID, today)
|
||||
if err == nil {
|
||||
// 已签到
|
||||
user, _ := s.userRepo.FindByID(userID)
|
||||
if user != nil {
|
||||
return false, user.Exp, false, nil
|
||||
}
|
||||
return false, 0, false, nil
|
||||
}
|
||||
|
||||
// 创建签到记录
|
||||
ci := &model.UserCheckIn{
|
||||
UserID: userID,
|
||||
Date: today,
|
||||
}
|
||||
if err := s.checkRepo.CreateCheckIn(ci); err != nil {
|
||||
// 唯一索引冲突 → 已签到
|
||||
user, _ := s.userRepo.FindByID(userID)
|
||||
if user != nil {
|
||||
return false, user.Exp, false, nil
|
||||
}
|
||||
return false, 0, false, nil
|
||||
}
|
||||
|
||||
// 增加经验值
|
||||
newExp, levelUp, err = s.AddExp(userID, 5)
|
||||
return true, newExp, levelUp, err
|
||||
}
|
||||
|
||||
// CompleteTask 完成首次任务(严格防重)
|
||||
// 返回:新经验值、是否升级、错误
|
||||
func (s *LevelService) CompleteTask(userID uint, taskType string) (newExp int, levelUp bool, err error) {
|
||||
// 检查是否已完成
|
||||
_, err = s.taskRepo.FindTask(userID, taskType)
|
||||
if err == nil {
|
||||
// 已完成过,不重复奖励
|
||||
user, _ := s.userRepo.FindByID(userID)
|
||||
if user != nil {
|
||||
return user.Exp, false, nil
|
||||
}
|
||||
return 0, false, nil
|
||||
}
|
||||
|
||||
// 创建任务记录
|
||||
task := &model.UserTask{
|
||||
UserID: userID,
|
||||
TaskType: taskType,
|
||||
}
|
||||
if err := s.taskRepo.CreateTask(task); err != nil {
|
||||
// 唯一索引冲突 → 已完成
|
||||
user, _ := s.userRepo.FindByID(userID)
|
||||
if user != nil {
|
||||
return user.Exp, false, nil
|
||||
}
|
||||
return 0, false, nil
|
||||
}
|
||||
|
||||
// 增加经验值
|
||||
return s.AddExp(userID, 10)
|
||||
}
|
||||
@ -1,6 +1,10 @@
|
||||
package service
|
||||
|
||||
import "metazone.cc/metalab/internal/model"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"metazone.cc/metalab/internal/model"
|
||||
)
|
||||
|
||||
// userAuthStore AuthService 所需的最小仓储接口(ISP:7 个方法)
|
||||
type userAuthStore interface {
|
||||
@ -58,3 +62,26 @@ type postNotifier interface {
|
||||
NotifyPostApproved(userID uint, postTitle string, postID uint)
|
||||
NotifyPostRejected(userID uint, postTitle, reason string, postID uint)
|
||||
}
|
||||
|
||||
// userLevelStore LevelService 所需的用户仓储接口
|
||||
type userLevelStore interface {
|
||||
FindByID(id uint) (*model.User, error)
|
||||
AddExp(userID uint, delta int) error
|
||||
}
|
||||
|
||||
// checkInStore 签到记录仓储接口
|
||||
type checkInStore interface {
|
||||
FindCheckIn(userID uint, date time.Time) (*model.UserCheckIn, error)
|
||||
CreateCheckIn(ci *model.UserCheckIn) error
|
||||
}
|
||||
|
||||
// taskStore 任务完成记录仓储接口
|
||||
type taskStore interface {
|
||||
FindTask(userID uint, taskType string) (*model.UserTask, error)
|
||||
CreateTask(task *model.UserTask) error
|
||||
}
|
||||
|
||||
// levelNotifier 等级升级通知接口
|
||||
type levelNotifier interface {
|
||||
Create(userID uint, notifyType, title, content string, relatedID *uint) error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user