fix 代码生成bug,完善错误处理,修改数据权限返回值
This commit is contained in:
parent
aa7aec1b60
commit
c6efe923db
@ -9,6 +9,7 @@ package sysAuthRule
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gogf/gf/v2/database/gdb"
|
"github.com/gogf/gf/v2/database/gdb"
|
||||||
"github.com/gogf/gf/v2/errors/gerror"
|
"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))
|
var gList = make([]*model.SysAuthRuleInfoRes, 0, len(list))
|
||||||
for _, v := range list {
|
for _, v := range list {
|
||||||
if v.MenuType == 2 {
|
if v.MenuType == 1 || v.MenuType == 2 {
|
||||||
gList = append(gList, v)
|
gList = append(gList, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -287,6 +288,9 @@ func (s *sSysAuthRule) DeleteMenuByIds(ctx context.Context, ids []int) (err erro
|
|||||||
ids = append(ids, childrenIds...)
|
ids = append(ids, childrenIds...)
|
||||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||||
return g.Try(ctx, func(ctx context.Context) {
|
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()
|
_, err = dao.SysAuthRule.Ctx(ctx).Where("id in (?)", ids).Delete()
|
||||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||||
//删除权限
|
//删除权限
|
||||||
|
@ -10,11 +10,12 @@ package sysUser
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/gogf/gf/v2/container/garray"
|
"github.com/gogf/gf/v2/container/garray"
|
||||||
"github.com/gogf/gf/v2/encoding/gurl"
|
"github.com/gogf/gf/v2/encoding/gurl"
|
||||||
"github.com/gogf/gf/v2/net/ghttp"
|
"github.com/gogf/gf/v2/net/ghttp"
|
||||||
"github.com/tiger1103/gfast/v3/library/libWebsocket"
|
"github.com/tiger1103/gfast/v3/library/libWebsocket"
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/gogf/gf/v2/container/gset"
|
"github.com/gogf/gf/v2/container/gset"
|
||||||
"github.com/gogf/gf/v2/database/gdb"
|
"github.com/gogf/gf/v2/database/gdb"
|
||||||
@ -111,8 +112,7 @@ func (s *sSysUser) GetUserByPhone(ctx context.Context, phone string) (user *mode
|
|||||||
// GetUserById 通过用户名获取用户信息
|
// GetUserById 通过用户名获取用户信息
|
||||||
func (s *sSysUser) GetUserById(ctx context.Context, id uint64) (user *model.LoginUserRes, err error) {
|
func (s *sSysUser) GetUserById(ctx context.Context, id uint64) (user *model.LoginUserRes, err error) {
|
||||||
err = g.Try(ctx, func(ctx context.Context) {
|
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, "获取用户信息失败")
|
liberr.ErrIsNil(ctx, err, "获取用户信息失败")
|
||||||
})
|
})
|
||||||
return
|
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") {
|
if !s.AccessRule(ctx, req.UserInfo.Id, "api/v1/system/user/all") {
|
||||||
m = s.GetAuthDeptWhere(
|
where := s.GetAuthDeptWhere(
|
||||||
ctx,
|
ctx,
|
||||||
m,
|
|
||||||
req.UserInfo,
|
req.UserInfo,
|
||||||
"sys_user", "dept_id", "id",
|
"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 {
|
if req.PageSize == 0 {
|
||||||
req.PageSize = consts.PageSize
|
req.PageSize = consts.PageSize
|
||||||
@ -992,30 +995,30 @@ func (s *sSysUser) GetDataWhere(ctx context.Context, userInfo *model.ContextUser
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetAuthWhere 获取数据权限判断条件-按创建人id获取(created_by),数据权限会跟随创建人转移
|
// 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 (
|
var (
|
||||||
//当前请求api接口对应的菜单
|
//当前请求api接口对应的菜单
|
||||||
url = gstr.TrimLeft(ghttp.RequestFromCtx(ctx).Request.URL.Path, "/")
|
url = gstr.TrimLeft(ghttp.RequestFromCtx(ctx).Request.URL.Path, "/")
|
||||||
menuId uint
|
menuId uint
|
||||||
err error
|
err error
|
||||||
nm *gdb.Model
|
nm g.Map
|
||||||
)
|
)
|
||||||
//获取菜单ID
|
//获取菜单ID
|
||||||
menuId, err = service.SysAuthRule().GetIdByName(ctx, url)
|
menuId, err = service.SysAuthRule().GetIdByName(ctx, url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
g.Log().Error(ctx, err)
|
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 {
|
if err != nil {
|
||||||
g.Log().Error(ctx, err)
|
g.Log().Error(ctx, err)
|
||||||
return m
|
return nil
|
||||||
}
|
}
|
||||||
return nm
|
return nm
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAuthDataWhere 获取数据权限判断条件-按创建用户
|
// 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{} //本人数据权限
|
whereJustMe := g.Map{} //本人数据权限
|
||||||
createdUserField := "created_by"
|
createdUserField := "created_by"
|
||||||
//表别名
|
//表别名
|
||||||
@ -1081,42 +1084,39 @@ func (s *sSysUser) GetAuthDataWhere(ctx context.Context, m *gdb.Model, userInfo
|
|||||||
}
|
}
|
||||||
endLoop:
|
endLoop:
|
||||||
if deptIdArr.Size() > 0 {
|
if deptIdArr.Size() > 0 {
|
||||||
nm = m.WhereIn(createdUserField, dao.SysUser.Ctx(ctx).Fields(dao.SysUser.Columns().Id).
|
where = g.Map{createdUserField + " IN(select id from sys_user where dept_id in(?))": deptIdArr.Slice()}
|
||||||
WhereIn(dao.SysUser.Columns().DeptId, deptIdArr.Slice()))
|
|
||||||
} else if len(whereJustMe) > 0 {
|
} else if len(whereJustMe) > 0 {
|
||||||
nm = m.Where(whereJustMe)
|
where = whereJustMe
|
||||||
} else {
|
|
||||||
nm = m
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAuthDeptWhere 获取部门数据权限判断条件-按部门id获取(dept_id),数据权限依赖部门,不会随创建人转移
|
// 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 (
|
var (
|
||||||
//当前请求api接口对应的菜单
|
//当前请求api接口对应的菜单
|
||||||
url = gstr.TrimLeft(ghttp.RequestFromCtx(ctx).Request.URL.Path, "/")
|
url = gstr.TrimLeft(ghttp.RequestFromCtx(ctx).Request.URL.Path, "/")
|
||||||
menuId uint
|
menuId uint
|
||||||
err error
|
err error
|
||||||
nm *gdb.Model
|
nm g.Map
|
||||||
)
|
)
|
||||||
//获取菜单ID
|
//获取菜单ID
|
||||||
menuId, err = service.SysAuthRule().GetIdByName(ctx, url)
|
menuId, err = service.SysAuthRule().GetIdByName(ctx, url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
g.Log().Error(ctx, err)
|
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 {
|
if err != nil {
|
||||||
g.Log().Error(ctx, err)
|
g.Log().Error(ctx, err)
|
||||||
return m
|
return nil
|
||||||
}
|
}
|
||||||
return nm
|
return nm
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAuthDeptDataWhere 获取数据权限判断条件-按部门
|
// 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{} //本人数据权限
|
whereJustMe := g.Map{} //本人数据权限
|
||||||
deptField := "dept_id"
|
deptField := "dept_id"
|
||||||
createdUserField := "created_by"
|
createdUserField := "created_by"
|
||||||
@ -1188,11 +1188,9 @@ func (s *sSysUser) GetAuthDeptDataWhere(ctx context.Context, m *gdb.Model, userI
|
|||||||
}
|
}
|
||||||
endLoop:
|
endLoop:
|
||||||
if deptIdArr.Size() > 0 {
|
if deptIdArr.Size() > 0 {
|
||||||
nm = m.WhereIn(deptField, deptIdArr.Slice())
|
data = g.Map{deptField: deptIdArr.Slice()}
|
||||||
} else if len(whereJustMe) > 0 {
|
} else if len(whereJustMe) > 0 {
|
||||||
nm = m.Where(whereJustMe)
|
data = whereJustMe
|
||||||
} else {
|
|
||||||
nm = m
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
|
@ -58,8 +58,8 @@ type (
|
|||||||
AccessRule(ctx context.Context, userId uint64, rule string) bool
|
AccessRule(ctx context.Context, userId uint64, rule string) bool
|
||||||
GetUserSelector(ctx context.Context, req *system.UserSelectorReq) (total interface{}, userList []*model.SysUserSimpleRes, err error)
|
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)
|
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
|
GetAuthWhere(ctx context.Context, userInfo *model.ContextUser, field ...string) g.Map
|
||||||
GetAuthDeptWhere(ctx context.Context, m *gdb.Model, userInfo *model.ContextUser, field ...string) *gdb.Model
|
GetAuthDeptWhere(ctx context.Context, userInfo *model.ContextUser, field ...string) g.Map
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
17
internal/consts/err_code.go
Normal file
17
internal/consts/err_code.go
Normal 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
|
||||||
|
)
|
@ -9,5 +9,5 @@ package consts
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
Logo = `CiAgIF9fX19fX19fX19fXyAgICAgICAgICAgX18gCiAgLyBfX19fLyBfX19fL19fXyBfX19fX18vIC9fCiAvIC8gX18vIC9fICAvIF9fIGAvIF9fXy8gX18vCi8gL18vIC8gX18vIC8gL18vIChfXyAgKSAvXyAgClxfX19fL18vICAgIFxfXyxfL19fX18vXF9fLyAg`
|
Logo = `CiAgIF9fX19fX19fX19fXyAgICAgICAgICAgX18gCiAgLyBfX19fLyBfX19fL19fXyBfX19fX18vIC9fCiAvIC8gX18vIC9fICAvIF9fIGAvIF9fXy8gX18vCi8gL18vIC8gX18vIC8gL18vIChfXyAgKSAvXyAgClxfX19fL18vICAgIFxfXyxfL19fX18vXF9fLyAg`
|
||||||
Version = "3.3.3"
|
Version = "3.3.4"
|
||||||
)
|
)
|
||||||
|
@ -10,19 +10,15 @@ package mounter
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/gogf/gf/v2/net/ghttp"
|
"github.com/gogf/gf/v2/net/ghttp"
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type MountHandler func(ctx context.Context, s *ghttp.Server)
|
type MountHandler func(ctx context.Context, s *ghttp.Server)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
funcOptions = make([]MountHandler, 0)
|
funcOptions = make([]MountHandler, 0)
|
||||||
fLock sync.Mutex
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Mount(handler MountHandler) {
|
func Mount(handler MountHandler) {
|
||||||
fLock.Lock()
|
|
||||||
defer fLock.Unlock()
|
|
||||||
funcOptions = append(funcOptions, handler)
|
funcOptions = append(funcOptions, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,16 +9,19 @@ package liberr
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/gogf/gf/v2/errors/gcode"
|
||||||
|
"github.com/gogf/gf/v2/errors/gerror"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
|
"github.com/tiger1103/gfast/v3/internal/consts"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ErrIsNil(ctx context.Context, err error, msg ...string) {
|
func ErrIsNil(ctx context.Context, err error, msg ...string) {
|
||||||
if !g.IsNil(err) {
|
if !g.IsNil(err) {
|
||||||
if len(msg) > 0 {
|
if len(msg) > 0 {
|
||||||
g.Log().Error(ctx, err.Error())
|
g.Log().Error(ctx, err.Error())
|
||||||
panic(msg[0])
|
panic(NewCode(consts.CodeError, msg[0]))
|
||||||
} else {
|
} else {
|
||||||
panic(err)
|
panic(NewCode(consts.CodeError, err.Error()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -28,3 +31,18 @@ func ValueIsNil(value interface{}, msg string) {
|
|||||||
panic(msg)
|
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()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -106,14 +106,14 @@ export interface {{.table.ClassName}}TableDataState {
|
|||||||
{{$hasDeptSelector = true}}
|
{{$hasDeptSelector = true}}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if $hasUserSelector}}
|
{{if $hasDeptSelector}}
|
||||||
////
|
////
|
||||||
export interface LinkedDeptData {
|
export interface LinkedDeptData {
|
||||||
deptId:number;
|
deptId:number;
|
||||||
deptName:string;
|
deptName:string;
|
||||||
}
|
}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if $hasDeptSelector}}
|
{{if $hasUserSelector}}
|
||||||
////
|
////
|
||||||
export interface LinkedUserData {
|
export interface LinkedUserData {
|
||||||
id:number;
|
id:number;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user