2023-04-23 11:31:00 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"one-api/common"
|
|
|
|
|
"one-api/model"
|
|
|
|
|
"strconv"
|
2025-07-14 21:59:42 +08:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-04-23 11:31:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func GetAllTokens(c *gin.Context) {
|
|
|
|
|
userId := c.GetInt("id")
|
2025-07-14 21:59:42 +08:00
|
|
|
pageInfo := common.GetPageQuery(c)
|
|
|
|
|
tokens, err := model.GetAllUserTokens(userId, pageInfo.GetStartIdx(), pageInfo.GetPageSize())
|
2023-04-23 11:31:00 +08:00
|
|
|
if err != nil {
|
2025-07-14 21:59:42 +08:00
|
|
|
common.ApiError(c, err)
|
2023-04-23 11:31:00 +08:00
|
|
|
return
|
|
|
|
|
}
|
🚀 feat(pagination): unify backend-driven pagination & improve channel tag aggregation
SUMMARY
• Migrated Token, Task, Midjourney, Channel, Redemption tables to true server-side pagination.
• Added total / page / page_size metadata in API responses; switched all affected React tables to consume new structure.
• Implemented counting helpers:
– model/token.go CountUserTokens
– model/task.go TaskCountAllTasks / TaskCountAllUserTask
– model/midjourney.go CountAllTasks / CountAllUserTask
– model/channel.go CountAllChannels / CountAllTags
• Refactored controllers (token, task, midjourney, channel) for 1-based paging & aggregated returns.
• Redesigned `ChannelsTable.js`:
– `loadChannels`, `syncPageData`, `enrichChannels` for tag-mode grouping without recursion.
– Fixed runtime white-screen (maximum call-stack) by removing child duplication.
– Pagination, search, tag-mode, idSort all hot-reload correctly.
• Removed unused `log` import in controller/midjourney.go.
BREAKING CHANGES
Front-end consumers must now expect data.items / total / page / page_size from list endpoints (`/api/channel`, `/api/task`, `/api/mj`, `/api/token`, etc.).
2025-06-12 17:25:25 +08:00
|
|
|
total, _ := model.CountUserTokens(userId)
|
2025-07-14 21:59:42 +08:00
|
|
|
pageInfo.SetTotal(int(total))
|
|
|
|
|
pageInfo.SetItems(tokens)
|
|
|
|
|
common.ApiSuccess(c, pageInfo)
|
2023-04-23 11:31:00 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SearchTokens(c *gin.Context) {
|
|
|
|
|
userId := c.GetInt("id")
|
|
|
|
|
keyword := c.Query("keyword")
|
2023-11-10 00:10:41 +08:00
|
|
|
token := c.Query("token")
|
|
|
|
|
tokens, err := model.SearchUserTokens(userId, keyword, token)
|
2023-04-23 11:31:00 +08:00
|
|
|
if err != nil {
|
2025-07-14 21:59:42 +08:00
|
|
|
common.ApiError(c, err)
|
2023-04-23 11:31:00 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": true,
|
|
|
|
|
"message": "",
|
|
|
|
|
"data": tokens,
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetToken(c *gin.Context) {
|
|
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
2023-04-23 12:43:10 +08:00
|
|
|
userId := c.GetInt("id")
|
2023-04-23 11:31:00 +08:00
|
|
|
if err != nil {
|
2025-07-14 21:59:42 +08:00
|
|
|
common.ApiError(c, err)
|
2023-04-23 11:31:00 +08:00
|
|
|
return
|
|
|
|
|
}
|
2023-04-23 12:43:10 +08:00
|
|
|
token, err := model.GetTokenByIds(id, userId)
|
2023-04-23 11:31:00 +08:00
|
|
|
if err != nil {
|
2025-07-14 21:59:42 +08:00
|
|
|
common.ApiError(c, err)
|
2023-04-23 11:31:00 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": true,
|
|
|
|
|
"message": "",
|
|
|
|
|
"data": token,
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 09:28:41 +08:00
|
|
|
func GetTokenStatus(c *gin.Context) {
|
|
|
|
|
tokenId := c.GetInt("token_id")
|
|
|
|
|
userId := c.GetInt("id")
|
|
|
|
|
token, err := model.GetTokenByIds(tokenId, userId)
|
|
|
|
|
if err != nil {
|
2025-07-14 21:59:42 +08:00
|
|
|
common.ApiError(c, err)
|
2023-05-10 09:28:41 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
expiredAt := token.ExpiredTime
|
|
|
|
|
if expiredAt == -1 {
|
|
|
|
|
expiredAt = 0
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"object": "credit_summary",
|
|
|
|
|
"total_granted": token.RemainQuota,
|
|
|
|
|
"total_used": 0, // not supported currently
|
|
|
|
|
"total_available": token.RemainQuota,
|
|
|
|
|
"expires_at": expiredAt * 1000,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-23 11:31:00 +08:00
|
|
|
func AddToken(c *gin.Context) {
|
|
|
|
|
token := model.Token{}
|
|
|
|
|
err := c.ShouldBindJSON(&token)
|
|
|
|
|
if err != nil {
|
2025-07-14 21:59:42 +08:00
|
|
|
common.ApiError(c, err)
|
2023-04-23 11:31:00 +08:00
|
|
|
return
|
|
|
|
|
}
|
2023-08-12 16:58:29 +08:00
|
|
|
if len(token.Name) > 30 {
|
2023-04-23 12:43:10 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": false,
|
2023-08-12 10:49:30 +08:00
|
|
|
"message": "令牌名称过长",
|
2023-04-23 12:43:10 +08:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-09-24 20:19:18 +08:00
|
|
|
key, err := common.GenerateKey()
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": false,
|
|
|
|
|
"message": "生成令牌失败",
|
|
|
|
|
})
|
|
|
|
|
common.SysError("failed to generate token key: " + err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-23 12:43:10 +08:00
|
|
|
cleanToken := model.Token{
|
2024-01-11 19:18:48 +08:00
|
|
|
UserId: c.GetInt("id"),
|
|
|
|
|
Name: token.Name,
|
2024-09-24 20:19:18 +08:00
|
|
|
Key: key,
|
2024-01-11 19:18:48 +08:00
|
|
|
CreatedTime: common.GetTimestamp(),
|
|
|
|
|
AccessedTime: common.GetTimestamp(),
|
|
|
|
|
ExpiredTime: token.ExpiredTime,
|
|
|
|
|
RemainQuota: token.RemainQuota,
|
|
|
|
|
UnlimitedQuota: token.UnlimitedQuota,
|
|
|
|
|
ModelLimitsEnabled: token.ModelLimitsEnabled,
|
|
|
|
|
ModelLimits: token.ModelLimits,
|
2024-09-17 20:49:51 +08:00
|
|
|
AllowIps: token.AllowIps,
|
2024-09-18 05:19:10 +08:00
|
|
|
Group: token.Group,
|
2023-04-23 12:43:10 +08:00
|
|
|
}
|
|
|
|
|
err = cleanToken.Insert()
|
2023-04-23 11:31:00 +08:00
|
|
|
if err != nil {
|
2025-07-14 21:59:42 +08:00
|
|
|
common.ApiError(c, err)
|
2023-04-23 11:31:00 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": true,
|
|
|
|
|
"message": "",
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DeleteToken(c *gin.Context) {
|
|
|
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
2023-04-23 12:43:10 +08:00
|
|
|
userId := c.GetInt("id")
|
|
|
|
|
err := model.DeleteTokenById(id, userId)
|
2023-04-23 11:31:00 +08:00
|
|
|
if err != nil {
|
2025-07-14 21:59:42 +08:00
|
|
|
common.ApiError(c, err)
|
2023-04-23 11:31:00 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": true,
|
|
|
|
|
"message": "",
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func UpdateToken(c *gin.Context) {
|
2023-04-23 12:43:10 +08:00
|
|
|
userId := c.GetInt("id")
|
2023-04-26 10:45:34 +08:00
|
|
|
statusOnly := c.Query("status_only")
|
2023-04-23 11:31:00 +08:00
|
|
|
token := model.Token{}
|
|
|
|
|
err := c.ShouldBindJSON(&token)
|
|
|
|
|
if err != nil {
|
2025-07-14 21:59:42 +08:00
|
|
|
common.ApiError(c, err)
|
2023-04-23 11:31:00 +08:00
|
|
|
return
|
|
|
|
|
}
|
2023-08-12 16:58:29 +08:00
|
|
|
if len(token.Name) > 30 {
|
2023-08-12 10:49:30 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": false,
|
|
|
|
|
"message": "令牌名称过长",
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-23 12:43:10 +08:00
|
|
|
cleanToken, err := model.GetTokenByIds(token.Id, userId)
|
|
|
|
|
if err != nil {
|
2025-07-14 21:59:42 +08:00
|
|
|
common.ApiError(c, err)
|
2023-04-23 12:43:10 +08:00
|
|
|
return
|
|
|
|
|
}
|
2023-04-24 20:52:40 +08:00
|
|
|
if token.Status == common.TokenStatusEnabled {
|
2023-07-02 14:47:06 +08:00
|
|
|
if cleanToken.Status == common.TokenStatusExpired && cleanToken.ExpiredTime <= common.GetTimestamp() && cleanToken.ExpiredTime != -1 {
|
2023-04-24 20:52:40 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": false,
|
2023-07-02 14:47:06 +08:00
|
|
|
"message": "令牌已过期,无法启用,请先修改令牌过期时间,或者设置为永不过期",
|
2023-04-24 20:52:40 +08:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-28 14:57:20 +08:00
|
|
|
if cleanToken.Status == common.TokenStatusExhausted && cleanToken.RemainQuota <= 0 && !cleanToken.UnlimitedQuota {
|
2023-04-24 20:52:40 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": false,
|
2023-05-16 12:09:17 +08:00
|
|
|
"message": "令牌可用额度已用尽,无法启用,请先修改令牌剩余额度,或者设置为无限额度",
|
2023-04-24 20:52:40 +08:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-26 10:45:34 +08:00
|
|
|
if statusOnly != "" {
|
|
|
|
|
cleanToken.Status = token.Status
|
|
|
|
|
} else {
|
|
|
|
|
// If you add more fields, please also update token.Update()
|
|
|
|
|
cleanToken.Name = token.Name
|
|
|
|
|
cleanToken.ExpiredTime = token.ExpiredTime
|
2023-05-16 12:09:17 +08:00
|
|
|
cleanToken.RemainQuota = token.RemainQuota
|
|
|
|
|
cleanToken.UnlimitedQuota = token.UnlimitedQuota
|
2024-01-08 16:23:54 +08:00
|
|
|
cleanToken.ModelLimitsEnabled = token.ModelLimitsEnabled
|
|
|
|
|
cleanToken.ModelLimits = token.ModelLimits
|
2024-09-17 20:49:51 +08:00
|
|
|
cleanToken.AllowIps = token.AllowIps
|
2024-09-18 05:19:10 +08:00
|
|
|
cleanToken.Group = token.Group
|
2023-04-26 10:45:34 +08:00
|
|
|
}
|
2023-04-23 12:43:10 +08:00
|
|
|
err = cleanToken.Update()
|
2023-04-23 11:31:00 +08:00
|
|
|
if err != nil {
|
2025-07-14 21:59:42 +08:00
|
|
|
common.ApiError(c, err)
|
2023-04-23 11:31:00 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": true,
|
|
|
|
|
"message": "",
|
2023-04-23 12:43:10 +08:00
|
|
|
"data": cleanToken,
|
2023-04-23 11:31:00 +08:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-06-22 16:35:30 +08:00
|
|
|
|
|
|
|
|
type TokenBatch struct {
|
|
|
|
|
Ids []int `json:"ids"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DeleteTokenBatch(c *gin.Context) {
|
|
|
|
|
tokenBatch := TokenBatch{}
|
|
|
|
|
if err := c.ShouldBindJSON(&tokenBatch); err != nil || len(tokenBatch.Ids) == 0 {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": false,
|
|
|
|
|
"message": "参数错误",
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
userId := c.GetInt("id")
|
|
|
|
|
count, err := model.BatchDeleteTokens(tokenBatch.Ids, userId)
|
|
|
|
|
if err != nil {
|
2025-07-14 21:59:42 +08:00
|
|
|
common.ApiError(c, err)
|
2025-06-22 16:35:30 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": true,
|
|
|
|
|
"message": "",
|
|
|
|
|
"data": count,
|
|
|
|
|
})
|
|
|
|
|
}
|