Skip to content

Commit

Permalink
chore: add more log types
Browse files Browse the repository at this point in the history
  • Loading branch information
songquanpeng committed Jun 10, 2023
1 parent 74f508e commit d29c273
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"encoding/json"
"fmt"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"net/http"
Expand Down Expand Up @@ -351,6 +352,9 @@ func UpdateUser(c *gin.Context) {
})
return
}
if originUser.Quota != updatedUser.Quota {
model.RecordLog(originUser.Id, model.LogTypeManage, fmt.Sprintf("管理员将用户额度从 %d 点修改为 %d 点", originUser.Quota, updatedUser.Quota))
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
Expand Down
2 changes: 2 additions & 0 deletions model/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const (
LogTypeUnknown = iota
LogTypeTopup
LogTypeConsume
LogTypeManage
LogTypeSystem
)

func RecordLog(userId int, logType int, content string) {
Expand Down
11 changes: 9 additions & 2 deletions model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"errors"
"fmt"
"gorm.io/gorm"
"one-api/common"
"strings"
Expand Down Expand Up @@ -73,8 +74,14 @@ func (user *User) Insert() error {
}
user.Quota = common.QuotaForNewUser
user.AccessToken = common.GetUUID()
err = DB.Create(user).Error
return err
result := DB.Create(user)
if result.Error != nil {
return result.Error
}
if common.QuotaForNewUser > 0 {
RecordLog(user.Id, LogTypeSystem, fmt.Sprintf("新用户注册赠送 %d 点额度", common.QuotaForNewUser))
}
return nil
}

func (user *User) Update(updatePassword bool) error {
Expand Down
4 changes: 4 additions & 0 deletions web/src/components/LogsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ function renderType(type) {
return <Label basic color='green'> 充值 </Label>;
case 2:
return <Label basic color='olive'> 消费 </Label>;
case 3:
return <Label basic color='orange'> 管理 </Label>;
case 4:
return <Label basic color='red'> 系统 </Label>;
default:
return <Label basic color='black'> 未知 </Label>;
}
Expand Down

0 comments on commit d29c273

Please sign in to comment.