From 1b18e7103e3760d58d65f319e4aa3815600e4a6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=B6=E7=88=B8?= Date: Sun, 8 Dec 2019 23:18:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9C=8D=E5=8A=A1=E5=99=A8?= =?UTF-8?q?=20&=20=E5=B1=95=E7=A4=BA=E6=9C=8D=E5=8A=A1=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/dashboard/controller/common_page.go | 5 +++- cmd/dashboard/controller/controller.go | 4 +++ cmd/dashboard/controller/member_api.go | 28 ++++++++++++++++++ cmd/dashboard/controller/member_page.go | 14 +++++++++ cmd/dashboard/main.go | 9 ++++++ model/server.go | 8 ++++++ resource/static/main.css | 38 +++---------------------- resource/static/main.js | 7 +++-- resource/template/common/footer.html | 5 ++++ resource/template/common/menu.html | 3 ++ resource/template/component/server.html | 18 ++++++++++++ resource/template/page/home.html | 18 +++++++++++- resource/template/page/server.html | 30 +++++++++++++++++++ 13 files changed, 149 insertions(+), 38 deletions(-) create mode 100644 model/server.go create mode 100644 resource/template/component/server.html create mode 100644 resource/template/page/server.html diff --git a/cmd/dashboard/controller/common_page.go b/cmd/dashboard/controller/common_page.go index 767657e..90ec2a2 100644 --- a/cmd/dashboard/controller/common_page.go +++ b/cmd/dashboard/controller/common_page.go @@ -26,7 +26,10 @@ func (cp *commonPage) home(c *gin.Context) { if ok && isLogin.(bool) { admin = dao.Admin } + var servers []model.Server + dao.DB.Find(&servers) c.HTML(http.StatusOK, "page/home", mygin.CommonEnvironment(c, gin.H{ - "Admin": admin, + "Admin": admin, + "Servers": servers, })) } diff --git a/cmd/dashboard/controller/controller.go b/cmd/dashboard/controller/controller.go index 7c373da..58e4cc2 100644 --- a/cmd/dashboard/controller/controller.go +++ b/cmd/dashboard/controller/controller.go @@ -13,6 +13,10 @@ import ( // ServeWeb .. func ServeWeb() { + gin.SetMode(gin.ReleaseMode) + if dao.Conf.Debug { + gin.SetMode(gin.DebugMode) + } r := gin.Default() r.Use(mygin.RecordPath) r.SetFuncMap(template.FuncMap{ diff --git a/cmd/dashboard/controller/member_api.go b/cmd/dashboard/controller/member_api.go index c720251..c1ecd43 100644 --- a/cmd/dashboard/controller/member_api.go +++ b/cmd/dashboard/controller/member_api.go @@ -6,6 +6,7 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/naiba/com" "github.com/p14yground/nezha/model" "github.com/p14yground/nezha/pkg/mygin" @@ -27,6 +28,33 @@ func (ma *memberAPI) serve() { })) mr.POST("/logout", ma.logout) + mr.POST("/server", ma.addServer) +} + +type serverForm struct { + Name string `binding:"required"` +} + +func (ma *memberAPI) addServer(c *gin.Context) { + var sf serverForm + err := c.ShouldBindJSON(&sf) + if err == nil { + var s model.Server + s.Name = sf.Name + s.Secret = com.MD5(fmt.Sprintf("%s%s%d", time.Now(), sf.Name, dao.Admin.ID)) + s.Secret = s.Secret[:10] + err = dao.DB.Create(&s).Error + } + if err != nil { + c.JSON(http.StatusOK, model.Response{ + Code: http.StatusBadRequest, + Message: fmt.Sprintf("请求错误:%s", err), + }) + return + } + c.JSON(http.StatusOK, model.Response{ + Code: http.StatusOK, + }) } type logoutForm struct { diff --git a/cmd/dashboard/controller/member_page.go b/cmd/dashboard/controller/member_page.go index 39061cf..fdf9f00 100644 --- a/cmd/dashboard/controller/member_page.go +++ b/cmd/dashboard/controller/member_page.go @@ -1,8 +1,12 @@ package controller import ( + "net/http" + "github.com/gin-gonic/gin" + "github.com/p14yground/nezha/model" "github.com/p14yground/nezha/pkg/mygin" + "github.com/p14yground/nezha/service/dao" ) type memberPage struct { @@ -18,4 +22,14 @@ func (mp *memberPage) serve() { Btn: "点此登录", Redirect: "/login", })) + mr.GET("/server", mp.server) +} + +func (mp *memberPage) server(c *gin.Context) { + var servers []model.Server + dao.DB.Find(&servers) + c.HTML(http.StatusOK, "page/server", mygin.CommonEnvironment(c, gin.H{ + "Title": "服务器管理", + "Servers": servers, + })) } diff --git a/cmd/dashboard/main.go b/cmd/dashboard/main.go index 736a26e..4b276de 100644 --- a/cmd/dashboard/main.go +++ b/cmd/dashboard/main.go @@ -26,7 +26,16 @@ func init() { if err != nil { panic(err) } + if dao.Conf.Debug { + dao.DB = dao.DB.Debug() + } dao.Cache = cache.New(5*time.Minute, 10*time.Minute) + + initDB() +} + +func initDB() { + dao.DB.AutoMigrate(model.Server{}) } func main() { diff --git a/model/server.go b/model/server.go new file mode 100644 index 0000000..685f23d --- /dev/null +++ b/model/server.go @@ -0,0 +1,8 @@ +package model + +// Server .. +type Server struct { + Common + Name string + Secret string +} diff --git a/resource/static/main.css b/resource/static/main.css index 7f4576a..3b430ee 100644 --- a/resource/static/main.css +++ b/resource/static/main.css @@ -20,38 +20,8 @@ margin-top: 75px; } -.avatar-list img { - width: 2.6rem !important; - height: 2.6rem !important; - background-color: white; -} - -.card-list { - width: 100% !important; - padding-right: unset !important; -} - -.card-list .card { - width: calc(33.33333333% - 1.1em) !important; - margin-right: unset !important; - margin-top: unset !important; - -} - -.card-list .card:nth-child(1) { - margin-top: 0.875em !important; -} - -.card-list .card:nth-child(2) { - margin-top: 0.875em !important; -} - -.card-list .card:nth-child(3) { - margin-top: 0.875em !important; -} - -.grid-list>.grid>.row { - border-bottom: solid gainsboro 2px; - padding-bottom: .6em; - margin-bottom: .6em; +.footer.segment { + margin-top: 40px !important; + padding-top: 40px; + padding-bottom: 40px; } \ No newline at end of file diff --git a/resource/static/main.js b/resource/static/main.js index d30138e..b330e3b 100644 --- a/resource/static/main.js +++ b/resource/static/main.js @@ -1,5 +1,4 @@ $('.ui.checkbox').checkbox(); -$('.ui.dropdown').dropdown(); const confirmBtn = $('.mini.confirm.modal .positive.button') function showConfirm(title, content, callFn, extData) { @@ -32,7 +31,7 @@ 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 === 'permission') ? parseInt(item.value) : item.value; + obj[item.name] = (item.name.endsWith('_id') || item.name === 'id') ? parseInt(item.value) : item.value; return obj; }, {}); $.post(URL, JSON.stringify(data)).done(function (resp) { @@ -54,6 +53,10 @@ function showFormModal(modelSelector, formID, URL, getData) { }).modal('show') } +function addServer() { + showFormModal('.server.modal', '#serverForm', '/api/server') +} + function logout(id) { $.post('/api/logout', JSON.stringify({ id: id })).done(function (resp) { if (resp.code == 200) { diff --git a/resource/template/common/footer.html b/resource/template/common/footer.html index f30fbe1..538feb0 100644 --- a/resource/template/common/footer.html +++ b/resource/template/common/footer.html @@ -1,4 +1,9 @@ {{define "common/footer"}} + diff --git a/resource/template/common/menu.html b/resource/template/common/menu.html index 0f3ee3b..c520d08 100644 --- a/resource/template/common/menu.html +++ b/resource/template/common/menu.html @@ -5,6 +5,9 @@ 首页 + {{if .Admin}} + 服务器 + {{end}}