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

+2 -2
View File
@@ -30,12 +30,12 @@ func GetLoginLogList(c *gin.Context) {
size := c.Request.FormValue("pageSize")
if size != "" {
pageSize = tools.StrToInt(err, size)
pageSize, err = tools.StringToInt(size)
}
index := c.Request.FormValue("pageIndex")
if index != "" {
pageIndex = tools.StrToInt(err, index)
pageIndex, err = tools.StringToInt(index)
}
data.Username = c.Request.FormValue("username")
+2 -2
View File
@@ -30,12 +30,12 @@ func GetOperLogList(c *gin.Context) {
size := c.Request.FormValue("pageSize")
if size != "" {
pageSize = tools.StrToInt(err, size)
pageSize, err = tools.StringToInt(size)
}
index := c.Request.FormValue("pageIndex")
if index != "" {
pageIndex = tools.StrToInt(err, index)
pageIndex, err = tools.StringToInt(index)
}
data.OperName = c.Request.FormValue("operName")
+211
View File
@@ -0,0 +1,211 @@
package scol
import (
"github.com/gin-gonic/gin"
"go-admin/app/admin/service"
common "go-admin/common/models"
"net/http"
"go-admin/app/admin/models"
"go-admin/app/admin/service/dto"
"go-admin/common/actions"
"go-admin/common/apis"
"go-admin/common/log"
"go-admin/tools"
)
type Scol struct {
apis.Api
}
func (e *Scol) GetScolList(c *gin.Context) {
msgID := tools.GenerateMsgIDFromContext(c)
d := new(dto.ScolSearch)
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
req := d.Generate()
//查询列表
err = req.Bind(c)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
//数据权限检查
p := actions.GetPermissionFromContext(c)
list := make([]models.Scol, 0)
var count int64
serviceStudent := service.Scol{}
serviceStudent.MsgID = msgID
serviceStudent.Orm = db
err = serviceStudent.GetScolPage(req, p, &list, &count)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "查询失败")
return
}
e.PageOK(c, list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}
func (e *Scol) GetScol(c *gin.Context) {
control := new(dto.ScolById)
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
msgID := tools.GenerateMsgIDFromContext(c)
//查看详情
req := control.Generate()
err = req.Bind(c)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
var object common.ActiveRecord
object, err = control.GenerateM()
if err != nil {
e.Error(c, http.StatusInternalServerError, err, "模型生成失败")
return
}
//数据权限检查
p := actions.GetPermissionFromContext(c)
serviceScol := service.Scol{}
serviceScol.MsgID = msgID
serviceScol.Orm = db
err = serviceScol.GetScol(req, p, &object)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "查询失败")
return
}
e.OK(c, object, "查看成功")
}
func (e *Scol) InsertScol(c *gin.Context) {
control := new(dto.ScolControl)
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
msgID := tools.GenerateMsgIDFromContext(c)
//新增操作
req := control.Generate()
err = req.Bind(c)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
var object common.ActiveRecord
object, err = req.GenerateM()
if err != nil {
e.Error(c, http.StatusInternalServerError, err, "模型生成失败")
return
}
// 设置创建人
object.SetCreateBy(tools.GetUserIdUint(c))
serviceScol := service.Scol{}
serviceScol.Orm = db
serviceScol.MsgID = msgID
err = serviceScol.InsertScol(object)
if err != nil {
log.Error(err)
e.Error(c, http.StatusInternalServerError, err, "创建失败")
return
}
e.OK(c, object.GetId(), "创建成功")
}
func (e *Scol) UpdateScol(c *gin.Context) {
control := new(dto.ScolControl)
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
msgID := tools.GenerateMsgIDFromContext(c)
req := control.Generate()
//更新操作
err = req.Bind(c)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
var object common.ActiveRecord
object, err = req.GenerateM()
if err != nil {
e.Error(c, http.StatusInternalServerError, err, "模型生成失败")
return
}
object.SetUpdateBy(tools.GetUserIdUint(c))
//数据权限检查
p := actions.GetPermissionFromContext(c)
serviceScol := service.Scol{}
serviceScol.Orm = db
serviceScol.MsgID = msgID
err = serviceScol.UpdateScol(object, p)
if err != nil {
log.Error(err)
return
}
e.OK(c, object.GetId(), "更新成功")
}
func (e *Scol) DeleteScol(c *gin.Context) {
control := new(dto.ScolById)
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
msgID := tools.GenerateMsgIDFromContext(c)
//删除操作
req := control.Generate()
err = req.Bind(c)
if err != nil {
log.Errorf("MsgID[%s] Bind error: %s", msgID, err)
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
var object common.ActiveRecord
object, err = req.GenerateM()
if err != nil {
e.Error(c, http.StatusInternalServerError, err, "模型生成失败")
return
}
// 设置编辑人
object.SetUpdateBy(tools.GetUserIdUint(c))
// 数据权限检查
p := actions.GetPermissionFromContext(c)
serviceScol := service.Scol{}
serviceScol.Orm = db
serviceScol.MsgID = msgID
err = serviceScol.RemoveScol(req, object, p)
if err != nil {
log.Error(err)
return
}
e.OK(c, object.GetId(), "删除成功")
}
+212
View File
@@ -0,0 +1,212 @@
package student
import (
"github.com/gin-gonic/gin"
"go-admin/app/admin/service"
common "go-admin/common/models"
"net/http"
"go-admin/app/admin/models"
"go-admin/app/admin/service/dto"
"go-admin/common/actions"
"go-admin/common/apis"
"go-admin/common/log"
"go-admin/tools"
"go-admin/tools/app"
)
type Student struct {
apis.Api
}
func (e *Student) GetStudentList(c *gin.Context) {
msgID := tools.GenerateMsgIDFromContext(c)
d := new(dto.StudentSearch)
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
req := d.Generate()
//查询列表
err = req.Bind(c)
if err != nil {
app.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
//数据权限检查
p := actions.GetPermissionFromContext(c)
list := make([]models.Student, 0)
var count int64
serviceStudent := service.Student{}
serviceStudent.MsgID = msgID
serviceStudent.Orm = db
err = serviceStudent.GetStudentPage(req, p, &list, &count)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "查询失败")
return
}
e.PageOK(c, list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}
func (e *Student) GetStudent(c *gin.Context) {
control := new(dto.StudentById)
d := new(dto.StudentSearch)
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
msgID := tools.GenerateMsgIDFromContext(c)
//查看详情
req := control.Generate()
err = req.Bind(c)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
var object common.ActiveRecord
object, err = req.GenerateM()
if err != nil {
e.Error(c, http.StatusInternalServerError, err, "模型生成失败")
return
}
//数据权限检查
p := actions.GetPermissionFromContext(c)
serviceStudent := service.Student{}
serviceStudent.MsgID = msgID
serviceStudent.Orm = db
err = serviceStudent.GetStudent(d, p, &object)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "查询失败")
return
}
e.OK(c, object, "查看成功")
}
func (e *Student) InsertStudent(c *gin.Context) {
control := new(dto.StudentControl)
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
msgID := tools.GenerateMsgIDFromContext(c)
//新增操作
req := control.Generate()
err = req.Bind(c)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
var object common.ActiveRecord
object, err = req.GenerateM()
if err != nil {
e.Error(c, http.StatusInternalServerError, err, "模型生成失败")
return
}
// 设置创建人
object.SetCreateBy(tools.GetUserIdUint(c))
serviceStudent := service.Student{}
serviceStudent.Orm = db
serviceStudent.MsgID = msgID
err = serviceStudent.InsertStudent(object)
if err != nil {
log.Error(err)
e.Error(c, http.StatusInternalServerError, err, "创建失败")
return
}
e.OK(c, object.GetId(), "创建成功")
}
func (e *Student) UpdateStudent(c *gin.Context) {
control := new(dto.StudentControl)
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
msgID := tools.GenerateMsgIDFromContext(c)
req := control.Generate()
//更新操作
err = req.Bind(c)
if err != nil {
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
var object common.ActiveRecord
object, err = req.GenerateM()
if err != nil {
e.Error(c, http.StatusInternalServerError, err, "模型生成失败")
return
}
object.SetUpdateBy(tools.GetUserIdUint(c))
//数据权限检查
p := actions.GetPermissionFromContext(c)
serviceStudent := service.Student{}
serviceStudent.Orm = db
serviceStudent.MsgID = msgID
err = serviceStudent.UpdateStudent(object, p)
if err != nil {
log.Error(err)
return
}
e.OK(c, object.GetId(), "更新成功")
}
func (e *Student) DeleteStudent(c *gin.Context) {
control := new(dto.StudentById)
db, err := tools.GetOrm(c)
if err != nil {
log.Error(err)
return
}
msgID := tools.GenerateMsgIDFromContext(c)
//删除操作
req := control.Generate()
err = req.Bind(c)
if err != nil {
log.Errorf("MsgID[%s] Bind error: %s", msgID, err)
e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
return
}
var object common.ActiveRecord
object, err = req.GenerateM()
if err != nil {
e.Error(c, http.StatusInternalServerError, err, "模型生成失败")
return
}
// 设置编辑人
object.SetUpdateBy(tools.GetUserIdUint(c))
// 数据权限检查
p := actions.GetPermissionFromContext(c)
serviceStudent := service.Student{}
serviceStudent.Orm = db
serviceStudent.MsgID = msgID
err = serviceStudent.RemoveStudent(object, p)
if err != nil {
log.Error(err)
return
}
e.OK(c, object.GetId(), "删除成功")
}
+2 -2
View File
@@ -17,10 +17,10 @@ func GetSysCategoryList(c *gin.Context) {
var pageIndex = 1
if size := c.Request.FormValue("pageSize"); size != "" {
pageSize = tools.StrToInt(err, size)
pageSize, err = tools.StringToInt(size)
}
if index := c.Request.FormValue("pageIndex"); index != "" {
pageIndex = tools.StrToInt(err, index)
pageIndex, err = tools.StringToInt(index)
}
data.Name = c.Request.FormValue("name")
+2 -2
View File
@@ -17,10 +17,10 @@ func GetSysContentList(c *gin.Context) {
var pageIndex = 1
if size := c.Request.FormValue("pageSize"); size != "" {
pageSize = tools.StrToInt(err, size)
pageSize, err = tools.StringToInt(size)
}
if index := c.Request.FormValue("pageIndex"); index != "" {
pageIndex = tools.StrToInt(err, index)
pageIndex, err = tools.StringToInt(index)
}
data.CateId = c.Request.FormValue("cateId")
-58
View File
@@ -1,19 +1,14 @@
package sysjob
import (
"time"
"github.com/gin-gonic/gin"
"go-admin/app/admin/models"
"go-admin/app/admin/service"
"go-admin/app/jobs"
"go-admin/common/apis"
"go-admin/common/dto"
"go-admin/common/log"
"go-admin/tools"
"go-admin/tools/app"
"go-admin/tools/app/msg"
)
type SysJob struct {
@@ -73,56 +68,3 @@ func (e *SysJob) StartJobForService(c *gin.Context) {
}
app.OK(c, nil, s.Msg)
}
func RemoveJob(c *gin.Context) {
var data models.SysJob
var v dto.GeneralDelDto
err := c.BindUri(&v)
tools.HasError(err, "", 500)
data.JobId = v.Id
err = data.Get(data.JobId)
tools.HasError(err, "", 500)
cn := jobs.Remove(data.EntryId)
select {
case res := <-cn:
if res {
_, _ = data.RemoveEntryID(data.EntryId)
app.OK(c, nil, msg.DeletedSuccess)
}
case <-time.After(time.Second * 1):
app.OK(c, nil, msg.TimeOut)
}
}
func StartJob(c *gin.Context) {
var data models.SysJob
var v dto.GeneralGetDto
err := c.BindUri(&v)
tools.HasError(err, "", 500)
data.JobId = v.Id
err = data.Get(data.JobId)
tools.HasError(err, "", 500)
if data.JobType == 1 {
var j = &jobs.HttpJob{}
j.InvokeTarget = data.InvokeTarget
j.CronExpression = data.CronExpression
j.JobId = data.JobId
j.Name = data.JobName
data.EntryId, err = jobs.AddJob(j)
} else {
var j = &jobs.ExecJob{}
j.InvokeTarget = data.InvokeTarget
j.CronExpression = data.CronExpression
j.JobId = data.JobId
j.Name = data.JobName
j.Args = data.Args
data.EntryId, err = jobs.AddJob(j)
}
tools.HasError(err, "", 500)
err = data.Update(data.JobId)
tools.HasError(err, "", 500)
app.OK(c, nil, msg.DeletedSuccess)
}
+2 -2
View File
@@ -30,11 +30,11 @@ func GetConfigList(c *gin.Context) {
var pageIndex = 1
if size := c.Request.FormValue("pageSize"); size != "" {
pageSize = tools.StrToInt(err, size)
pageSize, err = tools.StringToInt(size)
}
if index := c.Request.FormValue("pageIndex"); index != "" {
pageIndex = tools.StrToInt(err, index)
pageIndex, err = tools.StringToInt(index)
}
data.ConfigKey = c.Request.FormValue("configKey")
+2 -2
View File
@@ -29,11 +29,11 @@ func GetDictDataList(c *gin.Context) {
var pageIndex = 1
if size := c.Request.FormValue("pageSize"); size != "" {
pageSize = tools.StrToInt(err, size)
pageSize, err = tools.StringToInt(size)
}
if index := c.Request.FormValue("pageIndex"); index != "" {
pageIndex = tools.StrToInt(err, index)
pageIndex, err = tools.StringToInt(index)
}
data.DictLabel = c.Request.FormValue("dictLabel")
+2 -2
View File
@@ -29,11 +29,11 @@ func GetDictTypeList(c *gin.Context) {
var pageIndex = 1
if size := c.Request.FormValue("pageSize"); size != "" {
pageSize = tools.StrToInt(err, size)
pageSize, err = tools.StringToInt(size)
}
if index := c.Request.FormValue("pageIndex"); index != "" {
pageIndex = tools.StrToInt(err, index)
pageIndex, err = tools.StringToInt(index)
}
data.DictName = c.Request.FormValue("dictName")
+2 -2
View File
@@ -25,11 +25,11 @@ func GetPostList(c *gin.Context) {
var pageIndex = 1
if size := c.Request.FormValue("pageSize"); size != "" {
pageSize = tools.StrToInt(err, size)
pageSize, err = tools.StringToInt(size)
}
if index := c.Request.FormValue("pageIndex"); index != "" {
pageIndex = tools.StrToInt(err, index)
pageIndex, err = tools.StringToInt(index)
}
id := c.Request.FormValue("postId")
+2 -2
View File
@@ -27,11 +27,11 @@ func GetRoleList(c *gin.Context) {
var pageIndex = 1
if size := c.Request.FormValue("pageSize"); size != "" {
pageSize = tools.StrToInt(err, size)
pageSize, err = tools.StringToInt(size)
}
if index := c.Request.FormValue("pageIndex"); index != "" {
pageIndex = tools.StrToInt(err, index)
pageIndex, err = tools.StringToInt(index)
}
data.RoleKey = c.Request.FormValue("roleKey")
+2 -2
View File
@@ -27,12 +27,12 @@ func GetSysUserList(c *gin.Context) {
size := c.Request.FormValue("pageSize")
if size != "" {
pageSize = tools.StrToInt(err, size)
pageSize, err = tools.StringToInt(size)
}
index := c.Request.FormValue("pageIndex")
if index != "" {
pageIndex = tools.StrToInt(err, index)
pageIndex, err = tools.StringToInt(index)
}
data.Username = c.Request.FormValue("username")
+2 -2
View File
@@ -25,11 +25,11 @@ func GetDBColumnList(c *gin.Context) {
var pageIndex = 1
if size := c.Request.FormValue("pageSize"); size != "" {
pageSize = tools2.StrToInt(err, size)
pageSize, err = tools2.StringToInt(size)
}
if index := c.Request.FormValue("pageIndex"); index != "" {
pageIndex = tools2.StrToInt(err, index)
pageIndex, err = tools2.StringToInt(index)
}
data.TableName = c.Request.FormValue("tableName")
+2 -2
View File
@@ -32,11 +32,11 @@ func GetDBTableList(c *gin.Context) {
}
if size := c.Request.FormValue("pageSize"); size != "" {
pageSize = tools2.StrToInt(err, size)
pageSize, err = tools2.StringToInt(size)
}
if index := c.Request.FormValue("pageIndex"); index != "" {
pageIndex = tools2.StrToInt(err, index)
pageIndex, err = tools2.StringToInt(index)
}
data.TableName = c.Request.FormValue("tableName")
+114
View File
@@ -151,6 +151,120 @@ func GenCodeV2(c *gin.Context) {
app.OK(c, "", "Code generated successfully!")
}
func GenCodeV3(c *gin.Context) {
table := tools.SysTables{}
id, err := tools2.StringToInt(c.Param("tableId"))
tools2.HasError(err, "", -1)
table.TableId = id
tab, _ := table.Get()
if tab.IsActions == 1 {
ActionsGenV3(tab)
} else {
NOActionsGenV3(tab)
}
app.OK(c, "", "Code generated successfully!")
}
func NOActionsGenV3(tab tools.SysTables) {
basePath := "template/v3/"
routerFile := basePath + "no_actions/router_check_role.go.template"
if tab.IsAuth == 2 {
routerFile = basePath + "no_actions/router_no_check_role.go.template"
}
t1, err := template.ParseFiles(basePath + "model.go.template")
tools2.HasError(err, "", -1)
t2, err := template.ParseFiles(basePath + "no_actions/apis.go.template")
tools2.HasError(err, "", -1)
t3, err := template.ParseFiles(routerFile)
tools2.HasError(err, "", -1)
t4, err := template.ParseFiles(basePath + "js.go.template")
tools2.HasError(err, "", -1)
t5, err := template.ParseFiles(basePath + "vue.go.template")
tools2.HasError(err, "", -1)
t6, err := template.ParseFiles(basePath + "dto.go.template")
tools2.HasError(err, "", -1)
t7, err := template.ParseFiles(basePath + "no_actions/service.go.template")
tools2.HasError(err, "", -1)
_ = tools2.PathCreate("./app/" + tab.PackageName + "/apis/" + tab.ModuleName)
_ = tools2.PathCreate("./app/" + tab.PackageName + "/models/")
_ = tools2.PathCreate("./app/" + tab.PackageName + "/router/")
_ = tools2.PathCreate("./app/" + tab.PackageName + "/service/dto/")
_ = tools2.PathCreate(config.GenConfig.FrontPath + "/api/")
_ = tools2.PathCreate(config.GenConfig.FrontPath + "/views/" + tab.BusinessName)
var b1 bytes.Buffer
err = t1.Execute(&b1, tab)
var b2 bytes.Buffer
err = t2.Execute(&b2, tab)
var b3 bytes.Buffer
err = t3.Execute(&b3, tab)
var b4 bytes.Buffer
err = t4.Execute(&b4, tab)
var b5 bytes.Buffer
err = t5.Execute(&b5, tab)
var b6 bytes.Buffer
err = t6.Execute(&b6, tab)
var b7 bytes.Buffer
err = t7.Execute(&b7, tab)
tools2.FileCreate(b1, "./app/"+tab.PackageName+"/models/"+tab.BusinessName+".go")
tools2.FileCreate(b2, "./app/"+tab.PackageName+"/apis/"+tab.ModuleName+"/"+tab.BusinessName+".go")
tools2.FileCreate(b3, "./app/"+tab.PackageName+"/router/"+tab.BusinessName+".go")
tools2.FileCreate(b4, config.GenConfig.FrontPath+"/api/"+tab.BusinessName+".js")
tools2.FileCreate(b5, config.GenConfig.FrontPath+"/views/"+tab.BusinessName+"/index.vue")
tools2.FileCreate(b6, "./app/"+tab.PackageName+"/service/dto/"+tab.BusinessName+".go")
tools2.FileCreate(b7, "./app/"+tab.PackageName+"/service/"+tab.BusinessName+".go")
}
func ActionsGenV3(tab tools.SysTables) {
basePath := "template/v3/"
routerFile := basePath + "actions/router_check_role.go.template"
if tab.IsAuth == 2 {
routerFile = basePath + "actions/router_no_check_role.go.template"
}
t1, err := template.ParseFiles(basePath + "model.go.template")
tools2.HasError(err, "", -1)
t3, err := template.ParseFiles(routerFile)
tools2.HasError(err, "", -1)
t4, err := template.ParseFiles(basePath + "js.go.template")
tools2.HasError(err, "", -1)
t5, err := template.ParseFiles(basePath + "vue.go.template")
tools2.HasError(err, "", -1)
t6, err := template.ParseFiles(basePath + "dto.go.template")
tools2.HasError(err, "", -1)
_ = tools2.PathCreate("./app/" + tab.PackageName + "/models/")
_ = tools2.PathCreate("./app/" + tab.PackageName + "/router/")
_ = tools2.PathCreate("./app/" + tab.PackageName + "/service/dto/")
_ = tools2.PathCreate(config.GenConfig.FrontPath + "/api/")
_ = tools2.PathCreate(config.GenConfig.FrontPath + "/views/" + tab.BusinessName)
var b1 bytes.Buffer
err = t1.Execute(&b1, tab)
var b3 bytes.Buffer
err = t3.Execute(&b3, tab)
var b4 bytes.Buffer
err = t4.Execute(&b4, tab)
var b5 bytes.Buffer
err = t5.Execute(&b5, tab)
var b6 bytes.Buffer
err = t6.Execute(&b6, tab)
tools2.FileCreate(b1, "./app/"+tab.PackageName+"/models/"+tab.BusinessName+".go")
tools2.FileCreate(b3, "./app/"+tab.PackageName+"/router/"+tab.BusinessName+".go")
tools2.FileCreate(b4, config.GenConfig.FrontPath+"/api/"+tab.BusinessName+".js")
tools2.FileCreate(b5, config.GenConfig.FrontPath+"/views/"+tab.BusinessName+"/index.vue")
tools2.FileCreate(b6, "./app/"+tab.PackageName+"/service/dto/"+tab.BusinessName+".go")
}
func NOActionsGen(tab tools.SysTables) {
routerfile := "template/routercheckrole.go.template"
-19
View File
@@ -1,19 +0,0 @@
package handler
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
jwt "go-admin/pkg/jwtauth"
)
func NoFound(c *gin.Context) {
claims := jwt.ExtractClaims(c)
log.Printf("NoRoute claims: %#v\n", claims)
c.JSON(http.StatusOK, gin.H{
"code": "404",
"message": "not found",
})
}
-166
View File
@@ -1,166 +0,0 @@
package sd
import (
"fmt"
"net/http"
"runtime"
"time"
"github.com/gin-gonic/gin"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"go-admin/tools/app"
)
const (
B = 1
KB = 1024 * B
MB = 1024 * KB
GB = 1024 * MB
)
// 健康状况
// @Summary 健康状况 HealthCheck shows OK as the ping-pong result.
// @Description 健康状况
// @Accept text/html
// @Produce text/html
// @Success 200 {string} string "OK"
// @Router /sd/health [get]
// @BasePath
func HealthCheck(c *gin.Context) {
app.OK(c, "", "OK")
}
// @Summary 服务器硬盘使用量
// @Description 服务器硬盘使用量 DiskCheck checks the disk usage.
// @Accept text/html
// @Produce text/html
// @Success 200 {string} string "OK - Free space: 16321MB (15GB) / 51200MB (50GB) | Used: 31%"
// @Failure 500 {string} string "CRITICAL"
// @Failure 429 {string} string "WARNING"
// @Router /sd/disk [get]
// @BasePath
func DiskCheck(c *gin.Context) {
u, _ := disk.Usage("/")
usedMB := int(u.Used) / MB
usedGB := int(u.Used) / GB
totalMB := int(u.Total) / MB
totalGB := int(u.Total) / GB
usedPercent := int(u.UsedPercent)
status := http.StatusOK
text := "OK"
if usedPercent >= 95 {
status = http.StatusOK
text = "CRITICAL"
} else if usedPercent >= 90 {
status = http.StatusTooManyRequests
text = "WARNING"
}
message := fmt.Sprintf("%s - Free space: %dMB (%dGB) / %dMB (%dGB) | Used: %d%%", text, usedMB, usedGB, totalMB, totalGB, usedPercent)
c.String(status, "\n"+message)
}
// @Summary OS
// @Description Os
// @Accept text/html
// @Produce text/html
// @Success 200 {string} string ""
// @Router /sd/os [get]
// @BasePath
func OSCheck(c *gin.Context) {
status := http.StatusOK
app.Custum(c, gin.H{
"code": 200,
"status": status,
"goOs": runtime.GOOS,
"compiler": runtime.Compiler,
"numCpu": runtime.NumCPU(),
"version": runtime.Version(),
"numGoroutine": runtime.NumGoroutine(),
})
}
// @Summary CPU 使用量
// @Description CPU 使用量 DiskCheck checks the disk usage.
// @Accept text/html
// @Produce text/html
// @Success 200 {string} string ""
// @Router /sd/cpu [get]
// @BasePath
func CPUCheck(c *gin.Context) {
cores, _ := cpu.Counts(false)
cpus, err := cpu.Percent(time.Duration(200)*time.Millisecond, true)
if err == nil {
for i, c := range cpus {
fmt.Printf("cpu%d : %f%%\n", i, c)
}
}
a, _ := load.Avg()
l1 := a.Load1
l5 := a.Load5
l15 := a.Load15
status := http.StatusOK
text := "OK"
if l5 >= float64(cores-1) {
status = http.StatusInternalServerError
text = "CRITICAL"
} else if l5 >= float64(cores-2) {
status = http.StatusTooManyRequests
text = "WARNING"
}
app.Custum(c, gin.H{
"code": 200,
"msg": text,
"status": status,
"cores": cores,
"load1": l1,
"load5": l5,
"load15": l15,
})
}
// @Summary 内存使用量
// @Description 内存使用量 RAMCheck checks the disk usage.
// @Accept text/html
// @Produce text/html
// @Success 200 {string} string ""
// @Router /sd/ram [get]
// @BasePath
func RAMCheck(c *gin.Context) {
u, _ := mem.VirtualMemory()
usedMB := int(u.Used) / MB
totalMB := int(u.Total) / MB
usedPercent := int(u.UsedPercent)
status := http.StatusOK
text := "OK"
if usedPercent >= 95 {
status = http.StatusInternalServerError
text = "CRITICAL"
} else if usedPercent >= 90 {
status = http.StatusTooManyRequests
text = "WARNING"
}
app.Custum(c, gin.H{
"code": 200,
"msg": text,
"status": status,
"used": usedMB,
"total": totalMB,
"usedPercent": usedPercent,
})
}
-2
View File
@@ -15,6 +15,4 @@ func InitMiddleware(r *gin.Engine) {
r.Use(Options)
// Secure is a middleware function that appends security
r.Use(Secure)
// Set X-Request-Id header
r.Use(RequestId())
}
-26
View File
@@ -1,26 +0,0 @@
package middleware
import (
"github.com/gin-gonic/gin"
"github.com/satori/go.uuid"
)
func RequestId() gin.HandlerFunc {
return func(c *gin.Context) {
// Check for incoming header, use it if exists
requestId := c.Request.Header.Get("X-Request-Id")
// Create request id with UUID4
if requestId == "" {
u4 := uuid.NewV4()
requestId = u4.String()
}
// Expose it for use in the application
c.Set("X-Request-Id", requestId)
// Set X-Request-Id header
c.Writer.Header().Set("X-Request-Id", requestId)
c.Next()
}
}
+27
View File
@@ -0,0 +1,27 @@
package models
import (
"gorm.io/gorm"
"go-admin/common/models"
)
type Scol struct {
gorm.Model
models.ControlBy
Name string `json:"name" gorm:"type:varchar(255);comment:名称"` //
}
func (Scol) TableName() string {
return "scol"
}
func (e *Scol) Generate() models.ActiveRecord {
o := *e
return &o
}
func (e *Scol) GetId() interface{} {
return e.ID
}
+26
View File
@@ -0,0 +1,26 @@
package models
import (
"gorm.io/gorm"
"go-admin/common/models"
)
type Student struct {
gorm.Model
models.ControlBy
Name string `json:"name" gorm:"type:varchar(255);comment:姓名"` //
}
func (Student) TableName() string {
return "student"
}
func (e *Student) Generate() models.ActiveRecord {
o := *e
return &o
}
func (e *Student) GetId() interface{} {
return e.ID
}
+1 -1
View File
@@ -9,7 +9,7 @@ import (
)
type SysJob struct {
JobId int `json:"jobId" gorm:"primary_key;AUTO_INCREMENT"` // 编码
JobId uint `json:"jobId" gorm:"primary_key;AUTO_INCREMENT"` // 编码
JobName string `json:"jobName" gorm:"size:255;"` // 名称
JobGroup string `json:"jobGroup" gorm:"size:255;"` // 任务分组
JobType int `json:"jobType" gorm:"size:1;"` // 任务类型
+25
View File
@@ -0,0 +1,25 @@
package router
import (
"github.com/gin-gonic/gin"
"go-admin/app/admin/apis/scol"
"go-admin/app/admin/middleware"
jwt "go-admin/pkg/jwtauth"
)
func init() {
routerCheckRole = append(routerCheckRole, registerScolRouter)
}
// 需认证的路由代码
func registerScolRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
api := &scol.Scol{}
r := v1.Group("/scol").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
{
r.GET("", api.GetScolList)
r.GET("/:id", api.GetScol)
r.POST("", api.InsertScol)
r.PUT("/:id", api.UpdateScol)
r.DELETE("", api.DeleteScol)
}
}
+25
View File
@@ -0,0 +1,25 @@
package router
import (
"github.com/gin-gonic/gin"
"go-admin/app/admin/apis/student"
"go-admin/app/admin/middleware"
jwt "go-admin/pkg/jwtauth"
)
func init() {
routerCheckRole = append(routerCheckRole, registerStudentRouter)
}
// 需认证的路由代码
func registerStudentRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
stu := &student.Student{}
r := v1.Group("/student").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
{
r.GET("", stu.GetStudentList)
r.GET("/:id", stu.GetStudent)
r.POST("", stu.InsertStudent)
r.PUT("/:id", stu.UpdateStudent)
r.DELETE("", stu.DeleteStudent)
}
}
+1 -6
View File
@@ -25,15 +25,12 @@ func InitSysRouter(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware) *gin.Rou
sysBaseRouter(g)
// 静态文件
sysStaticFileRouter(g)
// swagger;注意:生产环境可以注释掉
sysSwaggerRouter(g)
// 无需认证
sysNoCheckRoleRouter(g)
// 需要认证
sysCheckRoleRouterInit(g, authMiddleware)
return g
}
@@ -44,13 +41,11 @@ func sysBaseRouter(r *gin.RouterGroup) {
go ws.WebsocketManager.SendAllService()
r.GET("/", system.HelloWorld)
r.GET("/info", handler.Ping)
}
func sysStaticFileRouter(r *gin.RouterGroup) {
mime.AddExtensionType(".js", "application/javascript")
r.Static("/static", "./static")
r.Static("/form-generator", "./static/form-generator")
}
@@ -65,7 +60,7 @@ func sysNoCheckRoleRouter(r *gin.RouterGroup) {
v1.GET("/monitor/server", monitor.ServerInfo)
v1.GET("/getCaptcha", system.GenerateCaptchaHandler)
v1.GET("/gen/preview/:tableId", Preview)
v1.GET("/gen/toproject/:tableId", GenCodeV2)
v1.GET("/gen/toproject/:tableId", GenCodeV3)
v1.GET("/gen/todb/:tableId", GenMenuAndApi)
v1.GET("/gen/tabletree", GetSysTablesTree)
v1.GET("/menuTreeselect", system.GetMenuTreeelect)
+89
View File
@@ -0,0 +1,89 @@
package dto
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"go-admin/app/admin/models"
"go-admin/common/dto"
"go-admin/common/log"
common "go-admin/common/models"
"go-admin/tools"
)
type ScolSearch struct {
dto.Pagination `search:"-"`
Name string `form:"name" search:"type:contains;column:name;table:scol" comment:"名称"`
}
func (m *ScolSearch) GetNeedSearch() interface{} {
return *m
}
func (m *ScolSearch) Bind(ctx *gin.Context) error {
msgID := tools.GenerateMsgIDFromContext(ctx)
err := ctx.ShouldBind(m)
if err != nil {
log.Debugf("MsgID[%s] ShouldBind error: %s", msgID, err.Error())
}
return err
}
func (m *ScolSearch) Generate() dto.Index {
o := *m
return &o
}
type ScolControl struct {
ID uint `uri:"ID" comment:""` //
Name string `json:"name" comment:"名称"`
}
func (s *ScolControl) Bind(ctx *gin.Context) error {
msgID := tools.GenerateMsgIDFromContext(ctx)
err := ctx.ShouldBindUri(s)
if err != nil {
log.Debugf("MsgID[%s] ShouldBindUri error: %s", msgID, err.Error())
return err
}
err = ctx.ShouldBind(s)
if err != nil {
log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error())
}
return err
}
func (s *ScolControl) Generate() dto.Control {
cp := *s
return &cp
}
func (s *ScolControl) GenerateM() (common.ActiveRecord, error) {
return &models.Scol{
Model: gorm.Model{ID: s.ID},
Name: s.Name,
}, nil
}
func (s *ScolControl) GetId() interface{} {
return s.ID
}
type ScolById struct {
dto.ObjectById
}
func (s *ScolById) Generate() dto.Control {
cp := *s
return &cp
}
func (s *ScolById) GenerateM() (common.ActiveRecord, error) {
return &models.Scol{}, nil
}
+87
View File
@@ -0,0 +1,87 @@
package dto
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"go-admin/app/admin/models"
"go-admin/common/dto"
"go-admin/common/log"
common "go-admin/common/models"
)
type StudentSearch struct {
dto.Pagination `search:"-"`
Name string `form:"name" search:"type:exact;column:name;table:student" comment:"姓名"`
}
func (m *StudentSearch) GetNeedSearch() interface{} {
return *m
}
func (m *StudentSearch) Bind(ctx *gin.Context) error {
err := ctx.Bind(m)
if err != nil {
log.Errorf("MsgID[%s] Bind error: %#v", err)
}
return err
}
func (m *StudentSearch) Generate() dto.Index {
o := *m
return &o
}
type StudentControl struct {
ID uint `uri:"ID" comment:""` //
Name string `json:"name" comment:"姓名"`
}
func (s *StudentControl) Bind(ctx *gin.Context) error {
err := ctx.BindUri(s)
if err != nil {
log.Errorf("MsgID[%s] Bind error: %#v", err)
return err
}
err = ctx.Bind(s)
if err != nil {
log.Errorf("MsgID[%s] Bind error: %#v", err)
}
return err
}
func (s *StudentControl) Generate() dto.Control {
cp := *s
return &cp
}
func (s *StudentControl) GenerateM() (common.ActiveRecord, error) {
return &models.Student{
Model: gorm.Model{ID: s.ID},
Name: s.Name,
}, nil
}
func (s *StudentControl) GetId() interface{} {
return s.ID
}
type StudentById struct {
dto.ObjectById
}
func (s *StudentById) Generate() dto.Control {
cp := *s
return &cp
}
func (s *StudentById) GenerateM() (common.ActiveRecord, error) {
return &models.Student{}, nil
}
type StudentItem struct {
gorm.Model
common.ControlBy
Name string `json:"name" gorm:"type:varchar(255);comment:姓名"` //
}
+2 -2
View File
@@ -37,7 +37,7 @@ func (m *SysJobSearch) Generate() dto.Index {
}
type SysJobControl struct {
JobId int `json:"jobId"`
JobId uint `json:"jobId"`
JobName string `json:"jobName" validate:"required"` // 名称
JobGroup string `json:"jobGroup"` // 任务分组
JobType int `json:"jobType"` // 任务类型
@@ -93,7 +93,7 @@ func (s *SysJobById) GenerateM() (common.ActiveRecord, error) {
}
type SysJobItem struct {
JobId int `json:"jobId"`
JobId uint `json:"jobId"`
JobName string `json:"jobName" validate:"required"` // 名称
JobGroup string `json:"jobGroup"` // 任务分组
JobType int `json:"jobType"` // 任务类型
+119
View File
@@ -0,0 +1,119 @@
package service
import (
"errors"
"go-admin/app/admin/models"
"go-admin/common/actions"
cDto "go-admin/common/dto"
"go-admin/common/log"
common "go-admin/common/models"
"go-admin/common/service"
"gorm.io/gorm"
)
type Scol struct {
service.Service
}
// GetScolPage 获取Scol列表
func (e *Scol) GetScolPage(c cDto.Index, p *actions.DataPermission, list *[]models.Scol, count *int64) error {
var err error
var data models.Scol
msgID := e.MsgID
err = e.Orm.Table(data.TableName()).
Scopes(
cDto.MakeCondition(c.GetNeedSearch()),
cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
actions.Permission(data.TableName(), p),
).
Find(list).Limit(-1).Offset(-1).
Count(count).Error
if err != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
// GetScol 获取Scol对象
func (e *Scol) GetScol(c cDto.Control, p *actions.DataPermission, m *common.ActiveRecord) error {
var err error
var data models.Scol
msgID := e.MsgID
db := e.Orm.Table(data.TableName()).
Scopes(
actions.Permission(data.TableName(), p),
).Where(c.GetId()).First(m)
err = db.Error
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
err = errors.New("查看对象不存在或无权查看")
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
if db.Error != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
// InsertScol 创建Scol对象
func (e *Scol) InsertScol(model common.ActiveRecord) error {
var err error
var data models.Scol
msgID := e.MsgID
err = e.Orm.Table(data.TableName()).
Create(model).Error
if err != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
// UpdateScol 修改Scol对象
func (e *Scol) UpdateScol(c common.ActiveRecord, p *actions.DataPermission) error {
var err error
var data models.Scol
msgID := e.MsgID
db := e.Orm.Table(data.TableName()).
Scopes(
actions.Permission(data.TableName(), p),
).Where(c.GetId()).Updates(c)
if db.Error != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
if db.RowsAffected == 0 {
return errors.New("无权更新该数据")
}
return nil
}
// RemoveScol 删除Scol
func (e *Scol) RemoveScol(d cDto.Control, c common.ActiveRecord, p *actions.DataPermission) error {
var err error
var data models.Scol
msgID := e.MsgID
db := e.Orm.Table(data.TableName()).
Scopes(
actions.Permission(data.TableName(), p),
).Where(d.GetId()).Delete(c)
if db.Error != nil {
err = db.Error
log.Errorf("MsgID[%s] Delete error: %s", msgID, err)
return err
}
if db.RowsAffected == 0 {
err = errors.New("无权删除该数据")
return err
}
return nil
}
+121
View File
@@ -0,0 +1,121 @@
package service
import (
"errors"
"go-admin/app/admin/models"
"go-admin/app/admin/service/dto"
"go-admin/common/actions"
cDto "go-admin/common/dto"
"go-admin/common/log"
common "go-admin/common/models"
"go-admin/common/service"
"gorm.io/gorm"
)
type Student struct {
service.Service
}
// GetStudentPage 获取student列表
func (e *Student) GetStudentPage(c cDto.Index, p *actions.DataPermission, list *[]models.Student, count *int64) error {
var err error
var data models.Student
msgID := e.MsgID
err = e.Orm.Table(data.TableName()).
Scopes(
cDto.MakeCondition(c.GetNeedSearch()),
cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
actions.Permission(data.TableName(), p),
).
Find(list).Limit(-1).Offset(-1).
Count(count).Error
if err != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
// GetStudent 获取student对象
func (e *Student) GetStudent(c *dto.StudentSearch, p *actions.DataPermission, model *common.ActiveRecord) error {
var err error
var data models.Student
msgID := e.MsgID
db := e.Orm.Table(data.TableName()).
Scopes(
cDto.MakeCondition(c.GetNeedSearch()),
actions.Permission(data.TableName(), p),
).
First(model)
err = db.Error
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
err = errors.New("查看对象不存在或无权查看")
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
if db.Error != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
// InsertStudent 创建student对象
func (e *Student) InsertStudent(model common.ActiveRecord) error {
var err error
var data models.Student
msgID := e.MsgID
err = e.Orm.Table(data.TableName()).
Create(model).Error
if err != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
// UpdateStudent 修改student对象
func (e *Student) UpdateStudent(c common.ActiveRecord, p *actions.DataPermission) error {
var err error
var data models.Student
msgID := e.MsgID
db := e.Orm.Table(data.TableName()).
Scopes(
actions.Permission(data.TableName(), p),
).Where(c.GetId()).Updates(c)
if db.Error != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
if db.RowsAffected == 0 {
return errors.New("无权更新该数据")
}
return nil
}
// RemoveStudent 删除student
func (e *Student) RemoveStudent(c common.ActiveRecord, p *actions.DataPermission) error {
var err error
var data models.Student
msgID := e.MsgID
db := e.Orm.Table(data.TableName()).
Scopes(
actions.Permission(data.TableName(), p),
).Where(c.GetId()).Delete(c)
if db.Error != nil {
err = db.Error
log.Errorf("MsgID[%s] Delete error: %s", msgID, err)
return err
}
if db.RowsAffected == 0 {
err = errors.New("无权删除该数据")
return err
}
return nil
}