修改删除节点

This commit is contained in:
奶爸 2020-03-22 20:55:27 +08:00
parent 9b4817a7d8
commit 6ff9f6fc75
4 changed files with 80 additions and 7 deletions

View File

@ -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))

View File

@ -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 ? '修改<i class="edit icon"></i>' : '添加<i class="add icon"></i>')
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) {

View File

@ -3,13 +3,18 @@
<div class="header">添加服务器</div>
<div class="content">
<form id="serverForm" class="ui form">
<input type="hidden" name="id">
<div class="field">
<label>备注</label>
<input type="text" name="name" placeholder="爱因斯坦-光速1号">
</div>
<div class="secret field">
<label>密钥</label>
<input type="text" name="secret">
</div>
</form>
</div>
<div class="actions">
<div class=" actions">
<div class="ui negative button">取消</div>
<button class="ui positive right labeled icon button">绑定<i class="checkmark icon"></i>
</button>

View File

@ -3,8 +3,13 @@
{{template "common/menu" .}}
<div class="nb-container">
<div class="ui container">
<button class="ui right labeled positive icon button" onclick="addOrEditServer()"><i class="add icon"></i> 添加服务器
</button>
<div class="ui grid">
<div class="right floated right aligned twelve wide column">
<button class="ui right labeled positive icon button" onclick="addOrEditServer()"><i
class="add icon"></i> 添加服务器
</button>
</div>
</div>
<table class="ui very basic table">
<thead>
<tr>
@ -25,7 +30,8 @@
<button class="ui button" onclick="addOrEditServer({{$server}})">
<i class="edit icon"></i>
</button>
<button class="ui button">
<button class="ui button"
onclick="showConfirm('删除节点','确认删除此监控节点?',deleteRequest,'/api/server/'+{{$server.ID}})">
<i class="delete icon"></i>
</button>
</div>