- 前端: React 18 + TypeScript + Vite + Tailwind CSS + shadcn/ui - 后端: Go + Gin + GORM + JWT (golang-jwt/jwt/v5) - 功能: 服务器管理、K8s集群、监控告警、CI/CD、自动化运维、审批流程、审计日志 - 文档: 设计文档(ROADMAP/PDD/架构/模块)
436 lines
11 KiB
Go
436 lines
11 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"ezops/internal/models"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ApprovalHandler 审批处理器
|
|
type ApprovalHandler struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewApprovalHandler 创建审批处理器
|
|
func NewApprovalHandler(db *gorm.DB) *ApprovalHandler {
|
|
return &ApprovalHandler{db: db}
|
|
}
|
|
|
|
// ListTemplates 获取审批模板列表
|
|
// @Summary 获取审批模板列表
|
|
// @Description 获取所有审批模板列表
|
|
// @Tags 审批流程
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param page query int false "页码"
|
|
// @Param page_size query int false "每页数量"
|
|
// @Success 200 {object} models.Response{data=models.ListResponse}
|
|
// @Router /approvals/templates [get]
|
|
func (h *ApprovalHandler) ListTemplates(c *gin.Context) {
|
|
var query models.PageQuery
|
|
c.ShouldBindQuery(&query)
|
|
|
|
var total int64
|
|
h.db.Model(&models.ApprovalTemplate{}).Count(&total)
|
|
|
|
var templates []models.ApprovalTemplate
|
|
h.db.Offset(query.GetOffset()).Limit(query.GetLimit()).Find(&templates)
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: models.ListResponse{
|
|
List: templates,
|
|
Total: total,
|
|
},
|
|
})
|
|
}
|
|
|
|
// GetTemplate 获取审批模板详情
|
|
// @Summary 获取审批模板详情
|
|
// @Description 获取指定审批模板的详细信息
|
|
// @Tags 审批流程
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "模板ID"
|
|
// @Success 200 {object} models.Response{data=models.ApprovalTemplate}
|
|
// @Router /approvals/templates/{id} [get]
|
|
func (h *ApprovalHandler) GetTemplate(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var template models.ApprovalTemplate
|
|
if err := h.db.First(&template, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, models.Response{
|
|
Code: 404,
|
|
Message: "模板不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: template,
|
|
})
|
|
}
|
|
|
|
// CreateTemplate 创建审批模板
|
|
// @Summary 创建审批模板
|
|
// @Description 创建新审批模板
|
|
// @Tags 审批流程
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param request body models.CreateApprovalTemplateRequest true "创建请求"
|
|
// @Success 200 {object} models.Response{data=models.ApprovalTemplate}
|
|
// @Router /approvals/templates [post]
|
|
func (h *ApprovalHandler) CreateTemplate(c *gin.Context) {
|
|
var req models.CreateApprovalTemplateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, models.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
template := models.ApprovalTemplate{
|
|
Name: req.Name,
|
|
Code: req.Code,
|
|
Description: req.Description,
|
|
Type: req.Type,
|
|
FormSchema: req.FormSchema,
|
|
FlowConfig: req.FlowConfig,
|
|
Enabled: true,
|
|
}
|
|
|
|
if err := h.db.Create(&template).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, models.Response{
|
|
Code: 500,
|
|
Message: "创建失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "创建成功",
|
|
Data: template,
|
|
})
|
|
}
|
|
|
|
// UpdateTemplate 更新审批模板
|
|
// @Summary 更新审批模板
|
|
// @Description 更新指定审批模板
|
|
// @Tags 审批流程
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "模板ID"
|
|
// @Param request body models.CreateApprovalTemplateRequest true "更新请求"
|
|
// @Success 200 {object} models.Response{data=models.ApprovalTemplate}
|
|
// @Router /approvals/templates/{id} [put]
|
|
func (h *ApprovalHandler) UpdateTemplate(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var req models.CreateApprovalTemplateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, models.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
var template models.ApprovalTemplate
|
|
if err := h.db.First(&template, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, models.Response{
|
|
Code: 404,
|
|
Message: "模板不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
updates := map[string]interface{}{
|
|
"name": req.Name,
|
|
"description": req.Description,
|
|
"type": req.Type,
|
|
"form_schema": req.FormSchema,
|
|
"flow_config": req.FlowConfig,
|
|
}
|
|
|
|
if err := h.db.Model(&template).Updates(updates).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, models.Response{
|
|
Code: 500,
|
|
Message: "更新失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
h.db.First(&template, id)
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "更新成功",
|
|
Data: template,
|
|
})
|
|
}
|
|
|
|
// DeleteTemplate 删除审批模板
|
|
// @Summary 删除审批模板
|
|
// @Description 删除指定审批模板
|
|
// @Tags 审批流程
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "模板ID"
|
|
// @Success 200 {object} models.Response
|
|
// @Router /approvals/templates/{id} [delete]
|
|
func (h *ApprovalHandler) DeleteTemplate(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
if err := h.db.Delete(&models.ApprovalTemplate{}, id).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, models.Response{
|
|
Code: 500,
|
|
Message: "删除失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "删除成功",
|
|
})
|
|
}
|
|
|
|
// ListProcesses 获取审批流程列表
|
|
// @Summary 获取审批流程列表
|
|
// @Description 获取所有审批流程列表
|
|
// @Tags 审批流程
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param page query int false "页码"
|
|
// @Param page_size query int false "每页数量"
|
|
// @Param status query string false "状态过滤"
|
|
// @Success 200 {object} models.Response{data=models.ListResponse}
|
|
// @Router /approvals/processes [get]
|
|
func (h *ApprovalHandler) ListProcesses(c *gin.Context) {
|
|
var query models.PageQuery
|
|
c.ShouldBindQuery(&query)
|
|
|
|
db := h.db.Model(&models.ApprovalProcess{}).Preload("Template").Preload("ApplicantUser")
|
|
|
|
// 状态过滤
|
|
if status := c.Query("status"); status != "" {
|
|
db = db.Where("status = ?", status)
|
|
}
|
|
|
|
var total int64
|
|
db.Count(&total)
|
|
|
|
var processes []models.ApprovalProcess
|
|
db.Order("created_at DESC").
|
|
Offset(query.GetOffset()).
|
|
Limit(query.GetLimit()).
|
|
Find(&processes)
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: models.ListResponse{
|
|
List: processes,
|
|
Total: total,
|
|
},
|
|
})
|
|
}
|
|
|
|
// GetProcess 获取审批流程详情
|
|
// @Summary 获取审批流程详情
|
|
// @Description 获取指定审批流程的详细信息
|
|
// @Tags 审批流程
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "流程ID"
|
|
// @Success 200 {object} models.Response{data=models.ApprovalProcess}
|
|
// @Router /approvals/processes/{id} [get]
|
|
func (h *ApprovalHandler) GetProcess(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var process models.ApprovalProcess
|
|
if err := h.db.Preload("Template").Preload("ApplicantUser").Preload("Steps.ApproverUser").First(&process, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, models.Response{
|
|
Code: 404,
|
|
Message: "流程不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: process,
|
|
})
|
|
}
|
|
|
|
// CreateProcess 创建审批流程
|
|
// @Summary 创建审批流程
|
|
// @Description 创建新审批流程
|
|
// @Tags 审批流程
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param request body models.CreateApprovalProcessRequest true "创建请求"
|
|
// @Success 200 {object} models.Response{data=models.ApprovalProcess}
|
|
// @Router /approvals/processes [post]
|
|
func (h *ApprovalHandler) CreateProcess(c *gin.Context) {
|
|
var req models.CreateApprovalProcessRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, models.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// 获取模板
|
|
var template models.ApprovalTemplate
|
|
if err := h.db.First(&template, req.TemplateID).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, models.Response{
|
|
Code: 404,
|
|
Message: "模板不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
process := models.ApprovalProcess{
|
|
TemplateID: req.TemplateID,
|
|
Title: req.Title,
|
|
FormData: req.FormData,
|
|
Status: "pending",
|
|
CurrentStep: 0,
|
|
TotalSteps: 1, // 简化处理
|
|
}
|
|
|
|
if err := h.db.Create(&process).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, models.Response{
|
|
Code: 500,
|
|
Message: "创建失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
h.db.Preload("Template").First(&process, process.ID)
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "创建成功",
|
|
Data: process,
|
|
})
|
|
}
|
|
|
|
// ApproveProcess 审批通过
|
|
// @Summary 审批通过
|
|
// @Description 审批通过指定流程
|
|
// @Tags 审批流程
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "流程ID"
|
|
// @Param request body models.ApproveRequest true "审批请求"
|
|
// @Success 200 {object} models.Response{data=models.ApprovalProcess}
|
|
// @Router /approvals/processes/{id}/approve [post]
|
|
func (h *ApprovalHandler) ApproveProcess(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var req models.ApproveRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, models.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误",
|
|
})
|
|
return
|
|
}
|
|
|
|
var process models.ApprovalProcess
|
|
if err := h.db.First(&process, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, models.Response{
|
|
Code: 404,
|
|
Message: "流程不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
process.Status = "approved"
|
|
// now := time.Now()
|
|
// process.ApprovedAt = &now
|
|
|
|
if err := h.db.Save(&process).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, models.Response{
|
|
Code: 500,
|
|
Message: "审批失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "审批通过",
|
|
Data: process,
|
|
})
|
|
}
|
|
|
|
// RejectProcess 审批拒绝
|
|
// @Summary 审批拒绝
|
|
// @Description 审批拒绝指定流程
|
|
// @Tags 审批流程
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "流程ID"
|
|
// @Param request body models.ApproveRequest true "审批请求"
|
|
// @Success 200 {object} models.Response{data=models.ApprovalProcess}
|
|
// @Router /approvals/processes/{id}/reject [post]
|
|
func (h *ApprovalHandler) RejectProcess(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var req models.ApproveRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, models.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误",
|
|
})
|
|
return
|
|
}
|
|
|
|
var process models.ApprovalProcess
|
|
if err := h.db.First(&process, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, models.Response{
|
|
Code: 404,
|
|
Message: "流程不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
process.Status = "rejected"
|
|
process.RejectReason = req.Comment
|
|
// now := time.Now()
|
|
// process.RejectedAt = &now
|
|
|
|
if err := h.db.Save(&process).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, models.Response{
|
|
Code: 500,
|
|
Message: "拒绝失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "已拒绝",
|
|
Data: process,
|
|
})
|
|
}
|