fix 代码生成bug,完善错误处理,修改数据权限返回值

This commit is contained in:
yxh 2025-05-22 16:41:01 +08:00
parent aa7aec1b60
commit c6efe923db
8 changed files with 72 additions and 39 deletions

View File

@ -9,6 +9,7 @@ package sysAuthRule
import (
"context"
"errors"
"fmt"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gerror"
@ -99,7 +100,7 @@ func (s *sSysAuthRule) GetIsButtonList(ctx context.Context) ([]*model.SysAuthRul
}
var gList = make([]*model.SysAuthRuleInfoRes, 0, len(list))
for _, v := range list {
if v.MenuType == 2 {
if v.MenuType == 1 || v.MenuType == 2 {
gList = append(gList, v)
}
}
@ -287,6 +288,9 @@ func (s *sSysAuthRule) DeleteMenuByIds(ctx context.Context, ids []int) (err erro
ids = append(ids, childrenIds...)
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
return g.Try(ctx, func(ctx context.Context) {
if len(ids) == 0 {
liberr.ErrIsNil(ctx, errors.New("请选择需要删除的菜单"))
}
_, err = dao.SysAuthRule.Ctx(ctx).Where("id in (?)", ids).Delete()
liberr.ErrIsNil(ctx, err, "删除失败")
//删除权限

View File

@ -10,11 +10,12 @@ package sysUser
import (
"context"
"fmt"
"reflect"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/encoding/gurl"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/tiger1103/gfast/v3/library/libWebsocket"
"reflect"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/database/gdb"
@ -111,8 +112,7 @@ func (s *sSysUser) GetUserByPhone(ctx context.Context, phone string) (user *mode
// GetUserById 通过用户名获取用户信息
func (s *sSysUser) GetUserById(ctx context.Context, id uint64) (user *model.LoginUserRes, err error) {
err = g.Try(ctx, func(ctx context.Context) {
user = &model.LoginUserRes{}
err = dao.SysUser.Ctx(ctx).Fields(user).WherePri(id).Scan(user)
err = dao.SysUser.Ctx(ctx).Fields(user).WherePri(id).Scan(&user)
liberr.ErrIsNil(ctx, err, "获取用户信息失败")
})
return
@ -475,12 +475,15 @@ func (s *sSysUser) List(ctx context.Context, req *system.UserSearchReq) (total i
}
//判断权限,普通管理只能按数据权限查看
if !s.AccessRule(ctx, req.UserInfo.Id, "api/v1/system/user/all") {
m = s.GetAuthDeptWhere(
where := s.GetAuthDeptWhere(
ctx,
m,
req.UserInfo,
"sys_user", "dept_id", "id",
).WhereNotIn(dao.SysUser.Columns().Id, s.NotCheckAuthAdminIds(ctx).Slice())
)
if len(where) > 0 {
m = m.Where(where)
}
m = m.WhereNotIn(dao.SysUser.Columns().Id, s.NotCheckAuthAdminIds(ctx).Slice())
}
if req.PageSize == 0 {
req.PageSize = consts.PageSize
@ -992,30 +995,30 @@ func (s *sSysUser) GetDataWhere(ctx context.Context, userInfo *model.ContextUser
}
// GetAuthWhere 获取数据权限判断条件-按创建人id获取(created_by),数据权限会跟随创建人转移
func (s *sSysUser) GetAuthWhere(ctx context.Context, m *gdb.Model, userInfo *model.ContextUser, field ...string) *gdb.Model {
func (s *sSysUser) GetAuthWhere(ctx context.Context, userInfo *model.ContextUser, field ...string) g.Map {
var (
//当前请求api接口对应的菜单
url = gstr.TrimLeft(ghttp.RequestFromCtx(ctx).Request.URL.Path, "/")
menuId uint
err error
nm *gdb.Model
nm g.Map
)
//获取菜单ID
menuId, err = service.SysAuthRule().GetIdByName(ctx, url)
if err != nil {
g.Log().Error(ctx, err)
return m
return nil
}
nm, err = s.GetAuthDataWhere(ctx, m, userInfo, menuId, field...)
nm, err = s.GetAuthDataWhere(ctx, userInfo, menuId, field...)
if err != nil {
g.Log().Error(ctx, err)
return m
return nil
}
return nm
}
// GetAuthDataWhere 获取数据权限判断条件-按创建用户
func (s *sSysUser) GetAuthDataWhere(ctx context.Context, m *gdb.Model, userInfo *model.ContextUser, menuId uint, field ...string) (nm *gdb.Model, err error) {
func (s *sSysUser) GetAuthDataWhere(ctx context.Context, userInfo *model.ContextUser, menuId uint, field ...string) (where g.Map, err error) {
whereJustMe := g.Map{} //本人数据权限
createdUserField := "created_by"
//表别名
@ -1081,42 +1084,39 @@ func (s *sSysUser) GetAuthDataWhere(ctx context.Context, m *gdb.Model, userInfo
}
endLoop:
if deptIdArr.Size() > 0 {
nm = m.WhereIn(createdUserField, dao.SysUser.Ctx(ctx).Fields(dao.SysUser.Columns().Id).
WhereIn(dao.SysUser.Columns().DeptId, deptIdArr.Slice()))
where = g.Map{createdUserField + " IN(select id from sys_user where dept_id in(?))": deptIdArr.Slice()}
} else if len(whereJustMe) > 0 {
nm = m.Where(whereJustMe)
} else {
nm = m
where = whereJustMe
}
})
return
}
// GetAuthDeptWhere 获取部门数据权限判断条件-按部门id获取(dept_id),数据权限依赖部门,不会随创建人转移
func (s *sSysUser) GetAuthDeptWhere(ctx context.Context, m *gdb.Model, userInfo *model.ContextUser, field ...string) *gdb.Model {
func (s *sSysUser) GetAuthDeptWhere(ctx context.Context, userInfo *model.ContextUser, field ...string) g.Map {
var (
//当前请求api接口对应的菜单
url = gstr.TrimLeft(ghttp.RequestFromCtx(ctx).Request.URL.Path, "/")
menuId uint
err error
nm *gdb.Model
nm g.Map
)
//获取菜单ID
menuId, err = service.SysAuthRule().GetIdByName(ctx, url)
if err != nil {
g.Log().Error(ctx, err)
return m
return nil
}
nm, err = s.GetAuthDeptDataWhere(ctx, m, userInfo, menuId, field...)
nm, err = s.GetAuthDeptDataWhere(ctx, userInfo, menuId, field...)
if err != nil {
g.Log().Error(ctx, err)
return m
return nil
}
return nm
}
// GetAuthDeptDataWhere 获取数据权限判断条件-按部门
func (s *sSysUser) GetAuthDeptDataWhere(ctx context.Context, m *gdb.Model, userInfo *model.ContextUser, menuId uint, field ...string) (nm *gdb.Model, err error) {
func (s *sSysUser) GetAuthDeptDataWhere(ctx context.Context, userInfo *model.ContextUser, menuId uint, field ...string) (data g.Map, err error) {
whereJustMe := g.Map{} //本人数据权限
deptField := "dept_id"
createdUserField := "created_by"
@ -1188,11 +1188,9 @@ func (s *sSysUser) GetAuthDeptDataWhere(ctx context.Context, m *gdb.Model, userI
}
endLoop:
if deptIdArr.Size() > 0 {
nm = m.WhereIn(deptField, deptIdArr.Slice())
data = g.Map{deptField: deptIdArr.Slice()}
} else if len(whereJustMe) > 0 {
nm = m.Where(whereJustMe)
} else {
nm = m
data = whereJustMe
}
})
return

View File

@ -58,8 +58,8 @@ type (
AccessRule(ctx context.Context, userId uint64, rule string) bool
GetUserSelector(ctx context.Context, req *system.UserSelectorReq) (total interface{}, userList []*model.SysUserSimpleRes, err error)
GetUsersByRoleId(ctx context.Context, roleId uint) (users []*model.SysUserRoleDeptRes, err error)
GetAuthWhere(ctx context.Context, m *gdb.Model, userInfo *model.ContextUser, field ...string) *gdb.Model
GetAuthDeptWhere(ctx context.Context, m *gdb.Model, userInfo *model.ContextUser, field ...string) *gdb.Model
GetAuthWhere(ctx context.Context, userInfo *model.ContextUser, field ...string) g.Map
GetAuthDeptWhere(ctx context.Context, userInfo *model.ContextUser, field ...string) g.Map
}
)

View File

@ -0,0 +1,17 @@
/*
* @desc:错误码信息
* @company:云南奇讯科技有限公司
* @Author: yixiaohu<yxh669@qq.com>
* @Date: 2025/5/20 10:20
*/
package consts
const (
// CodeInfo 提醒
CodeInfo = 1001
// CodeWarning 警告
CodeWarning = 1002
// CodeError 错误
CodeError = 1003
)

View File

@ -9,5 +9,5 @@ package consts
const (
Logo = `CiAgIF9fX19fX19fX19fXyAgICAgICAgICAgX18gCiAgLyBfX19fLyBfX19fL19fXyBfX19fX18vIC9fCiAvIC8gX18vIC9fICAvIF9fIGAvIF9fXy8gX18vCi8gL18vIC8gX18vIC8gL18vIChfXyAgKSAvXyAgClxfX19fL18vICAgIFxfXyxfL19fX18vXF9fLyAg`
Version = "3.3.3"
Version = "3.3.4"
)

View File

@ -10,19 +10,15 @@ package mounter
import (
"context"
"github.com/gogf/gf/v2/net/ghttp"
"sync"
)
type MountHandler func(ctx context.Context, s *ghttp.Server)
var (
funcOptions = make([]MountHandler, 0)
fLock sync.Mutex
)
func Mount(handler MountHandler) {
fLock.Lock()
defer fLock.Unlock()
funcOptions = append(funcOptions, handler)
}

View File

@ -9,16 +9,19 @@ package liberr
import (
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/tiger1103/gfast/v3/internal/consts"
)
func ErrIsNil(ctx context.Context, err error, msg ...string) {
if !g.IsNil(err) {
if len(msg) > 0 {
g.Log().Error(ctx, err.Error())
panic(msg[0])
panic(NewCode(consts.CodeError, msg[0]))
} else {
panic(err)
panic(NewCode(consts.CodeError, err.Error()))
}
}
}
@ -28,3 +31,18 @@ func ValueIsNil(value interface{}, msg string) {
panic(msg)
}
}
func NewCode(code int, msg string) error {
return gerror.NewCode(gcode.New(code, msg, nil))
}
func ErrIsNilCode(ctx context.Context, err error, code int, msg ...string) {
if !g.IsNil(err) {
if len(msg) > 0 {
g.Log().Error(ctx, err.Error())
panic(NewCode(code, msg[0]))
} else {
panic(NewCode(code, err.Error()))
}
}
}

View File

@ -106,14 +106,14 @@ export interface {{.table.ClassName}}TableDataState {
{{$hasDeptSelector = true}}
{{end}}
{{end}}
{{if $hasUserSelector}}
{{if $hasDeptSelector}}
////
export interface LinkedDeptData {
deptId:number;
deptName:string;
}
{{end}}
{{if $hasDeptSelector}}
{{if $hasUserSelector}}
////
export interface LinkedUserData {
id:number;