feat: now user can check its topup & consume history (close #78, close #95)

This commit is contained in:
JustSong
2023-06-10 16:04:04 +08:00
parent 145bb14cb2
commit 74f508e847
8 changed files with 240 additions and 3 deletions

View File

@@ -1,6 +1,9 @@
package model
import "one-api/common"
import (
"gorm.io/gorm"
"one-api/common"
)
type Log struct {
Id int `json:"id"`
@@ -10,6 +13,12 @@ type Log struct {
Content string `json:"content"`
}
const (
LogTypeUnknown = iota
LogTypeTopup
LogTypeConsume
)
func RecordLog(userId int, logType int, content string) {
log := &Log{
UserId: userId,
@@ -29,7 +38,13 @@ func GetAllLogs(logType int, startIdx int, num int) (logs []*Log, err error) {
}
func GetUserLogs(userId int, logType int, startIdx int, num int) (logs []*Log, err error) {
err = DB.Where("user_id = ? and type = ?", userId, logType).Order("id desc").Limit(num).Offset(startIdx).Find(&logs).Error
var tx *gorm.DB
if logType == LogTypeUnknown {
tx = DB.Where("user_id = ?", userId)
} else {
tx = DB.Where("user_id = ? and type = ?", userId, logType)
}
err = tx.Order("id desc").Limit(num).Offset(startIdx).Omit("id").Find(&logs).Error
return logs, err
}
@@ -39,6 +54,6 @@ func SearchAllLogs(keyword string) (logs []*Log, err error) {
}
func SearchUserLogs(userId int, keyword string) (logs []*Log, err error) {
err = DB.Where("user_id = ? and type = ?", userId, keyword).Order("id desc").Limit(common.MaxRecentItems).Find(&logs).Error
err = DB.Where("user_id = ? and type = ?", userId, keyword).Order("id desc").Limit(common.MaxRecentItems).Omit("id").Find(&logs).Error
return logs, err
}

View File

@@ -79,6 +79,10 @@ func InitDB() (err error) {
if err != nil {
return err
}
err = db.AutoMigrate(&Log{})
if err != nil {
return err
}
err = createRootAccountIfNeed()
return err
} else {

View File

@@ -2,6 +2,7 @@ package model
import (
"errors"
"fmt"
"one-api/common"
)
@@ -65,6 +66,7 @@ func Redeem(key string, userId int) (quota int, err error) {
if err != nil {
common.SysError("更新兑换码状态失败:" + err.Error())
}
RecordLog(userId, LogTypeTopup, fmt.Sprintf("通过兑换码充值 %d 点额度", redemption.Quota))
}()
return redemption.Quota, nil
}