优化系统

This commit is contained in:
2025-11-06 23:10:17 +08:00
parent f4244c09b9
commit 0cd0b9c705
23 changed files with 1086 additions and 129 deletions
+15 -11
View File
@@ -93,7 +93,7 @@ func (c *AuthController) Login() {
"nickname": user.Nickname,
"tenant_id": user.TenantId,
"role": user.Role, // 角色ID
"type": "user", // 标识是用户登录
"type": "user", // 标识是用户登录
}
} else if employee != nil {
// 员工登录
@@ -101,16 +101,16 @@ func (c *AuthController) Login() {
usernameForToken = employee.EmployeeNo
tenantId = employee.TenantId
userInfo = map[string]interface{}{
"id": employee.Id,
"username": employee.EmployeeNo,
"name": employee.Name,
"email": employee.Email,
"phone": employee.Phone,
"tenant_id": employee.TenantId,
"id": employee.Id,
"username": employee.EmployeeNo,
"name": employee.Name,
"email": employee.Email,
"phone": employee.Phone,
"tenant_id": employee.TenantId,
"department_id": employee.DepartmentId,
"position_id": employee.PositionId,
"role": employee.Role, // 角色ID
"type": "employee", // 标识是员工登录
"position_id": employee.PositionId,
"role": employee.Role, // 角色ID
"type": "employee", // 标识是员工登录
}
} else {
c.Data["json"] = map[string]interface{}{
@@ -122,7 +122,11 @@ func (c *AuthController) Login() {
}
// 使用models包中的GenerateToken函数生成token
tokenString, err = models.GenerateToken(userId, usernameForToken, tenantId)
userType := "user"
if employee != nil {
userType = "employee"
}
tokenString, err = models.GenerateToken(userId, usernameForToken, tenantId, userType)
if err != nil {
c.Data["json"] = map[string]interface{}{
+108
View File
@@ -0,0 +1,108 @@
package controllers
import (
"server/models"
"github.com/beego/beego/v2/client/orm"
beego "github.com/beego/beego/v2/server/web"
)
// DashboardController 仪表盘控制器
type DashboardController struct {
beego.Controller
}
// GetPlatformStats 获取平台统计数据(平台用户使用)
// @router /api/dashboard/platform-stats [get]
func (c *DashboardController) GetPlatformStats() {
// 获取租户总数
tenants, err := models.GetTenantList()
tenantCount := int64(0)
if err == nil {
tenantCount = int64(len(tenants))
}
// 获取知识库总数及增长率(所有租户)
knowledgeCount, currentMonthKnowledge, lastMonthKnowledge, knowledgeGrowthRate, _ := models.GetKnowledgeCountWithGrowth(0)
// 获取用户总数(所有租户的用户,使用简单查询)
o := orm.NewOrm()
var userCount int64
o.Raw("SELECT COUNT(*) FROM yz_users WHERE delete_time IS NULL").QueryRow(&userCount)
// 获取员工总数(所有租户的员工)
var employeeCount int64
o.Raw("SELECT COUNT(*) FROM yz_tenant_employees WHERE delete_time IS NULL").QueryRow(&employeeCount)
c.Data["json"] = map[string]interface{}{
"code": 0,
"message": "success",
"data": map[string]interface{}{
"tenantCount": tenantCount,
"userCount": userCount,
"knowledgeCount": map[string]interface{}{
"total": knowledgeCount,
"currentMonth": currentMonthKnowledge,
"lastMonth": lastMonthKnowledge,
"growthRate": knowledgeGrowthRate,
},
"employeeCount": employeeCount,
},
}
c.ServeJSON()
}
// GetTenantStats 获取租户统计数据(租户员工使用)
// @router /api/dashboard/tenant-stats [get]
func (c *DashboardController) GetTenantStats() {
// 获取租户ID(从JWT token中获取)
tenantId := 0
if tenantIdVal, ok := c.Ctx.Input.GetData("tenantId").(int); ok && tenantIdVal > 0 {
if userType, ok := c.Ctx.Input.GetData("userType").(string); ok && userType == "employee" {
tenantId = tenantIdVal
}
}
if tenantId <= 0 {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "无法获取租户信息",
"data": nil,
}
c.ServeJSON()
return
}
// 获取知识库数量及增长率
knowledgeCount, currentMonthKnowledge, lastMonthKnowledge, knowledgeGrowthRate, _ := models.GetKnowledgeCountWithGrowth(tenantId)
// 获取员工数量(简单查询)
o := orm.NewOrm()
var employeeCount int64
o.Raw("SELECT COUNT(*) FROM yz_tenant_employees WHERE tenant_id = ? AND delete_time IS NULL", tenantId).QueryRow(&employeeCount)
// 获取部门数量
var departmentCount int64
o.Raw("SELECT COUNT(*) FROM yz_tenant_departments WHERE tenant_id = ? AND delete_time IS NULL", tenantId).QueryRow(&departmentCount)
// 获取职位数量
var positionCount int64
o.Raw("SELECT COUNT(*) FROM yz_tenant_positions WHERE tenant_id = ? AND delete_time IS NULL", tenantId).QueryRow(&positionCount)
c.Data["json"] = map[string]interface{}{
"code": 0,
"message": "success",
"data": map[string]interface{}{
"knowledgeCount": map[string]interface{}{
"total": knowledgeCount,
"currentMonth": currentMonthKnowledge,
"lastMonth": lastMonthKnowledge,
"growthRate": knowledgeGrowthRate,
},
"employeeCount": employeeCount,
"departmentCount": departmentCount,
"positionCount": positionCount,
},
}
c.ServeJSON()
}
+45 -9
View File
@@ -23,7 +23,7 @@ func (c *KnowledgeController) List() {
categoryId, _ := c.GetInt("categoryId", 0)
share, _ := c.GetInt8("share", -1) // Default -1 to query all
keyword := c.GetString("keyword", "")
// 获取租户ID(如果是员工登录,从JWT token中获取)
tenantId := 0
if tenantIdVal, ok := c.Ctx.Input.GetData("tenantId").(int); ok && tenantIdVal > 0 {
@@ -58,6 +58,42 @@ func (c *KnowledgeController) List() {
c.ServeJSON()
}
// GetCount 获取知识库数量及增长率
// @router /api/knowledge/count [get]
func (c *KnowledgeController) GetCount() {
// 获取租户ID(如果是员工登录,从JWT token中获取)
tenantId := 0
if tenantIdVal, ok := c.Ctx.Input.GetData("tenantId").(int); ok && tenantIdVal > 0 {
// 检查是否是员工登录(type === "employee"
if userType, ok := c.Ctx.Input.GetData("userType").(string); ok && userType == "employee" {
tenantId = tenantIdVal
}
}
totalCount, currentMonthCount, lastMonthCount, growthRate, err := models.GetKnowledgeCountWithGrowth(tenantId)
if err != nil {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "获取知识库数量失败: " + err.Error(),
"data": nil,
}
c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{
"code": 0,
"message": "success",
"data": map[string]interface{}{
"count": totalCount,
"currentMonthCount": currentMonthCount,
"lastMonthCount": lastMonthCount,
"growthRate": growthRate,
},
}
c.ServeJSON()
}
// Detail 获取知识详情
// @router /api/knowledge/detail [get]
func (c *KnowledgeController) Detail() {
@@ -71,7 +107,7 @@ func (c *KnowledgeController) Detail() {
c.ServeJSON()
return
}
// 获取租户ID(如果是员工登录,从JWT token中获取)
tenantId := 0
if tenantIdVal, ok := c.Ctx.Input.GetData("tenantId").(int); ok && tenantIdVal > 0 {
@@ -130,7 +166,7 @@ func (c *KnowledgeController) Create() {
// Add share parsing (default 0 for personal)
share, _ := c.GetInt8("share", 0)
knowledge.Share = share
// 获取租户ID(如果是员工登录,从JWT token中获取并设置)
if tenantIdVal, ok := c.Ctx.Input.GetData("tenantId").(int); ok && tenantIdVal > 0 {
// 检查是否是员工登录(type === "employee"
@@ -184,7 +220,7 @@ func (c *KnowledgeController) Update() {
c.ServeJSON()
return
}
// 获取租户ID(如果是员工登录,从JWT token中获取)
tenantId := 0
if tenantIdVal, ok := c.Ctx.Input.GetData("tenantId").(int); ok && tenantIdVal > 0 {
@@ -247,7 +283,7 @@ func (c *KnowledgeController) Delete() {
if deleteBy == "" {
deleteBy = "system" // 默认值
}
// 获取租户ID(如果是员工登录,从JWT token中获取)
tenantId := 0
if tenantIdVal, ok := c.Ctx.Input.GetData("tenantId").(int); ok && tenantIdVal > 0 {
@@ -287,7 +323,7 @@ func (c *KnowledgeController) GetCategories() {
tenantId = tenantIdVal
}
}
categories, err := models.GetAllCategories(tenantId)
if err != nil {
c.Data["json"] = map[string]interface{}{
@@ -332,7 +368,7 @@ func (c *KnowledgeController) GetTags() {
tenantId = tenantIdVal
}
}
tags, err := models.GetAllTags(tenantId)
if err != nil {
c.Data["json"] = map[string]interface{}{
@@ -380,7 +416,7 @@ func (c *KnowledgeController) AddCategory() {
c.ServeJSON()
return
}
// 获取租户ID(如果是员工登录,从JWT token中获取并设置)
if tenantIdVal, ok := c.Ctx.Input.GetData("tenantId").(int); ok && tenantIdVal > 0 {
// 检查是否是员工登录(type === "employee"
@@ -423,7 +459,7 @@ func (c *KnowledgeController) AddTag() {
c.ServeJSON()
return
}
// 获取租户ID(如果是员工登录,从JWT token中获取并设置)
if tenantIdVal, ok := c.Ctx.Input.GetData("tenantId").(int); ok && tenantIdVal > 0 {
// 检查是否是员工登录(type === "employee"
+63 -23
View File
@@ -6,8 +6,8 @@ import (
"server/models"
"strconv"
beego "github.com/beego/beego/v2/server/web"
"github.com/beego/beego/v2/core/logs"
beego "github.com/beego/beego/v2/server/web"
)
// PermissionController 权限管理控制器
@@ -16,9 +16,50 @@ type PermissionController struct {
}
// GetAllMenuPermissions 获取所有菜单权限列表(用于分配权限)
// 根据当前登录用户的权限和选中角色的default值过滤菜单
// 如果提供了roleId参数,根据该角色的default值过滤菜单:
// - role.default=1(平台用户角色):只能分配default=1或default=0的菜单
// - role.default=2(租户用户角色):只能分配default=2或default=0的菜单
func (c *PermissionController) GetAllMenuPermissions() {
menus, err := models.GetAllMenuPermissions()
// 从JWT中获取用户ID和用户类型
userIdData := c.Ctx.Input.GetData("userId")
userTypeData := c.Ctx.Input.GetData("userType")
if userIdData == nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "未获取到用户信息",
}
c.ServeJSON()
return
}
userId, ok := userIdData.(int)
if !ok {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "用户ID格式错误",
}
c.ServeJSON()
return
}
userType := "user" // 默认为平台用户
if userTypeData != nil {
if ut, ok := userTypeData.(string); ok {
userType = ut
}
}
// 获取可选的roleId参数(用于根据角色的default值过滤菜单)
var roleId int
if roleIdParam, err := c.GetInt("roleId"); err == nil && roleIdParam > 0 {
roleId = roleIdParam
}
// 根据用户类型、权限和角色default值获取菜单列表
menus, err := models.GetAllMenuPermissionsForUser(userId, userType, roleId)
if err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
@@ -32,7 +73,7 @@ func (c *PermissionController) GetAllMenuPermissions() {
"data": menus,
}
}
c.ServeJSON()
}
@@ -47,7 +88,7 @@ func (c *PermissionController) GetRolePermissions() {
c.ServeJSON()
return
}
permissions, err := models.GetRolePermissions(roleId)
if err != nil {
c.Data["json"] = map[string]interface{}{
@@ -62,7 +103,7 @@ func (c *PermissionController) GetRolePermissions() {
"data": permissions,
}
}
c.ServeJSON()
}
@@ -77,12 +118,12 @@ func (c *PermissionController) AssignRolePermissions() {
c.ServeJSON()
return
}
// 解析请求体
var requestData struct {
MenuIds []int `json:"menu_ids"`
}
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &requestData); err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
@@ -92,7 +133,7 @@ func (c *PermissionController) AssignRolePermissions() {
c.ServeJSON()
return
}
// 获取当前用户ID(从JWT中获取)
userIdData := c.Ctx.Input.GetData("userId")
var createBy string
@@ -102,10 +143,10 @@ func (c *PermissionController) AssignRolePermissions() {
createBy = strconv.Itoa(userId)
}
}
// 记录日志(用于调试)
logs.Info(fmt.Sprintf("开始为角色 %d 分配权限,共 %d 个菜单", roleId, len(requestData.MenuIds)))
// 分配权限
err = models.AssignRolePermissions(roleId, requestData.MenuIds, createBy)
if err != nil {
@@ -122,7 +163,7 @@ func (c *PermissionController) AssignRolePermissions() {
"message": "分配权限成功",
}
}
c.ServeJSON()
}
@@ -138,7 +179,7 @@ func (c *PermissionController) GetUserPermissions() {
c.ServeJSON()
return
}
userId, ok := userIdData.(int)
if !ok {
c.Data["json"] = map[string]interface{}{
@@ -148,7 +189,7 @@ func (c *PermissionController) GetUserPermissions() {
c.ServeJSON()
return
}
permissions, err := models.GetUserPermissions(userId)
if err != nil {
c.Data["json"] = map[string]interface{}{
@@ -163,7 +204,7 @@ func (c *PermissionController) GetUserPermissions() {
"data": permissions,
}
}
c.ServeJSON()
}
@@ -179,7 +220,7 @@ func (c *PermissionController) GetUserMenuTree() {
c.ServeJSON()
return
}
userId, ok := userIdData.(int)
if !ok {
c.Data["json"] = map[string]interface{}{
@@ -189,7 +230,7 @@ func (c *PermissionController) GetUserMenuTree() {
c.ServeJSON()
return
}
menuTree, err := models.GetUserMenuTree(userId)
if err != nil {
c.Data["json"] = map[string]interface{}{
@@ -204,7 +245,7 @@ func (c *PermissionController) GetUserMenuTree() {
"data": menuTree,
}
}
c.ServeJSON()
}
@@ -220,7 +261,7 @@ func (c *PermissionController) CheckPermission() {
c.ServeJSON()
return
}
userId, ok := userIdData.(int)
if !ok {
c.Data["json"] = map[string]interface{}{
@@ -230,7 +271,7 @@ func (c *PermissionController) CheckPermission() {
c.ServeJSON()
return
}
// 获取权限标识
permission := c.GetString("permission")
if permission == "" {
@@ -241,7 +282,7 @@ func (c *PermissionController) CheckPermission() {
c.ServeJSON()
return
}
hasPermission, err := models.CheckUserPermission(userId, permission)
if err != nil {
c.Data["json"] = map[string]interface{}{
@@ -258,7 +299,6 @@ func (c *PermissionController) CheckPermission() {
},
}
}
c.ServeJSON()
}
+58 -5
View File
@@ -12,9 +12,33 @@ type RoleController struct {
}
// GetAllRoles 获取所有角色
// 根据当前登录用户的 tenant_id 和 userType 过滤角色
// @router /api/roles [get]
func (c *RoleController) GetAllRoles() {
roles, err := models.GetAllRoles()
// 从JWT中获取租户ID和用户类型
tenantIdData := c.Ctx.Input.GetData("tenantId")
userTypeData := c.Ctx.Input.GetData("userType")
tenantId := 0
if tenantIdData != nil {
if tid, ok := tenantIdData.(int); ok {
tenantId = tid
}
}
// 如果请求参数中有 tenant_id,优先使用请求参数
if requestTenantId, err := c.GetInt("tenant_id"); err == nil && requestTenantId > 0 {
tenantId = requestTenantId
}
userType := "user" // 默认为平台用户
if userTypeData != nil {
if ut, ok := userTypeData.(string); ok {
userType = ut
}
}
roles, err := models.GetAllRoles(tenantId, userType)
if err != nil {
c.Data["json"] = map[string]interface{}{
"code": 1,
@@ -126,11 +150,11 @@ func (c *RoleController) GetRoleByTenantId() {
c.ServeJSON()
return
}
// 获取当前登录用户类型(从JWT中间件获取)
userType, _ := c.Ctx.Input.GetData("userType").(string)
isEmployee := userType == "employee"
// 转换为前端需要的格式,确保包含 tenantId 和 default 字段
// 如果是租户登录(employee),过滤掉 default=1 的角色
roleList := make([]map[string]interface{}, 0)
@@ -139,7 +163,7 @@ func (c *RoleController) GetRoleByTenantId() {
if isEmployee && role.Default == 1 {
continue
}
roleList = append(roleList, map[string]interface{}{
"roleId": role.RoleId,
"tenantId": role.TenantId,
@@ -155,7 +179,7 @@ func (c *RoleController) GetRoleByTenantId() {
"updateBy": role.UpdateBy,
})
}
c.Data["json"] = map[string]interface{}{
"code": 0,
"message": "获取角色列表成功",
@@ -199,6 +223,21 @@ func (c *RoleController) CreateRole() {
return
}
// 如果请求中没有 tenant_id,从JWT中获取
if role.TenantId == 0 {
if tenantIdData := c.Ctx.Input.GetData("tenantId"); tenantIdData != nil {
if tid, ok := tenantIdData.(int); ok && tid > 0 {
role.TenantId = tid
}
}
}
// 获取当前用户名(用于记录创建操作)
if username, ok := c.Ctx.Input.GetData("username").(string); ok && username != "" {
role.CreateBy = username
role.UpdateBy = username
}
// 检查角色代码是否已存在
existingRole, err := models.GetRoleByCode(role.RoleCode)
if err == nil && existingRole != nil {
@@ -291,6 +330,20 @@ func (c *RoleController) UpdateRole() {
// 设置角色ID
role.RoleId = roleId
// 如果请求中没有 tenant_id,从JWT中获取
if role.TenantId == 0 {
if tenantIdData := c.Ctx.Input.GetData("tenantId"); tenantIdData != nil {
if tid, ok := tenantIdData.(int); ok && tid > 0 {
role.TenantId = tid
}
}
}
// 获取当前用户名(用于记录更新操作)
if username, ok := c.Ctx.Input.GetData("username").(string); ok && username != "" {
role.UpdateBy = username
}
err = models.UpdateRole(&role)
if err != nil {
c.Data["json"] = map[string]interface{}{