【优化】租户功能,框架优化

This commit is contained in:
PandaGoAdmin
2022-07-18 18:17:11 +08:00
parent d33bd39570
commit ae38e7bcef
60 changed files with 861 additions and 647 deletions

View File

@@ -8,6 +8,7 @@ import (
"pandax/base/ctx"
"pandax/base/ginx"
"pandax/base/utils"
"strconv"
)
type SystemApiApi struct {
@@ -120,5 +121,6 @@ func (s *SystemApiApi) GetAllApis(rc *ctx.ReqCtx) {
// @Router /casbin/getPolicyPathByRoleId [get]
func (s *SystemApiApi) GetPolicyPathByRoleId(rc *ctx.ReqCtx) {
roleKey := rc.GinCtx.Query("roleKey")
rc.ResData = casbin.GetPolicyPathByRoleId(roleKey)
tenantId := strconv.Itoa(int(rc.LoginAccount.TenantId))
rc.ResData = casbin.GetPolicyPathByRoleId(tenantId, roleKey)
}

View File

@@ -8,8 +8,8 @@ import (
"pandax/base/biz"
"pandax/base/ctx"
"pandax/base/ginx"
"pandax/base/global"
"pandax/base/utils"
"pandax/pkg/global"
)
type DeptApi struct {

View File

@@ -5,11 +5,10 @@ import (
entity "pandax/apps/system/entity"
services "pandax/apps/system/services"
"pandax/base/biz"
"pandax/base/config"
"pandax/base/ctx"
"pandax/base/ginx"
"pandax/base/global"
"pandax/base/utils"
"pandax/pkg/global"
)
type DictApi struct {
@@ -132,7 +131,7 @@ func (p *DictApi) ExportDictType(rc *ctx.ReqCtx) {
dictType := rc.GinCtx.Query("dictType")
list := p.DictType.FindList(entity.SysDictType{Status: status, DictName: dictName, DictType: dictType})
fileName := utils.GetFileName(config.Conf.Server.ExcelDir, filename)
fileName := utils.GetFileName(global.Conf.Server.ExcelDir, filename)
utils.InterfaceToExcel(*list, fileName)
rc.Download(fileName)
}

View File

@@ -8,8 +8,8 @@ import (
"pandax/base/biz"
"pandax/base/ctx"
"pandax/base/ginx"
"pandax/base/global"
"pandax/base/utils"
"pandax/pkg/global"
)
type PostApi struct {

View File

@@ -7,11 +7,11 @@ import (
services "pandax/apps/system/services"
"pandax/base/biz"
"pandax/base/casbin"
"pandax/base/config"
"pandax/base/ctx"
"pandax/base/ginx"
"pandax/base/global"
"pandax/base/utils"
"pandax/pkg/global"
"strconv"
)
type RoleApi struct {
@@ -79,11 +79,13 @@ func (r *RoleApi) InsertRole(rc *ctx.ReqCtx) {
var role entity.SysRole
ginx.BindJsonAndValid(rc.GinCtx, &role)
role.CreateBy = rc.LoginAccount.UserName
role.TenantId = rc.LoginAccount.TenantId
insert := r.RoleApp.Insert(role)
role.RoleId = insert.RoleId
r.RoleMenuApp.Insert(insert.RoleId, role.MenuIds)
//添加权限
casbin.UpdateCasbin(role.RoleKey, role.ApiIds)
tenantId := strconv.Itoa(int(rc.LoginAccount.TenantId))
casbin.UpdateCasbin(tenantId, role.RoleKey, role.ApiIds)
}
// @Summary 修改用户角色
@@ -106,7 +108,8 @@ func (r *RoleApi) UpdateRole(rc *ctx.ReqCtx) {
// 添加角色菜单绑定
r.RoleMenuApp.Insert(role.RoleId, role.MenuIds)
//修改api权限
casbin.UpdateCasbin(role.RoleKey, role.ApiIds)
tenantId := strconv.Itoa(int(rc.LoginAccount.TenantId))
casbin.UpdateCasbin(tenantId, role.RoleKey, role.ApiIds)
}
// @Summary 修改用户角色状态
@@ -198,7 +201,7 @@ func (p *RoleApi) ExportRole(rc *ctx.ReqCtx) {
roleKey := rc.GinCtx.Query("roleKey")
list := p.RoleApp.FindList(entity.SysRole{Status: status, RoleName: roleName, RoleKey: roleKey})
fileName := utils.GetFileName(config.Conf.Server.ExcelDir, filename)
fileName := utils.GetFileName(global.Conf.Server.ExcelDir, filename)
utils.InterfaceToExcel(*list, fileName)
rc.Download(fileName)
}

View File

@@ -22,7 +22,7 @@ type SysTenantsApi struct {
// @Param pageSize query int false "页条数"
// @Param pageNum query int false "页码"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /admin/tenant/list [get]
// @Router /system/tenant/list [get]
// @Security
func (p *SysTenantsApi) GetSysTenantsList(rc *ctx.ReqCtx) {
data := entity.SysTenants{}
@@ -39,12 +39,28 @@ func (p *SysTenantsApi) GetSysTenantsList(rc *ctx.ReqCtx) {
}
}
// @Summary SysTenants列表数据
// @Tags SysTenants
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /system/tenant/lists [get]
// @Security
func (p *SysTenantsApi) GetSysTenantsAll(rc *ctx.ReqCtx) {
list := make([]entity.SysTenants, 0)
if rc.LoginAccount.RoleKey == "admin" {
data := entity.SysTenants{}
list = *p.SysTenantsApp.FindList(data)
} else {
list = append(list, *p.SysTenantsApp.FindOne(rc.LoginAccount.TenantId))
}
rc.ResData = list
}
// @Summary 获取SysTenants
// @Description 获取JSON
// @Tags SysTenants
// @Param tenantId path int true "tenantId"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /admin/tenant/{tenantId} [get]
// @Router /system/tenant/{tenantId} [get]
// @Security
func (p *SysTenantsApi) GetSysTenants(rc *ctx.ReqCtx) {
tenantId := ginx.PathParamInt(rc.GinCtx, "tenantId")
@@ -59,7 +75,7 @@ func (p *SysTenantsApi) GetSysTenants(rc *ctx.ReqCtx) {
// @Param data body entity.SysTenants true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /admin/tenant [post]
// @Router /system/tenant [post]
// @Security X-TOKEN
func (p *SysTenantsApi) InsertSysTenants(rc *ctx.ReqCtx) {
var data entity.SysTenants
@@ -76,7 +92,7 @@ func (p *SysTenantsApi) InsertSysTenants(rc *ctx.ReqCtx) {
// @Param data body entity.SysTenants true "body"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /admin/tenant [put]
// @Router /system/tenant [put]
// @Security X-TOKEN
func (p *SysTenantsApi) UpdateSysTenants(rc *ctx.ReqCtx) {
var data entity.SysTenants
@@ -91,7 +107,7 @@ func (p *SysTenantsApi) UpdateSysTenants(rc *ctx.ReqCtx) {
// @Param tenantId path string true "tenantId"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": 400, "message": "删除失败"}"
// @Router /admin/tenant/{tenantId} [delete]
// @Router /system/tenant/{tenantId} [delete]
func (p *SysTenantsApi) DeleteSysTenants(rc *ctx.ReqCtx) {
tenantId := rc.GinCtx.Param("tenantId")
tenantIds := utils.IdsStrToIdsIntGroup(tenantId)

View File

@@ -16,11 +16,10 @@ import (
"pandax/apps/system/services"
"pandax/base/biz"
"pandax/base/captcha"
"pandax/base/config"
"pandax/base/ctx"
"pandax/base/ginx"
"pandax/base/global"
"pandax/base/utils"
"pandax/pkg/global"
"strings"
"time"
)
@@ -52,9 +51,10 @@ func (u *UserApi) GenerateCaptcha(c *gin.Context) {
// @Router /system/user/refreshToken [get]
func (u *UserApi) RefreshToken(rc *ctx.ReqCtx) {
tokenStr := rc.GinCtx.Request.Header.Get("X-TOKEN")
token, err := ctx.RefreshToken(tokenStr)
j := ctx.NewJWT("", []byte(global.Conf.Jwt.Key), jwt.SigningMethodHS256)
token, err := j.RefreshToken(tokenStr)
biz.ErrIsNil(err, "刷新token失败")
rc.ResData = map[string]any{"token": token, "expire": time.Now().Unix() + config.Conf.Jwt.ExpireTime}
rc.ResData = map[string]any{"token": token, "expire": time.Now().Unix() + global.Conf.Jwt.ExpireTime}
}
// @Tags Base
@@ -70,27 +70,26 @@ func (u *UserApi) Login(rc *ctx.ReqCtx) {
login := u.UserApp.Login(entity.Login{Username: l.Username, Password: l.Password})
role := u.RoleApp.FindOne(login.RoleId)
token, err := ctx.CreateToken(
ctx.Claims{
UserId: login.UserId,
UserName: login.Username,
RoleId: login.RoleId,
RoleKey: role.RoleKey,
DeptId: login.DeptId,
PostId: login.PostId,
StandardClaims: jwt.StandardClaims{
NotBefore: time.Now().Unix() - 1000, // 签名生效时间
ExpiresAt: time.Now().Unix() + config.Conf.Jwt.ExpireTime, // 过期时间 7天 配置文件
Issuer: "PandaX", // 签名的发行者
},
})
j := ctx.NewJWT("", []byte(global.Conf.Jwt.Key), jwt.SigningMethodHS256)
token, err := j.CreateToken(ctx.Claims{
UserId: login.UserId,
TenantId: login.TenantId,
UserName: login.Username,
RoleId: login.RoleId,
RoleKey: role.RoleKey,
DeptId: login.DeptId,
PostId: login.PostId,
StandardClaims: jwt.StandardClaims{
NotBefore: time.Now().Unix() - 1000, // 签名生效时间
ExpiresAt: time.Now().Unix() + global.Conf.Jwt.ExpireTime, // 过期时间 7天 配置文件
Issuer: "PandaX", // 签名的发行者
},
})
biz.ErrIsNil(err, "生成Token失败")
rc.ResData = map[string]any{
"token": token,
"expire": time.Now().Unix() + config.Conf.Jwt.ExpireTime,
"expire": time.Now().Unix() + global.Conf.Jwt.ExpireTime,
}
var loginLog logEntity.LogLogin
@@ -420,7 +419,7 @@ func (u *UserApi) ExportUser(rc *ctx.ReqCtx) {
user.Username = userName
user.Phone = phone
list := u.UserApp.FindList(user)
fileName := utils.GetFileName(config.Conf.Server.ExcelDir, filename)
fileName := utils.GetFileName(global.Conf.Server.ExcelDir, filename)
utils.InterfaceToExcel(*list, fileName)
rc.Download(fileName)
}

View File

@@ -8,6 +8,7 @@ import (
type SysRole struct {
model.BaseModel
RoleId int64 `json:"roleId" gorm:"primary_key;AUTO_INCREMENT"`
TenantId int64 `json:"tenantId" gorm:"type:int"`
RoleName string `json:"roleName" gorm:"type:varchar(128);comment:角色名称"`
Status string `json:"status" gorm:"type:varchar(1);comment:状态"`
RoleKey string `json:"roleKey" gorm:"type:varchar(128);comment:角色代码"`

View File

@@ -12,21 +12,23 @@ type SysUserId struct {
}
type SysUserB struct {
NickName string `gorm:"type:varchar(128)" json:"nickName"` // 昵称
Phone string `gorm:"type:varchar(11)" json:"phone"` // 手机号
RoleId int64 `gorm:"type:int" json:"roleId"` // 角色编码
Salt string `gorm:"type:varchar(255)" json:"salt"` //
Avatar string `gorm:"type:varchar(255)" json:"avatar"` //头像
Sex string `gorm:"type:varchar(255)" json:"sex"` //性别
Email string `gorm:"type:varchar(128)" json:"email"` //邮箱
DeptId int64 `gorm:"type:int" json:"deptId"` //部门编码
PostId int64 `gorm:"type:int" json:"postId"` //职位编码
RoleIds string `gorm:"type:varchar(255)" json:"roleIds"` //多角色
PostIds string `gorm:"type:varchar(255)" json:"postIds"` // 多岗位
CreateBy string `gorm:"type:varchar(128)" json:"createBy"` //
UpdateBy string `gorm:"type:varchar(128)" json:"updateBy"` //
Remark string `gorm:"type:varchar(255)" json:"remark"` //备注
Status string `gorm:"type:varchar(1);" json:"status"`
TenantId int64 `gorm:"type:int" json:"tenantId"` //租户id
NickName string `gorm:"type:varchar(128)" json:"nickName"` // 昵称
Phone string `gorm:"type:varchar(11)" json:"phone"` // 手机号
RoleId int64 `gorm:"type:int" json:"roleId"` // 角色编码
Salt string `gorm:"type:varchar(255)" json:"salt"` //
Avatar string `gorm:"type:varchar(255)" json:"avatar"` //头像
Sex string `gorm:"type:varchar(255)" json:"sex"` //性别
Email string `gorm:"type:varchar(128)" json:"email"` //邮箱
DeptId int64 `gorm:"type:int" json:"deptId"` //部门编码
PostId int64 `gorm:"type:int" json:"postId"` //职位编码
RoleIds string `gorm:"type:varchar(255)" json:"roleIds"` //多角色
PostIds string `gorm:"type:varchar(255)" json:"postIds"` // 多岗位
CreateBy string `gorm:"type:varchar(128)" json:"createBy"` //
UpdateBy string `gorm:"type:varchar(128)" json:"updateBy"` //
Remark string `gorm:"type:varchar(255)" json:"remark"` //备注
Status string `gorm:"type:varchar(1);" json:"status"`
SysTenants SysTenants `gorm:"foreignKey:TenantId;references:Id" json:"tenants"`
model.BaseModel
}

View File

@@ -23,7 +23,10 @@ func InitSysTenantRouter(router *gin.RouterGroup) {
routerGroup.GET("list", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(SysTenantListLog).Handle(s.GetSysTenantsList)
})
SysTenantAllLog := ctx.NewLogInfo("获取SysTenant列表")
routerGroup.GET("lists", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(SysTenantAllLog).Handle(s.GetSysTenantsAll)
})
SysTenantLog := ctx.NewLogInfo("获取SysTenant信息")
routerGroup.GET(":tenantId", func(c *gin.Context) {
ctx.NewReqCtxWithGin(c).WithLog(SysTenantLog).Handle(s.GetSysTenants)

View File

@@ -6,7 +6,7 @@ import (
"pandax/apps/system/entity"
"pandax/base/biz"
"pandax/base/casbin"
"pandax/base/global"
"pandax/pkg/global"
)
type (

View File

@@ -3,7 +3,7 @@ package services
import (
"pandax/apps/system/entity"
"pandax/base/biz"
"pandax/base/global"
"pandax/pkg/global"
)
type (

View File

@@ -5,7 +5,7 @@ import (
"github.com/kakuilan/kgo"
"pandax/apps/system/entity"
"pandax/base/biz"
"pandax/base/global"
"pandax/pkg/global"
)
type (

View File

@@ -3,7 +3,7 @@ package services
import (
"pandax/apps/system/entity"
"pandax/base/biz"
"pandax/base/global"
"pandax/pkg/global"
)
type (

View File

@@ -3,7 +3,7 @@ package services
import (
"pandax/apps/system/entity"
"pandax/base/biz"
"pandax/base/global"
"pandax/pkg/global"
)
type (

View File

@@ -3,7 +3,7 @@ package services
import (
"pandax/apps/system/entity"
"pandax/base/biz"
"pandax/base/global"
"pandax/pkg/global"
)
type (

View File

@@ -3,7 +3,7 @@ package services
import (
"pandax/apps/system/entity"
"pandax/base/biz"
"pandax/base/global"
"pandax/pkg/global"
)
type (

View File

@@ -3,7 +3,7 @@ package services
import (
"pandax/apps/system/entity"
"pandax/base/biz"
"pandax/base/global"
"pandax/pkg/global"
)
type (

View File

@@ -4,7 +4,7 @@ import (
"errors"
"pandax/apps/system/entity"
"pandax/base/biz"
"pandax/base/global"
"pandax/pkg/global"
)
type (

View File

@@ -4,7 +4,7 @@ import (
"fmt"
"pandax/apps/system/entity"
"pandax/base/biz"
"pandax/base/global"
"pandax/pkg/global"
)
type (

View File

@@ -4,7 +4,7 @@ import (
"fmt"
"pandax/apps/system/entity"
"pandax/base/biz"
"pandax/base/global"
"pandax/pkg/global"
)
type (

View File

@@ -8,7 +8,7 @@ package services
import (
"pandax/apps/system/entity"
"pandax/base/biz"
"pandax/base/global"
"pandax/pkg/global"
)
type (
@@ -38,7 +38,7 @@ func (m *SysTenantModelImpl) Insert(data entity.SysTenants) *entity.SysTenants {
func (m *SysTenantModelImpl) FindOne(tenantId int64) *entity.SysTenants {
resData := new(entity.SysTenants)
err := global.Db.Table(m.table).Where("tenant_id = ?", tenantId).First(resData).Error
err := global.Db.Table(m.table).Where("id = ?", tenantId).First(resData).Error
biz.ErrIsNil(err, "查询SysTenant失败")
return resData
}
@@ -81,5 +81,5 @@ func (m *SysTenantModelImpl) Update(data entity.SysTenants) *entity.SysTenants {
}
func (m *SysTenantModelImpl) Delete(tenantIds []int64) {
biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.SysTenants{}, "tenant_id in (?)", tenantIds).Error, "删除SysTenant失败")
biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.SysTenants{}, "id in (?)", tenantIds).Error, "删除SysTenant失败")
}

View File

@@ -5,7 +5,8 @@ import (
"golang.org/x/crypto/bcrypt"
"pandax/apps/system/entity"
"pandax/base/biz"
"pandax/base/global"
"pandax/pkg/global"
"time"
)
type (
@@ -31,12 +32,19 @@ var SysUserModelDao SysUserModel = &sysUserModelImpl{
func (m *sysUserModelImpl) Login(u entity.Login) *entity.SysUser {
user := new(entity.SysUser)
err := global.Db.Table(m.table).Where("username = ? ", u.Username).Find(user)
biz.ErrIsNil(err.Error, "查询用户信息失败")
// 验证密码
b := kgo.KEncr.PasswordVerify([]byte(u.Password), []byte(user.Password))
biz.IsTrue(b, "密码错误")
//验证租户
if SysTenantModelDao.FindOne(user.TenantId).ExpireTime.Unix() < time.Now().Unix() {
biz.IsTrue(b, "租户已经过期")
}
return user
}
@@ -61,27 +69,25 @@ func (m *sysUserModelImpl) FindOne(data entity.SysUser) *entity.SysUserView {
if data.UserId != 0 {
db = db.Where("user_id = ?", data.UserId)
}
if data.TenantId != 0 {
db = db.Where("tenant_id = ?", data.TenantId)
}
if data.Username != "" {
db = db.Where("username = ?", data.Username)
}
if data.Password != "" {
db = db.Where("password = ?", data.Password)
}
if data.RoleId != 0 {
db = db.Where("role_id = ?", data.RoleId)
}
if data.DeptId != 0 {
db = db.Where("dept_id = ?", data.DeptId)
}
if data.PostId != 0 {
db = db.Where("post_id = ?", data.PostId)
}
biz.ErrIsNil(db.First(resData).Error, "查询用户失败")
biz.ErrIsNil(db.Preload("SysTenants").First(resData).Error, "查询用户失败")
return resData
}
@@ -96,7 +102,9 @@ func (m *sysUserModelImpl) FindListPage(page, pageSize int, data entity.SysUser)
if data.Username != "" {
db = db.Where("sys_users.username = ?", data.Username)
}
if data.TenantId != 0 {
db = db.Where("sys_users.tenant_id = ?", data.TenantId)
}
if data.NickName != "" {
db = db.Where("sys_users.nick_name like ?", "%"+data.NickName+"%")
}
@@ -113,7 +121,7 @@ func (m *sysUserModelImpl) FindListPage(page, pageSize int, data entity.SysUser)
}
db.Where("sys_users.delete_time IS NULL")
err := db.Count(&total).Error
err = db.Limit(pageSize).Offset(offset).Find(&list).Error
err = db.Limit(pageSize).Offset(offset).Preload("SysTenants").Find(&list).Error
biz.ErrIsNil(err, "查询用户分页列表失败")
return &list, total
}
@@ -126,7 +134,9 @@ func (m *sysUserModelImpl) FindList(data entity.SysUser) *[]entity.SysUserView {
if data.UserId != 0 {
db = db.Where("user_id = ?", data.UserId)
}
if data.TenantId != 0 {
db = db.Where("tenant_id = ?", data.TenantId)
}
if data.Username != "" {
db = db.Where("username = ?", data.Username)
}