mh_goadmin_server/common/actions/index.go

59 lines
1.3 KiB
Go
Raw Normal View History

2023-09-16 02:56:39 +00:00
package actions
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"go-admin/common/dto"
"go-admin/common/log"
"go-admin/common/models"
"go-admin/tools"
"go-admin/tools/app"
)
// IndexAction 通用查询动作
func IndexAction(m models.ActiveRecord, d dto.Index, f func() interface{}) gin.HandlerFunc {
return func(c *gin.Context) {
db, err := tools.GetOrm(c)
if err != nil {
2023-10-14 08:19:04 +00:00
log.Error(err.Error())
2023-09-16 02:56:39 +00:00
return
}
msgID := tools.GenerateMsgIDFromContext(c)
list := f()
object := m.Generate()
req := d.Generate()
var count int64
//查询列表
err = req.Bind(c)
if err != nil {
app.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
//数据权限检查
p := GetPermissionFromContext(c)
err = db.WithContext(c).Model(object).
Scopes(
dto.MakeCondition(req.GetNeedSearch()),
dto.Paginate(req.GetPageSize(), req.GetPageIndex()),
Permission(object.TableName(), p),
).
Find(list).Limit(-1).Offset(-1).
Count(&count).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
log.Errorf("MsgID[%s] Index error: %s", msgID, err)
app.Error(c, http.StatusInternalServerError, err, "查询失败")
return
}
app.PageOK(c, list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
c.Next()
}
}