This commit is contained in:
Sakurasan
2023-06-10 19:40:47 +08:00
parent bae47a43ad
commit bc39ce1ddd
2 changed files with 28 additions and 2 deletions

View File

@@ -178,7 +178,8 @@ func Handleinit(c *gin.Context) {
} }
func HandleMe(c *gin.Context) { func HandleMe(c *gin.Context) {
u, err := store.GetUserByID(1) token := c.GetHeader("Authorization")
u, err := store.GetUserByToken(token[7:])
if err != nil { if err != nil {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"error": err.Error(), "error": err.Error(),
@@ -190,7 +191,7 @@ func HandleMe(c *gin.Context) {
int(u.ID), int(u.ID),
u.UpdatedAt.Format(time.RFC3339), u.UpdatedAt.Format(time.RFC3339),
u.Name, u.Name,
u.Token, u.Token[:15] + "*****" + u.Token[len(u.Token)-15:],
u.CreatedAt.Format(time.RFC3339), u.CreatedAt.Format(time.RFC3339),
} }
c.JSON(http.StatusOK, resJSON) c.JSON(http.StatusOK, resJSON)
@@ -601,6 +602,22 @@ func Cost(model string, promptCount, completionCount int) float64 {
func HandleUsage(c *gin.Context) { func HandleUsage(c *gin.Context) {
fromStr := c.Query("from") fromStr := c.Query("from")
toStr := c.Query("to") toStr := c.Query("to")
getMonthStartAndEnd := func() (start, end string) {
loc, _ := time.LoadLocation("Local")
now := time.Now().In(loc)
year, month, _ := now.Date()
startOfMonth := time.Date(year, month, 1, 0, 0, 0, 0, loc)
endOfMonth := startOfMonth.AddDate(0, 1, 0)
start = startOfMonth.Format("2006-01-02")
end = endOfMonth.Format("2006-01-02")
return
}
if fromStr == "" || toStr == "" {
fromStr, toStr = getMonthStartAndEnd()
}
usage, err := store.QueryUsage(fromStr, toStr) usage, err := store.QueryUsage(fromStr, toStr)
if err != nil { if err != nil {

View File

@@ -73,6 +73,15 @@ func GetUserByName(name string) (*User, error) {
return &user, nil return &user, nil
} }
func GetUserByToken(token string) (*User, error) {
var user User
result := db.Where("token = ?", token).First(&user)
if result.Error != nil {
return nil, result.Error
}
return &user, nil
}
func GetUserID(authkey string) (int, error) { func GetUserID(authkey string) (int, error) {
var user User var user User
result := db.Where(&User{Token: authkey}).First(&user) result := db.Where(&User{Token: authkey}).First(&user)