Files
2026-07-10 11:28:16 +08:00

387 lines
9.5 KiB
Go

package controller
import (
"time"
"ppgo_job/consts"
"ppgo_job/dao"
"ppgo_job/model/entity"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
// ==================== 服务器管理 ====================
func ServerAjaxSave(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
groupId := r.Get("group_id", 0).Int()
serverName := r.Get("server_name").String()
serverIp := r.Get("server_ip").String()
port := r.Get("port", 22).Int()
serverAccount := r.Get("server_account").String()
password := r.Get("password").String()
connType := r.Get("connection_type", 0).Int()
authType := r.Get("type", 0).Int()
privateKeySrc := r.Get("private_key_src").String()
detail := r.Get("detail").String()
if serverName == "" || serverIp == "" || serverAccount == "" {
ajaxMsg(r, "服务器名称、IP、账号不能为空", consts.MsgErr)
return
}
now := time.Now().Unix()
if id > 0 {
srv, _ := dao.TaskServer.GetById(ctx, id)
if srv == nil {
ajaxMsg(r, "服务器不存在", consts.MsgErr)
return
}
if password != "" {
srv.Password = password
}
if privateKeySrc != "" {
srv.PrivateKeySrc = privateKeySrc
}
srv.GroupId = groupId
srv.ServerName = serverName
srv.ServerIp = serverIp
srv.Port = port
srv.ServerAccount = serverAccount
srv.ConnectionType = connType
srv.Type = authType
srv.Detail = detail
srv.UpdateTime = now
_ = dao.TaskServer.Update(ctx, srv)
ajaxMsg(r, "更新成功", consts.MsgOK)
} else {
_, err := dao.TaskServer.Insert(ctx, &entity.TaskServer{
GroupId: groupId, ServerName: serverName, ServerIp: serverIp,
Port: port, ServerAccount: serverAccount, Password: password,
ConnectionType: connType, Type: authType,
PrivateKeySrc: privateKeySrc, Detail: detail,
CreateTime: now, UpdateTime: now, Status: 0,
})
if err != nil {
ajaxMsg(r, "添加失败: "+err.Error(), consts.MsgErr)
return
}
ajaxMsg(r, "添加成功", consts.MsgOK)
}
}
// ServerAjaxDel 删除服务器
func ServerAjaxDel(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
if id <= 0 {
ajaxMsg(r, "参数错误", consts.MsgErr)
return
}
_ = dao.TaskServer.Delete(ctx, id)
ajaxMsg(r, "删除成功", consts.MsgOK)
}
// ServerAjaxTest 测试服务器连接
func ServerAjaxTest(r *ghttp.Request) {
// 暂不实现实际测试,返回成功
ajaxMsg(r, "连接测试功能暂未实现", consts.MsgOK)
}
// ServerGetByGroupId 按分组获取服务器列表
func ServerGetByGroupId(r *ghttp.Request) {
ctx := r.GetCtx()
groupId := r.Get("groupId", 0).Int()
list, _, _ := dao.TaskServer.GetList(ctx, 1, 1000, "group_id", groupId)
r.Response.WriteJson(list)
}
// ServerApiSave Agent注册/更新API
func ServerApiSave(r *ghttp.Request) {
ajaxMsg(r, "Agent API 待完善", consts.MsgOK)
}
// ServerApiStatus Agent更新状态API
func ServerApiStatus(r *ghttp.Request) {
ajaxMsg(r, "Agent API 待完善", consts.MsgOK)
}
// ServerApiGet Agent获取信息API
func ServerApiGet(r *ghttp.Request) {
r.Response.WriteJson(g.Map{"status": 0, "message": "ok"})
}
// ==================== 任务分组 ====================
// GroupAjaxSave 保存任务分组
func GroupAjaxSave(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
groupName := r.Get("group_name").String()
description := r.Get("description").String()
if groupName == "" {
ajaxMsg(r, "分组名称不能为空", consts.MsgErr)
return
}
now := time.Now().Unix()
if id > 0 {
g, _ := dao.Group.GetById(ctx, id)
if g == nil {
ajaxMsg(r, "分组不存在", consts.MsgErr)
return
}
g.GroupName = groupName
g.Description = description
g.UpdateTime = now
_ = dao.Group.Update(ctx, g)
ajaxMsg(r, "更新成功", consts.MsgOK)
} else {
_, err := dao.Group.Insert(ctx, &entity.Group{
GroupName: groupName, Description: description,
CreateTime: now, UpdateTime: now, Status: 1,
})
if err != nil {
ajaxMsg(r, "添加失败: "+err.Error(), consts.MsgErr)
return
}
ajaxMsg(r, "添加成功", consts.MsgOK)
}
}
// GroupAjaxDel 删除任务分组
func GroupAjaxDel(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
if id <= 0 {
ajaxMsg(r, "参数错误", consts.MsgErr)
return
}
_ = dao.Task.ResetGroupId(ctx, id)
_ = dao.Group.Delete(ctx, id)
ajaxMsg(r, "删除成功", consts.MsgOK)
}
// ==================== 资源分组 ====================
func ServerGroupAjaxSave(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
groupName := r.Get("group_name").String()
description := r.Get("description").String()
if groupName == "" {
ajaxMsg(r, "分组名称不能为空", consts.MsgErr)
return
}
now := time.Now().Unix()
if id > 0 {
sg, _ := dao.ServerGroup.GetById(ctx, id)
if sg == nil {
ajaxMsg(r, "分组不存在", consts.MsgErr)
return
}
sg.GroupName = groupName
sg.Description = description
sg.UpdateTime = now
_ = dao.ServerGroup.Update(ctx, sg)
ajaxMsg(r, "更新成功", consts.MsgOK)
} else {
_, err := dao.ServerGroup.Insert(ctx, &entity.ServerGroup{
GroupName: groupName, Description: description,
CreateTime: now, UpdateTime: now, Status: 1,
})
if err != nil {
ajaxMsg(r, "添加失败: "+err.Error(), consts.MsgErr)
return
}
ajaxMsg(r, "添加成功", consts.MsgOK)
}
}
func ServerGroupAjaxDel(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
if id <= 0 {
ajaxMsg(r, "参数错误", consts.MsgErr)
return
}
_ = dao.ServerGroup.Delete(ctx, id)
ajaxMsg(r, "删除成功", consts.MsgOK)
}
// ==================== 禁用命令 ====================
func BanAjaxSave(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
code := r.Get("code").String()
if code == "" {
ajaxMsg(r, "命令关键字不能为空", consts.MsgErr)
return
}
now := time.Now().Unix()
if id > 0 {
b, _ := dao.Ban.GetById(ctx, id)
if b == nil {
ajaxMsg(r, "记录不存在", consts.MsgErr)
return
}
b.Code = code
b.UpdateTime = now
_ = dao.Ban.Update(ctx, b)
ajaxMsg(r, "更新成功", consts.MsgOK)
} else {
_, err := dao.Ban.Insert(ctx, &entity.Ban{Code: code, CreateTime: now, UpdateTime: now, Status: 1})
if err != nil {
ajaxMsg(r, "添加失败: "+err.Error(), consts.MsgErr)
return
}
ajaxMsg(r, "添加成功", consts.MsgOK)
}
}
func BanAjaxDel(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
if id <= 0 {
ajaxMsg(r, "参数错误", consts.MsgErr)
return
}
_ = dao.Ban.Delete(ctx, id)
ajaxMsg(r, "删除成功", consts.MsgOK)
}
// ==================== 通知模板 ====================
func NotifyTplTable(r *ghttp.Request) {
ctx := r.GetCtx()
page := r.Get("page", 1).Int()
limit := r.Get("limit", 20).Int()
var filters []interface{}
if sortStr := parseSort(r); sortStr != "" {
filters = append(filters, "@order", sortStr)
}
list, total, err := dao.NotifyTpl.GetList(ctx, page, limit, filters...)
if err != nil {
ajaxList(r, "查询失败", consts.MsgErr, 0, nil)
return
}
rows := make([]g.Map, 0)
for _, v := range list {
tplTypeText := ""
switch v.TplType {
case consts.NotifyTypeEmail:
tplTypeText = "邮件"
case consts.NotifyTypeSMS:
tplTypeText = "短信"
case consts.NotifyTypeDingtalk:
tplTypeText = "钉钉"
case consts.NotifyTypeWechat:
tplTypeText = "微信"
}
statusText := "正常"
if v.Status == 0 {
statusText = "禁用"
}
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, "type": v.Type, "tpl_name": v.TplName, "tpl_type_text": tplTypeText, "create_time": ct, "update_time": ut, "status_text": statusText})
}
ajaxList(r, "成功", 0, total, rows)
}
func NotifyTplAjaxSave(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
tplName := r.Get("tpl_name").String()
tplType := r.Get("tpl_type", 0).Int()
title := r.Get("title").String()
content := r.Get("content").String()
tp := r.Get("type").String()
if tp == "" {
tp = "default"
}
if tplName == "" {
ajaxMsg(r, "模板名称不能为空", consts.MsgErr)
return
}
now := time.Now().Unix()
if id > 0 {
t, _ := dao.NotifyTpl.GetById(ctx, id)
if t == nil {
ajaxMsg(r, "模板不存在", consts.MsgErr)
return
}
t.TplName = tplName
t.TplType = tplType
t.Title = title
t.Content = content
t.Type = tp
t.UpdateTime = now
_ = dao.NotifyTpl.Update(ctx, t)
ajaxMsg(r, "更新成功", consts.MsgOK)
} else {
_, err := dao.NotifyTpl.Insert(ctx, &entity.NotifyTpl{
TplName: tplName, TplType: tplType, Title: title,
Content: content, Type: tp,
CreateTime: now, UpdateTime: now, Status: 1,
})
if err != nil {
ajaxMsg(r, "添加失败: "+err.Error(), consts.MsgErr)
return
}
ajaxMsg(r, "添加成功", consts.MsgOK)
}
}
func NotifyTplAjaxDel(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
if id <= 0 {
ajaxMsg(r, "参数错误", consts.MsgErr)
return
}
_ = dao.NotifyTpl.Delete(ctx, id)
ajaxMsg(r, "删除成功", consts.MsgOK)
}
func NotifyTplAjaxByType(r *ghttp.Request) {
ctx := r.GetCtx()
notifyType := r.Get("notify_type", 0).Int()
tpl, _ := dao.NotifyTpl.GetByTplType(ctx, notifyType, "system")
if tpl == nil {
tpl, _ = dao.NotifyTpl.GetByTplType(ctx, notifyType, "default")
}
if tpl == nil {
r.Response.WriteJson(g.Map{
"code": 0,
"data": make([]g.Map, 0),
})
return
}
r.Response.WriteJson(g.Map{
"code": 0,
"data": []g.Map{
{
"id": tpl.Id,
"tpl_name": tpl.TplName,
"title": tpl.Title,
"content": tpl.Content,
},
},
})
}