Skip to content

Commit

Permalink
Merge pull request #384 from go-admin-team/dev
Browse files Browse the repository at this point in the history
admin中的公共中间件,提到common 495a9c5
  • Loading branch information
mss-boot authored Mar 30, 2021
2 parents afab1ef + dd16744 commit 8557e1e
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 33 deletions.
2 changes: 1 addition & 1 deletion app/admin/apis/system/sys_dept/sys_dept.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,4 @@ func (e *SysDept) GetDeptTreeRoleSelect(c *gin.Context) {
"depts": result,
"checkedKeys": menuIds,
}, "")
}
}
16 changes: 7 additions & 9 deletions app/admin/router/init_router.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package router

import (
"github.com/go-admin-team/go-admin-core/sdk"
"github.com/go-admin-team/go-admin-core/sdk/api"
"github.com/go-admin-team/go-admin-core/sdk/pkg"
"os"

"github.com/gin-gonic/gin"
log "github.com/go-admin-team/go-admin-core/logger"
"github.com/go-admin-team/go-admin-core/sdk"
"github.com/go-admin-team/go-admin-core/sdk/api"
"github.com/go-admin-team/go-admin-core/sdk/config"
"github.com/go-admin-team/go-admin-core/sdk/pkg"

"go-admin/app/admin/middleware"
"go-admin/app/admin/middleware/handler"
common "go-admin/common/middleware"

"github.com/go-admin-team/go-admin-core/sdk/config"
"go-admin/common/middleware/handler"
)

// InitRouter 路由初始化,不要怀疑,这里用到了
Expand All @@ -38,9 +36,9 @@ func InitRouter() {
r.Use(common.Sentinel()).
Use(common.RequestId(pkg.TrafficKey)).
Use(api.SetRequestLogger)
middleware.InitMiddleware(r)
common.InitMiddleware(r)
// the jwt middleware
authMiddleware, err := middleware.AuthInit()
authMiddleware, err := common.AuthInit()
if err != nil {
log.Fatalf("JWT Init Error, %s", err.Error())
}
Expand Down
25 changes: 12 additions & 13 deletions app/admin/router/sys_router.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package router

import (
"go-admin/app/admin/apis/system/sys_dept"
"go-admin/app/admin/apis/system/sys_menu"
//"go-admin/app/admin/models/tools"
middleware2 "go-admin/common/middleware"
"mime"

"github.com/gin-gonic/gin"
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
"github.com/go-admin-team/go-admin-core/sdk/pkg/ws"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"

"go-admin/app/admin/apis/monitor"
"go-admin/app/admin/apis/public"
"go-admin/app/admin/apis/system"
"go-admin/app/admin/apis/system/dict"
"go-admin/app/admin/apis/system/sys_dept"
"go-admin/app/admin/apis/system/sys_menu"
"go-admin/app/admin/apis/tools"
"go-admin/app/admin/middleware/handler"
"go-admin/common/middleware"
"go-admin/common/middleware/handler"
_ "go-admin/docs"

"github.com/gin-gonic/gin"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
)

func InitSysRouter(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware) *gin.RouterGroup {
Expand Down Expand Up @@ -121,12 +120,12 @@ func sysCheckRoleRouterInit(r *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddle
func registerBaseRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
api := sys_menu.SysMenu{}
api2 := sys_dept.SysDept{}
v1auth := v1.Group("").Use(authMiddleware.MiddlewareFunc()).Use(middleware2.AuthCheckRole())
v1auth := v1.Group("").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
{
//v1auth.GET("/getinfo", system.GetInfo)
v1auth.GET("/roleMenuTreeselect/:roleId", api.GetMenuTreeSelect)
v1.GET("/menuTreeselect", api.GetMenuTreeSelect)
v1auth.GET("/roleDeptTreeselect/:roleId", api2.GetDeptTreeRoleSelect )
v1auth.GET("/roleDeptTreeselect/:roleId", api2.GetDeptTreeRoleSelect)
//GetDeptTreeRoleselect)
v1auth.POST("/logout", handler.LogOut)
}
Expand Down Expand Up @@ -172,7 +171,7 @@ func registerBaseRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddlewar
func registerDictRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
dictApi := &dict.SysDictType{}
dataApi := &dict.SysDictData{}
dicts := v1.Group("/dict").Use(authMiddleware.MiddlewareFunc()).Use(middleware2.AuthCheckRole())
dicts := v1.Group("/dict").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
{

dicts.GET("/data", dataApi.GetSysDictDataList)
Expand Down Expand Up @@ -217,4 +216,4 @@ func registerPublicRouter(v1 *gin.RouterGroup) {
file := &public.File{}
p.POST("/uploadFile", file.UploadFile)
}
}
}
2 changes: 1 addition & 1 deletion app/admin/service/sys_dept.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,4 @@ func digitDeptLabel(deptList *[]system.SysDept, dept dto.DeptLabel) dto.DeptLabe
}
dept.Children = min
return dept
}
}
2 changes: 1 addition & 1 deletion app/admin/middleware/auth.go → common/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/go-admin-team/go-admin-core/sdk/config"
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
"go-admin/app/admin/middleware/handler"
"go-admin/common/middleware/handler"
)

// AuthInit jwt验证new
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ var store = base64Captcha.DefaultMemStore

func PayloadFunc(data interface{}) jwt.MapClaims {
if v, ok := data.(map[string]interface{}); ok {
u, _ := v["user"].(system.SysUser)
r, _ := v["role"].(system.SysRole)
u, _ := v["user"].(SysUser)
r, _ := v["role"].(SysRole)
return jwt.MapClaims{
jwt.IdentityKey: u.UserId,
jwt.RoleIdKey: r.RoleId,
Expand Down Expand Up @@ -69,7 +69,7 @@ func Authenticator(c *gin.Context) (interface{}, error) {
return nil, jwt.ErrFailedAuthentication
}

var loginVals system.Login
var loginVals Login
var status = "2"
var msg = "登录成功"
var username = ""
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package system
package handler

import (
log "github.com/go-admin-team/go-admin-core/logger"
Expand Down
File renamed without changes.
24 changes: 24 additions & 0 deletions common/middleware/handler/role.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package handler

import "go-admin/common/models"

type SysRole struct {
RoleId int `json:"roleId" gorm:"primaryKey;autoIncrement"` // 角色编码
RoleName string `json:"roleName" gorm:"size:128;"` // 角色名称
Status string `json:"status" gorm:"size:4;"` //
RoleKey string `json:"roleKey" gorm:"size:128;"` //角色代码
RoleSort int `json:"roleSort" gorm:""` //角色排序
Flag string `json:"flag" gorm:"size:128;"` //
Remark string `json:"remark" gorm:"size:255;"` //备注
Admin bool `json:"admin" gorm:"size:4;"`
DataScope string `json:"dataScope" gorm:"size:128;"`
Params string `json:"params" gorm:"-"`
MenuIds []int `json:"menuIds" gorm:"-"`
DeptIds []int `json:"deptIds" gorm:"-"`
models.ControlBy
models.ModelTime
}

func (SysRole) TableName() string {
return "sys_role"
}
40 changes: 40 additions & 0 deletions common/middleware/handler/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package handler

import (
"go-admin/common/models"
"gorm.io/gorm"
)

type SysUser struct {
models.ControlBy
models.ModelTime
UserId int `gorm:"primaryKey;autoIncrement;comment:编码" json:"userId"`
Username string `json:"username" gorm:"type:varchar(64);comment:用户名"`
Password string `json:"-" gorm:"type:varchar(128);comment:密码"`
NickName string `json:"nickName" gorm:"type:varchar(128);comment:昵称"`
Phone string `json:"phone" gorm:"type:varchar(11);comment:手机号"`
RoleId int `json:"roleId" gorm:"type:bigint(20);comment:角色ID"`
Salt string `json:"-" gorm:"type:varchar(255);comment:加盐"`
Avatar string `json:"avatar" gorm:"type:varchar(255);comment:头像"`
Sex string `json:"sex" gorm:"type:varchar(255);comment:性别"`
Email string `json:"email" gorm:"type:varchar(128);comment:邮箱"`
DeptId int `json:"deptId" gorm:"type:bigint(20);comment:部门"`
PostId int `json:"postId" gorm:"type:bigint(20);comment:岗位"`
Remark string `json:"remark" gorm:"type:varchar(255);comment:备注"`
Status string `json:"status" gorm:"type:varchar(4);comment:状态"`
DeptIds []int `json:"deptIds" gorm:"-"`
PostIds []int `json:"postIds" gorm:"-"`
RoleIds []int `json:"roleIds" gorm:"-"`
//Dept *SysDept `json:"dept"`
}

func (SysUser) TableName() string {
return "sys_user"
}

func (e *SysUser) AfterFind(_ *gorm.DB) error {
e.DeptIds = []int{e.DeptId}
e.PostIds = []int{e.PostId}
e.RoleIds = []int{e.RoleId}
return nil
}
File renamed without changes.
6 changes: 2 additions & 4 deletions app/admin/middleware/init.go → common/middleware/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package middleware

import (
"github.com/gin-gonic/gin"

"go-admin/common/middleware"
)

func InitMiddleware(r *gin.Engine) {
// 数据库链接
r.Use(middleware.WithContextDb)
r.Use(WithContextDb)
// 日志处理
r.Use(LoggerToFile())
// 自定义错误处理
Expand All @@ -21,4 +19,4 @@ func InitMiddleware(r *gin.Engine) {
r.Use(Secure)
// 链路追踪
//r.Use(middleware.Trace())
}
}
File renamed without changes.

0 comments on commit 8557e1e

Please sign in to comment.