From 347bb7faa0bd7fbe7344df6346135189c174df30 Mon Sep 17 00:00:00 2001 From: naiba Date: Fri, 8 Jan 2021 21:04:50 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E5=B1=95=E7=A4=BA=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=20close=20#29?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/dashboard/controller/common_page.go | 4 ++-- cmd/dashboard/controller/member_api.go | 28 +++++++++++++---------- cmd/dashboard/controller/member_page.go | 2 +- cmd/dashboard/main.go | 6 ++--- model/server.go | 5 ++-- resource/static/main.js | 5 +++- resource/template/common/footer.html | 2 +- resource/template/component/server.html | 4 ++++ resource/template/dashboard/server.html | 8 +++---- resource/template/theme-default/home.html | 5 ++-- resource/template/theme-hotaru/home.html | 5 ++-- service/dao/dao.go | 15 +++++++++++- service/rpc/auth.go | 10 ++++---- service/rpc/nezha.go | 6 ++--- 14 files changed, 63 insertions(+), 42 deletions(-) diff --git a/cmd/dashboard/controller/common_page.go b/cmd/dashboard/controller/common_page.go index cfe7c8a..f37e215 100644 --- a/cmd/dashboard/controller/common_page.go +++ b/cmd/dashboard/controller/common_page.go @@ -27,7 +27,7 @@ func (cp *commonPage) home(c *gin.Context) { dao.ServerLock.RLock() defer dao.ServerLock.RUnlock() data := gin.H{ - "Servers": dao.ServerList, + "Servers": dao.SortedServerList, "CustomCode": dao.Conf.Site.CustomCode, } u, ok := c.Get(model.CtxKeyAuthorizedUser) @@ -54,7 +54,7 @@ func (cp *commonPage) ws(c *gin.Context) { defer conn.Close() for { dao.ServerLock.RLock() - err = conn.WriteJSON(dao.ServerList) + err = conn.WriteJSON(dao.SortedServerList) dao.ServerLock.RUnlock() if err != nil { break diff --git a/cmd/dashboard/controller/member_api.go b/cmd/dashboard/controller/member_api.go index a62cc23..eb3f666 100644 --- a/cmd/dashboard/controller/member_api.go +++ b/cmd/dashboard/controller/member_api.go @@ -56,7 +56,8 @@ func (ma *memberAPI) delete(c *gin.Context) { defer dao.ServerLock.Unlock() err = dao.DB.Delete(&model.Server{}, "id = ?", id).Error if err == nil { - delete(dao.ServerList, strconv.FormatUint(id, 10)) + delete(dao.ServerList, id) + dao.ReSortServer() } case "notification": err = dao.DB.Delete(&model.Notification{}, "id = ?", id).Error @@ -82,9 +83,10 @@ func (ma *memberAPI) delete(c *gin.Context) { } type serverForm struct { - ID uint64 - Name string `binding:"required"` - Secret string + ID uint64 + Name string `binding:"required"` + DisplayIndex int + Secret string } func (ma *memberAPI) addOrEditServer(c *gin.Context) { @@ -97,14 +99,15 @@ func (ma *memberAPI) addOrEditServer(c *gin.Context) { defer dao.ServerLock.Unlock() s.Name = sf.Name s.Secret = sf.Secret + s.DisplayIndex = sf.DisplayIndex s.ID = sf.ID - } - if sf.ID == 0 { - s.Secret = com.MD5(fmt.Sprintf("%s%s%d", time.Now(), sf.Name, admin.ID)) - s.Secret = s.Secret[:10] - err = dao.DB.Create(&s).Error - } else { - err = dao.DB.Save(&s).Error + if sf.ID == 0 { + s.Secret = com.MD5(fmt.Sprintf("%s%s%d", time.Now(), sf.Name, admin.ID)) + s.Secret = s.Secret[:10] + err = dao.DB.Create(&s).Error + } else { + err = dao.DB.Save(&s).Error + } } if err != nil { c.JSON(http.StatusOK, model.Response{ @@ -115,7 +118,8 @@ func (ma *memberAPI) addOrEditServer(c *gin.Context) { } s.Host = &model.Host{} s.State = &model.State{} - dao.ServerList[fmt.Sprintf("%d", s.ID)] = &s + dao.ServerList[s.ID] = &s + dao.ReSortServer() c.JSON(http.StatusOK, model.Response{ Code: http.StatusOK, }) diff --git a/cmd/dashboard/controller/member_page.go b/cmd/dashboard/controller/member_page.go index 8c22b8b..97c0cde 100644 --- a/cmd/dashboard/controller/member_page.go +++ b/cmd/dashboard/controller/member_page.go @@ -32,7 +32,7 @@ func (mp *memberPage) server(c *gin.Context) { defer dao.ServerLock.RUnlock() c.HTML(http.StatusOK, "dashboard/server", mygin.CommonEnvironment(c, gin.H{ "Title": "服务器管理", - "Servers": dao.ServerList, + "Servers": dao.SortedServerList, })) } diff --git a/cmd/dashboard/main.go b/cmd/dashboard/main.go index ac983fc..ea3c50c 100644 --- a/cmd/dashboard/main.go +++ b/cmd/dashboard/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "time" "github.com/patrickmn/go-cache" @@ -17,7 +16,7 @@ import ( func init() { var err error - dao.ServerList = make(map[string]*model.Server) + dao.ServerList = make(map[uint64]*model.Server) dao.Conf = &model.Config{} err = dao.Conf.Read("data/config.yaml") if err != nil { @@ -43,8 +42,9 @@ func initDB() { innerS := s innerS.Host = &model.Host{} innerS.State = &model.State{} - dao.ServerList[fmt.Sprintf("%d", innerS.ID)] = &innerS + dao.ServerList[innerS.ID] = &innerS } + dao.ReSortServer() } func main() { diff --git a/model/server.go b/model/server.go index 8b2dc58..de87d14 100644 --- a/model/server.go +++ b/model/server.go @@ -11,8 +11,9 @@ import ( // Server .. type Server struct { Common - Name string - Secret string `json:"-"` + Name string + DisplayIndex int // 展示权重,越大越靠前 + Secret string `json:"-"` Host *Host `gorm:"-"` State *State `gorm:"-"` diff --git a/resource/static/main.js b/resource/static/main.js index 2aedffb..aa5c653 100644 --- a/resource/static/main.js +++ b/resource/static/main.js @@ -36,7 +36,10 @@ function showFormModal(modelSelector, formID, URL, getData) { form.children('.message').remove() btn.toggleClass('loading') const data = getData ? getData() : $(formID).serializeArray().reduce(function (obj, item) { - obj[item.name] = (item.name.endsWith('_id') || item.name === 'id' || item.name === 'ID' || item.name === 'RequestType' || item.name === 'RequestMethod') ? parseInt(item.value) : item.value; + obj[item.name] = (item.name.endsWith('_id') || + item.name === 'id' || item.name === 'ID' || + item.name === 'RequestType' || item.name === 'RequestMethod' || + item.name === 'DisplayIndex') ? parseInt(item.value) : item.value; return obj; }, {}); $.post(URL, JSON.stringify(data)).done(function (resp) { diff --git a/resource/template/common/footer.html b/resource/template/common/footer.html index 99fe3eb..e5842a1 100644 --- a/resource/template/common/footer.html +++ b/resource/template/common/footer.html @@ -9,7 +9,7 @@ - + diff --git a/resource/template/component/server.html b/resource/template/component/server.html index 14aaeb6..cb22edb 100644 --- a/resource/template/component/server.html +++ b/resource/template/component/server.html @@ -8,6 +8,10 @@ +
+ + +
diff --git a/resource/template/dashboard/server.html b/resource/template/dashboard/server.html index 7077490..74d2c37 100644 --- a/resource/template/dashboard/server.html +++ b/resource/template/dashboard/server.html @@ -13,20 +13,20 @@ - + + - {{range $server := .Servers}} - + + -
ID权重 备注IP 密钥最后活跃 管理
{{$server.ID}}{{$server.DisplayIndex}} {{$server.Name}}{{$server.Host.IP}} {{$server.Secret}}{{$server.LastActive}}