Files
ppgo_job/controller/system.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

309 lines
9.4 KiB
Go

package controller
import (
"ppgo_job/consts"
"ppgo_job/dao"
"ppgo_job/libs"
"ppgo_job/model/entity"
"strings"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/util/gconv"
)
// ==================== 权限因子 ====================
func AuthAjaxSave(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
authName := r.Get("auth_name").String()
authUrl := r.Get("auth_url").String()
pid := r.Get("pid", 1).Int()
sort := r.Get("sort", 0).Int()
icon := r.Get("icon").String()
isShow := r.Get("is_show", 1).Int()
if authName == "" { ajaxMsg(r, "权限名称不能为空", consts.MsgErr); return }
now := time.Now().Unix()
if id > 0 {
a, _ := dao.Auth.GetById(ctx, id)
if a == nil { ajaxMsg(r, "权限不存在", consts.MsgErr); return }
a.AuthName = authName; a.AuthUrl = authUrl; a.Pid = pid
a.Sort = sort; a.Icon = icon; a.IsShow = isShow; a.UpdateTime = now
_ = dao.Auth.Update(ctx, a)
ajaxMsg(r, "更新成功", consts.MsgOK)
} else {
_, err := dao.Auth.Insert(ctx, &entity.Auth{
AuthName: authName, AuthUrl: authUrl, Pid: pid,
Sort: sort, Icon: icon, IsShow: isShow,
CreateTime: now, UpdateTime: now, Status: 1,
})
if err != nil { ajaxMsg(r, "添加失败: "+err.Error(), consts.MsgErr); return }
ajaxMsg(r, "添加成功", consts.MsgOK)
}
}
func AuthAjaxDel(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
if id <= 0 { ajaxMsg(r, "参数错误", consts.MsgErr); return }
a, _ := dao.Auth.GetById(ctx, id)
if a == nil { ajaxMsg(r, "权限不存在", consts.MsgErr); return }
a.Status = 0; a.UpdateTime = time.Now().Unix()
_ = dao.Auth.Update(ctx, a)
ajaxMsg(r, "删除成功", consts.MsgOK)
}
func AuthGetNodes(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"`
}
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,
})
}
}
if nodes == nil {
nodes = []node{}
}
r.Response.WriteJson(nodes)
}
// AuthGetNode 获取单个权限节点
func AuthGetNode(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
if id <= 0 {
r.Response.WriteJson(g.Map{"status": -1, "message": "参数错误"})
return
}
a, _ := dao.Auth.GetById(ctx, id)
if a == nil {
r.Response.WriteJson(g.Map{"status": -1, "message": "权限不存在"})
return
}
r.Response.WriteJson(g.Map{"status": 0, "message": a})
}
// ==================== 角色管理 ====================
func RoleAdd(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)
taskGroup := make(map[int]string)
for _, g := range tgl { taskGroup[g.Id] = g.GroupName }
display(r, "role/add.html", g.Map{
"pageTitle": "新增角色",
"authList": authList,
"serverGroup": sgl,
"taskGroup": taskGroup,
"flash": g.Map{"error": ""},
})
}
func RoleEdit(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
role, _ := dao.Role.GetById(ctx, id)
if role == nil { ajaxMsg(r, "角色不存在", consts.MsgErr); return }
sgl, _, _ := dao.ServerGroup.GetList(ctx, 1, 1000, "status", 1)
tgl, _, _ := dao.Group.GetList(ctx, 1, 1000, "status", 1)
taskGroup := make(map[int]string)
for _, g := range tgl { taskGroup[g.Id] = g.GroupName }
authList, _ := dao.Auth.GetList(ctx)
display(r, "role/edit.html", g.Map{
"pageTitle": "编辑角色",
"role": gconv.Map(role),
"authList": authList,
"serverGroup": sgl,
"taskGroup": taskGroup,
"flash": g.Map{"error": ""},
})
}
func RoleAjaxSave(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
roleName := r.Get("role_name").String()
detail := r.Get("detail").String()
authIdsStr := r.Get("auth_ids").String()
serverGroupIds := r.Get("server_group_ids").String()
taskGroupIds := r.Get("task_group_ids").String()
if roleName == "" { ajaxMsg(r, "角色名称不能为空", consts.MsgErr); return }
now := time.Now().Unix()
if id > 0 {
role, _ := dao.Role.GetById(ctx, id)
if role == nil { ajaxMsg(r, "角色不存在", consts.MsgErr); return }
role.RoleName = roleName; role.Detail = detail
role.ServerGroupIds = serverGroupIds; role.TaskGroupIds = taskGroupIds
role.UpdateTime = now
_ = dao.Role.Update(ctx, role)
// 更新权限关联
if authIdsStr != "" {
authIds := strings.Split(authIdsStr, ",")
intIds := make([]int, 0)
for _, v := range authIds {
if aid := gconv.Int(v); aid > 0 { intIds = append(intIds, aid) }
}
_ = dao.RoleAuth.MultiAdd(ctx, id, intIds)
}
ajaxMsg(r, "更新成功", consts.MsgOK)
} else {
newId, err := dao.Role.Insert(ctx, &entity.Role{
RoleName: roleName, Detail: detail,
ServerGroupIds: serverGroupIds, TaskGroupIds: taskGroupIds,
CreateTime: now, UpdateTime: now, Status: 1,
})
if err != nil { ajaxMsg(r, "添加失败: "+err.Error(), consts.MsgErr); return }
if authIdsStr != "" {
authIds := strings.Split(authIdsStr, ",")
intIds := make([]int, 0)
for _, v := range authIds {
if aid := gconv.Int(v); aid > 0 { intIds = append(intIds, aid) }
}
_ = dao.RoleAuth.BatchAdd(ctx, int(newId), intIds)
}
ajaxMsg(r, "添加成功", consts.MsgOK)
}
}
func RoleAjaxDel(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
if id <= 0 { ajaxMsg(r, "参数错误", consts.MsgErr); return }
rl, _ := dao.Role.GetById(ctx, id)
if rl == nil { ajaxMsg(r, "角色不存在", consts.MsgErr); return }
rl.Status = 0; rl.UpdateTime = time.Now().Unix()
_ = dao.Role.Update(ctx, rl)
_ = dao.RoleAuth.DeleteByRoleId(ctx, id)
ajaxMsg(r, "删除成功", consts.MsgOK)
}
// ==================== 管理员管理 ====================
func AdminAdd(r *ghttp.Request) {
ctx := r.GetCtx()
roles, _, _ := dao.Role.GetList(ctx, 1, 1000, "status", 1)
display(r, "admin/add.html", g.Map{
"pageTitle": "新增管理员",
"roleList": roles,
"flash": g.Map{"error": ""},
})
}
func AdminEdit(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
admin, _ := dao.Admin.GetById(ctx, id)
if admin == nil { ajaxMsg(r, "管理员不存在", consts.MsgErr); return }
roles, _, _ := dao.Role.GetList(ctx, 1, 1000, "status", 1)
display(r, "admin/edit.html", g.Map{
"pageTitle": "编辑管理员",
"admin": gconv.Map(admin),
"roleList": roles,
"flash": g.Map{"error": ""},
})
}
func AdminAjaxSave(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
loginName := r.Get("login_name").String()
realName := r.Get("real_name").String()
password := r.Get("password").String()
roleIds := r.Get("role_ids").String()
phone := r.Get("phone").String()
email := r.Get("email").String()
if loginName == "" { ajaxMsg(r, "登录名不能为空", consts.MsgErr); return }
now := time.Now().Unix()
if id > 0 {
a, _ := dao.Admin.GetById(ctx, id)
if a == nil { ajaxMsg(r, "管理员不存在", consts.MsgErr); return }
a.RealName = realName; a.RoleIds = roleIds
a.Phone = phone; a.Email = email; a.UpdateTime = now
if password != "" {
pwd, salt := libs.HashPassword(password)
a.Password = pwd; a.Salt = salt
}
_ = dao.Admin.Update(ctx, a)
ajaxMsg(r, "更新成功", consts.MsgOK)
} else {
pwd, salt := libs.HashPassword("123456")
if password != "" {
pwd, salt = libs.HashPassword(password)
}
_, err := dao.Admin.Insert(ctx, &entity.Admin{
LoginName: loginName, RealName: realName,
Password: pwd, Salt: salt, RoleIds: roleIds,
Phone: phone, Email: email,
CreateTime: now, UpdateTime: now, Status: 1,
})
if err != nil { ajaxMsg(r, "添加失败: "+err.Error(), consts.MsgErr); return }
ajaxMsg(r, "添加成功", consts.MsgOK)
}
}
func AdminAjaxDel(r *ghttp.Request) {
ctx := r.GetCtx()
id := r.Get("id", 0).Int()
if id <= 0 { ajaxMsg(r, "参数错误", consts.MsgErr); return }
a, _ := dao.Admin.GetById(ctx, id)
if a == nil { ajaxMsg(r, "管理员不存在", consts.MsgErr); return }
if a.Id == 1 { ajaxMsg(r, "不能禁用超级管理员", consts.MsgErr); return }
newStatus := 0
if a.Status == 0 { newStatus = 1 }
_ = dao.Admin.UpdateFields(ctx, id, g.Map{"status": newStatus, "update_time": time.Now().Unix()})
ajaxMsg(r, "操作成功", consts.MsgOK)
}
// ==================== 用户资料 ====================
func UserAjaxSave(r *ghttp.Request) {
ctx := r.GetCtx()
userId := r.GetCtxVar("userId", 0).Int()
if userId <= 0 { ajaxMsg(r, "未登录", consts.MsgErr); return }
realName := r.Get("real_name").String()
phone := r.Get("phone").String()
email := r.Get("email").String()
dingtalk := r.Get("dingtalk").String()
wechat := r.Get("wechat").String()
oldPwd := r.Get("old_password").String()
newPwd := r.Get("new_password").String()
admin, _ := dao.Admin.GetById(ctx, userId)
if admin == nil { ajaxMsg(r, "用户不存在", consts.MsgErr); return }
now := time.Now().Unix()
admin.RealName = realName; admin.Phone = phone; admin.Email = email
admin.Dingtalk = dingtalk; admin.Wechat = wechat; admin.UpdateTime = now
if newPwd != "" {
if !libs.VerifyPassword(oldPwd, admin.Password, admin.Salt) {
ajaxMsg(r, "旧密码错误", consts.MsgErr); return
}
pwd, salt := libs.HashPassword(newPwd)
admin.Password = pwd
admin.Salt = salt
}
_ = dao.Admin.Update(ctx, admin)
ajaxMsg(r, "保存成功", consts.MsgOK)
}