增加透传给前端的自定义字段方便前端扩展功能

This commit is contained in:
naiba 2024-09-28 00:16:54 +08:00
parent bdf36276da
commit 106d58575b
12 changed files with 40 additions and 18 deletions

View File

@ -209,7 +209,7 @@ func (cp *commonPage) network(c *gin.Context) {
}))
}
func (cp *commonPage) getServerStat(c *gin.Context) ([]byte, error) {
func (cp *commonPage) getServerStat(c *gin.Context, withPublicNote bool) ([]byte, error) {
_, isMember := c.Get(model.CtxKeyAuthorizedUser)
_, isViewPasswordVerfied := c.Get(model.CtxKeyViewPasswordVerified)
authorized := isMember || isViewPasswordVerfied
@ -219,16 +219,15 @@ func (cp *commonPage) getServerStat(c *gin.Context) ([]byte, error) {
var servers []*model.Server
if authorized {
servers = singleton.SortedServerList
} else {
filteredServers := make([]*model.Server, len(singleton.SortedServerListForGuest))
for i, server := range singleton.SortedServerListForGuest {
filteredServer := *server
filteredServer.DDNSDomain = "redacted"
filteredServers[i] = &filteredServer
for _, server := range singleton.SortedServerListForGuest {
item := *server
if item.HideForGuest && !authorized {
continue
}
servers = filteredServers
if !withPublicNote {
item.PublicNote = ""
}
servers = append(servers, &item)
}
return utils.Json.Marshal(Data{
@ -240,7 +239,7 @@ func (cp *commonPage) getServerStat(c *gin.Context) ([]byte, error) {
}
func (cp *commonPage) home(c *gin.Context) {
stat, err := cp.getServerStat(c)
stat, err := cp.getServerStat(c, true)
if err != nil {
mygin.ShowErrorPage(c, mygin.ErrInfo{
Code: http.StatusInternalServerError,
@ -285,7 +284,7 @@ func (cp *commonPage) ws(c *gin.Context) {
defer conn.Close()
count := 0
for {
stat, err := cp.getServerStat(c)
stat, err := cp.getServerStat(c, false)
if err != nil {
continue
}

View File

@ -306,6 +306,7 @@ type serverForm struct {
Secret string
Tag string
Note string
PublicNote string
HideForGuest string
EnableDDNS string
EnableIPv4 string
@ -326,6 +327,7 @@ func (ma *memberAPI) addOrEditServer(c *gin.Context) {
s.ID = sf.ID
s.Tag = sf.Tag
s.Note = sf.Note
s.PublicNote = sf.PublicNote
s.HideForGuest = sf.HideForGuest == "on"
s.EnableDDNS = sf.EnableDDNS == "on"
s.EnableIPv4 = sf.EnableIPv4 == "on"

View File

@ -15,7 +15,8 @@ type Server struct {
Name string
Tag string // 分组名
Secret string `gorm:"uniqueIndex" json:"-"`
Note string `json:"-"` // 管理员可见备注
Note string `json:"-"` // 管理员可见备注
PublicNote string `json:"PublicNote,omitempty"` // 公开备注
DisplayIndex int // 展示排序,越大越靠前
HideForGuest bool // 对游客隐藏
EnableDDNS bool `json:"-"` // 是否启用DDNS 未在配置文件中启用DDNS 或 DDNS检查时间为0时此项无效
@ -54,12 +55,13 @@ func boolToString(b bool) string {
return "false"
}
func (s Server) Marshal() template.JS {
func (s Server) MarshalForDashboard() template.JS {
name, _ := utils.Json.Marshal(s.Name)
tag, _ := utils.Json.Marshal(s.Tag)
note, _ := utils.Json.Marshal(s.Note)
secret, _ := utils.Json.Marshal(s.Secret)
ddnsDomain, _ := utils.Json.Marshal(s.DDNSDomain)
ddnsProfile, _ := utils.Json.Marshal(s.DDNSProfile)
return template.JS(fmt.Sprintf(`{"ID":%d,"Name":%s,"Secret":%s,"DisplayIndex":%d,"Tag":%s,"Note":%s,"HideForGuest": %s,"EnableDDNS": %s,"EnableIPv4": %s,"EnableIpv6": %s,"DDNSDomain": %s,"DDNSProfile": %s}`, s.ID, name, secret, s.DisplayIndex, tag, note, boolToString(s.HideForGuest), boolToString(s.EnableDDNS), boolToString(s.EnableIPv4), boolToString(s.EnableIpv6), ddnsDomain, ddnsProfile)) // #nosec
publicNote, _ := utils.Json.Marshal(s.PublicNote)
return template.JS(fmt.Sprintf(`{"ID":%d,"Name":%s,"Secret":%s,"DisplayIndex":%d,"Tag":%s,"Note":%s,"HideForGuest": %s,"EnableDDNS": %s,"EnableIPv4": %s,"EnableIpv6": %s,"DDNSDomain": %s,"DDNSProfile": %s,"PublicNote": %s}`, s.ID, name, secret, s.DisplayIndex, tag, note, boolToString(s.HideForGuest), boolToString(s.EnableDDNS), boolToString(s.EnableIPv4), boolToString(s.EnableIpv6), ddnsDomain, ddnsProfile, publicNote))
}

View File

@ -18,7 +18,7 @@ func TestServerMarshal(t *testing.T) {
Name: patterns[i],
Tag: patterns[i],
}
serverStr := string(server.Marshal())
serverStr := string(server.MarshalForDashboard())
var serverRestore Server
if utils.Json.Unmarshal([]byte(serverStr), &serverRestore) != nil {
t.Fatalf("Error: %s", serverStr)

View File

@ -226,6 +226,9 @@ other = "Secret"
[Note]
other = "Note"
[PublicNote]
other = "Public Note"
[LinuxOneKeyInstall]
other = "Linux One-Command Install"

View File

@ -226,6 +226,9 @@ other = "Secreto"
[Note]
other = "Nota"
[PublicNote]
other = "Nota Pública"
[LinuxOneKeyInstall]
other = "Instalación Linux con Un Solo Clic"

View File

@ -226,6 +226,9 @@ other = "密钥"
[Note]
other = "备注"
[PublicNote]
other = "公开备注"
[LinuxOneKeyInstall]
other = "Linux 一键安装"

View File

@ -226,6 +226,9 @@ other = "金鑰"
[Note]
other = "備註"
[PublicNote]
other = "公開備註"
[LinuxOneKeyInstall]
other = "Linux 一鍵安裝"

View File

@ -331,6 +331,7 @@ function addOrEditServer(server, conf) {
.find("input[name=DisplayIndex]")
.val(server ? server.DisplayIndex : null);
modal.find("textarea[name=Note]").val(server ? server.Note : null);
modal.find("textarea[name=PublicNote]").val(server ? server.PublicNote : null);
if (server) {
modal.find(".secret.field").attr("style", "");
modal.find(".command.field").attr("style", "");

View File

@ -10,7 +10,7 @@
<script src="https://unpkg.com/semantic-ui@2.4.0/dist/semantic.min.js"></script>
<script src="/static/semantic-ui-alerts.min.js"></script>
<script src="https://unpkg.com/vue@2.6.14/dist/vue.min.js"></script>
<script src="/static/main.js?v20240714"></script>
<script src="/static/main.js?v2024927"></script>
<script>
(function () {
updateLang({{.LANG }});

View File

@ -56,6 +56,10 @@
<label>{{tr "Note"}}</label>
<textarea name="Note"></textarea>
</div>
<div class="field">
<label>{{tr "PublicNote"}}</label>
<textarea name="PublicNote"></textarea>
</div>
<div class="command field">
<label>{{tr "LinuxOneKeyInstall"}}</label>
<div class="ui message">

View File

@ -36,6 +36,7 @@
<th>{{tr "Secret"}}</th>
<th>{{tr "OneKeyInstall"}}</th>
<th>{{tr "Note"}}</th>
<th>{{tr "PublicNote"}}</th>
<th>{{tr "Administration"}}</th>
</tr>
</thead>
@ -76,12 +77,13 @@
</button>
</td>
<td style="word-break: break-word;white-space: pre-wrap;">{{$server.Note}}</td>
<td style="word-break: break-word;white-space: pre-wrap;">{{$server.PublicNote}}</td>
<td>
<div class="ui mini icon buttons">
<button class="ui button" onclick="connectToServer({{$server.ID}})">
<i class="terminal icon"></i>
</button>
<button class="ui button" onclick="addOrEditServer({{$server.Marshal}})">
<button class="ui button" onclick="addOrEditServer({{$server.MarshalForDashboard}})">
<i class="edit icon"></i>
</button>
<button class="ui button"