增加统一倍率调整按钮

This commit is contained in:
andy committed 2026-09-23 15:46:21 +08:00
1 parent be655b6569
commit 4195525dc7
23 files changed
+750 -16

No files matched your search

+34 -1
View File
@@ -3,6 +3,7 @@ package httpapi
import (
"encoding/json"
"errors"
"io"
"math"
"net/http"
"strconv"
@@ -232,12 +233,18 @@ func (h *billingHandler) adjust(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, result)
}
func (h *billingHandler) prices(w http.ResponseWriter, r *http.Request) {
if !allow(w, r, http.MethodGet) {
if r.Method != http.MethodGet && r.Method != http.MethodPatch {
w.Header().Set("Allow", "GET, PATCH")
writeAPIError(w, http.StatusMethodNotAllowed, "方法不允许。")
return
}
if _, ok := h.authorize(w, r, PlatformSuperAdmin); !ok {
return
}
if r.Method == http.MethodPatch {
h.updatePrices(w, r)
return
}
rules, err := h.service.ListPrices(r.Context())
if err != nil {
writeDomainError(w, err)
@@ -245,6 +252,32 @@ func (h *billingHandler) prices(w http.ResponseWriter, r *http.Request) {
}
writeJSON(w, 200, map[string]any{"priceRules": rules})
}
// Called only after the collection route has authorized a platform super admin.
func (h *billingHandler) updatePrices(w http.ResponseWriter, r *http.Request) {
var body struct {
RuleIDs []string `json:"ruleIds"`
MarkupMultiplier any `json:"markupMultiplier"`
}
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(&body); err != nil {
writeAPIError(w, 400, "请提供规则列表和上浮倍率,不能修改标准价格或参数。")
return
}
if err := decoder.Decode(&struct{}{}); err != io.EOF {
writeAPIError(w, 400, "请求必须包含一个有效的 JSON 对象。")
return
}
rules, err := h.service.UpdatePrices(r.Context(), billing.BulkPricePatch{
RuleIDs: body.RuleIDs, MarkupMultiplier: numberValue(body.MarkupMultiplier),
})
if err != nil {
writeDomainError(w, err)
return
}
writeJSON(w, 200, map[string]any{"priceRules": rules})
}
func (h *billingHandler) price(w http.ResponseWriter, r *http.Request, id string) {
if !allow(w, r, http.MethodPatch) {
return