This commit is contained in:
zhangwenjian committed 2020-09-29 08:41:20 +08:00
1 parent 677cdb7821
commit bc648c3f2f
60 files changed
+2319 -550

No files matched your search

+1 -1
View File
@@ -40,7 +40,7 @@ func DeleteAction(control dto.Control) gin.HandlerFunc {
object.SetUpdateBy(tools.GetUserIdUint(c))
//数据权限检查
p := getPermissionFromContext(c)
p := GetPermissionFromContext(c)
db = db.WithContext(c).Scopes(
Permission(object.TableName(), p),
+1 -1
View File
@@ -37,7 +37,7 @@ func IndexAction(m models.ActiveRecord, d dto.Index, f func() interface{}) gin.H
}
//数据权限检查
p := getPermissionFromContext(c)
p := GetPermissionFromContext(c)
err = db.WithContext(c).Model(object).
Scopes(
+14 -9
View File
@@ -13,7 +13,7 @@ import (
"go-admin/tools/config"
)
type dataPermission struct {
type DataPermission struct {
DataScope string
UserId int
DeptId int
@@ -29,7 +29,7 @@ func PermissionAction() gin.HandlerFunc {
}
msgID := tools.GenerateMsgIDFromContext(c)
var p = new(dataPermission)
var p = new(DataPermission)
if userId := tools.GetUserIdStr(c); userId != "" {
p, err = newDataPermission(db, userId)
if err != nil {
@@ -44,9 +44,9 @@ func PermissionAction() gin.HandlerFunc {
}
}
func newDataPermission(tx *gorm.DB, userId interface{}) (*dataPermission, error) {
func newDataPermission(tx *gorm.DB, userId interface{}) (*DataPermission, error) {
var err error
p := &dataPermission{}
p := &DataPermission{}
err = tx.Table("sys_user").
Select("sys_user.user_id", "sys_role.role_id", "sys_user.dept_id", "sys_role.data_scope").
@@ -60,7 +60,7 @@ func newDataPermission(tx *gorm.DB, userId interface{}) (*dataPermission, error)
return p, nil
}
func Permission(tableName string, p *dataPermission) func(db *gorm.DB) *gorm.DB {
func Permission(tableName string, p *DataPermission) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
if !config.ApplicationConfig.EnableDP {
return db
@@ -80,13 +80,18 @@ func Permission(tableName string, p *dataPermission) func(db *gorm.DB) *gorm.DB
}
}
func getPermissionFromContext(c *gin.Context) *dataPermission {
p := new(dataPermission)
func getPermissionFromContext(c *gin.Context) *DataPermission {
p := new(DataPermission)
if pm, ok := c.Get(PermissionKey); ok {
switch pm.(type) {
case *dataPermission:
p = pm.(*dataPermission)
case *DataPermission:
p = pm.(*DataPermission)
}
}
return p
}
// PermissionForNoAction 提供非action写法数据范围约束
func GetPermissionFromContext(c *gin.Context) *DataPermission {
return getPermissionFromContext(c)
}
+1 -1
View File
@@ -38,7 +38,7 @@ func UpdateAction(control dto.Control) gin.HandlerFunc {
object.SetUpdateBy(tools.GetUserIdUint(c))
//数据权限检查
p := getPermissionFromContext(c)
p := GetPermissionFromContext(c)
db = db.WithContext(c).Scopes(
Permission(object.TableName(), p),
+1 -1
View File
@@ -46,7 +46,7 @@ func ViewAction(control dto.Control, f func() interface{}) gin.HandlerFunc {
}
//数据权限检查
p := getPermissionFromContext(c)
p := GetPermissionFromContext(c)
err = db.Model(object).WithContext(c).Scopes(
Permission(object.TableName(), p),
+42
View File
@@ -3,13 +3,55 @@ package apis
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"net/http"
"go-admin/common/models"
"go-admin/tools"
)
type Api struct {
}
// GetOrm 获取Orm DB
func (e *Api) GetOrm(c *gin.Context) (*gorm.DB, error) {
return tools.GetOrm(c)
}
// Error 通常错误数据处理
func (e *Api) Error(c *gin.Context, code int, err error, msg string) {
var res models.Response
if err != nil {
res.Msg = err.Error()
}
if msg != "" {
res.Msg = msg
}
res.RequestId = tools.GenerateMsgIDFromContext(c)
c.JSON(http.StatusOK, res.ReturnError(code))
}
// OK 通常成功数据处理
func (e *Api) OK(c *gin.Context, data interface{}, msg string) {
var res models.Response
res.Data = data
if msg != "" {
res.Msg = msg
}
res.RequestId = tools.GenerateMsgIDFromContext(c)
c.JSON(http.StatusOK, res.ReturnOK())
}
// PageOK 分页数据处理
func (e *Api) PageOK(c *gin.Context, result interface{}, count int, pageIndex int, pageSize int, msg string) {
var res models.Page
res.List = result
res.Count = count
res.PageIndex = pageIndex
res.PageSize = pageSize
e.OK(c, res, msg)
}
// Custom 兼容函数
func (e *Api) Custom(c *gin.Context, data gin.H) {
c.JSON(http.StatusOK, data)
}
-7
View File
@@ -27,13 +27,6 @@ func (s *ObjectById) Bind(ctx *gin.Context) error {
func (s *ObjectById) GetId() interface{} {
if len(s.Ids) > 0 {
return s.Ids
//ids := make([]int64, 0)
//var i int64
//for _, id := range s.Ids {
// i, _ = id.Int64()
// ids = append(ids, i)
//}
//return ids
}
return s.Id
}
+5 -5
View File
@@ -8,12 +8,12 @@ import (
)
type GeneralDelDto struct {
Id int `uri:"id" json:"id" validate:"required"`
Ids []int `json:"ids"`
Id uint `uri:"id" json:"id" validate:"required"`
Ids []uint `json:"ids"`
}
func (g GeneralDelDto) GetIds() []int {
ids := make([]int, 0)
func (g GeneralDelDto) GetIds() []uint {
ids := make([]uint, 0)
if g.Id != 0 {
ids = append(ids, g.Id)
}
@@ -36,7 +36,7 @@ func (g GeneralDelDto) GetIds() []int {
}
type GeneralGetDto struct {
Id int `uri:"id" json:"id" validate:"required"`
Id uint `uri:"id" json:"id" validate:"required"`
}
func MakeCondition(q interface{}) func(db *gorm.DB) *gorm.DB {
+30
View File
@@ -0,0 +1,30 @@
package models
type Response struct {
// 代码
Code int `json:"code" example:"200"`
// 数据集
Data interface{} `json:"data"`
// 消息
Msg string `json:"msg"`
RequestId string `json:"requestId"`
}
type Page struct {
List interface{} `json:"list"`
Count int `json:"count"`
PageIndex int `json:"pageIndex"`
PageSize int `json:"pageSize"`
}
// ReturnOK 正常返回
func (res *Response) ReturnOK() *Response {
res.Code = 200
return res
}
// ReturnError 错误返回
func (res *Response) ReturnError(code int) *Response {
res.Code = code
return res
}