332 lines
7.9 KiB
Go
332 lines
7.9 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 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 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()
|
|
dingtalk := r.Get("dingtalk").String()
|
|
wechat := r.Get("wechat").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
|
|
a.Dingtalk = dingtalk
|
|
a.Wechat = wechat
|
|
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,
|
|
Dingtalk: dingtalk, Wechat: wechat,
|
|
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)
|
|
}
|