Skip to content

Commit 469d374

Browse files
authored
fix: defaut ui triage (#4802)
* fix: theme-aware payment paths, auto-group validation, route guards, perf group filtering - Add common.ThemeAwarePath to generate correct redirect URLs based on active theme (default vs classic), replacing hardcoded /console/* paths in 7 controllers and service/quota.go (#4765) - Validate auto-group availability against getUserGroups before defaulting form values; playground falls back to 'default' group when 'auto' is unavailable (#4796, #4799) - Enforce HeaderNavModules settings in rankings route (frontend + backend API) and SidebarModulesAdmin in playground route to block direct URL access when features are disabled (#4704, #4512) - Filter perf_metrics API response to only include currently configured groups, hiding stale data from deleted groups (#4790) - Preserve query params (pay=success/fail) in /console/topup → /wallet frontend redirect * fix: update hero section text and localization strings for clarity
1 parent a720064 commit 469d374

27 files changed

Lines changed: 262 additions & 72 deletions

File tree

common/constants.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/tls"
55
//"os"
66
//"strconv"
7+
"strings"
78
"sync"
89
"sync/atomic"
910
"time"
@@ -36,6 +37,26 @@ func SetTheme(t string) {
3637
}
3738
}
3839

40+
// ThemeAwarePath rewrites legacy /console/* paths to the default-theme
41+
// equivalents when the active theme is "default". For "classic" (or any
42+
// other theme) the path is returned unchanged. The function only touches
43+
// known prefixes so it is safe to call with arbitrary suffixes and query
44+
// strings.
45+
func ThemeAwarePath(suffix string) string {
46+
if GetTheme() != "default" {
47+
return suffix
48+
}
49+
switch {
50+
case strings.HasPrefix(suffix, "/console/topup"):
51+
return strings.Replace(suffix, "/console/topup", "/wallet", 1)
52+
case strings.HasPrefix(suffix, "/console/log"):
53+
return strings.Replace(suffix, "/console/log", "/usage-logs", 1)
54+
case strings.HasPrefix(suffix, "/console/personal"):
55+
return strings.Replace(suffix, "/console/personal", "/profile", 1)
56+
}
57+
return suffix
58+
}
59+
3960
// var ChatLink = ""
4061
// var ChatLink2 = ""
4162
var QuotaPerUnit = 500 * 1000.0 // $0.002 / 1K tokens

controller/perf_metrics.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strconv"
66

77
perfmetrics "github.com/QuantumNous/new-api/pkg/perf_metrics"
8+
"github.com/QuantumNous/new-api/setting/ratio_setting"
89

910
"github.com/gin-gonic/gin"
1011
)
@@ -62,8 +63,21 @@ func GetPerfMetrics(c *gin.Context) {
6263
return
6364
}
6465

66+
result.Groups = filterActiveGroups(result.Groups)
67+
6568
c.JSON(http.StatusOK, gin.H{
6669
"success": true,
6770
"data": result,
6871
})
6972
}
73+
74+
func filterActiveGroups(groups []perfmetrics.GroupResult) []perfmetrics.GroupResult {
75+
activeGroups := ratio_setting.GetGroupRatioCopy()
76+
filtered := make([]perfmetrics.GroupResult, 0, len(groups))
77+
for _, g := range groups {
78+
if _, ok := activeGroups[g.Group]; ok || g.Group == "auto" {
79+
filtered = append(filtered, g)
80+
}
81+
}
82+
return filtered
83+
}

controller/rankings.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,51 @@ package controller
33
import (
44
"net/http"
55

6+
"github.com/QuantumNous/new-api/common"
67
"github.com/QuantumNous/new-api/service"
78
"github.com/gin-gonic/gin"
89
)
910

11+
func isRankingsEnabled() bool {
12+
common.OptionMapRWMutex.RLock()
13+
raw := common.OptionMap["HeaderNavModules"]
14+
common.OptionMapRWMutex.RUnlock()
15+
16+
if raw == "" {
17+
return true
18+
}
19+
20+
var parsed map[string]interface{}
21+
if err := common.Unmarshal([]byte(raw), &parsed); err != nil {
22+
return true
23+
}
24+
rankings, ok := parsed["rankings"]
25+
if !ok {
26+
return true
27+
}
28+
switch v := rankings.(type) {
29+
case bool:
30+
return v
31+
case map[string]interface{}:
32+
if enabled, ok := v["enabled"]; ok {
33+
if b, ok := enabled.(bool); ok {
34+
return b
35+
}
36+
}
37+
return true
38+
}
39+
return true
40+
}
41+
1042
func GetRankings(c *gin.Context) {
43+
if !isRankingsEnabled() {
44+
c.JSON(http.StatusForbidden, gin.H{
45+
"success": false,
46+
"message": "rankings is disabled",
47+
})
48+
return
49+
}
50+
1151
result, err := service.GetRankingsSnapshot(c.DefaultQuery("period", "week"))
1252
if err != nil {
1353
c.JSON(http.StatusBadRequest, gin.H{

controller/return_path.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package controller
2+
3+
import (
4+
"strings"
5+
6+
"github.com/QuantumNous/new-api/common"
7+
"github.com/QuantumNous/new-api/setting/system_setting"
8+
)
9+
10+
func paymentReturnPath(suffix string) string {
11+
base := strings.TrimRight(system_setting.ServerAddress, "/")
12+
return base + common.ThemeAwarePath(suffix)
13+
}

controller/subscription_payment_epay.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/QuantumNous/new-api/model"
1313
"github.com/QuantumNous/new-api/service"
1414
"github.com/QuantumNous/new-api/setting/operation_setting"
15-
"github.com/QuantumNous/new-api/setting/system_setting"
1615
"github.com/gin-gonic/gin"
1716
"github.com/samber/lo"
1817
)
@@ -173,7 +172,7 @@ func SubscriptionEpayReturn(c *gin.Context) {
173172
if c.Request.Method == "POST" {
174173
// POST 请求:从 POST body 解析参数
175174
if err := c.Request.ParseForm(); err != nil {
176-
c.Redirect(http.StatusFound, system_setting.ServerAddress+"/console/topup?pay=fail")
175+
c.Redirect(http.StatusFound, paymentReturnPath("/console/topup?pay=fail"))
177176
return
178177
}
179178
params = lo.Reduce(lo.Keys(c.Request.PostForm), func(r map[string]string, t string, i int) map[string]string {
@@ -189,29 +188,29 @@ func SubscriptionEpayReturn(c *gin.Context) {
189188
}
190189

191190
if len(params) == 0 {
192-
c.Redirect(http.StatusFound, system_setting.ServerAddress+"/console/topup?pay=fail")
191+
c.Redirect(http.StatusFound, paymentReturnPath("/console/topup?pay=fail"))
193192
return
194193
}
195194

196195
client := GetEpayClient()
197196
if client == nil {
198-
c.Redirect(http.StatusFound, system_setting.ServerAddress+"/console/topup?pay=fail")
197+
c.Redirect(http.StatusFound, paymentReturnPath("/console/topup?pay=fail"))
199198
return
200199
}
201200
verifyInfo, err := client.Verify(params)
202201
if err != nil || !verifyInfo.VerifyStatus {
203-
c.Redirect(http.StatusFound, system_setting.ServerAddress+"/console/topup?pay=fail")
202+
c.Redirect(http.StatusFound, paymentReturnPath("/console/topup?pay=fail"))
204203
return
205204
}
206205
if verifyInfo.TradeStatus == epay.StatusTradeSuccess {
207206
LockOrder(verifyInfo.ServiceTradeNo)
208207
defer UnlockOrder(verifyInfo.ServiceTradeNo)
209208
if err := model.CompleteSubscriptionOrder(verifyInfo.ServiceTradeNo, common.GetJsonString(verifyInfo), model.PaymentProviderEpay, verifyInfo.Type); err != nil {
210-
c.Redirect(http.StatusFound, system_setting.ServerAddress+"/console/topup?pay=fail")
209+
c.Redirect(http.StatusFound, paymentReturnPath("/console/topup?pay=fail"))
211210
return
212211
}
213-
c.Redirect(http.StatusFound, system_setting.ServerAddress+"/console/topup?pay=success")
212+
c.Redirect(http.StatusFound, paymentReturnPath("/console/topup?pay=success"))
214213
return
215214
}
216-
c.Redirect(http.StatusFound, system_setting.ServerAddress+"/console/topup?pay=pending")
215+
c.Redirect(http.StatusFound, paymentReturnPath("/console/topup?pay=pending"))
217216
}

controller/subscription_payment_stripe.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/QuantumNous/new-api/logger"
1111
"github.com/QuantumNous/new-api/model"
1212
"github.com/QuantumNous/new-api/setting"
13-
"github.com/QuantumNous/new-api/setting/system_setting"
1413
"github.com/gin-gonic/gin"
1514
"github.com/stripe/stripe-go/v81"
1615
"github.com/stripe/stripe-go/v81/checkout/session"
@@ -111,8 +110,8 @@ func genStripeSubscriptionLink(referenceId string, customerId string, email stri
111110

112111
params := &stripe.CheckoutSessionParams{
113112
ClientReferenceID: stripe.String(referenceId),
114-
SuccessURL: stripe.String(system_setting.ServerAddress + "/console/topup"),
115-
CancelURL: stripe.String(system_setting.ServerAddress + "/console/topup"),
113+
SuccessURL: stripe.String(paymentReturnPath("/console/topup")),
114+
CancelURL: stripe.String(paymentReturnPath("/console/topup")),
116115
LineItems: []*stripe.CheckoutSessionLineItemParams{
117116
{
118117
Price: stripe.String(priceId),

controller/telegram.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TelegramBind(c *gin.Context) {
6666
return
6767
}
6868

69-
c.Redirect(302, "/console/personal")
69+
c.Redirect(302, common.ThemeAwarePath("/console/personal"))
7070
}
7171

7272
func TelegramLogin(c *gin.Context) {

controller/topup.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/QuantumNous/new-api/service"
1515
"github.com/QuantumNous/new-api/setting"
1616
"github.com/QuantumNous/new-api/setting/operation_setting"
17-
"github.com/QuantumNous/new-api/setting/system_setting"
1817

1918
"github.com/Calcium-Ion/go-epay/epay"
2019
"github.com/gin-gonic/gin"
@@ -208,7 +207,7 @@ func RequestEpay(c *gin.Context) {
208207
}
209208

210209
callBackAddress := service.GetCallbackAddress()
211-
returnUrl, _ := url.Parse(system_setting.ServerAddress + "/console/log")
210+
returnUrl, _ := url.Parse(paymentReturnPath("/console/log"))
212211
notifyUrl, _ := url.Parse(callBackAddress + "/api/user/epay/notify")
213212
tradeNo := fmt.Sprintf("%s%d", common.GetRandomString(6), time.Now().Unix())
214213
tradeNo = fmt.Sprintf("USR%dNO%s", id, tradeNo)

controller/topup_stripe.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/QuantumNous/new-api/model"
1616
"github.com/QuantumNous/new-api/setting"
1717
"github.com/QuantumNous/new-api/setting/operation_setting"
18-
"github.com/QuantumNous/new-api/setting/system_setting"
1918

2019
"github.com/gin-gonic/gin"
2120
"github.com/stripe/stripe-go/v81"
@@ -348,10 +347,10 @@ func genStripeLink(referenceId string, customerId string, email string, amount i
348347

349348
// Use custom URLs if provided, otherwise use defaults
350349
if successURL == "" {
351-
successURL = system_setting.ServerAddress + "/console/log"
350+
successURL = paymentReturnPath("/console/log")
352351
}
353352
if cancelURL == "" {
354-
cancelURL = system_setting.ServerAddress + "/console/topup"
353+
cancelURL = paymentReturnPath("/console/topup")
355354
}
356355

357356
params := &stripe.CheckoutSessionParams{

controller/topup_waffo.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/QuantumNous/new-api/service"
1515
"github.com/QuantumNous/new-api/setting"
1616
"github.com/QuantumNous/new-api/setting/operation_setting"
17-
"github.com/QuantumNous/new-api/setting/system_setting"
1817
"github.com/gin-gonic/gin"
1918
"github.com/thanhpk/randstr"
2019
waffo "github.com/waffo-com/waffo-go"
@@ -237,7 +236,7 @@ func RequestWaffoPay(c *gin.Context) {
237236
if setting.WaffoNotifyUrl != "" {
238237
notifyUrl = setting.WaffoNotifyUrl
239238
}
240-
returnUrl := system_setting.ServerAddress + "/console/topup?show_history=true"
239+
returnUrl := paymentReturnPath("/console/topup?show_history=true")
241240
if setting.WaffoReturnUrl != "" {
242241
returnUrl = setting.WaffoReturnUrl
243242
}

0 commit comments

Comments
 (0)