Files
CaIon 14a9acd368 feat(custom): CNY native billing, daily check-in button, subscription plans page, usage docs and promo banner
Based on QuantumNous/new-api v1.0.0-rc.36 (shallow history squashed).

- CNY quota display mode: RMB-denominated top-up and crediting across Epay/Waffo channels
- New CheckinButton in both authenticated and public headers (Turnstile supported)
- New /subscribe page with public plan snapshot, Alipay/balance payment, auto group upgrade/reset
- New /docs 12-section beginner guide with anchor navigation
- New home PromoBanner linking to /docs; i18n synced (en/zh/zh-TW/fr/ja/ru/vi)
- README: document custom features, live demo https://ai.88531.cn
2026-09-23 16:28:52 +08:00

85 lines
2.6 KiB
Go

package controller
import (
"errors"
"net/http"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/i18n"
"github.com/QuantumNous/new-api/model"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
func manageUserQuota(c *gin.Context, req ManageRequest) {
action := "generic"
params := model.AuditFields{
"target_user_id": req.Id,
"mode": req.Mode,
"requested_quota": req.Value,
}
switch req.Mode {
case "add":
action = "user.quota_add"
case "subtract":
action = "user.quota_subtract"
case "override":
action = "user.quota_override"
default:
params["action"] = "add_quota"
params["method"] = c.Request.Method
params["route"] = c.FullPath()
}
success := false
defer func() {
content := auditContentEN(action, params)
if !success {
// Failed requests have no committed balance changes to render.
content = "Failed user quota adjustment"
}
model.RecordOperationAuditLog(c.GetInt("id"), c.GetInt("role"), content, c.ClientIP(), action, params,
auditOperatorInfo(c), &model.AuditRequestInfo{
Method: c.Request.Method, Route: c.FullPath(), Status: c.Writer.Status(), Success: success,
}, c)
markAuditLogged(c)
}()
adjustment, err := model.AdjustUserQuota(req.Id, c.GetInt("role"), req.Mode, req.Value)
if err != nil {
switch {
case errors.Is(err, model.ErrInvalidUserQuotaAdjustment):
params["failure_reason"] = "invalid_parameters"
if (req.Mode == "add" || req.Mode == "subtract") && req.Value <= 0 {
common.ApiErrorI18n(c, i18n.MsgUserQuotaChangeZero)
} else {
common.ApiErrorI18n(c, i18n.MsgInvalidParams)
}
case errors.Is(err, model.ErrUserQuotaPermission):
params["failure_reason"] = "permission_denied"
common.ApiErrorI18n(c, i18n.MsgUserNoPermissionHigherLevel)
case errors.Is(err, gorm.ErrRecordNotFound):
params["failure_reason"] = "target_not_found"
common.ApiErrorI18n(c, i18n.MsgUserNotExists)
case errors.Is(err, model.ErrWalletQuotaLimitExceeded):
params["failure_reason"] = "quota_limit_exceeded"
common.ApiError(c, err)
default:
params["failure_reason"] = "database_error"
common.ApiError(c, err)
}
return
}
params["target_username"] = adjustment.Username
params["from"] = adjustment.Before
params["to"] = adjustment.After
if req.Mode != "override" {
params["quota"] = req.Value
}
success = true
operation := model.AuditOperation{Action: action, Params: params}
model.RecordLogWithAdminInfo(adjustment.UserID, model.LogTypeTopup,
auditContentEN(action, params), auditOperatorInfo(c), &operation, c)
c.JSON(http.StatusOK, gin.H{"success": true, "message": ""})
}