mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
【优化】租户功能,框架优化
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user