[dashboard 0.3.3] 添加计划任务可搜索服务器

This commit is contained in:
naiba 2021-01-20 22:15:47 +08:00
parent ec17948fe4
commit 7592655c2d
7 changed files with 79 additions and 9 deletions

View File

@ -33,7 +33,7 @@ func (ma *memberAPI) serve() {
Redirect: "/login", Redirect: "/login",
})) }))
mr.POST("/logout", ma.logout) mr.GET("/search-server", ma.searchServer)
mr.POST("/server", ma.addOrEditServer) mr.POST("/server", ma.addOrEditServer)
mr.POST("/monitor", ma.addOrEditMonitor) mr.POST("/monitor", ma.addOrEditMonitor)
mr.POST("/cron", ma.addOrEditCron) mr.POST("/cron", ma.addOrEditCron)
@ -41,6 +41,7 @@ func (ma *memberAPI) serve() {
mr.POST("/alert-rule", ma.addOrEditAlertRule) mr.POST("/alert-rule", ma.addOrEditAlertRule)
mr.POST("/setting", ma.updateSetting) mr.POST("/setting", ma.updateSetting)
mr.DELETE("/:model/:id", ma.delete) mr.DELETE("/:model/:id", ma.delete)
mr.POST("/logout", ma.logout)
} }
func (ma *memberAPI) delete(c *gin.Context) { func (ma *memberAPI) delete(c *gin.Context) {
@ -102,6 +103,33 @@ func (ma *memberAPI) delete(c *gin.Context) {
}) })
} }
type searchResult struct {
Name string `json:"name,omitempty"`
Value uint64 `json:"value,omitempty"`
Text string `json:"text,omitempty"`
}
func (ma *memberAPI) searchServer(c *gin.Context) {
var servers []model.Server
likeWord := "%" + c.Query("word") + "%"
dao.DB.Select("id,name").Where("id = ? OR name LIKE ? OR tag LIKE ? OR note LIKE ?",
c.Query("word"), likeWord, likeWord, likeWord).Find(&servers)
var resp []searchResult
for i := 0; i < len(servers); i++ {
resp = append(resp, searchResult{
Value: servers[i].ID,
Name: servers[i].Name,
Text: servers[i].Name,
})
}
c.JSON(http.StatusOK, map[string]interface{}{
"success": true,
"results": resp,
})
}
type serverForm struct { type serverForm struct {
ID uint64 ID uint64
Name string `binding:"required"` Name string `binding:"required"`

View File

@ -36,10 +36,22 @@ function showFormModal(modelSelector, formID, URL, getData) {
form.children('.message').remove() form.children('.message').remove()
btn.toggleClass('loading') btn.toggleClass('loading')
const data = getData ? getData() : $(formID).serializeArray().reduce(function (obj, item) { const data = getData ? getData() : $(formID).serializeArray().reduce(function (obj, item) {
obj[item.name] = (item.name.endsWith('_id') || // ID 类的数据
if ((item.name.endsWith('_id') ||
item.name === 'id' || item.name === 'ID' || item.name === 'id' || item.name === 'ID' ||
item.name === 'RequestType' || item.name === 'RequestMethod' || item.name === 'RequestType' || item.name === 'RequestMethod' ||
item.name === 'DisplayIndex' || item.name === 'Type') ? parseInt(item.value) : item.value; item.name === 'DisplayIndex' || item.name === 'Type')) {
obj[item.name] = parseInt(item.value);
} else {
obj[item.name] = item.value;
}
if (item.name == 'ServersRaw') {
if (item.value.length > 2) {
obj[item.name] = '[' + item.value.substr(3, item.value.length - 1) + ']'
}
}
return obj; return obj;
}, {}); }, {});
$.post(URL, JSON.stringify(data)).done(function (resp) { $.post(URL, JSON.stringify(data)).done(function (resp) {
@ -108,6 +120,7 @@ function addOrEditServer(server) {
modal.find('input[name=name]').val(server ? server.Name : null) modal.find('input[name=name]').val(server ? server.Name : null)
modal.find('input[name=Tag]').val(server ? server.Tag : null) modal.find('input[name=Tag]').val(server ? server.Tag : null)
modal.find('input[name=DisplayIndex]').val(server ? server.DisplayIndex : null) modal.find('input[name=DisplayIndex]').val(server ? server.DisplayIndex : null)
modal.find('textarea[name=Note]').val(server ? server.Note : null)
if (server) { if (server) {
modal.find('.secret.field').attr('style', '') modal.find('.secret.field').attr('style', '')
modal.find('input[name=secret]').val(server.Secret) modal.find('input[name=secret]').val(server.Secret)
@ -136,7 +149,16 @@ function addOrEditCron(cron) {
modal.find('input[name=ID]').val(cron ? cron.ID : null) modal.find('input[name=ID]').val(cron ? cron.ID : null)
modal.find('input[name=Name]').val(cron ? cron.Name : null) modal.find('input[name=Name]').val(cron ? cron.Name : null)
modal.find('input[name=Scheduler]').val(cron ? cron.Scheduler : null) modal.find('input[name=Scheduler]').val(cron ? cron.Scheduler : null)
modal.find('input[name=ServersRaw]').val(cron ? cron.ServersRaw : '[]') var servers
if (cron) {
servers = cron.ServersRaw
serverList = JSON.parse(servers)
const node = modal.find('i.dropdown.icon')
for (let i = 0; i < serverList.length; i++) {
node.after('<a class="ui label transition visible" data-value="' + serverList[i] + '" style="display: inline-block !important;">ID:' + serverList[i] + '<i class="delete icon"></i></a>')
}
}
modal.find('input[name=ServersRaw]').val(cron ? '[],' + servers.substr(1, servers.length - 2) : '[]')
modal.find('textarea[name=Command]').val(cron ? cron.Command : null) modal.find('textarea[name=Command]').val(cron ? cron.Command : null)
if (cron && cron.PushSuccessful) { if (cron && cron.PushSuccessful) {
modal.find('.ui.push-successful.checkbox').checkbox('set checked') modal.find('.ui.push-successful.checkbox').checkbox('set checked')
@ -197,3 +219,16 @@ function logout(id) {
}); });
}) })
} }
$(document).ready(() => {
try {
$('.ui.servers.search.dropdown').dropdown({
clearable: true,
apiSettings: {
url: '/api/search-server?word={query}',
cache: false,
},
})
} catch (error) {
}
})

View File

@ -8,7 +8,7 @@
<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.1/dist/semantic.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.1/dist/semantic.min.js"></script>
<script src="/static/semantic-ui-alerts.min.js"></script> <script src="/static/semantic-ui-alerts.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.min.js"></script>
<script src="/static/main.js?v202101190958"></script> <script src="/static/main.js?v202101202159"></script>
</body> </body>
</html> </html>

View File

@ -18,7 +18,12 @@
</div> </div>
<div class="field"> <div class="field">
<label>执行服务器ID列表</label> <label>执行服务器ID列表</label>
<input type="text" name="ServersRaw" placeholder="[10,7,19]"> <div class="ui fluid multiple servers search selection dropdown">
<input type="hidden" name="ServersRaw">
<i class="dropdown icon"></i>
<div class="default text">输入ID/备注以搜索</div>
<div class="menu"></div>
</div>
</div> </div>
<div class="field"> <div class="field">
<div class="ui push-successful checkbox"> <div class="ui push-successful checkbox">
@ -30,7 +35,8 @@
<div class="ui warning message"> <div class="ui warning message">
<p> <p>
计划的格式为:<code>* * * * *</code> 分 时 天 月 星期,详情见 <a 计划的格式为:<code>* * * * *</code> 分 时 天 月 星期,详情见 <a
href="https://pkg.go.dev/github.com/robfig/cron/v3#hdr-CRON_Expression_Format" target="_blank">计划表达式格式</a><br> href="https://pkg.go.dev/github.com/robfig/cron/v3#hdr-CRON_Expression_Format"
target="_blank">计划表达式格式</a><br>
命令Shell 命令,就像写脚本一样就可以,如果遇到 xxx 命令找不到,可能是 <code>PATH</code> 环境变量的问题,<code>source ~/.bashrc</code> 命令Shell 命令,就像写脚本一样就可以,如果遇到 xxx 命令找不到,可能是 <code>PATH</code> 环境变量的问题,<code>source ~/.bashrc</code>
或者使用绝对路径执行。 或者使用绝对路径执行。
</p> </p>

View File

@ -42,7 +42,7 @@
</button> </button>
<button class="ui button" <button class="ui button"
onclick="showConfirm('删除计划任务','确认删除此计划任务?',deleteRequest,'/api/cron/'+{{$cron.ID}})"> onclick="showConfirm('删除计划任务','确认删除此计划任务?',deleteRequest,'/api/cron/'+{{$cron.ID}})">
<i class="delete icon"></i> <i class="trash alternate outline icon"></i>
</button> </button>
</div> </div>
</td> </td>

View File

@ -15,6 +15,7 @@
<div class="field"> <div class="field">
<select name="Theme"> <select name="Theme">
<option value="default"{{if eq .Conf.Site.Theme "default"}} selected="selected"{{end}}>默认主题</option> <option value="default"{{if eq .Conf.Site.Theme "default"}} selected="selected"{{end}}>默认主题</option>
<option value="daynight"{{if eq .Conf.Site.Theme "daynight"}} selected="selected"{{end}}>JackieSung DayNight</option>
<option value="hotaru"{{if eq .Conf.Site.Theme "hotaru"}} selected="selected"{{end}}>CokeMine Hotaru</option> <option value="hotaru"{{if eq .Conf.Site.Theme "hotaru"}} selected="selected"{{end}}>CokeMine Hotaru</option>
</select> </select>
</div> </div>

View File

@ -34,7 +34,7 @@ var CronLock sync.RWMutex
var Crons map[uint64]*model.Cron var Crons map[uint64]*model.Cron
var Cron *cron.Cron var Cron *cron.Cron
var Version = "v0.3.2" var Version = "v0.3.3"
func ReSortServer() { func ReSortServer() {
ServerLock.RLock() ServerLock.RLock()