Files
ppgo_job/controller/task.go
T
lmk 610b85e3a5 refactor: 从 Beego 迁移到 GoFrame v2
- 替换 Beego 框架为 GoFrame v2
- 重构项目结构: controller/service/dao/middleware 分层
- 替换自定义 crons 包为 gcron
- 模板从 views/ 迁移到 resource/template/
- 配置从 conf/app.conf 迁移到 config.yml
- 数据库从 MySQL 切换为 SQLite (modernc.org/sqlite)
- 移除 agent/ 远程执行器(待后续迁移)
- 移除 crons/ 自定义定时器包
- 静态资源整理到 resource/static/
2026-07-09 11:06:12 +08:00

556 lines
22 KiB
Go

package controller
import (
"context"
"fmt"
"ppgo_job/consts"
"ppgo_job/dao"
"ppgo_job/model/entity"
"ppgo_job/service/scheduler"
"strings"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gcron"
"github.com/gogf/gf/v2/util/gconv"
)
// ===== 任务管理 =====
func TaskList(r *ghttp.Request) {
ctx := r.GetCtx()
groupId := r.Get("groupId", 0).Int()
groups, _, _ := dao.Group.GetList(ctx, 1, 1000, "status", 1)
taskGroup := make(map[int]string)
for _, g := range groups { taskGroup[g.Id] = g.GroupName }
display(r, "task/list.html", g.Map{"pageTitle":"任务管理","taskGroup":taskGroup,"groupId":groupId,"flash":g.Map{"error":""}})
}
func TaskTable(r *ghttp.Request) {
ctx := r.GetCtx()
page := r.Get("page", 1).Int()
limit := r.Get("limit", 20).Int()
groupId := r.Get("groupId", -1).Int()
status := r.Get("status", -1).Int()
taskName := strings.TrimSpace(r.Get("task_name", "").String())
var filters []interface{}
if groupId >= 0 { filters = append(filters, "group_id", groupId) }
if status >= 0 { filters = append(filters, "status >=", status) }
if taskName != "" { filters = append(filters, "task_name like", "%"+taskName+"%") }
list, total, err := dao.Task.GetList(ctx, page, limit, filters...)
if err != nil { ajaxList(r, "查询失败", consts.MsgErr, 0, nil); return }
rows := make([]g.Map, 0)
for _, t := range list {
st := "运行中"
if t.Status == -1 { st = "已删除" } else if t.Status == 0 { st = "已暂停" } else if t.Status == 2 { st = "待审核" } else if t.Status == 3 { st = "审核失败" }
nt := ""
pt := ""; if t.PrevTime > 0 { pt = time.Unix(t.PrevTime, 0).Format("2006-01-02 15:04:05") }
ct := ""; ut := ""
if t.CreateTime > 0 { ct = time.Unix(t.CreateTime, 0).Format("2006-01-02 15:04:05") }
if t.UpdateTime > 0 { ut = time.Unix(t.UpdateTime, 0).Format("2006-01-02 15:04:05") }
rows = append(rows, g.Map{"id":t.Id,"task_name":t.TaskName,"group_id":t.GroupId,"cron_spec":t.CronSpec,"command":t.Command,"status":t.Status,"status_text":st,"prev_time":t.PrevTime,"prev_time_str":pt,"next_time":nt,"execute_times":t.ExecuteTimes,"server_ids":t.ServerIds,"create_time_str":ct,"update_time_str":ut})
}
ajaxList(r, "成功", 0, total, rows)
}
func TaskAdd(r *ghttp.Request) {
ctx := r.GetCtx()
groups, _, _ := dao.Group.GetList(ctx, 1, 1000, "status", 1)
taskGroup := make(map[int]string)
for _, g := range groups { taskGroup[g.Id] = g.GroupName }
sgl, _, _ := dao.ServerGroup.GetList(ctx, 1, 1000, "status", 1)
sl, _, _ := dao.TaskServer.GetList(ctx, 1, 1000)
serverGroup := make(map[int]g.Map)
for _, sg := range sgl {
servers := make(map[int]string)
for _, s := range sl { if s.GroupId == sg.Id { servers[s.Id] = s.ServerName } }
serverGroup[sg.Id] = g.Map{"GroupName":sg.GroupName,"Servers":servers}
}
al, _, _ := dao.Admin.GetList(ctx, 1, 1000)
adminInfo := make([]g.Map, 0)
for _, a := range al { adminInfo = append(adminInfo, g.Map{"Id":a.Id,"RealName":a.RealName}) }
display(r, "task/add.html", g.Map{"pageTitle":"新增任务","taskGroup":taskGroup,"serverGroup":serverGroup,"adminInfo":adminInfo,"flash":g.Map{"error":""}})
}
func TaskEdit(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
task, _ := dao.Task.GetById(ctx, id)
if task == nil { ajaxMsg(r, "任务不存在", consts.MsgErr); return }
groups, _, _ := dao.Group.GetList(ctx, 1, 1000, "status", 1)
taskGroup := make(map[int]string)
for _, g := range groups { taskGroup[g.Id] = g.GroupName }
sgl, _, _ := dao.ServerGroup.GetList(ctx, 1, 1000, "status", 1)
sl, _, _ := dao.TaskServer.GetList(ctx, 1, 1000)
serverGroup := make(map[int]g.Map)
for _, sg := range sgl {
servers := make(map[int]string)
for _, s := range sl { if s.GroupId == sg.Id { servers[s.Id] = s.ServerName } }
serverGroup[sg.Id] = g.Map{"GroupName": sg.GroupName, "Servers": servers}
}
al, _, _ := dao.Admin.GetList(ctx, 1, 1000)
adminInfo := make([]g.Map, 0)
for _, a := range al { adminInfo = append(adminInfo, g.Map{"Id": a.Id, "RealName": a.RealName}) }
tplList, _, _ := dao.NotifyTpl.GetList(ctx, 1, 1000)
userId := r.GetCtxVar("userId", 0).Int()
isAdmin := 0
if userId == 1 { isAdmin = 1 }
display(r, "task/edit.html", g.Map{"pageTitle":"编辑任务","task":task,"taskGroup":taskGroup,"serverGroup":serverGroup,"adminInfo":adminInfo,"notifyTpl":tplList,"isAdmin":isAdmin,"flash":g.Map{"error":""}})
}
func TaskCopy(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
task, _ := dao.Task.GetById(ctx, id)
if task == nil { ajaxMsg(r, "任务不存在", consts.MsgErr); return }
groups, _, _ := dao.Group.GetList(ctx, 1, 1000, "status", 1)
taskGroup := make(map[int]string)
for _, g := range groups { taskGroup[g.Id] = g.GroupName }
sgl, _, _ := dao.ServerGroup.GetList(ctx, 1, 1000, "status", 1)
sl, _, _ := dao.TaskServer.GetList(ctx, 1, 1000)
serverGroup := make(map[int]g.Map)
for _, sg := range sgl {
servers := make(map[int]string)
for _, s := range sl { if s.GroupId == sg.Id { servers[s.Id] = s.ServerName } }
serverGroup[sg.Id] = g.Map{"GroupName": sg.GroupName, "Servers": servers}
}
al, _, _ := dao.Admin.GetList(ctx, 1, 1000)
adminInfo := make([]g.Map, 0)
for _, a := range al { adminInfo = append(adminInfo, g.Map{"Id": a.Id, "RealName": a.RealName}) }
tplList, _, _ := dao.NotifyTpl.GetList(ctx, 1, 1000)
userId := r.GetCtxVar("userId", 0).Int()
isAdmin := 0
if userId == 1 { isAdmin = 1 }
r.Response.WriteTpl("task/copy.html", g.Map{"pageTitle":"复制任务","task":task,"taskGroup":taskGroup,"serverGroup":serverGroup,"adminInfo":adminInfo,"notifyTpl":tplList,"isAdmin":isAdmin,"flash":g.Map{"error":""}})
}
func TaskDetail(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
task, _ := dao.Task.GetById(ctx, id)
if task == nil { ajaxMsg(r, "任务不存在", consts.MsgErr); return }
st := "运行中"
if task.Status == -1 { st = "已删除" } else if task.Status == 0 { st = "已暂停" } else if task.Status == 2 { st = "待审核" } else if task.Status == 3 { st = "审核失败" }
groupName := ""
if g, _ := dao.Group.GetById(ctx, task.GroupId); g != nil { groupName = g.GroupName }
serverName := "本地服务器"
if task.ServerIds != "" && task.ServerIds != "0" {
if s, _ := dao.TaskServer.GetById(ctx, gconv.Int(task.ServerIds)); s != nil { serverName = s.ServerName }
}
serverType := "同时执行"
if task.ServerType == 1 { serverType = "轮询执行" }
createName := ""; updateName := ""
if a, _ := dao.Admin.GetById(ctx, task.CreateId); a != nil { createName = a.RealName }
if a, _ := dao.Admin.GetById(ctx, task.UpdateId); a != nil { updateName = a.RealName }
notifyTplName := ""
if task.IsNotify == 1 && task.NotifyTplId > 0 {
if t, _ := dao.NotifyTpl.GetById(ctx, task.NotifyTplId); t != nil { notifyTplName = t.TplName }
}
al, _, _ := dao.Admin.GetList(ctx, 1, 1000)
adminInfo := make([]g.Map, 0)
for _, a := range al { adminInfo = append(adminInfo, g.Map{"Id":a.Id,"RealName":a.RealName}) }
ct := ""; ut := ""
if task.CreateTime > 0 { ct = time.Unix(task.CreateTime, 0).Format("2006-01-02 15:04:05") }
if task.UpdateTime > 0 { ut = time.Unix(task.UpdateTime, 0).Format("2006-01-02 15:04:05") }
r.Response.WriteTpl("task/detail.html", g.Map{
"pageTitle":"任务详情","task":task,"TextStatus":st,"GroupName":groupName,"serverName":serverName,
"ServerType":serverType,"NotifyTplName":notifyTplName,"adminInfo":adminInfo,
"CreateName":createName,"UpdateName":updateName,"CreateTime":ct,"UpdateTime":ut,"flash":g.Map{"error":""},
})
}
func TaskAjaxSave(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
taskName := r.Get("task_name").String()
cronSpec := r.Get("cron_spec").String()
command := r.Get("command").String()
groupId := r.Get("group_id", 0).Int()
serverIds := r.Get("server_ids").String()
serverType := r.Get("server_type", 0).Int()
concurrent := r.Get("concurrent", 0).Int()
timeout := r.Get("timeout", 0).Int()
isNotify := r.Get("is_notify", 0).Int()
notifyType := r.Get("notify_type", 0).Int()
notifyTplId := r.Get("notify_tpl_id", 0).Int()
notifyUserIds := r.Get("notify_user_ids").String()
description := r.Get("description").String()
if taskName == "" || cronSpec == "" || command == "" { ajaxMsg(r, "任务名称、Cron表达式、命令不能为空", consts.MsgErr); return }
// 验证 Cron 表达式(临时注册再移除,仅用于校验格式)
if _, err := gcron.AddSingleton(ctx, cronSpec, func(ctx context.Context) {}, "cron_validator"); err != nil {
ajaxMsg(r, "Cron表达式格式错误: "+err.Error(), consts.MsgErr)
return
}
gcron.Remove("cron_validator")
if err := scheduler.Scheduler.CheckCommand(ctx, command); err != nil { ajaxMsg(r, err.Error(), consts.MsgErr); return }
now := time.Now().Unix()
userId := r.GetCtxVar("userId", 0).Int()
if id > 0 {
task, _ := dao.Task.GetById(ctx, id)
if task == nil { ajaxMsg(r, "任务不存在", consts.MsgErr); return }
task.TaskName = taskName; task.Description = description; task.GroupId = groupId
task.ServerIds = serverIds; task.ServerType = serverType; task.CronSpec = cronSpec
task.Concurrent = concurrent; task.Command = command; task.Timeout = timeout
task.IsNotify = isNotify; task.NotifyType = notifyType; task.NotifyTplId = notifyTplId
task.NotifyUserIds = notifyUserIds; task.UpdateId = userId; task.UpdateTime = now
if err := dao.Task.Update(ctx, task); err != nil { ajaxMsg(r, "更新失败: "+err.Error(), consts.MsgErr); return }
if task.Status == 1 { scheduler.Scheduler.UpdateTask(ctx, task) }
ajaxMsg(r, "更新成功", consts.MsgOK)
} else {
status := 2
if userId == 1 { status = 0 }
t := &entity.Task{
TaskName: taskName, Description: description, GroupId: groupId,
ServerIds: serverIds, ServerType: serverType, CronSpec: cronSpec,
Concurrent: concurrent, Command: command, Timeout: timeout,
Status: status, IsNotify: isNotify, NotifyType: notifyType,
NotifyTplId: notifyTplId, NotifyUserIds: notifyUserIds,
CreateId: userId, UpdateId: userId, CreateTime: now, UpdateTime: now,
}
id, err := dao.Task.Insert(ctx, t)
if err != nil { ajaxMsg(r, "添加失败: "+err.Error(), consts.MsgErr); return }
ajaxMsg(r, id, consts.MsgOK)
}
}
func TaskAjaxDel(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
if id <= 0 { ajaxMsg(r, "参数错误", consts.MsgErr); return }
task, _ := dao.Task.GetById(ctx, id)
if task == nil { ajaxMsg(r, "任务不存在", consts.MsgErr); return }
_ = dao.Task.Delete(ctx, id)
if task.Status == 1 { scheduler.Scheduler.RemoveTask(ctx, id) }
ajaxMsg(r, "删除成功", consts.MsgOK)
}
func TaskAjaxStart(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
if id <= 0 { ajaxMsg(r, "参数错误", consts.MsgErr); return }
_ = dao.Task.UpdateFields(ctx, id, g.Map{"status": 1})
task, _ := dao.Task.GetById(ctx, id)
if task != nil { scheduler.Scheduler.AddTask(ctx, task) }
ajaxMsg(r, "启动成功", consts.MsgOK)
}
func TaskAjaxPause(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
if id <= 0 { ajaxMsg(r, "参数错误", consts.MsgErr); return }
_ = dao.Task.UpdateFields(ctx, id, g.Map{"status": 0})
scheduler.Scheduler.RemoveTask(ctx, id)
ajaxMsg(r, "暂停成功", consts.MsgOK)
}
func TaskAjaxRun(r *ghttp.Request) {
id := r.Get("id", 0).Int()
if id <= 0 { ajaxMsg(r, "参数错误", consts.MsgErr); return }
_ = scheduler.Scheduler.RunTaskNow(r.GetCtx(), id)
ajaxMsg(r, "任务已触发执行", consts.MsgOK)
}
func TaskAuditList(r *ghttp.Request) {
display(r, "task/auditlist.html", g.Map{"pageTitle":"任务审核","flash":g.Map{"error":""}})
}
func TaskAjaxAudit(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
if id <= 0 { ajaxMsg(r, "参数错误", consts.MsgErr); return }
_ = dao.Task.UpdateFields(ctx, id, g.Map{"status": 1})
task, _ := dao.Task.GetById(ctx, id)
if task != nil { scheduler.Scheduler.AddTask(ctx, task) }
ajaxMsg(r, "审核通过,任务已启动", consts.MsgOK)
}
func TaskAjaxNopass(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
if id <= 0 { ajaxMsg(r, "参数错误", consts.MsgErr); return }
_ = dao.Task.UpdateFields(ctx, id, g.Map{"status": 3})
ajaxMsg(r, "已设置为审核失败", consts.MsgOK)
}
func TaskAjaxBatchStart(r *ghttp.Request) {
ctx := r.GetCtx()
ids := r.Get("ids").String()
for _, idStr := range strings.Split(ids, ",") {
if id := gconv.Int(strings.TrimSpace(idStr)); id > 0 {
_ = dao.Task.UpdateFields(ctx, id, g.Map{"status": 1})
if t, _ := dao.Task.GetById(ctx, id); t != nil { scheduler.Scheduler.AddTask(ctx, t) }
}
}
ajaxMsg(r, "批量启动成功", consts.MsgOK)
}
func TaskAjaxBatchPause(r *ghttp.Request) {
ctx := r.GetCtx()
ids := r.Get("ids").String()
for _, idStr := range strings.Split(ids, ",") {
if id := gconv.Int(strings.TrimSpace(idStr)); id > 0 {
_ = dao.Task.UpdateFields(ctx, id, g.Map{"status": 0})
scheduler.Scheduler.RemoveTask(ctx, id)
}
}
ajaxMsg(r, "批量暂停成功", consts.MsgOK)
}
func TaskAjaxBatchDel(r *ghttp.Request) {
ctx := r.GetCtx()
ids := r.Get("ids").String()
for _, idStr := range strings.Split(ids, ",") {
if id := gconv.Int(strings.TrimSpace(idStr)); id > 0 {
_ = dao.Task.Delete(ctx, id)
scheduler.Scheduler.RemoveTask(ctx, id)
}
}
ajaxMsg(r, "批量删除成功", consts.MsgOK)
}
func TaskAjaxBatchAudit(r *ghttp.Request) {
ctx := r.GetCtx()
ids := r.Get("ids").String()
for _, idStr := range strings.Split(ids, ",") {
if id := gconv.Int(strings.TrimSpace(idStr)); id > 0 {
_ = dao.Task.UpdateFields(ctx, id, g.Map{"status": 1})
if t, _ := dao.Task.GetById(ctx, id); t != nil { scheduler.Scheduler.AddTask(ctx, t) }
}
}
ajaxMsg(r, "批量审核通过", consts.MsgOK)
}
func TaskAjaxBatchNoPass(r *ghttp.Request) {
ctx := r.GetCtx()
ids := r.Get("ids").String()
for _, idStr := range strings.Split(ids, ",") {
if id := gconv.Int(strings.TrimSpace(idStr)); id > 0 { _ = dao.Task.UpdateFields(ctx, id, g.Map{"status": 3}) }
}
ajaxMsg(r, "批量审核不通过", consts.MsgOK)
}
func TaskApiTask(r *ghttp.Request) { ajaxMsg(r, "API接口待完善", consts.MsgOK) }
func TaskApiStart(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
if id <= 0 { ajaxMsg(r, "参数错误", consts.MsgErr); return }
_ = dao.Task.UpdateFields(ctx, id, g.Map{"status": 1})
if t, _ := dao.Task.GetById(ctx, id); t != nil { scheduler.Scheduler.AddTask(ctx, t) }
ajaxMsg(r, id, consts.MsgOK)
}
func TaskApiPause(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
if id <= 0 { ajaxMsg(r, "参数错误", consts.MsgErr); return }
_ = dao.Task.UpdateFields(ctx, id, g.Map{"status": 0})
scheduler.Scheduler.RemoveTask(ctx, id)
ajaxMsg(r, id, consts.MsgOK)
}
func TaskLogAjaxDel(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
if id <= 0 { ajaxMsg(r, "参数错误", consts.MsgErr); return }
_ = dao.TaskLog.DeleteById(ctx, id)
ajaxMsg(r, "删除成功", consts.MsgOK)
}
// ===== 服务器管理 =====
func ServerList(r *ghttp.Request) {
ctx := r.GetCtx()
groups, _, _ := dao.ServerGroup.GetList(ctx, 1, 1000, "status", 1)
serverGroup := make(map[int]string)
for _, g := range groups { serverGroup[g.Id] = g.GroupName }
display(r, "server/list.html", g.Map{"pageTitle":"服务器列表","serverGroup":serverGroup,"flash":g.Map{"error":""}})
}
func ServerTable(r *ghttp.Request) {
ctx := r.GetCtx()
page := r.Get("page", 1).Int()
limit := r.Get("limit", 20).Int()
groupId := r.Get("groupId", -1).Int()
var filters []interface{}
if groupId >= 0 { filters = append(filters, "group_id", groupId) }
list, total, err := dao.TaskServer.GetList(ctx, page, limit, filters...)
if err != nil { ajaxList(r, "查询失败", consts.MsgErr, 0, nil); return }
rows := make([]g.Map, 0)
for _, s := range list {
statusText := "正常"
if s.Status == 1 { statusText = "异常" } else if s.Status == 2 { statusText = "已禁用" }
connType := "SSH"
if s.ConnectionType == 1 { connType = "Telnet" } else if s.ConnectionType == 2 { connType = "Agent" }
ct := ""; ut := ""
if s.CreateTime > 0 { ct = time.Unix(s.CreateTime, 0).Format("2006-01-02 15:04:05") }
if s.UpdateTime > 0 { ut = time.Unix(s.UpdateTime, 0).Format("2006-01-02 15:04:05") }
rows = append(rows, g.Map{"id":s.Id,"server_name":s.ServerName,"server_ip":s.ServerIp,"port":s.Port,"status":s.Status,"statusText":statusText,"connTypeText":connType,"connection_type":s.ConnectionType,"create_time_str":ct,"update_time_str":ut})
}
ajaxList(r, "成功", 0, total, rows)
}
// ===== 任务分组 =====
func GroupList(r *ghttp.Request) {
display(r, "group/list.html", g.Map{"pageTitle":"任务分组","flash":g.Map{"error":""}})
}
func GroupTable(r *ghttp.Request) {
ctx := r.GetCtx()
page := r.Get("page", 1).Int()
limit := r.Get("limit", 20).Int()
list, total, err := dao.Group.GetList(ctx, page, limit)
if err != nil { ajaxList(r, "查询失败", consts.MsgErr, 0, nil); return }
rows := make([]g.Map, 0)
for _, v := range list {
ct := ""; ut := ""
if v.CreateTime > 0 { ct = time.Unix(v.CreateTime, 0).Format("2006-01-02 15:04:05") }
if v.UpdateTime > 0 { ut = time.Unix(v.UpdateTime, 0).Format("2006-01-02 15:04:05") }
rows = append(rows, g.Map{"id":v.Id,"group_name":v.GroupName,"description":v.Description,"create_time":ct,"update_time":ut,"status":v.Status})
}
ajaxList(r, "成功", 0, total, rows)
}
// ===== 资源分组 =====
func ServerGroupList(r *ghttp.Request) {
display(r, "servergroup/list.html", g.Map{"pageTitle":"资源分组","flash":g.Map{"error":""}})
}
func ServerGroupTable(r *ghttp.Request) {
ctx := r.GetCtx()
page := r.Get("page", 1).Int()
limit := r.Get("limit", 20).Int()
list, total, err := dao.ServerGroup.GetList(ctx, page, limit)
if err != nil { ajaxList(r, "查询失败", consts.MsgErr, 0, nil); return }
rows := make([]g.Map, 0)
for _, v := range list {
ct := ""; ut := ""
if v.CreateTime > 0 { ct = time.Unix(v.CreateTime, 0).Format("2006-01-02 15:04:05") }
if v.UpdateTime > 0 { ut = time.Unix(v.UpdateTime, 0).Format("2006-01-02 15:04:05") }
rows = append(rows, g.Map{"id":v.Id,"group_name":v.GroupName,"description":v.Description,"create_time":ct,"update_time":ut,"status":v.Status})
}
ajaxList(r, "成功", 0, total, rows)
}
// ===== 执行日志 =====
func TaskLogList(r *ghttp.Request) {
taskId := r.Get("task_id", 0).Int()
display(r, "tasklog/list.html", g.Map{"pageTitle":"执行日志","task_id":taskId,"flash":g.Map{"error":""}})
}
func TaskLogTable(r *ghttp.Request) {
ctx := r.GetCtx()
page := r.Get("page", 1).Int()
limit := r.Get("limit", 20).Int()
taskId := r.Get("task_id", -1).Int()
var filters []interface{}
if taskId > 0 { filters = append(filters, "task_id", taskId) }
list, total, err := dao.TaskLog.GetList(ctx, page, limit, filters...)
if err != nil { ajaxList(r, "查询失败", consts.MsgErr, 0, nil); return }
rows := make([]g.Map, 0)
for _, l := range list {
st := "成功"
if l.Status == -1 { st = "失败" } else if l.Status == -2 { st = "超时" }
startTime := ""
if l.CreateTime > 0 {
startTime = time.Unix(l.CreateTime, 0).Format("2006-01-02 15:04:05")
}
outputSize := len(l.Output)
sizeText := fmt.Sprintf("%d B", outputSize)
if outputSize >= 1024 {
sizeText = fmt.Sprintf("%.1f KB", float64(outputSize)/1024)
}
rows = append(rows, g.Map{
"id": l.Id, "task_id": l.TaskId, "server_name": l.ServerName,
"status": l.Status, "statusText": st,
"process_time": l.ProcessTime,
"start_time": startTime,
"output_size": sizeText,
"output": l.Output, "error": l.Error,
})
}
ajaxList(r, "成功", 0, total, rows)
}
// ===== 禁用命令 =====
func BanList(r *ghttp.Request) {
display(r, "ban/list.html", g.Map{"pageTitle":"禁用命令","flash":g.Map{"error":""}})
}
func BanTable(r *ghttp.Request) {
ctx := r.GetCtx()
page := r.Get("page", 1).Int()
limit := r.Get("limit", 20).Int()
list, total, err := dao.Ban.GetList(ctx, page, limit)
if err != nil { ajaxList(r, "查询失败", consts.MsgErr, 0, nil); return }
rows := make([]g.Map, 0)
for _, v := range list {
ct := ""
if v.CreateTime > 0 { ct = time.Unix(v.CreateTime, 0).Format("2006-01-02 15:04:05") }
rows = append(rows, g.Map{"id":v.Id,"code":v.Code,"create_time":ct,"status":v.Status})
}
ajaxList(r, "成功", 0, total, rows)
}
// ===== 权限管理 =====
func AuthIndex(r *ghttp.Request) {
display(r, "auth/list.html", g.Map{"pageTitle":"权限因子","flash":g.Map{"error":""}})
}
func RoleList(r *ghttp.Request) {
display(r, "role/list.html", g.Map{"pageTitle":"角色列表","flash":g.Map{"error":""}})
}
func RoleTable(r *ghttp.Request) {
ctx := r.GetCtx()
page := r.Get("page", 1).Int()
limit := r.Get("limit", 20).Int()
list, total, err := dao.Role.GetList(ctx, page, limit)
if err != nil { ajaxList(r, "查询失败", consts.MsgErr, 0, nil); return }
rows := make([]g.Map, 0)
for _, v := range list {
ct := ""; ut := ""
if v.CreateTime > 0 { ct = time.Unix(v.CreateTime, 0).Format("2006-01-02 15:04:05") }
if v.UpdateTime > 0 { ut = time.Unix(v.UpdateTime, 0).Format("2006-01-02 15:04:05") }
rows = append(rows, g.Map{"id":v.Id,"role_name":v.RoleName,"detail":v.Detail,"create_time":ct,"update_time":ut,"status":v.Status})
}
ajaxList(r, "成功", 0, total, rows)
}
func AdminList(r *ghttp.Request) {
display(r, "admin/list.html", g.Map{"pageTitle":"管理员列表","flash":g.Map{"error":""}})
}
func AdminTable(r *ghttp.Request) {
ctx := r.GetCtx()
page := r.Get("page", 1).Int()
limit := r.Get("limit", 20).Int()
list, total, err := dao.Admin.GetList(ctx, page, limit)
if err != nil { ajaxList(r, "查询失败", consts.MsgErr, 0, nil); return }
rows := make([]g.Map, 0)
for _, a := range list {
statusText := "正常"
if a.Status == 0 { statusText = "禁用" }
ct := ""; ut := ""; lt := ""
if a.CreateTime > 0 { ct = time.Unix(a.CreateTime, 0).Format("2006-01-02 15:04:05") }
if a.UpdateTime > 0 { ut = time.Unix(a.UpdateTime, 0).Format("2006-01-02 15:04:05") }
if a.LastLogin > 0 { lt = time.Unix(a.LastLogin, 0).Format("2006-01-02 15:04:05") }
rows = append(rows, g.Map{"id":a.Id,"login_name":a.LoginName,"real_name":a.RealName,"email":a.Email,"phone":a.Phone,"status":a.Status,"statusText":statusText,"last_login":lt,"create_time_str":ct,"update_time_str":ut})
}
ajaxList(r, "成功", 0, total, rows)
}
func UserEdit(r *ghttp.Request) {
ctx := r.GetCtx()
userId := r.GetCtxVar("userId", 0).Int()
admin, _ := dao.Admin.GetById(ctx, userId)
if admin == nil { ajaxMsg(r, "用户不存在", consts.MsgErr); return }
display(r, "user/edit.html", g.Map{"pageTitle":"资料修改","admin":g.Map{"id":admin.Id,"login_name":admin.LoginName,"real_name":admin.RealName,"phone":admin.Phone,"email":admin.Email,"dingtalk":admin.Dingtalk,"wechat":admin.Wechat},"flash":g.Map{"error":""}})
}