518 lines
13 KiB
Go
518 lines
13 KiB
Go
package controller
|
|
|
|
import (
|
|
"ppgo_job/dao"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
)
|
|
|
|
// ===== 用户信息 =====
|
|
|
|
// ApiUserInfo 返回当前用户信息
|
|
func ApiUserInfo(r *ghttp.Request) {
|
|
userId := r.GetCtxVar("userId", 0).Int()
|
|
userName := r.GetCtxVar("loginUserName", "").String()
|
|
sideMenu1 := r.GetCtxVar("SideMenu1", nil).Slice()
|
|
sideMenu2 := r.GetCtxVar("SideMenu2", nil).Slice()
|
|
|
|
jsonRes(r, 0, "ok", g.Map{
|
|
"user": g.Map{
|
|
"id": userId,
|
|
"realName": userName,
|
|
},
|
|
"menus": buildMenuTree(sideMenu1, sideMenu2),
|
|
})
|
|
}
|
|
|
|
// ApiMenus 返回菜单树
|
|
func ApiMenus(r *ghttp.Request) {
|
|
sideMenu1 := r.GetCtxVar("SideMenu1", nil).Slice()
|
|
sideMenu2 := r.GetCtxVar("SideMenu2", nil).Slice()
|
|
jsonRes(r, 0, "ok", buildMenuTree(sideMenu1, sideMenu2))
|
|
}
|
|
|
|
func buildMenuTree(menu1, menu2 []interface{}) []g.Map {
|
|
tree := make([]g.Map, 0)
|
|
for _, m1 := range menu1 {
|
|
item := m1.(g.Map)
|
|
children := make([]g.Map, 0)
|
|
for _, m2 := range menu2 {
|
|
sub := m2.(g.Map)
|
|
if sub["Pid"] == item["Id"] {
|
|
children = append(children, g.Map{
|
|
"id": sub["Id"],
|
|
"name": sub["AuthName"],
|
|
"path": sub["AuthUrl"],
|
|
"icon": sub["Icon"],
|
|
})
|
|
}
|
|
}
|
|
tree = append(tree, g.Map{
|
|
"id": item["Id"],
|
|
"name": item["AuthName"],
|
|
"icon": item["Icon"],
|
|
"children": children,
|
|
})
|
|
}
|
|
return tree
|
|
}
|
|
|
|
// ===== 任务管理 API =====
|
|
|
|
// ApiTaskAddData 返回新增任务需要的关联数据
|
|
func ApiTaskAddData(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([]g.Map, 0)
|
|
for _, sg := range sgl {
|
|
servers := make([]g.Map, 0)
|
|
for _, s := range sl {
|
|
if s.GroupId == sg.Id {
|
|
servers = append(servers, g.Map{"id": s.Id, "name": s.ServerName})
|
|
}
|
|
}
|
|
serverGroup = append(serverGroup, g.Map{
|
|
"groupName": sg.GroupName,
|
|
"servers": servers,
|
|
})
|
|
}
|
|
|
|
al, _, _ := dao.Admin.GetList(ctx, 1, 1000)
|
|
adminList := make([]g.Map, 0)
|
|
for _, a := range al {
|
|
adminList = append(adminList, g.Map{"id": a.Id, "realName": a.RealName})
|
|
}
|
|
|
|
tplList, _, _ := dao.NotifyTpl.GetList(ctx, 1, 1000)
|
|
|
|
jsonRes(r, 0, "ok", g.Map{
|
|
"taskGroup": taskGroup,
|
|
"serverGroup": serverGroup,
|
|
"adminList": adminList,
|
|
"notifyTpls": tplList,
|
|
})
|
|
}
|
|
|
|
// ApiTaskEditData 返回编辑任务需要的关联数据
|
|
func ApiTaskEditData(r *ghttp.Request) {
|
|
ctx := r.GetCtx()
|
|
id := r.Get("id", 0).Int()
|
|
|
|
task, _ := dao.Task.GetById(ctx, id)
|
|
if task == nil {
|
|
jsonError(r, "任务不存在")
|
|
return
|
|
}
|
|
|
|
// 获取关联数据(复用新增的数据)
|
|
// 通过内部方式获取 addData
|
|
addData := getTaskAddData(r)
|
|
|
|
al, _, _ := dao.Admin.GetList(ctx, 1, 1000)
|
|
adminList := make([]g.Map, 0)
|
|
for _, a := range al {
|
|
adminList = append(adminList, 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")
|
|
}
|
|
|
|
jsonRes(r, 0, "ok", g.Map{
|
|
"task": task,
|
|
"createTime": ct,
|
|
"updateTime": ut,
|
|
"taskGroup": addData["taskGroup"],
|
|
"serverGroup": addData["serverGroup"],
|
|
"adminList": adminList,
|
|
"notifyTpls": addData["notifyTpls"],
|
|
})
|
|
}
|
|
|
|
// getTaskAddData 获取任务关联数据(内部复用)
|
|
func getTaskAddData(r *ghttp.Request) map[string]interface{} {
|
|
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([]g.Map, 0)
|
|
for _, sg := range sgl {
|
|
servers := make([]g.Map, 0)
|
|
for _, s := range sl {
|
|
if s.GroupId == sg.Id {
|
|
servers = append(servers, g.Map{"id": s.Id, "name": s.ServerName})
|
|
}
|
|
}
|
|
serverGroup = append(serverGroup, g.Map{"groupName": sg.GroupName, "servers": servers})
|
|
}
|
|
|
|
tplList, _, _ := dao.NotifyTpl.GetList(ctx, 1, 1000)
|
|
|
|
return map[string]interface{}{
|
|
"taskGroup": taskGroup,
|
|
"serverGroup": serverGroup,
|
|
"notifyTpls": tplList,
|
|
}
|
|
}
|
|
|
|
// ApiTaskDetailData 返回任务详情数据
|
|
func ApiTaskDetailData(r *ghttp.Request) {
|
|
ctx := r.GetCtx()
|
|
id := r.Get("id", 0).Int()
|
|
|
|
task, _ := dao.Task.GetById(ctx, id)
|
|
if task == nil {
|
|
jsonError(r, "任务不存在")
|
|
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
|
|
}
|
|
|
|
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")
|
|
}
|
|
|
|
jsonRes(r, 0, "ok", g.Map{
|
|
"task": task,
|
|
"statusText": st,
|
|
"groupName": groupName,
|
|
"createTime": ct,
|
|
"updateTime": ut,
|
|
})
|
|
}
|
|
|
|
// ===== 服务器管理 API =====
|
|
|
|
// ApiServerAddData 返回新增服务器的关联数据
|
|
func ApiServerAddData(r *ghttp.Request) {
|
|
ctx := r.GetCtx()
|
|
groups, _, _ := dao.ServerGroup.GetList(ctx, 1, 1000, "status", 1)
|
|
jsonRes(r, 0, "ok", g.Map{"serverGroup": groups})
|
|
}
|
|
|
|
// ApiServerEditData 返回编辑服务器的关联数据
|
|
func ApiServerEditData(r *ghttp.Request) {
|
|
ctx := r.GetCtx()
|
|
id := r.Get("id", 0).Int()
|
|
server, _ := dao.TaskServer.GetById(ctx, id)
|
|
if server == nil {
|
|
jsonError(r, "服务器不存在")
|
|
return
|
|
}
|
|
groups, _, _ := dao.ServerGroup.GetList(ctx, 1, 1000, "status", 1)
|
|
ct := ""
|
|
ut := ""
|
|
if server.CreateTime > 0 {
|
|
ct = time.Unix(server.CreateTime, 0).Format("2006-01-02 15:04:05")
|
|
}
|
|
if server.UpdateTime > 0 {
|
|
ut = time.Unix(server.UpdateTime, 0).Format("2006-01-02 15:04:05")
|
|
}
|
|
jsonRes(r, 0, "ok", g.Map{
|
|
"server": server,
|
|
"serverGroup": groups,
|
|
"createTime": ct,
|
|
"updateTime": ut,
|
|
})
|
|
}
|
|
|
|
// ===== 分组管理 API =====
|
|
|
|
// ApiGroupAddData 返回新增任务分组的关联数据
|
|
func ApiGroupAddData(r *ghttp.Request) {
|
|
jsonRes(r, 0, "ok", g.Map{})
|
|
}
|
|
|
|
// ApiGroupEditData 返回编辑任务分组的关联数据
|
|
func ApiGroupEditData(r *ghttp.Request) {
|
|
ctx := r.GetCtx()
|
|
id := r.Get("id", 0).Int()
|
|
group, _ := dao.Group.GetById(ctx, id)
|
|
if group == nil {
|
|
jsonError(r, "分组不存在")
|
|
return
|
|
}
|
|
ct := ""
|
|
ut := ""
|
|
if group.CreateTime > 0 {
|
|
ct = time.Unix(group.CreateTime, 0).Format("2006-01-02 15:04:05")
|
|
}
|
|
if group.UpdateTime > 0 {
|
|
ut = time.Unix(group.UpdateTime, 0).Format("2006-01-02 15:04:05")
|
|
}
|
|
jsonRes(r, 0, "ok", g.Map{
|
|
"group": group,
|
|
"createTime": ct,
|
|
"updateTime": ut,
|
|
})
|
|
}
|
|
|
|
// ===== 日志管理 API =====
|
|
|
|
// ApiTaskLogDetailData 返回日志详情
|
|
func ApiTaskLogDetailData(r *ghttp.Request) {
|
|
ctx := r.GetCtx()
|
|
id := r.Get("id", 0).Int()
|
|
log, _ := dao.TaskLog.GetById(ctx, id)
|
|
if log == nil {
|
|
jsonError(r, "日志不存在")
|
|
return
|
|
}
|
|
startTime := ""
|
|
if log.CreateTime > 0 {
|
|
startTime = time.Unix(log.CreateTime, 0).Format("2006-01-02 15:04:05")
|
|
}
|
|
// 加载关联任务信息
|
|
taskInfo := g.Map{}
|
|
if task, _ := dao.Task.GetById(ctx, log.TaskId); task != nil {
|
|
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
|
|
}
|
|
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")
|
|
}
|
|
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
|
|
}
|
|
|
|
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})
|
|
}
|
|
|
|
notifyTplName := ""
|
|
if task.IsNotify == 1 && task.NotifyTplId > 0 {
|
|
if t, _ := dao.NotifyTpl.GetById(ctx, task.NotifyTplId); t != nil {
|
|
notifyTplName = t.TplName
|
|
}
|
|
}
|
|
serverName := "本地服务器"
|
|
if task.ServerIds != "" && task.ServerIds != "0" {
|
|
if sid := gconv.Int(task.ServerIds); sid > 0 {
|
|
if s, _ := dao.TaskServer.GetById(ctx, sid); s != nil {
|
|
serverName = s.ServerName
|
|
}
|
|
}
|
|
}
|
|
|
|
taskInfo = g.Map{
|
|
"taskName": task.TaskName, "statusText": st, "executeTimes": task.ExecuteTimes,
|
|
"groupName": groupName, "serverName": serverName, "serverType": task.ServerType,
|
|
"concurrent": task.Concurrent, "cronSpec": task.CronSpec, "command": task.Command,
|
|
"timeout": task.Timeout, "isNotify": task.IsNotify, "notifyType": task.NotifyType,
|
|
"notifyTplName": notifyTplName, "adminInfo": adminInfo,
|
|
"createTime": ct, "updateTime": ut, "createName": createName, "updateName": updateName,
|
|
}
|
|
}
|
|
|
|
jsonRes(r, 0, "ok", g.Map{
|
|
"log": log,
|
|
"startTime": startTime,
|
|
"task": taskInfo,
|
|
})
|
|
}
|
|
|
|
// ===== 通知模板 API =====
|
|
|
|
// ApiNotifyTplAddData 返回新增通知模板的关联数据
|
|
func ApiNotifyTplAddData(r *ghttp.Request) {
|
|
jsonRes(r, 0, "ok", g.Map{})
|
|
}
|
|
|
|
// ApiNotifyTplEditData 返回编辑通知模板的关联数据
|
|
func ApiNotifyTplEditData(r *ghttp.Request) {
|
|
ctx := r.GetCtx()
|
|
id := r.Get("id", 0).Int()
|
|
tpl, _ := dao.NotifyTpl.GetById(ctx, id)
|
|
if tpl == nil {
|
|
jsonError(r, "模板不存在")
|
|
return
|
|
}
|
|
ct := ""
|
|
ut := ""
|
|
if tpl.CreateTime > 0 {
|
|
ct = time.Unix(tpl.CreateTime, 0).Format("2006-01-02 15:04:05")
|
|
}
|
|
if tpl.UpdateTime > 0 {
|
|
ut = time.Unix(tpl.UpdateTime, 0).Format("2006-01-02 15:04:05")
|
|
}
|
|
jsonRes(r, 0, "ok", g.Map{
|
|
"tpl": tpl,
|
|
"createTime": ct,
|
|
"updateTime": ut,
|
|
})
|
|
}
|
|
|
|
// ===== 角色管理 API =====
|
|
|
|
// ApiRoleAddData 返回新增角色的关联数据
|
|
func ApiRoleAddData(r *ghttp.Request) {
|
|
ctx := r.GetCtx()
|
|
authList, _ := dao.Auth.GetList(ctx)
|
|
sgl, _, _ := dao.ServerGroup.GetList(ctx, 1, 1000, "status", 1)
|
|
tgl, _, _ := dao.Group.GetList(ctx, 1, 1000, "status", 1)
|
|
jsonRes(r, 0, "ok", g.Map{
|
|
"authList": authList,
|
|
"serverGroup": sgl,
|
|
"taskGroup": tgl,
|
|
})
|
|
}
|
|
|
|
// ApiRoleEditData 返回编辑角色的关联数据
|
|
func ApiRoleEditData(r *ghttp.Request) {
|
|
ctx := r.GetCtx()
|
|
id := r.Get("id", 0).Int()
|
|
role, _ := dao.Role.GetById(ctx, id)
|
|
if role == nil {
|
|
jsonError(r, "角色不存在")
|
|
return
|
|
}
|
|
authList, _ := dao.Auth.GetList(ctx)
|
|
sgl, _, _ := dao.ServerGroup.GetList(ctx, 1, 1000, "status", 1)
|
|
tgl, _, _ := dao.Group.GetList(ctx, 1, 1000, "status", 1)
|
|
jsonRes(r, 0, "ok", g.Map{
|
|
"role": role,
|
|
"authList": authList,
|
|
"serverGroup": sgl,
|
|
"taskGroup": tgl,
|
|
})
|
|
}
|
|
|
|
// ===== 管理员管理 API =====
|
|
|
|
// ApiAdminAddData 返回新增管理员的关联数据
|
|
func ApiAdminAddData(r *ghttp.Request) {
|
|
ctx := r.GetCtx()
|
|
roles, _, _ := dao.Role.GetList(ctx, 1, 1000, "status", 1)
|
|
jsonRes(r, 0, "ok", g.Map{"roleList": roles})
|
|
}
|
|
|
|
// ApiAdminEditData 返回编辑管理员的关联数据
|
|
func ApiAdminEditData(r *ghttp.Request) {
|
|
ctx := r.GetCtx()
|
|
id := r.Get("id", 0).Int()
|
|
admin, _ := dao.Admin.GetById(ctx, id)
|
|
if admin == nil {
|
|
jsonError(r, "管理员不存在")
|
|
return
|
|
}
|
|
roles, _, _ := dao.Role.GetList(ctx, 1, 1000, "status", 1)
|
|
jsonRes(r, 0, "ok", g.Map{
|
|
"admin": admin,
|
|
"roleList": roles,
|
|
})
|
|
}
|
|
|
|
// ===== 用户资料 API =====
|
|
|
|
// ApiUserEditData 返回当前用户资料
|
|
func ApiUserEditData(r *ghttp.Request) {
|
|
ctx := r.GetCtx()
|
|
userId := r.GetCtxVar("userId", 0).Int()
|
|
admin, _ := dao.Admin.GetById(ctx, userId)
|
|
if admin == nil {
|
|
jsonError(r, "用户不存在")
|
|
return
|
|
}
|
|
jsonRes(r, 0, "ok", g.Map{
|
|
"admin": g.Map{
|
|
"id": admin.Id,
|
|
"loginName": admin.LoginName,
|
|
"realName": admin.RealName,
|
|
"phone": admin.Phone,
|
|
"email": admin.Email,
|
|
"dingtalk": admin.Dingtalk,
|
|
"wechat": admin.Wechat,
|
|
},
|
|
})
|
|
}
|
|
|
|
// ===== 权限管理 API =====
|
|
|
|
// ApiAuthGetNodes 返回权限树(包装为统一格式)
|
|
func ApiAuthGetNodes(r *ghttp.Request) {
|
|
ctx := r.GetCtx()
|
|
list, _ := dao.Auth.GetList(ctx)
|
|
type node struct {
|
|
Id int `json:"id"`
|
|
Name string `json:"name"`
|
|
Pid int `json:"pId"`
|
|
Open bool `json:"open"`
|
|
AuthUrl string `json:"authUrl"`
|
|
Icon string `json:"icon"`
|
|
Sort int `json:"sort"`
|
|
IsShow int `json:"isShow"`
|
|
}
|
|
nodes := make([]node, 0)
|
|
for _, a := range list {
|
|
if a.Status == 1 {
|
|
nodes = append(nodes, node{Id: a.Id, Name: a.AuthName, Pid: a.Pid, Open: true, AuthUrl: a.AuthUrl, Icon: a.Icon, Sort: a.Sort, IsShow: a.IsShow})
|
|
}
|
|
}
|
|
if nodes == nil {
|
|
nodes = []node{}
|
|
}
|
|
jsonRes(r, 0, "ok", nodes)
|
|
}
|