Skip to content

Commit cfea733

Browse files
committed
Update.
1 parent e578d59 commit cfea733

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

cmd/dashboard/controller/controller.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ import (
2727
"github.com/xos/serverstatus/service/singleton"
2828
)
2929

30-
// WriteJSON 使用 Gin 原生 JSON 写出,保持历史行为,避免前端解析差异
30+
// WriteJSON encodes v to JSON once and writes with proper headers, using gzip when accepted.
3131
func WriteJSON(c *gin.Context, status int, v interface{}) {
32-
c.JSON(status, v)
32+
payload, err := utils.EncodeJSON(v)
33+
if err != nil {
34+
// best-effort empty array fallback
35+
payload, _ = utils.EncodeJSON([]any{})
36+
}
37+
WriteJSONPayload(c, status, payload)
3338
}
3439

3540
// WriteJSONPayload writes a pre-encoded JSON payload with gzip when client accepts it.

cmd/dashboard/controller/member_api.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,15 @@ func (ma *memberAPI) addOrEditMonitor(c *gin.Context) {
808808
var mf monitorForm
809809
var m model.Monitor
810810
err := c.ShouldBindJSON(&mf)
811+
if err != nil {
812+
// 回退到表单绑定,兼容 application/x-www-form-urlencoded
813+
if ferr := c.ShouldBind(&mf); ferr == nil {
814+
err = nil
815+
}
816+
}
811817
if err == nil {
818+
log.Printf("[Monitor] 收到提交: ID=%d Name=%s Type=%d Cover=%d Duration=%d SkipServersRaw=%s FailTasks=%s RecoverTasks=%s",
819+
mf.ID, mf.Name, mf.Type, mf.Cover, mf.Duration, mf.SkipServersRaw, mf.FailTriggerTasksRaw, mf.RecoverTriggerTasksRaw)
812820
m.Name = mf.Name
813821
m.Target = strings.TrimSpace(mf.Target)
814822
m.Type = mf.Type

pkg/mygin/error.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"net/http"
55

66
"github.com/gin-gonic/gin"
7-
7+
"github.com/xos/serverstatus/pkg/utils"
88
"github.com/xos/serverstatus/model"
99
"github.com/xos/serverstatus/service/singleton"
1010
)
@@ -37,5 +37,14 @@ func ShowErrorPage(c *gin.Context, i ErrInfo, isPage bool) {
3737

3838
// writeJSON 统一 JSON 输出路径(mygin 内部使用)
3939
func writeJSON(c *gin.Context, status int, v interface{}) {
40-
c.JSON(status, v)
40+
payload, err := utils.EncodeJSON(v)
41+
if err != nil {
42+
// 降级兜底
43+
payload, _ = utils.EncodeJSON(model.Response{Code: status, Message: http.StatusText(status)})
44+
}
45+
c.Status(status)
46+
c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
47+
if _, gz, _ := utils.GzipIfAccepted(c.Writer, c.Request, payload); !gz {
48+
_, _ = c.Writer.Write(payload)
49+
}
4150
}

resource/static/main.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,20 @@ function showFormModal(modelSelector, formID, URL, getData) {
202202
}
203203
});
204204

205-
$.post(URL, JSON.stringify(data))
205+
// 更优雅:使用 Semantic UI Dropdown API 获取多选值,避免依赖 onChange 或标签扫描
206+
$(formID).find('.ui.multiple.dropdown').each(function() {
207+
const $dropdown = $(this);
208+
const $hidden = $dropdown.find('input[type="hidden"]');
209+
const name = $hidden.attr('name');
210+
if (!name || !name.endsWith('Raw')) return;
211+
let values = $dropdown.dropdown('get values') || [];
212+
// 统一为数字(若能转换),再序列化为 JSON 数组字符串
213+
values = values.map(v => { const n = parseInt(v); return isNaN(n) ? v : n; });
214+
data[name] = JSON.stringify(values);
215+
});
216+
217+
// 使用表单方式提交(application/x-www-form-urlencoded),与历史行为一致
218+
$.post(URL, data)
206219
.done(function (resp) {
207220
if (resp.code == 200) {
208221
window.location.reload()

0 commit comments

Comments
 (0)