后端重构
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package system
|
||||
|
||||
import "kra/internal/biz/system"
|
||||
|
||||
// ApiGroup 系统API组
|
||||
type ApiGroup struct {
|
||||
BaseApi
|
||||
UserApi
|
||||
ApiApi
|
||||
AuthorityApi
|
||||
MenuApi
|
||||
DictionaryApi
|
||||
CasbinApi
|
||||
JwtApi
|
||||
OperationRecordApi
|
||||
ParamsApi
|
||||
}
|
||||
|
||||
// 业务层依赖
|
||||
var (
|
||||
userUsecase *system.UserUsecase
|
||||
apiUsecase *system.ApiUsecase
|
||||
authorityUsecase *system.AuthorityUsecase
|
||||
authorityBtnUsecase *system.AuthorityBtnUsecase
|
||||
menuUsecase *system.MenuUsecase
|
||||
dictionaryUsecase *system.DictionaryUsecase
|
||||
casbinUsecase *system.CasbinUsecase
|
||||
jwtBlacklistUsecase *system.JwtBlacklistUsecase
|
||||
operationRecordUsecase *system.OperationRecordUsecase
|
||||
paramsUsecase *system.ParamsUsecase
|
||||
)
|
||||
|
||||
// InitUsecases 初始化业务层依赖
|
||||
func InitUsecases(
|
||||
user *system.UserUsecase,
|
||||
api *system.ApiUsecase,
|
||||
authority *system.AuthorityUsecase,
|
||||
authorityBtn *system.AuthorityBtnUsecase,
|
||||
menu *system.MenuUsecase,
|
||||
dictionary *system.DictionaryUsecase,
|
||||
casbin *system.CasbinUsecase,
|
||||
jwtBlacklist *system.JwtBlacklistUsecase,
|
||||
operationRecord *system.OperationRecordUsecase,
|
||||
params *system.ParamsUsecase,
|
||||
) {
|
||||
userUsecase = user
|
||||
apiUsecase = api
|
||||
authorityUsecase = authority
|
||||
authorityBtnUsecase = authorityBtn
|
||||
menuUsecase = menu
|
||||
dictionaryUsecase = dictionary
|
||||
casbinUsecase = casbin
|
||||
jwtBlacklistUsecase = jwtBlacklist
|
||||
operationRecordUsecase = operationRecord
|
||||
paramsUsecase = params
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"kra/internal/biz/system"
|
||||
"kra/pkg/response"
|
||||
"kra/pkg/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ApiApi struct{}
|
||||
|
||||
// CreateApiRequest 创建API请求
|
||||
type CreateApiRequest struct {
|
||||
Path string `json:"path" binding:"required"`
|
||||
Description string `json:"description" binding:"required"`
|
||||
ApiGroup string `json:"apiGroup" binding:"required"`
|
||||
Method string `json:"method" binding:"required"`
|
||||
}
|
||||
|
||||
// UpdateApiRequest 更新API请求
|
||||
type UpdateApiRequest struct {
|
||||
ID uint `json:"ID" binding:"required"`
|
||||
Path string `json:"path" binding:"required"`
|
||||
Description string `json:"description" binding:"required"`
|
||||
ApiGroup string `json:"apiGroup" binding:"required"`
|
||||
Method string `json:"method" binding:"required"`
|
||||
}
|
||||
|
||||
// DeleteApiRequest 删除API请求
|
||||
type DeleteApiRequest struct {
|
||||
ID uint `json:"ID" binding:"required"`
|
||||
}
|
||||
|
||||
// GetApiListRequest 获取API列表请求
|
||||
type GetApiListRequest struct {
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
Path string `json:"path"`
|
||||
Description string `json:"description"`
|
||||
ApiGroup string `json:"apiGroup"`
|
||||
Method string `json:"method"`
|
||||
OrderKey string `json:"orderKey"`
|
||||
Desc bool `json:"desc"`
|
||||
}
|
||||
|
||||
// GetApiByIdRequest 根据ID获取API请求
|
||||
type GetApiByIdRequest struct {
|
||||
ID uint `json:"ID" binding:"required"`
|
||||
}
|
||||
|
||||
// DeleteApisByIdsRequest 批量删除API请求
|
||||
type DeleteApisByIdsRequest struct {
|
||||
Ids []uint `json:"ids" binding:"required"`
|
||||
}
|
||||
|
||||
// CreateApi 创建API
|
||||
func (a *ApiApi) CreateApi(c *gin.Context) {
|
||||
var req CreateApiRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
api := &system.Api{
|
||||
Path: req.Path,
|
||||
Description: req.Description,
|
||||
ApiGroup: req.ApiGroup,
|
||||
Method: req.Method,
|
||||
}
|
||||
|
||||
if err := apiUsecase.CreateApi(c, api); err != nil {
|
||||
response.FailWithMessage("创建失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("创建成功", c)
|
||||
}
|
||||
|
||||
// DeleteApi 删除API
|
||||
func (a *ApiApi) DeleteApi(c *gin.Context) {
|
||||
var req DeleteApiRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := apiUsecase.DeleteApi(c, req.ID); err != nil {
|
||||
response.FailWithMessage("删除失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// UpdateApi 更新API
|
||||
func (a *ApiApi) UpdateApi(c *gin.Context) {
|
||||
var req UpdateApiRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
api := &system.Api{
|
||||
ID: req.ID,
|
||||
Path: req.Path,
|
||||
Description: req.Description,
|
||||
ApiGroup: req.ApiGroup,
|
||||
Method: req.Method,
|
||||
}
|
||||
|
||||
if err := apiUsecase.UpdateApi(c, api); err != nil {
|
||||
response.FailWithMessage("修改失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("修改成功", c)
|
||||
}
|
||||
|
||||
// GetApiList 获取API列表
|
||||
func (a *ApiApi) GetApiList(c *gin.Context) {
|
||||
var req GetApiListRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if req.Page == 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = 10
|
||||
}
|
||||
|
||||
filters := make(map[string]interface{})
|
||||
if req.Path != "" {
|
||||
filters["path"] = req.Path
|
||||
}
|
||||
if req.Description != "" {
|
||||
filters["description"] = req.Description
|
||||
}
|
||||
if req.ApiGroup != "" {
|
||||
filters["api_group"] = req.ApiGroup
|
||||
}
|
||||
if req.Method != "" {
|
||||
filters["method"] = req.Method
|
||||
}
|
||||
|
||||
list, total, err := apiUsecase.GetApiList(c, req.Page, req.PageSize, filters)
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(response.PageResult{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
}, "获取成功", c)
|
||||
}
|
||||
|
||||
// GetApiById 根据ID获取API
|
||||
func (a *ApiApi) GetApiById(c *gin.Context) {
|
||||
var req GetApiByIdRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
api, err := apiUsecase.GetApiById(c, req.ID)
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"api": api}, "获取成功", c)
|
||||
}
|
||||
|
||||
// GetAllApis 获取所有API
|
||||
func (a *ApiApi) GetAllApis(c *gin.Context) {
|
||||
authorityID := utils.GetUserAuthorityId(c)
|
||||
apis, err := apiUsecase.GetAllApis(c, authorityID)
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"apis": apis}, "获取成功", c)
|
||||
}
|
||||
|
||||
// DeleteApisByIds 批量删除API
|
||||
func (a *ApiApi) DeleteApisByIds(c *gin.Context) {
|
||||
var req DeleteApisByIdsRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := apiUsecase.DeleteApisByIds(c, req.Ids); err != nil {
|
||||
response.FailWithMessage("删除失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// FreshCasbin 刷新Casbin缓存
|
||||
func (a *ApiApi) FreshCasbin(c *gin.Context) {
|
||||
if err := casbinUsecase.FreshCasbin(); err != nil {
|
||||
response.FailWithMessage("刷新失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("刷新成功", c)
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"kra/internal/biz/system"
|
||||
jwtPkg "kra/pkg/jwt"
|
||||
"kra/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type AuthorityApi struct{}
|
||||
|
||||
// CreateAuthorityRequest 创建角色请求
|
||||
type CreateAuthorityRequest struct {
|
||||
AuthorityId uint `json:"authorityId" binding:"required"`
|
||||
AuthorityName string `json:"authorityName" binding:"required"`
|
||||
ParentId *uint `json:"parentId"`
|
||||
DefaultRouter string `json:"defaultRouter"`
|
||||
}
|
||||
|
||||
// UpdateAuthorityRequest 更新角色请求
|
||||
type UpdateAuthorityRequest struct {
|
||||
AuthorityId uint `json:"authorityId" binding:"required"`
|
||||
AuthorityName string `json:"authorityName" binding:"required"`
|
||||
ParentId *uint `json:"parentId"`
|
||||
DefaultRouter string `json:"defaultRouter"`
|
||||
}
|
||||
|
||||
// DeleteAuthorityRequest 删除角色请求
|
||||
type DeleteAuthorityRequest struct {
|
||||
AuthorityId uint `json:"authorityId" binding:"required"`
|
||||
}
|
||||
|
||||
// CopyAuthorityRequest 复制角色请求
|
||||
type CopyAuthorityRequest struct {
|
||||
OldAuthorityId uint `json:"oldAuthorityId" binding:"required"`
|
||||
Authority CreateAuthorityRequest `json:"authority" binding:"required"`
|
||||
}
|
||||
|
||||
// SetDataAuthorityRequest 设置数据权限请求
|
||||
type SetDataAuthorityRequest struct {
|
||||
AuthorityId uint `json:"authorityId" binding:"required"`
|
||||
DataAuthorityId []uint `json:"dataAuthorityId"`
|
||||
}
|
||||
|
||||
// GetAuthorityListRequest 获取角色列表请求
|
||||
type GetAuthorityListRequest struct {
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
}
|
||||
|
||||
// CreateAuthority 创建角色
|
||||
func (a *AuthorityApi) CreateAuthority(c *gin.Context) {
|
||||
var req CreateAuthorityRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
authority := &system.Authority{
|
||||
AuthorityId: req.AuthorityId,
|
||||
AuthorityName: req.AuthorityName,
|
||||
ParentId: req.ParentId,
|
||||
DefaultRouter: req.DefaultRouter,
|
||||
}
|
||||
|
||||
created, err := authorityUsecase.CreateAuthority(c, authority)
|
||||
if err != nil {
|
||||
response.FailWithMessage("创建失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"authority": created}, "创建成功", c)
|
||||
}
|
||||
|
||||
// DeleteAuthority 删除角色
|
||||
func (a *AuthorityApi) DeleteAuthority(c *gin.Context) {
|
||||
var req DeleteAuthorityRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := authorityUsecase.DeleteAuthority(c, req.AuthorityId); err != nil {
|
||||
response.FailWithMessage("删除失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// UpdateAuthority 更新角色
|
||||
func (a *AuthorityApi) UpdateAuthority(c *gin.Context) {
|
||||
var req UpdateAuthorityRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
authority := &system.Authority{
|
||||
AuthorityId: req.AuthorityId,
|
||||
AuthorityName: req.AuthorityName,
|
||||
ParentId: req.ParentId,
|
||||
DefaultRouter: req.DefaultRouter,
|
||||
}
|
||||
|
||||
updated, err := authorityUsecase.UpdateAuthority(c, authority)
|
||||
if err != nil {
|
||||
response.FailWithMessage("更新失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"authority": updated}, "更新成功", c)
|
||||
}
|
||||
|
||||
// GetAuthorityList 获取角色列表
|
||||
func (a *AuthorityApi) GetAuthorityList(c *gin.Context) {
|
||||
// 从JWT获取当前用户的角色ID
|
||||
claims, exists := c.Get("claims")
|
||||
if !exists {
|
||||
response.FailWithMessage("获取用户信息失败", c)
|
||||
return
|
||||
}
|
||||
customClaims := claims.(*jwtPkg.CustomClaims)
|
||||
|
||||
list, err := authorityUsecase.GetAuthorityInfoList(c, customClaims.AuthorityID)
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"list": list}, "获取成功", c)
|
||||
}
|
||||
|
||||
// CopyAuthority 复制角色
|
||||
func (a *AuthorityApi) CopyAuthority(c *gin.Context) {
|
||||
var req CopyAuthorityRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 从JWT获取当前用户的角色ID
|
||||
claims, exists := c.Get("claims")
|
||||
if !exists {
|
||||
response.FailWithMessage("获取用户信息失败", c)
|
||||
return
|
||||
}
|
||||
customClaims := claims.(*jwtPkg.CustomClaims)
|
||||
|
||||
authority := &system.Authority{
|
||||
AuthorityId: req.Authority.AuthorityId,
|
||||
AuthorityName: req.Authority.AuthorityName,
|
||||
ParentId: req.Authority.ParentId,
|
||||
DefaultRouter: req.Authority.DefaultRouter,
|
||||
}
|
||||
|
||||
created, err := authorityUsecase.CopyAuthority(c, customClaims.AuthorityID, req.OldAuthorityId, authority)
|
||||
if err != nil {
|
||||
response.FailWithMessage("复制失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"authority": created}, "复制成功", c)
|
||||
}
|
||||
|
||||
// SetDataAuthority 设置数据权限
|
||||
func (a *AuthorityApi) SetDataAuthority(c *gin.Context) {
|
||||
var req SetDataAuthorityRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 从JWT获取当前用户的角色ID
|
||||
claims, exists := c.Get("claims")
|
||||
if !exists {
|
||||
response.FailWithMessage("获取用户信息失败", c)
|
||||
return
|
||||
}
|
||||
customClaims := claims.(*jwtPkg.CustomClaims)
|
||||
|
||||
if err := authorityUsecase.SetDataAuthority(c, customClaims.AuthorityID, req.AuthorityId, req.DataAuthorityId); err != nil {
|
||||
response.FailWithMessage("设置失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("设置成功", c)
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"kra/pkg/jwt"
|
||||
"kra/pkg/response"
|
||||
"kra/pkg/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type BaseApi struct{}
|
||||
|
||||
// LoginRequest 登录请求
|
||||
type LoginRequest struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
Captcha string `json:"captcha"`
|
||||
CaptchaId string `json:"captchaId"`
|
||||
}
|
||||
|
||||
// LoginResponse 登录响应
|
||||
type LoginResponse struct {
|
||||
User interface{} `json:"user"`
|
||||
Token string `json:"token"`
|
||||
ExpiresAt int64 `json:"expiresAt"`
|
||||
}
|
||||
|
||||
// JWT实例(需要在初始化时设置)
|
||||
var jwtInstance *jwt.JWT
|
||||
|
||||
// SetJWTInstance 设置JWT实例
|
||||
func SetJWTInstance(j *jwt.JWT) {
|
||||
jwtInstance = j
|
||||
}
|
||||
|
||||
// Login 用户登录
|
||||
func (b *BaseApi) Login(c *gin.Context) {
|
||||
var req LoginRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := userUsecase.Login(c, req.Username, req.Password)
|
||||
if err != nil {
|
||||
response.FailWithMessage("用户名不存在或者密码错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
if user.Enable != 1 {
|
||||
response.FailWithMessage("用户被禁止登录", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 生成token
|
||||
claims := jwtInstance.CreateClaims(jwt.BaseClaims{
|
||||
UUID: user.UUID.String(),
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
NickName: user.NickName,
|
||||
AuthorityID: user.AuthorityId,
|
||||
})
|
||||
|
||||
token, err := jwtInstance.CreateToken(claims.BaseClaims)
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取token失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
utils.SetToken(c, token, int(jwtInstance.ExpiresAt.Seconds()))
|
||||
|
||||
response.OkWithDetailed(LoginResponse{
|
||||
User: toUserResponse(user),
|
||||
Token: token,
|
||||
ExpiresAt: claims.ExpiresAt.UnixMilli(),
|
||||
}, "登录成功", c)
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"kra/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type CasbinApi struct{}
|
||||
|
||||
// CasbinRule Casbin规则
|
||||
type CasbinRule struct {
|
||||
Path string `json:"path"`
|
||||
Method string `json:"method"`
|
||||
}
|
||||
|
||||
// UpdateCasbinRequest 更新Casbin请求
|
||||
type UpdateCasbinRequest struct {
|
||||
AuthorityId uint `json:"authorityId" binding:"required"`
|
||||
CasbinInfos []CasbinRule `json:"casbinInfos"`
|
||||
}
|
||||
|
||||
// GetPolicyPathByAuthorityIdRequest 获取权限路径请求
|
||||
type GetPolicyPathByAuthorityIdRequest struct {
|
||||
AuthorityId uint `json:"authorityId" binding:"required"`
|
||||
}
|
||||
|
||||
// UpdateCasbin 更新Casbin权限
|
||||
func (ca *CasbinApi) UpdateCasbin(c *gin.Context) {
|
||||
var req UpdateCasbinRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 转换为业务层格式
|
||||
rules := make([]struct {
|
||||
Path string
|
||||
Method string
|
||||
}, len(req.CasbinInfos))
|
||||
for i, r := range req.CasbinInfos {
|
||||
rules[i] = struct {
|
||||
Path string
|
||||
Method string
|
||||
}{
|
||||
Path: r.Path,
|
||||
Method: r.Method,
|
||||
}
|
||||
}
|
||||
|
||||
if err := casbinUsecase.UpdateCasbin(0, req.AuthorityId, rules); err != nil {
|
||||
response.FailWithMessage("更新失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("更新成功", c)
|
||||
}
|
||||
|
||||
// GetPolicyPathByAuthorityId 获取权限路径
|
||||
func (ca *CasbinApi) GetPolicyPathByAuthorityId(c *gin.Context) {
|
||||
var req GetPolicyPathByAuthorityIdRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
paths := casbinUsecase.GetPolicyPathByAuthorityId(req.AuthorityId)
|
||||
|
||||
response.OkWithDetailed(gin.H{"paths": paths}, "获取成功", c)
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"kra/internal/biz/system"
|
||||
"kra/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type DictionaryApi struct{}
|
||||
|
||||
// CreateDictionaryRequest 创建字典请求
|
||||
type CreateDictionaryRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Type string `json:"type" binding:"required"`
|
||||
Status *bool `json:"status"`
|
||||
Desc string `json:"desc"`
|
||||
}
|
||||
|
||||
// UpdateDictionaryRequest 更新字典请求
|
||||
type UpdateDictionaryRequest struct {
|
||||
ID uint `json:"ID" binding:"required"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Status *bool `json:"status"`
|
||||
Desc string `json:"desc"`
|
||||
}
|
||||
|
||||
// GetDictionaryListRequest 获取字典列表请求
|
||||
type GetDictionaryListRequest struct {
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// CreateSysDictionary 创建字典
|
||||
func (d *DictionaryApi) CreateSysDictionary(c *gin.Context) {
|
||||
var req CreateDictionaryRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
dict := &system.Dictionary{
|
||||
Name: req.Name,
|
||||
Type: req.Type,
|
||||
Status: req.Status,
|
||||
Desc: req.Desc,
|
||||
}
|
||||
|
||||
if err := dictionaryUsecase.CreateDictionary(c, dict); err != nil {
|
||||
response.FailWithMessage("创建失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("创建成功", c)
|
||||
}
|
||||
|
||||
// DeleteSysDictionary 删除字典
|
||||
func (d *DictionaryApi) DeleteSysDictionary(c *gin.Context) {
|
||||
idStr := c.Query("ID")
|
||||
if idStr == "" {
|
||||
response.FailWithMessage("参数错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
response.FailWithMessage("参数错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := dictionaryUsecase.DeleteDictionary(c, uint(id)); err != nil {
|
||||
response.FailWithMessage("删除失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// UpdateSysDictionary 更新字典
|
||||
func (d *DictionaryApi) UpdateSysDictionary(c *gin.Context) {
|
||||
var req UpdateDictionaryRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
dict := &system.Dictionary{
|
||||
ID: req.ID,
|
||||
Name: req.Name,
|
||||
Type: req.Type,
|
||||
Status: req.Status,
|
||||
Desc: req.Desc,
|
||||
}
|
||||
|
||||
if err := dictionaryUsecase.UpdateDictionary(c, dict); err != nil {
|
||||
response.FailWithMessage("更新失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("更新成功", c)
|
||||
}
|
||||
|
||||
// FindSysDictionary 根据ID或type获取字典
|
||||
func (d *DictionaryApi) FindSysDictionary(c *gin.Context) {
|
||||
idStr := c.Query("ID")
|
||||
dictType := c.Query("type")
|
||||
|
||||
var id uint
|
||||
if idStr != "" {
|
||||
idUint, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
response.FailWithMessage("参数错误", c)
|
||||
return
|
||||
}
|
||||
id = uint(idUint)
|
||||
}
|
||||
|
||||
dict, err := dictionaryUsecase.GetDictionary(c, dictType, id, nil)
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"resysDictionary": dict}, "获取成功", c)
|
||||
}
|
||||
|
||||
// GetSysDictionaryList 获取字典列表
|
||||
func (d *DictionaryApi) GetSysDictionaryList(c *gin.Context) {
|
||||
name := c.Query("name")
|
||||
|
||||
list, err := dictionaryUsecase.GetDictionaryList(c, name)
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"list": list}, "获取成功", c)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"kra/pkg/response"
|
||||
"kra/pkg/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type JwtApi struct{}
|
||||
|
||||
// JsonInBlacklist JWT加入黑名单
|
||||
func (j *JwtApi) JsonInBlacklist(c *gin.Context) {
|
||||
token := utils.GetToken(c)
|
||||
if token == "" {
|
||||
response.FailWithMessage("token不存在", c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := jwtBlacklistUsecase.JsonInBlacklist(c, token); err != nil {
|
||||
response.FailWithMessage("jwt作废失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
utils.ClearToken(c)
|
||||
response.OkWithMessage("jwt作废成功", c)
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"kra/internal/biz/system"
|
||||
"kra/pkg/response"
|
||||
"kra/pkg/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type MenuApi struct{}
|
||||
|
||||
// AddBaseMenuRequest 添加菜单请求
|
||||
type AddBaseMenuRequest struct {
|
||||
ParentId uint `json:"parentId"`
|
||||
Path string `json:"path" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Hidden bool `json:"hidden"`
|
||||
Component string `json:"component"`
|
||||
Sort int `json:"sort"`
|
||||
Meta map[string]interface{} `json:"meta"`
|
||||
Parameters []MenuParameter `json:"parameters"`
|
||||
MenuBtn []MenuBtn `json:"menuBtn"`
|
||||
}
|
||||
|
||||
// MenuParameter 菜单参数
|
||||
type MenuParameter struct {
|
||||
Type string `json:"type"`
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
// MenuBtn 菜单按钮
|
||||
type MenuBtn struct {
|
||||
Name string `json:"name"`
|
||||
Desc string `json:"desc"`
|
||||
}
|
||||
|
||||
// UpdateBaseMenuRequest 更新菜单请求
|
||||
type UpdateBaseMenuRequest struct {
|
||||
ID uint `json:"ID" binding:"required"`
|
||||
ParentId uint `json:"parentId"`
|
||||
Path string `json:"path" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Hidden bool `json:"hidden"`
|
||||
Component string `json:"component"`
|
||||
Sort int `json:"sort"`
|
||||
Meta map[string]interface{} `json:"meta"`
|
||||
Parameters []MenuParameter `json:"parameters"`
|
||||
MenuBtn []MenuBtn `json:"menuBtn"`
|
||||
}
|
||||
|
||||
// DeleteBaseMenuRequest 删除菜单请求
|
||||
type DeleteBaseMenuRequest struct {
|
||||
ID uint `json:"ID" binding:"required"`
|
||||
}
|
||||
|
||||
// GetMenuByIdRequest 根据ID获取菜单请求
|
||||
type GetMenuByIdRequest struct {
|
||||
ID uint `json:"ID" binding:"required"`
|
||||
}
|
||||
|
||||
// AddMenuAuthorityRequest 添加菜单权限请求
|
||||
type AddMenuAuthorityRequest struct {
|
||||
AuthorityId uint `json:"authorityId" binding:"required"`
|
||||
Menus []uint `json:"menus"`
|
||||
}
|
||||
|
||||
// GetMenuAuthorityRequest 获取菜单权限请求
|
||||
type GetMenuAuthorityRequest struct {
|
||||
AuthorityId uint `json:"authorityId" binding:"required"`
|
||||
}
|
||||
|
||||
// AddBaseMenu 添加菜单
|
||||
func (m *MenuApi) AddBaseMenu(c *gin.Context) {
|
||||
var req AddBaseMenuRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
menu := &system.BaseMenu{
|
||||
ParentId: req.ParentId,
|
||||
Path: req.Path,
|
||||
Name: req.Name,
|
||||
Hidden: req.Hidden,
|
||||
Component: req.Component,
|
||||
Sort: req.Sort,
|
||||
}
|
||||
|
||||
if err := menuUsecase.AddBaseMenu(c, menu); err != nil {
|
||||
response.FailWithMessage("创建失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("创建成功", c)
|
||||
}
|
||||
|
||||
// DeleteBaseMenu 删除菜单
|
||||
func (m *MenuApi) DeleteBaseMenu(c *gin.Context) {
|
||||
var req DeleteBaseMenuRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := menuUsecase.DeleteBaseMenu(c, req.ID); err != nil {
|
||||
response.FailWithMessage("删除失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// UpdateBaseMenu 更新菜单
|
||||
func (m *MenuApi) UpdateBaseMenu(c *gin.Context) {
|
||||
var req UpdateBaseMenuRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
menu := &system.BaseMenu{
|
||||
ID: req.ID,
|
||||
ParentId: req.ParentId,
|
||||
Path: req.Path,
|
||||
Name: req.Name,
|
||||
Hidden: req.Hidden,
|
||||
Component: req.Component,
|
||||
Sort: req.Sort,
|
||||
}
|
||||
|
||||
if err := menuUsecase.UpdateBaseMenu(c, menu); err != nil {
|
||||
response.FailWithMessage("更新失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("更新成功", c)
|
||||
}
|
||||
|
||||
// GetMenu 获取用户菜单
|
||||
func (m *MenuApi) GetMenu(c *gin.Context) {
|
||||
authorityId := utils.GetUserAuthorityId(c)
|
||||
menus, err := menuUsecase.GetMenuTree(c, authorityId)
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"menus": menus}, "获取成功", c)
|
||||
}
|
||||
|
||||
// GetMenuList 获取菜单列表
|
||||
func (m *MenuApi) GetMenuList(c *gin.Context) {
|
||||
authorityId := utils.GetUserAuthorityId(c)
|
||||
menus, err := menuUsecase.GetInfoList(c, authorityId)
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"list": menus}, "获取成功", c)
|
||||
}
|
||||
|
||||
// GetBaseMenuTree 获取菜单树
|
||||
func (m *MenuApi) GetBaseMenuTree(c *gin.Context) {
|
||||
authorityId := utils.GetUserAuthorityId(c)
|
||||
menus, err := menuUsecase.GetBaseMenuTree(c, authorityId)
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"menus": menus}, "获取成功", c)
|
||||
}
|
||||
|
||||
// GetBaseMenuById 根据ID获取菜单
|
||||
func (m *MenuApi) GetBaseMenuById(c *gin.Context) {
|
||||
var req GetMenuByIdRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
menu, err := menuUsecase.GetBaseMenuById(c, req.ID)
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"menu": menu}, "获取成功", c)
|
||||
}
|
||||
|
||||
// AddMenuAuthority 添加菜单权限
|
||||
func (m *MenuApi) AddMenuAuthority(c *gin.Context) {
|
||||
var req AddMenuAuthorityRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取当前用户的角色ID
|
||||
adminAuthorityId := utils.GetUserAuthorityId(c)
|
||||
|
||||
// 构建菜单列表
|
||||
var menus []*system.BaseMenu
|
||||
for _, menuId := range req.Menus {
|
||||
menus = append(menus, &system.BaseMenu{ID: menuId})
|
||||
}
|
||||
|
||||
if err := menuUsecase.AddMenuAuthority(c, menus, adminAuthorityId, req.AuthorityId); err != nil {
|
||||
response.FailWithMessage("添加失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("添加成功", c)
|
||||
}
|
||||
|
||||
// GetMenuAuthority 获取菜单权限
|
||||
func (m *MenuApi) GetMenuAuthority(c *gin.Context) {
|
||||
var req GetMenuAuthorityRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
menus, err := menuUsecase.GetMenuAuthority(c, req.AuthorityId)
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"menus": menus}, "获取成功", c)
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"kra/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type OperationRecordApi struct{}
|
||||
|
||||
// DeleteOperationRecordRequest 删除操作记录请求
|
||||
type DeleteOperationRecordRequest struct {
|
||||
ID uint `json:"ID" binding:"required"`
|
||||
}
|
||||
|
||||
// DeleteOperationRecordByIdsRequest 批量删除操作记录请求
|
||||
type DeleteOperationRecordByIdsRequest struct {
|
||||
Ids []uint `json:"ids" binding:"required"`
|
||||
}
|
||||
|
||||
// GetOperationRecordListRequest 获取操作记录列表请求
|
||||
type GetOperationRecordListRequest struct {
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
Method string `json:"method"`
|
||||
Path string `json:"path"`
|
||||
Status int `json:"status"`
|
||||
}
|
||||
|
||||
// DeleteSysOperationRecord 删除操作记录
|
||||
func (o *OperationRecordApi) DeleteSysOperationRecord(c *gin.Context) {
|
||||
var req DeleteOperationRecordRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := operationRecordUsecase.DeleteOperationRecord(c, req.ID); err != nil {
|
||||
response.FailWithMessage("删除失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// DeleteSysOperationRecordByIds 批量删除操作记录
|
||||
func (o *OperationRecordApi) DeleteSysOperationRecordByIds(c *gin.Context) {
|
||||
var req DeleteOperationRecordByIdsRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := operationRecordUsecase.DeleteOperationRecordByIds(c, req.Ids); err != nil {
|
||||
response.FailWithMessage("删除失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// FindSysOperationRecord 根据ID获取操作记录
|
||||
func (o *OperationRecordApi) FindSysOperationRecord(c *gin.Context) {
|
||||
idStr := c.Query("ID")
|
||||
if idStr == "" {
|
||||
response.FailWithMessage("参数错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
var id uint
|
||||
// 简单转换
|
||||
for _, ch := range idStr {
|
||||
if ch >= '0' && ch <= '9' {
|
||||
id = id*10 + uint(ch-'0')
|
||||
}
|
||||
}
|
||||
|
||||
record, err := operationRecordUsecase.GetOperationRecord(c, id)
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"reSysOperationRecord": record}, "获取成功", c)
|
||||
}
|
||||
|
||||
// GetSysOperationRecordList 获取操作记录列表
|
||||
func (o *OperationRecordApi) GetSysOperationRecordList(c *gin.Context) {
|
||||
var req GetOperationRecordListRequest
|
||||
// GET请求从query获取参数
|
||||
req.Page = 1
|
||||
req.PageSize = 10
|
||||
|
||||
filters := make(map[string]interface{})
|
||||
if req.Method != "" {
|
||||
filters["method"] = req.Method
|
||||
}
|
||||
if req.Path != "" {
|
||||
filters["path"] = req.Path
|
||||
}
|
||||
if req.Status != 0 {
|
||||
filters["status"] = req.Status
|
||||
}
|
||||
|
||||
list, total, err := operationRecordUsecase.GetOperationRecordList(c, req.Page, req.PageSize, filters)
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(response.PageResult{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
}, "获取成功", c)
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"kra/internal/biz/system"
|
||||
"kra/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ParamsApi struct{}
|
||||
|
||||
// CreateParamsRequest 创建参数请求
|
||||
type CreateParamsRequest struct {
|
||||
Key string `json:"key" binding:"required"`
|
||||
Value string `json:"value" binding:"required"`
|
||||
Name string `json:"name"`
|
||||
Desc string `json:"desc"`
|
||||
}
|
||||
|
||||
// UpdateParamsRequest 更新参数请求
|
||||
type UpdateParamsRequest struct {
|
||||
ID uint `json:"ID" binding:"required"`
|
||||
Key string `json:"key" binding:"required"`
|
||||
Value string `json:"value" binding:"required"`
|
||||
Name string `json:"name"`
|
||||
Desc string `json:"desc"`
|
||||
}
|
||||
|
||||
// DeleteParamsRequest 删除参数请求
|
||||
type DeleteParamsRequest struct {
|
||||
ID uint `json:"ID" binding:"required"`
|
||||
}
|
||||
|
||||
// GetParamsListRequest 获取参数列表请求
|
||||
type GetParamsListRequest struct {
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
Key string `json:"key"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// CreateSysParams 创建参数
|
||||
func (p *ParamsApi) CreateSysParams(c *gin.Context) {
|
||||
var req CreateParamsRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
params := &system.Params{
|
||||
Key: req.Key,
|
||||
Value: req.Value,
|
||||
Name: req.Name,
|
||||
Desc: req.Desc,
|
||||
}
|
||||
|
||||
if err := paramsUsecase.Create(c, params); err != nil {
|
||||
response.FailWithMessage("创建失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("创建成功", c)
|
||||
}
|
||||
|
||||
// DeleteSysParams 删除参数
|
||||
func (p *ParamsApi) DeleteSysParams(c *gin.Context) {
|
||||
var req DeleteParamsRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := paramsUsecase.Delete(c, req.ID); err != nil {
|
||||
response.FailWithMessage("删除失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// UpdateSysParams 更新参数
|
||||
func (p *ParamsApi) UpdateSysParams(c *gin.Context) {
|
||||
var req UpdateParamsRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
params := &system.Params{
|
||||
ID: req.ID,
|
||||
Key: req.Key,
|
||||
Value: req.Value,
|
||||
Name: req.Name,
|
||||
Desc: req.Desc,
|
||||
}
|
||||
|
||||
if err := paramsUsecase.Update(c, params); err != nil {
|
||||
response.FailWithMessage("更新失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("更新成功", c)
|
||||
}
|
||||
|
||||
// GetSysParams 根据Key获取参数
|
||||
func (p *ParamsApi) GetSysParams(c *gin.Context) {
|
||||
key := c.Query("key")
|
||||
if key == "" {
|
||||
response.FailWithMessage("参数错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
params, err := paramsUsecase.GetByKey(c, key)
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"sysParams": params}, "获取成功", c)
|
||||
}
|
||||
|
||||
// GetSysParamsList 获取参数列表
|
||||
func (p *ParamsApi) GetSysParamsList(c *gin.Context) {
|
||||
var req GetParamsListRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
// GET请求使用默认值
|
||||
req.Page = 1
|
||||
req.PageSize = 10
|
||||
}
|
||||
|
||||
if req.Page == 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = 10
|
||||
}
|
||||
|
||||
filters := make(map[string]interface{})
|
||||
if req.Key != "" {
|
||||
filters["key"] = req.Key
|
||||
}
|
||||
if req.Name != "" {
|
||||
filters["name"] = req.Name
|
||||
}
|
||||
|
||||
list, total, err := paramsUsecase.GetList(c, req.Page, req.PageSize, filters)
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(response.PageResult{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
}, "获取成功", c)
|
||||
}
|
||||
@@ -0,0 +1,426 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"kra/internal/biz/system"
|
||||
"kra/pkg/response"
|
||||
"kra/pkg/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type UserApi struct{}
|
||||
|
||||
// RegisterRequest 注册请求
|
||||
type RegisterRequest struct {
|
||||
Username string `json:"userName" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
NickName string `json:"nickName"`
|
||||
HeaderImg string `json:"headerImg"`
|
||||
AuthorityId uint `json:"authorityId" binding:"required"`
|
||||
AuthorityIds []uint `json:"authorityIds"`
|
||||
Phone string `json:"phone"`
|
||||
Email string `json:"email"`
|
||||
Enable int `json:"enable"`
|
||||
}
|
||||
|
||||
// ChangePasswordRequest 修改密码请求
|
||||
type ChangePasswordRequest struct {
|
||||
Password string `json:"password" binding:"required"`
|
||||
NewPassword string `json:"newPassword" binding:"required"`
|
||||
}
|
||||
|
||||
// ResetPasswordRequest 重置密码请求
|
||||
type ResetPasswordRequest struct {
|
||||
ID uint `json:"ID" binding:"required"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
// GetUserListRequest 获取用户列表请求
|
||||
type GetUserListRequest struct {
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
Username string `json:"username"`
|
||||
NickName string `json:"nickName"`
|
||||
Phone string `json:"phone"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
// SetUserInfoRequest 设置用户信息请求
|
||||
type SetUserInfoRequest struct {
|
||||
ID uint `json:"ID" binding:"required"`
|
||||
NickName string `json:"nickName"`
|
||||
HeaderImg string `json:"headerImg"`
|
||||
Phone string `json:"phone"`
|
||||
Email string `json:"email"`
|
||||
Enable int `json:"enable"`
|
||||
AuthorityIds []uint `json:"authorityIds"`
|
||||
}
|
||||
|
||||
// SetSelfInfoRequest 设置自己信息请求
|
||||
type SetSelfInfoRequest struct {
|
||||
NickName string `json:"nickName"`
|
||||
HeaderImg string `json:"headerImg"`
|
||||
Phone string `json:"phone"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
// SetUserAuthorityRequest 设置用户角色请求
|
||||
type SetUserAuthorityRequest struct {
|
||||
AuthorityId uint `json:"authorityId" binding:"required"`
|
||||
}
|
||||
|
||||
// SetUserAuthoritiesRequest 设置用户多角色请求
|
||||
type SetUserAuthoritiesRequest struct {
|
||||
ID uint `json:"ID" binding:"required"`
|
||||
AuthorityIds []uint `json:"authorityIds" binding:"required"`
|
||||
}
|
||||
|
||||
// DeleteUserRequest 删除用户请求
|
||||
type DeleteUserRequest struct {
|
||||
ID uint `json:"ID" binding:"required"`
|
||||
}
|
||||
|
||||
// Register 用户注册
|
||||
func (u *UserApi) Register(c *gin.Context) {
|
||||
var req RegisterRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
user := &system.User{
|
||||
Username: req.Username,
|
||||
Password: req.Password,
|
||||
NickName: req.NickName,
|
||||
HeaderImg: req.HeaderImg,
|
||||
AuthorityId: req.AuthorityId,
|
||||
Phone: req.Phone,
|
||||
Email: req.Email,
|
||||
Enable: req.Enable,
|
||||
}
|
||||
|
||||
created, err := userUsecase.Register(c, user)
|
||||
if err != nil {
|
||||
response.FailWithDetailed(gin.H{"user": created}, "注册失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"user": toUserResponse(created)}, "注册成功", c)
|
||||
}
|
||||
|
||||
// ChangePassword 修改密码
|
||||
func (u *UserApi) ChangePassword(c *gin.Context) {
|
||||
var req ChangePasswordRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
userID := utils.GetUserID(c)
|
||||
if userID == 0 {
|
||||
response.FailWithMessage("请先登录", c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := userUsecase.ChangePassword(c, userID, req.Password, req.NewPassword); err != nil {
|
||||
response.FailWithMessage("修改失败,原密码与当前账户不符", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("修改成功", c)
|
||||
}
|
||||
|
||||
// ResetPassword 重置密码
|
||||
func (u *UserApi) ResetPassword(c *gin.Context) {
|
||||
var req ResetPasswordRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := userUsecase.ResetPassword(c, req.ID, req.Password); err != nil {
|
||||
response.FailWithMessage("重置失败"+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("重置成功", c)
|
||||
}
|
||||
|
||||
// GetUserInfo 获取用户信息
|
||||
func (u *UserApi) GetUserInfo(c *gin.Context) {
|
||||
uuid := utils.GetUserUuid(c)
|
||||
user, err := userUsecase.GetUserInfo(c, uuid.String())
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"userInfo": toUserResponse(user)}, "获取成功", c)
|
||||
}
|
||||
|
||||
// GetUserList 获取用户列表
|
||||
func (u *UserApi) GetUserList(c *gin.Context) {
|
||||
var req GetUserListRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if req.Page == 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = 10
|
||||
}
|
||||
|
||||
filters := make(map[string]string)
|
||||
if req.Username != "" {
|
||||
filters["username"] = req.Username
|
||||
}
|
||||
if req.NickName != "" {
|
||||
filters["nick_name"] = req.NickName
|
||||
}
|
||||
if req.Phone != "" {
|
||||
filters["phone"] = req.Phone
|
||||
}
|
||||
if req.Email != "" {
|
||||
filters["email"] = req.Email
|
||||
}
|
||||
|
||||
users, total, err := userUsecase.GetUserList(c, req.Page, req.PageSize, filters)
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
list := make([]interface{}, len(users))
|
||||
for i, u := range users {
|
||||
list[i] = toUserResponse(u)
|
||||
}
|
||||
|
||||
response.OkWithDetailed(response.PageResult{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
}, "获取成功", c)
|
||||
}
|
||||
|
||||
// SetUserInfo 设置用户信息
|
||||
func (u *UserApi) SetUserInfo(c *gin.Context) {
|
||||
var req SetUserInfoRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 设置用户角色
|
||||
if len(req.AuthorityIds) > 0 {
|
||||
authorityID := utils.GetUserAuthorityId(c)
|
||||
if err := userUsecase.SetUserAuthorities(c, authorityID, req.ID, req.AuthorityIds); err != nil {
|
||||
response.FailWithMessage("设置失败", c)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
user := &system.User{
|
||||
ID: req.ID,
|
||||
NickName: req.NickName,
|
||||
HeaderImg: req.HeaderImg,
|
||||
Phone: req.Phone,
|
||||
Email: req.Email,
|
||||
Enable: req.Enable,
|
||||
}
|
||||
|
||||
if err := userUsecase.SetUserInfo(c, user); err != nil {
|
||||
response.FailWithMessage("设置失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("设置成功", c)
|
||||
}
|
||||
|
||||
// SetSelfInfo 设置自己信息
|
||||
func (u *UserApi) SetSelfInfo(c *gin.Context) {
|
||||
var req SetSelfInfoRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
userID := utils.GetUserID(c)
|
||||
if userID == 0 {
|
||||
response.FailWithMessage("请先登录", c)
|
||||
return
|
||||
}
|
||||
|
||||
user := &system.User{
|
||||
ID: userID,
|
||||
NickName: req.NickName,
|
||||
HeaderImg: req.HeaderImg,
|
||||
Phone: req.Phone,
|
||||
Email: req.Email,
|
||||
}
|
||||
|
||||
if err := userUsecase.SetSelfInfo(c, user); err != nil {
|
||||
response.FailWithMessage("设置失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("设置成功", c)
|
||||
}
|
||||
|
||||
// SetSelfSetting 设置自己配置
|
||||
func (u *UserApi) SetSelfSetting(c *gin.Context) {
|
||||
var req map[string]interface{}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
userID := utils.GetUserID(c)
|
||||
if userID == 0 {
|
||||
response.FailWithMessage("请先登录", c)
|
||||
return
|
||||
}
|
||||
|
||||
setting, _ := json.Marshal(req)
|
||||
if err := userUsecase.SetSelfSetting(c, userID, setting); err != nil {
|
||||
response.FailWithMessage("设置失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("设置成功", c)
|
||||
}
|
||||
|
||||
// DeleteUser 删除用户
|
||||
func (u *UserApi) DeleteUser(c *gin.Context) {
|
||||
var req DeleteUserRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
currentUserID := utils.GetUserID(c)
|
||||
if currentUserID == req.ID {
|
||||
response.FailWithMessage("删除失败, 无法删除自己。", c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := userUsecase.DeleteUser(c, req.ID); err != nil {
|
||||
response.FailWithMessage("删除失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// SetUserAuthority 设置用户角色
|
||||
func (u *UserApi) SetUserAuthority(c *gin.Context) {
|
||||
var req SetUserAuthorityRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
userID := utils.GetUserID(c)
|
||||
if userID == 0 {
|
||||
response.FailWithMessage("请先登录", c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := userUsecase.SetUserAuthority(c, userID, req.AuthorityId); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 重新生成token
|
||||
claims := utils.GetUserInfo(c)
|
||||
if claims == nil {
|
||||
response.FailWithMessage("请先登录", c)
|
||||
return
|
||||
}
|
||||
|
||||
claims.AuthorityID = req.AuthorityId
|
||||
token, err := jwtInstance.CreateToken(claims.BaseClaims)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
c.Header("new-token", token)
|
||||
c.Header("new-expires-at", strconv.FormatInt(claims.ExpiresAt.Unix(), 10))
|
||||
utils.SetToken(c, token, int(claims.ExpiresAt.Unix()-time.Now().Unix()))
|
||||
|
||||
response.OkWithMessage("修改成功", c)
|
||||
}
|
||||
|
||||
// SetUserAuthorities 设置用户多角色
|
||||
func (u *UserApi) SetUserAuthorities(c *gin.Context) {
|
||||
var req SetUserAuthoritiesRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
authorityID := utils.GetUserAuthorityId(c)
|
||||
if err := userUsecase.SetUserAuthorities(c, authorityID, req.ID, req.AuthorityIds); err != nil {
|
||||
response.FailWithMessage("修改失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("修改成功", c)
|
||||
}
|
||||
|
||||
// toUserResponse 转换用户响应
|
||||
func toUserResponse(user *system.User) map[string]interface{} {
|
||||
if user == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
result := map[string]interface{}{
|
||||
"ID": user.ID,
|
||||
"uuid": user.UUID.String(),
|
||||
"userName": user.Username,
|
||||
"nickName": user.NickName,
|
||||
"sideMode": user.SideMode,
|
||||
"headerImg": user.HeaderImg,
|
||||
"baseColor": user.BaseColor,
|
||||
"authorityId": user.AuthorityId,
|
||||
"phone": user.Phone,
|
||||
"email": user.Email,
|
||||
"enable": user.Enable,
|
||||
}
|
||||
|
||||
if user.OriginSetting != nil {
|
||||
result["originSetting"] = string(user.OriginSetting)
|
||||
}
|
||||
|
||||
if user.Authority != nil {
|
||||
result["authority"] = map[string]interface{}{
|
||||
"authorityId": user.Authority.AuthorityId,
|
||||
"authorityName": user.Authority.AuthorityName,
|
||||
"parentId": user.Authority.ParentId,
|
||||
"defaultRouter": user.Authority.DefaultRouter,
|
||||
}
|
||||
}
|
||||
|
||||
if len(user.Authorities) > 0 {
|
||||
authorities := make([]map[string]interface{}, len(user.Authorities))
|
||||
for i, a := range user.Authorities {
|
||||
authorities[i] = map[string]interface{}{
|
||||
"authorityId": a.AuthorityId,
|
||||
"authorityName": a.AuthorityName,
|
||||
"parentId": a.ParentId,
|
||||
"defaultRouter": a.DefaultRouter,
|
||||
}
|
||||
}
|
||||
result["authorities"] = authorities
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user