Skip to content

Commit 8452cd3

Browse files
committed
Update.
1 parent 36e8367 commit 8452cd3

File tree

6 files changed

+51
-105
lines changed

6 files changed

+51
-105
lines changed

cmd/dashboard/controller/member_api.go

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7-
"io"
87
"log"
98
"net/http"
109
"strconv"
@@ -358,13 +357,13 @@ func (ma *memberAPI) addOrEditServer(c *gin.Context) {
358357
s.PublicNote = sf.PublicNote
359358
s.HideForGuest = sf.HideForGuest == "on"
360359
s.EnableDDNS = sf.EnableDDNS == "on"
361-
360+
362361
// 处理DDNSProfilesRaw,确保它是有效的JSON数组
363362
if sf.DDNSProfilesRaw == "" {
364363
sf.DDNSProfilesRaw = "[]"
365364
}
366365
s.DDNSProfilesRaw = sf.DDNSProfilesRaw
367-
366+
368367
// 尝试解析JSON,如果失败则设置为空数组
369368
err = utils.Json.Unmarshal([]byte(sf.DDNSProfilesRaw), &s.DDNSProfiles)
370369
if err != nil {
@@ -373,7 +372,7 @@ func (ma *memberAPI) addOrEditServer(c *gin.Context) {
373372
s.DDNSProfilesRaw = "[]"
374373
err = nil
375374
}
376-
375+
377376
if err == nil {
378377
if s.ID == 0 {
379378
s.Secret, err = utils.GenerateRandomString(18)
@@ -689,20 +688,14 @@ func (ma *memberAPI) batchUpdateServerGroup(c *gin.Context) {
689688
}
690689

691690
func (ma *memberAPI) forceUpdate(c *gin.Context) {
692-
// 添加调试日志以便比较
693-
log.Printf("forceUpdate - Content-Type: %s", c.GetHeader("Content-Type"))
694-
695691
var forceUpdateServers []uint64
696692
if err := c.ShouldBindJSON(&forceUpdateServers); err != nil {
697-
log.Printf("forceUpdate JSON解码失败: %v", err)
698693
c.JSON(http.StatusOK, model.Response{
699694
Code: http.StatusBadRequest,
700695
Message: err.Error(),
701696
})
702697
return
703698
}
704-
705-
log.Printf("forceUpdate 成功解析服务器列表: %v", forceUpdateServers)
706699

707700
var executeResult bytes.Buffer
708701

@@ -1128,59 +1121,22 @@ func (ma *memberAPI) updateSetting(c *gin.Context) {
11281121
}
11291122

11301123
func (ma *memberAPI) batchDeleteServer(c *gin.Context) {
1131-
// 记录请求头信息用于调试
1132-
log.Printf("batchDeleteServer - Content-Type: %s", c.GetHeader("Content-Type"))
1133-
log.Printf("batchDeleteServer - User-Agent: %s", c.GetHeader("User-Agent"))
1134-
1135-
// 先读取原始请求体进行调试分析
1136-
bodyBytes, err := io.ReadAll(c.Request.Body)
1137-
if err != nil {
1138-
log.Printf("读取请求体失败: %v", err)
1139-
c.JSON(http.StatusOK, model.Response{
1140-
Code: http.StatusBadRequest,
1141-
Message: fmt.Sprintf("读取请求失败: %v", err),
1142-
})
1143-
return
1144-
}
1145-
1146-
// 记录原始请求体信息
1147-
log.Printf("原始请求体内容: %q", string(bodyBytes))
1148-
log.Printf("请求体长度: %d", len(bodyBytes))
1149-
if len(bodyBytes) > 0 {
1150-
log.Printf("第一个字符: %q (ASCII: %d)", bodyBytes[0], bodyBytes[0])
1151-
if len(bodyBytes) > 1 {
1152-
log.Printf("第二个字符: %q (ASCII: %d)", bodyBytes[1], bodyBytes[1])
1153-
}
1154-
if len(bodyBytes) > 2 {
1155-
log.Printf("第三个字符: %q (ASCII: %d)", bodyBytes[2], bodyBytes[2])
1156-
}
1157-
}
1158-
1159-
// 重新设置请求体以供JSON解析
1160-
c.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes))
1161-
11621124
var servers []uint64
11631125
if err := c.ShouldBindJSON(&servers); err != nil {
1164-
log.Printf("JSON解码失败: %v", err)
11651126
c.JSON(http.StatusOK, model.Response{
11661127
Code: http.StatusBadRequest,
11671128
Message: fmt.Sprintf("JSON解码失败: %v", err),
11681129
})
11691130
return
11701131
}
1171-
1172-
log.Printf("成功解析服务器列表: %v", servers)
1173-
1174-
// 验证服务器ID列表
1132+
11751133
if len(servers) == 0 {
1176-
log.Printf("没有提供要删除的服务器ID")
11771134
c.JSON(http.StatusOK, model.Response{
11781135
Code: http.StatusBadRequest,
11791136
Message: "没有选择要删除的服务器",
11801137
})
11811138
return
1182-
}
1183-
log.Printf("准备删除服务器: %v", servers)
1139+
}
11841140
if err := singleton.DB.Unscoped().Delete(&model.Server{}, "id in (?)", servers).Error; err != nil {
11851141
c.JSON(http.StatusOK, model.Response{
11861142
Code: http.StatusBadRequest,

model/alertrule.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,16 @@ func (r *AlertRule) BeforeSave(tx *gorm.DB) error {
5959
func (r *AlertRule) AfterFind(tx *gorm.DB) error {
6060
var err error
6161
if err = utils.Json.Unmarshal([]byte(r.RulesRaw), &r.Rules); err != nil {
62-
return err
62+
// 更新数据库中的无效数据
63+
tx.Model(r).Update("rules_raw", "[]")
6364
}
6465
if err = utils.Json.Unmarshal([]byte(r.FailTriggerTasksRaw), &r.FailTriggerTasks); err != nil {
65-
return err
66+
// 更新数据库中的无效数据
67+
tx.Model(r).Update("fail_trigger_tasks_raw", "[]")
6668
}
6769
if err = utils.Json.Unmarshal([]byte(r.RecoverTriggerTasksRaw), &r.RecoverTriggerTasks); err != nil {
68-
return err
70+
// 更新数据库中的无效数据
71+
tx.Model(r).Update("recover_trigger_tasks_raw", "[]")
6972
}
7073
return nil
7174
}

model/cron.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package model
22

33
import (
4+
"log"
45
"time"
56

67
"github.com/robfig/cron/v3"
@@ -34,5 +35,32 @@ type Cron struct {
3435
}
3536

3637
func (c *Cron) AfterFind(tx *gorm.DB) error {
37-
return utils.Json.Unmarshal([]byte(c.ServersRaw), &c.Servers)
38+
if c.ServersRaw == "" {
39+
c.ServersRaw = "[]"
40+
c.Servers = []uint64{}
41+
return nil
42+
}
43+
44+
// 尝试解析JSON,如果失败则修复格式
45+
err := utils.Json.Unmarshal([]byte(c.ServersRaw), &c.Servers)
46+
if err != nil {
47+
// 检查是否是 "[]," 这种无效格式
48+
if c.ServersRaw == "[]," || c.ServersRaw == "," {
49+
c.ServersRaw = "[]"
50+
c.Servers = []uint64{}
51+
// 更新数据库中的无效数据
52+
tx.Model(c).Update("servers_raw", "[]")
53+
return nil
54+
}
55+
56+
// 其他格式错误,尝试修复
57+
log.Printf("解析Cron任务 %s 的ServersRaw失败(%s),重置为空数组: %v", c.Name, c.ServersRaw, err)
58+
c.ServersRaw = "[]"
59+
c.Servers = []uint64{}
60+
// 更新数据库中的无效数据
61+
tx.Model(c).Update("servers_raw", "[]")
62+
return nil
63+
}
64+
65+
return nil
3866
}

model/ddns.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ type DDNSRecordState struct {
106106
ServerID uint64 `gorm:"index:idx_ddns_record,unique" json:"server_id"`
107107
Domain string `gorm:"index:idx_ddns_record,unique;size:255" json:"domain"`
108108
RecordType string `gorm:"index:idx_ddns_record,unique;size:10" json:"record_type"`
109-
LastIP string `gorm:"size:45" json:"last_ip"` // 上次记录的IP地址
109+
LastIP string `gorm:"size:45" json:"last_ip"` // 上次记录的IP地址
110110
LastUpdate time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"last_update"` // 上次更新时间
111111
}
112112

model/monitor.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,18 @@ func (m *Monitor) AfterFind(tx *gorm.DB) error {
113113

114114
// 加载触发任务列表
115115
if err := utils.Json.Unmarshal([]byte(m.FailTriggerTasksRaw), &m.FailTriggerTasks); err != nil {
116-
return err
116+
log.Printf("解析Monitor %s 的FailTriggerTasksRaw失败(%s),重置为空数组: %v", m.Name, m.FailTriggerTasksRaw, err)
117+
m.FailTriggerTasks = []uint64{}
118+
m.FailTriggerTasksRaw = "[]"
119+
// 更新数据库中的无效数据
120+
tx.Model(m).Update("fail_trigger_tasks_raw", "[]")
117121
}
118122
if err := utils.Json.Unmarshal([]byte(m.RecoverTriggerTasksRaw), &m.RecoverTriggerTasks); err != nil {
119-
return err
123+
log.Printf("解析Monitor %s 的RecoverTriggerTasksRaw失败(%s),重置为空数组: %v", m.Name, m.RecoverTriggerTasksRaw, err)
124+
m.RecoverTriggerTasks = []uint64{}
125+
m.RecoverTriggerTasksRaw = "[]"
126+
// 更新数据库中的无效数据
127+
tx.Model(m).Update("recover_trigger_tasks_raw", "[]")
120128
}
121129

122130
return nil

resource/static/main.js

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,13 @@ function showConfirm(title, content, callFn, extData) {
5050
}
5151

5252
function postJson(url, data) {
53-
console.log('postJson 调用:', url, data);
54-
console.log('数据类型:', typeof data);
55-
console.log('是否为数组:', Array.isArray(data));
56-
if (Array.isArray(data)) {
57-
console.log('数组长度:', data.length);
58-
console.log('数组内容:', data);
59-
}
60-
6153
const jsonString = JSON.stringify(data);
62-
console.log('JSON序列化后:', jsonString);
63-
console.log('JSON字符串长度:', jsonString.length);
64-
if (jsonString.length > 0) {
65-
console.log('首字符:', jsonString.charAt(0), '(ASCII:', jsonString.charCodeAt(0), ')');
66-
if (jsonString.length > 1) {
67-
console.log('次字符:', jsonString.charAt(1), '(ASCII:', jsonString.charCodeAt(1), ')');
68-
}
69-
}
70-
7154
return $.ajax({
7255
url: url,
7356
type: "POST",
7457
contentType: "application/json",
7558
data: jsonString,
7659
}).done((resp) => {
77-
console.log('postJson 响应:', resp);
7860
if (resp.code == 200) {
7961
if (resp.message) {
8062
alert(resp.message);
@@ -88,8 +70,6 @@ function postJson(url, data) {
8870
}
8971
})
9072
.fail((err) => {
91-
console.error('postJson 失败:', err);
92-
console.error('响应文本:', err.responseText);
9373
alert("网络错误:" + err.responseText);
9474
});
9575
}
@@ -815,12 +795,9 @@ function initWebSocket() {
815795
const ws = new WebSocket(wsUrl);
816796

817797
ws.onopen = function() {
818-
console.log('WebSocket连接已建立');
819-
// 发送一个ping消息来测试连接
820798
try {
821799
ws.send(JSON.stringify({ type: 'ping' }));
822800
} catch (e) {
823-
console.error('发送ping消息失败:', e);
824801
}
825802
};
826803

@@ -843,23 +820,15 @@ function initWebSocket() {
843820
} else {
844821
window.extractTrafficData();
845822
}
846-
// 打印流量数据调试信息,只显示部分数据
847-
if (data.trafficData.length > 0) {
848-
const sample = data.trafficData[0];
849-
console.debug(`流量数据更新: ${data.trafficData.length}个服务器, 示例(ID:${sample.server_id}): ${sample.used_formatted}/${sample.max_formatted}`);
850-
}
851823
}
852824
} catch (e) {
853-
console.error('处理WebSocket消息时出错:', e);
854825
}
855826
};
856827

857828
ws.onerror = function(error) {
858-
console.error('WebSocket错误:', error);
859829
};
860830

861831
ws.onclose = function(event) {
862-
console.log('WebSocket连接已关闭,5秒后重试...');
863832
setTimeout(initWebSocket, 5000);
864833
};
865834

@@ -904,9 +873,6 @@ window.extractTrafficData = function() {
904873
maxBytes = item.max_bytes;
905874
usedBytes = item.used_bytes;
906875

907-
// 记录调试信息
908-
console.debug(`服务器 ${serverId} 流量数据更新: ${usedBytes}/${maxBytes} 字节`);
909-
910876
// 从字节数据计算百分比
911877
if (maxBytes > 0) {
912878
percent = (usedBytes / maxBytes) * 100;
@@ -963,10 +929,7 @@ window.extractTrafficData = function() {
963929
if (window.statusCards && typeof window.statusCards.updateTrafficData === 'function') {
964930
window.statusCards.updateTrafficData();
965931
}
966-
967-
console.log(`已更新 ${updatedCount} 个服务器的流量数据 (WebSocket字节源数据)`);
968932
} catch (e) {
969-
console.error('处理流量数据时出错:', e);
970933
}
971934
};
972935

@@ -994,7 +957,6 @@ window.parseTrafficToBytes = function(trafficStr) {
994957

995958
// 如果还是没匹配到,返回0
996959
if (!match) {
997-
console.warn('无法解析流量字符串:', trafficStr);
998960
return 0;
999961
}
1000962

@@ -1077,7 +1039,6 @@ class TrafficManager {
10771039
// Process raw traffic data
10781040
processTrafficData(rawData) {
10791041
if (!Array.isArray(rawData) || rawData.length === 0) {
1080-
console.warn('没有可用的流量数据');
10811042
return;
10821043
}
10831044

@@ -1097,9 +1058,6 @@ class TrafficManager {
10971058
maxBytes = item.max_bytes;
10981059
usedBytes = item.used_bytes;
10991060

1100-
// 为调试输出完整信息
1101-
console.debug(`服务器${serverId}: 收到流量字节数据 used=${usedBytes}, max=${maxBytes}`);
1102-
11031061
// 从字节数据计算百分比
11041062
if (maxBytes > 0) {
11051063
percent = (usedBytes / maxBytes) * 100;
@@ -1157,7 +1115,6 @@ class TrafficManager {
11571115
window.serverTrafficData = newData;
11581116
window.lastTrafficUpdateTime = this.lastUpdateTime;
11591117

1160-
console.log(`已更新 ${updatedCount} 个服务器的流量数据 (字节源数据)`);
11611118
this.notifySubscribers();
11621119
}
11631120

@@ -1205,7 +1162,6 @@ class TrafficManager {
12051162
try {
12061163
callback(this.trafficData, this.dataVersion);
12071164
} catch (e) {
1208-
console.error('Error in traffic data subscriber:', e);
12091165
}
12101166
});
12111167
}
@@ -1275,15 +1231,13 @@ function fetchServerTrafficData(serverId) {
12751231
if (data && data.code === 200) {
12761232
return data.data;
12771233
} else if (data && data.code === 403) {
1278-
console.warn('需要登录才能访问流量数据');
12791234
if (window.location.pathname !== '/login') {
12801235
window.location.href = '/login';
12811236
}
12821237
}
12831238
return null;
12841239
},
12851240
error: function(err) {
1286-
console.error('获取服务器流量数据失败:', err);
12871241
return null;
12881242
}
12891243
});
@@ -1370,7 +1324,6 @@ function updateServerNameMapping() {
13701324
}
13711325
})
13721326
.fail(function() {
1373-
console.warn('无法加载服务器名称映射');
13741327
});
13751328
}
13761329

@@ -1394,7 +1347,6 @@ function updateDDNSNameMapping() {
13941347
}
13951348
})
13961349
.fail(function() {
1397-
console.warn('无法加载DDNS名称映射');
13981350
});
13991351
}
14001352

@@ -1418,7 +1370,6 @@ function updateTaskNameMapping() {
14181370
}
14191371
})
14201372
.fail(function() {
1421-
console.warn('无法加载Task名称映射');
14221373
});
14231374
}
14241375

0 commit comments

Comments
 (0)