diff --git a/cmd/dashboard/controller/member_api.go b/cmd/dashboard/controller/member_api.go index 8ce949c..3af88b6 100644 --- a/cmd/dashboard/controller/member_api.go +++ b/cmd/dashboard/controller/member_api.go @@ -3,6 +3,7 @@ package controller import ( "fmt" "net/http" + "strconv" "time" "github.com/gin-gonic/gin" @@ -29,11 +30,37 @@ func (ma *memberAPI) serve() { mr.POST("/logout", ma.logout) mr.POST("/server", ma.addOrEditServer) + mr.DELETE("/server/:id", ma.delete) +} + +func (ma *memberAPI) delete(c *gin.Context) { + id, _ := strconv.ParseUint(c.Param("id"), 10, 64) + if id < 1 { + c.JSON(http.StatusOK, model.Response{ + Code: http.StatusBadRequest, + Message: "错误的 Server ID", + }) + return + } + dao.ServerLock.Lock() + defer dao.ServerLock.Unlock() + if err := dao.DB.Delete(&model.Server{}, "id = ?", id).Error; err != nil { + c.JSON(http.StatusOK, model.Response{ + Code: http.StatusBadRequest, + Message: fmt.Sprintf("数据库错误:%s", err), + }) + return + } + delete(dao.ServerList, strconv.FormatUint(id, 10)) + c.JSON(http.StatusOK, model.Response{ + Code: http.StatusOK, + }) } type serverForm struct { - ID uint64 - Name string `binding:"required"` + ID uint64 + Name string `binding:"required"` + Secret string } func (ma *memberAPI) addOrEditServer(c *gin.Context) { @@ -45,6 +72,8 @@ func (ma *memberAPI) addOrEditServer(c *gin.Context) { dao.ServerLock.Lock() defer dao.ServerLock.Unlock() s.Name = sf.Name + s.Secret = sf.Secret + s.ID = sf.ID } if sf.ID == 0 { s.Secret = com.MD5(fmt.Sprintf("%s%s%d", time.Now(), sf.Name, admin.ID)) diff --git a/resource/static/main.js b/resource/static/main.js index 2c9a446..8435d92 100644 --- a/resource/static/main.js +++ b/resource/static/main.js @@ -70,10 +70,43 @@ function showFormModal(modelSelector, formID, URL, getData) { }).modal('show') } -function addServer() { +function addOrEditServer(server) { + const modal = $('.server.modal') + modal.children('.header').text((server ? '修改' : '添加') + '服务器') + modal.find('.positive.button').html(server ? '修改' : '添加') + modal.find('input[name=id]').val(server ? server.ID : null) + modal.find('input[name=name]').val(server ? server.Name : null) + if (server) { + modal.find('.secret.field').attr('style', '') + modal.find('input[name=secret]').val(server.Secret) + } else { + modal.find('.secret.field').attr('style', 'display:none') + modal.find('input[name=secret]').val('') + } showFormModal('.server.modal', '#serverForm', '/api/server') } +function deleteRequest(api) { + $.ajax({ + url: api, + type: 'DELETE', + }).done(resp => { + if (resp.code == 200) { + if (resp.message) { + alert(resp.message) + } else { + alert('删除成功') + } + window.location.reload() + } else { + alert('删除失败 ' + resp.code + ':' + resp.message) + confirmBtn.toggleClass('loading') + } + }).fail(err => { + alert('网络错误:' + err.responseText) + }); +} + function logout(id) { $.post('/api/logout', JSON.stringify({ id: id })).done(function (resp) { if (resp.code == 200) { diff --git a/resource/template/component/server.html b/resource/template/component/server.html index ae31be9..80e080b 100644 --- a/resource/template/component/server.html +++ b/resource/template/component/server.html @@ -3,13 +3,18 @@