forked from songquanpeng/one-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: able to check topup history & consumption history (songquanpeng#78
- Loading branch information
1 parent
e0d0674
commit 45e9fd6
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package controller | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"one-api/common" | ||
"one-api/model" | ||
"strconv" | ||
) | ||
|
||
func GetAllLogs(c *gin.Context) { | ||
p, _ := strconv.Atoi(c.Query("p")) | ||
if p < 0 { | ||
p = 0 | ||
} | ||
logType, _ := strconv.Atoi(c.Query("type")) | ||
logs, err := model.GetAllLogs(logType, p*common.ItemsPerPage, common.ItemsPerPage) | ||
if err != nil { | ||
c.JSON(200, gin.H{ | ||
"success": false, | ||
"message": err.Error(), | ||
}) | ||
return | ||
} | ||
c.JSON(200, gin.H{ | ||
"success": true, | ||
"message": "", | ||
"data": logs, | ||
}) | ||
} | ||
|
||
func GetUserLogs(c *gin.Context) { | ||
p, _ := strconv.Atoi(c.Query("p")) | ||
if p < 0 { | ||
p = 0 | ||
} | ||
userId := c.GetInt("id") | ||
logType, _ := strconv.Atoi(c.Query("type")) | ||
logs, err := model.GetUserLogs(userId, logType, p*common.ItemsPerPage, common.ItemsPerPage) | ||
if err != nil { | ||
c.JSON(200, gin.H{ | ||
"success": false, | ||
"message": err.Error(), | ||
}) | ||
return | ||
} | ||
c.JSON(200, gin.H{ | ||
"success": true, | ||
"message": "", | ||
"data": logs, | ||
}) | ||
} | ||
|
||
func SearchAllLogs(c *gin.Context) { | ||
keyword := c.Query("keyword") | ||
logs, err := model.SearchAllLogs(keyword) | ||
if err != nil { | ||
c.JSON(200, gin.H{ | ||
"success": false, | ||
"message": err.Error(), | ||
}) | ||
return | ||
} | ||
c.JSON(200, gin.H{ | ||
"success": true, | ||
"message": "", | ||
"data": logs, | ||
}) | ||
} | ||
|
||
func SearchUserLogs(c *gin.Context) { | ||
keyword := c.Query("keyword") | ||
userId := c.GetInt("id") | ||
logs, err := model.SearchUserLogs(userId, keyword) | ||
if err != nil { | ||
c.JSON(200, gin.H{ | ||
"success": false, | ||
"message": err.Error(), | ||
}) | ||
return | ||
} | ||
c.JSON(200, gin.H{ | ||
"success": true, | ||
"message": "", | ||
"data": logs, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package model | ||
|
||
import "one-api/common" | ||
|
||
type Log struct { | ||
Id int `json:"id"` | ||
UserId int `json:"user_id" gorm:"index"` | ||
CreatedAt int64 `json:"created_at" gorm:"bigint"` | ||
Type int `json:"type" gorm:"index"` | ||
Content string `json:"content"` | ||
} | ||
|
||
func RecordLog(userId int, logType int, content string) { | ||
log := &Log{ | ||
UserId: userId, | ||
CreatedAt: common.GetTimestamp(), | ||
Type: logType, | ||
Content: content, | ||
} | ||
err := DB.Create(log).Error | ||
if err != nil { | ||
common.SysError("failed to record log: " + err.Error()) | ||
} | ||
} | ||
|
||
func GetAllLogs(logType int, startIdx int, num int) (logs []*Log, err error) { | ||
err = DB.Where("type = ?", logType).Order("id desc").Limit(num).Offset(startIdx).Find(&logs).Error | ||
return logs, err | ||
} | ||
|
||
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 | ||
return logs, err | ||
} | ||
|
||
func SearchAllLogs(keyword string) (logs []*Log, err error) { | ||
err = DB.Where("type = ? or content LIKE ?", keyword, keyword+"%").Order("id desc").Limit(common.MaxRecentItems).Find(&logs).Error | ||
return logs, err | ||
} | ||
|
||
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 | ||
return logs, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters