Initial commit
This commit is contained in:
165
controllers/auth_controller.go
Normal file
165
controllers/auth_controller.go
Normal file
@@ -0,0 +1,165 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.beifan.cn/trace-system/backend-go/models"
|
||||
"git.beifan.cn/trace-system/backend-go/services"
|
||||
)
|
||||
|
||||
// AuthController 认证控制器
|
||||
type AuthController struct {
|
||||
authService services.AuthService
|
||||
}
|
||||
|
||||
// NewAuthController 创建认证控制器实例
|
||||
func NewAuthController() *AuthController {
|
||||
return &AuthController{
|
||||
authService: services.AuthService{},
|
||||
}
|
||||
}
|
||||
|
||||
// Login 登录
|
||||
// @Summary 用户登录
|
||||
// @Description 验证用户身份并返回 JWT 令牌
|
||||
// @Tags 认证
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param loginData body models.LoginDTO true "登录数据"
|
||||
// @Success 200 {object} gin.H{message: string, accessToken: string, user: models.UserDTO}
|
||||
// @Failure 400 {object} gin.H{message: string}
|
||||
// @Failure 401 {object} gin.H{message: string}
|
||||
// @Router /auth/login [post]
|
||||
func (c *AuthController) Login(ctx *gin.Context) {
|
||||
var loginData models.LoginDTO
|
||||
if err := ctx.ShouldBindJSON(&loginData); err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "无效的请求数据",
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
user, err := c.authService.ValidateUser(loginData.Username, loginData.Password)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusUnauthorized, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
token, err := c.authService.GenerateToken(user)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "令牌生成失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"message": "登录成功",
|
||||
"accessToken": token,
|
||||
"user": models.UserDTO{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
Name: user.Name,
|
||||
Email: user.Email,
|
||||
Role: user.Role,
|
||||
CreatedAt: user.CreatedAt,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// GetProfile 获取用户信息
|
||||
// @Summary 获取用户信息
|
||||
// @Description 获取当前登录用户的个人信息
|
||||
// @Tags 认证
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Success 200 {object} models.UserDTO
|
||||
// @Failure 401 {object} gin.H{message: string}
|
||||
// @Router /auth/profile [get]
|
||||
func (c *AuthController) GetProfile(ctx *gin.Context) {
|
||||
userModel, ok := GetCurrentUser(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
profile, err := c.authService.GetProfile(userModel.ID)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusUnauthorized, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "获取用户信息成功", gin.H{
|
||||
"user": profile,
|
||||
})
|
||||
}
|
||||
|
||||
// ChangePassword 修改密码
|
||||
// @Summary 修改密码
|
||||
// @Description 修改当前登录用户的密码
|
||||
// @Tags 认证
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param passwordData body models.ChangePasswordDTO true "密码修改数据"
|
||||
// @Success 200 {object} gin.H{message: string}
|
||||
// @Failure 400 {object} gin.H{message: string}
|
||||
// @Failure 401 {object} gin.H{message: string}
|
||||
// @Router /auth/change-password [post]
|
||||
func (c *AuthController) ChangePassword(ctx *gin.Context) {
|
||||
userModel, ok := GetCurrentUser(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
var changePasswordData models.ChangePasswordDTO
|
||||
if !BindJSON(ctx, &changePasswordData) {
|
||||
return
|
||||
}
|
||||
|
||||
err := c.authService.ChangePassword(userModel.ID, changePasswordData.CurrentPassword, changePasswordData.NewPassword)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusUnauthorized, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "密码修改成功")
|
||||
}
|
||||
|
||||
// UpdateProfile 更新用户信息
|
||||
// @Summary 更新用户信息
|
||||
// @Description 更新当前登录用户的个人信息
|
||||
// @Tags 认证
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param profileData body models.UpdateProfileDTO true "用户信息更新数据"
|
||||
// @Success 200 {object} models.UserDTO
|
||||
// @Failure 400 {object} gin.H{message: string}
|
||||
// @Failure 401 {object} gin.H{message: string}
|
||||
// @Router /auth/profile [put]
|
||||
func (c *AuthController) UpdateProfile(ctx *gin.Context) {
|
||||
userModel, ok := GetCurrentUser(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
var updateProfileData models.UpdateProfileDTO
|
||||
if !BindJSON(ctx, &updateProfileData) {
|
||||
return
|
||||
}
|
||||
|
||||
profile, err := c.authService.UpdateProfile(userModel.ID, updateProfileData.Name, updateProfileData.Email)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusUnauthorized, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "用户信息更新成功", gin.H{
|
||||
"user": profile,
|
||||
})
|
||||
}
|
||||
200
controllers/companies_controller.go
Normal file
200
controllers/companies_controller.go
Normal file
@@ -0,0 +1,200 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.beifan.cn/trace-system/backend-go/services"
|
||||
)
|
||||
|
||||
// CompaniesController 企业管理控制器
|
||||
type CompaniesController struct {
|
||||
companiesService services.CompaniesService
|
||||
}
|
||||
|
||||
// NewCompaniesController 创建企业管理控制器实例
|
||||
func NewCompaniesController() *CompaniesController {
|
||||
return &CompaniesController{
|
||||
companiesService: services.CompaniesService{},
|
||||
}
|
||||
}
|
||||
|
||||
// FindAll 获取企业列表
|
||||
// @Summary 获取企业列表
|
||||
// @Description 获取企业列表,支持分页和搜索
|
||||
// @Tags 企业管理
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param page query int false "页码"
|
||||
// @Param limit query int false "每页数量"
|
||||
// @Param search query string false "搜索关键词"
|
||||
// @Success 200 {object} gin.H{message: string, data: []models.Company, pagination: gin.H{page: int, limit: int, total: int, totalPages: int}}
|
||||
// @Failure 401 {object} gin.H{message: string}
|
||||
// @Failure 500 {object} gin.H{message: string}
|
||||
// @Router /companies [get]
|
||||
func (c *CompaniesController) FindAll(ctx *gin.Context) {
|
||||
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
|
||||
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "20"))
|
||||
search := ctx.DefaultQuery("search", "")
|
||||
|
||||
companies, total, totalPages, err := c.companiesService.FindAll(page, limit, search)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"message": "获取企业列表成功",
|
||||
"data": companies,
|
||||
"pagination": gin.H{
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
"total": total,
|
||||
"totalPages": totalPages,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Create 创建企业
|
||||
// @Summary 创建企业
|
||||
// @Description 创建新的企业
|
||||
// @Tags 企业管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param companyData body gin.H{companyName: string} true "企业数据"
|
||||
// @Success 201 {object} gin.H{message: string, company: models.Company}
|
||||
// @Failure 400 {object} gin.H{message: string}
|
||||
// @Failure 401 {object} gin.H{message: string}
|
||||
// @Failure 409 {object} gin.H{message: string}
|
||||
// @Failure 500 {object} gin.H{message: string}
|
||||
// @Router /companies [post]
|
||||
func (c *CompaniesController) Create(ctx *gin.Context) {
|
||||
var companyData struct {
|
||||
CompanyName string `json:"companyName" validate:"required"`
|
||||
}
|
||||
if err := ctx.ShouldBindJSON(&companyData); err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "无效的请求数据",
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
company, err := c.companiesService.Create(companyData.CompanyName)
|
||||
if err != nil {
|
||||
if err.Error() == "企业名称已存在" {
|
||||
ctx.JSON(http.StatusConflict, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
} else {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusCreated, gin.H{
|
||||
"message": "企业创建成功",
|
||||
"company": company,
|
||||
})
|
||||
}
|
||||
|
||||
// Update 更新企业信息
|
||||
// @Summary 更新企业信息
|
||||
// @Description 更新企业信息
|
||||
// @Tags 企业管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param companyName path string true "企业名称"
|
||||
// @Param companyData body gin.H{companyName?: string, isActive?: bool} true "企业数据"
|
||||
// @Success 200 {object} gin.H{message: string, company: models.Company}
|
||||
// @Failure 400 {object} gin.H{message: string}
|
||||
// @Failure 401 {object} gin.H{message: string}
|
||||
// @Failure 404 {object} gin.H{message: string}
|
||||
// @Failure 409 {object} gin.H{message: string}
|
||||
// @Failure 500 {object} gin.H{message: string}
|
||||
// @Router /companies/{companyName} [put]
|
||||
func (c *CompaniesController) Update(ctx *gin.Context) {
|
||||
companyName := ctx.Param("companyName")
|
||||
|
||||
var companyData struct {
|
||||
CompanyName string `json:"companyName"`
|
||||
IsActive bool `json:"isActive"`
|
||||
}
|
||||
if err := ctx.ShouldBindJSON(&companyData); err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "无效的请求数据",
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
company, err := c.companiesService.Update(companyName, companyData.CompanyName, companyData.IsActive)
|
||||
if err != nil {
|
||||
if err.Error() == "企业不存在" {
|
||||
ctx.JSON(http.StatusNotFound, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
} else if err.Error() == "企业名称已存在" {
|
||||
ctx.JSON(http.StatusConflict, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
} else {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"message": "企业信息更新成功",
|
||||
"company": company,
|
||||
})
|
||||
}
|
||||
|
||||
// Delete 删除企业
|
||||
// @Summary 删除企业
|
||||
// @Description 删除企业
|
||||
// @Tags 企业管理
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param companyName path string true "企业名称"
|
||||
// @Success 200 {object} gin.H{message: string}
|
||||
// @Failure 400 {object} gin.H{message: string}
|
||||
// @Failure 401 {object} gin.H{message: string}
|
||||
// @Failure 404 {object} gin.H{message: string}
|
||||
// @Failure 500 {object} gin.H{message: string}
|
||||
// @Router /companies/{companyName} [delete]
|
||||
func (c *CompaniesController) Delete(ctx *gin.Context) {
|
||||
companyName := ctx.Param("companyName")
|
||||
|
||||
err := c.companiesService.Delete(companyName)
|
||||
if err != nil {
|
||||
if err.Error() == "企业不存在" {
|
||||
ctx.JSON(http.StatusNotFound, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
} else if err.Error() == "企业下还有序列号,无法删除" {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
} else {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"message": "企业删除成功",
|
||||
})
|
||||
}
|
||||
81
controllers/helper.go
Normal file
81
controllers/helper.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.beifan.cn/trace-system/backend-go/models"
|
||||
)
|
||||
|
||||
// GetCurrentUser 从上下文中获取当前用户
|
||||
// 如果用户未认证,返回 false 并返回 401 错误
|
||||
func GetCurrentUser(ctx *gin.Context) (models.User, bool) {
|
||||
user, exists := ctx.Get("user")
|
||||
if !exists {
|
||||
ctx.JSON(http.StatusUnauthorized, gin.H{
|
||||
"message": "未认证",
|
||||
})
|
||||
return models.User{}, false
|
||||
}
|
||||
|
||||
userModel, ok := user.(models.User)
|
||||
if !ok {
|
||||
ctx.JSON(http.StatusUnauthorized, gin.H{
|
||||
"message": "用户信息无效",
|
||||
})
|
||||
return models.User{}, false
|
||||
}
|
||||
|
||||
return userModel, true
|
||||
}
|
||||
|
||||
// BindJSON 绑定 JSON 请求体
|
||||
// 如果解析失败,返回错误并返回 400 响应
|
||||
func BindJSON(ctx *gin.Context, obj interface{}) bool {
|
||||
if err := ctx.ShouldBindJSON(obj); err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "无效的请求数据",
|
||||
"error": err.Error(),
|
||||
})
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// BindQuery 绑定查询参数
|
||||
// 如果解析失败,返回错误并返回 400 响应
|
||||
func BindQuery(ctx *gin.Context, obj interface{}) bool {
|
||||
if err := ctx.ShouldBindQuery(obj); err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "无效的查询参数",
|
||||
"error": err.Error(),
|
||||
})
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// ErrorResponse 返回错误响应
|
||||
func ErrorResponse(ctx *gin.Context, status int, message string, err ...error) {
|
||||
response := gin.H{
|
||||
"message": message,
|
||||
}
|
||||
if len(err) > 0 && err[0] != nil {
|
||||
response["error"] = err[0].Error()
|
||||
}
|
||||
ctx.JSON(status, response)
|
||||
}
|
||||
|
||||
// SuccessResponse 返回成功响应
|
||||
func SuccessResponse(ctx *gin.Context, message string, data ...gin.H) {
|
||||
response := gin.H{
|
||||
"message": message,
|
||||
}
|
||||
if len(data) > 0 {
|
||||
for key, value := range data[0] {
|
||||
response[key] = value
|
||||
}
|
||||
}
|
||||
ctx.JSON(http.StatusOK, response)
|
||||
}
|
||||
268
controllers/serials_controller.go
Normal file
268
controllers/serials_controller.go
Normal file
@@ -0,0 +1,268 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.beifan.cn/trace-system/backend-go/models"
|
||||
"git.beifan.cn/trace-system/backend-go/services"
|
||||
)
|
||||
|
||||
// SerialsController 序列号控制器
|
||||
type SerialsController struct {
|
||||
serialsService services.SerialsService
|
||||
}
|
||||
|
||||
// NewSerialsController 创建序列号控制器实例
|
||||
func NewSerialsController() *SerialsController {
|
||||
return &SerialsController{
|
||||
serialsService: services.SerialsService{},
|
||||
}
|
||||
}
|
||||
|
||||
// Generate 生成序列号
|
||||
// @Summary 生成序列号
|
||||
// @Description 生成指定数量的序列号
|
||||
// @Tags 序列号管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param generateData body models.GenerateSerialDTO true "生成数据"
|
||||
// @Success 200 {object} gin.H{message: string, serials: []models.Serial}
|
||||
// @Failure 400 {object} gin.H{message: string}
|
||||
// @Failure 401 {object} gin.H{message: string}
|
||||
// @Failure 500 {object} gin.H{message: string}
|
||||
// @Router /serials/generate [post]
|
||||
func (c *SerialsController) Generate(ctx *gin.Context) {
|
||||
userModel, ok := GetCurrentUser(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
var generateData models.GenerateSerialDTO
|
||||
if !BindJSON(ctx, &generateData) {
|
||||
return
|
||||
}
|
||||
|
||||
serials, err := c.serialsService.Generate(
|
||||
generateData.CompanyName,
|
||||
generateData.Quantity,
|
||||
generateData.ValidDays,
|
||||
userModel.ID,
|
||||
)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "成功生成 "+strconv.Itoa(len(serials))+" 个序列号", gin.H{
|
||||
"serials": serials,
|
||||
})
|
||||
}
|
||||
|
||||
// GenerateWithPrefix 带前缀生成序列号
|
||||
// @Summary 带前缀生成序列号
|
||||
// @Description 生成带有指定前缀的序列号
|
||||
// @Tags 序列号管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param generateData body models.GenerateWithPrefixDTO true "生成数据"
|
||||
// @Success 200 {object} gin.H{message: string, serials: []models.Serial}
|
||||
// @Failure 400 {object} gin.H{message: string}
|
||||
// @Failure 401 {object} gin.H{message: string}
|
||||
// @Failure 500 {object} gin.H{message: string}
|
||||
// @Router /serials/generate-with-prefix [post]
|
||||
func (c *SerialsController) GenerateWithPrefix(ctx *gin.Context) {
|
||||
userModel, ok := GetCurrentUser(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
var generateData models.GenerateWithPrefixDTO
|
||||
if !BindJSON(ctx, &generateData) {
|
||||
return
|
||||
}
|
||||
|
||||
serials, err := c.serialsService.Generate(
|
||||
generateData.CompanyName,
|
||||
generateData.Quantity,
|
||||
generateData.ValidDays,
|
||||
userModel.ID,
|
||||
generateData.SerialPrefix,
|
||||
)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "成功生成 "+strconv.Itoa(len(serials))+" 个序列号", gin.H{
|
||||
"serials": serials,
|
||||
})
|
||||
}
|
||||
|
||||
// GenerateQRCode 生成二维码
|
||||
// @Summary 生成二维码
|
||||
// @Description 为指定序列号生成查询二维码
|
||||
// @Tags 序列号管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param serialNumber path string true "序列号"
|
||||
// @Param qrCodeData body models.QRCodeDTO false "二维码数据"
|
||||
// @Success 200 {object} gin.H{message: string, qrCodeData: string, queryUrl: string}
|
||||
// @Failure 400 {object} gin.H{message: string}
|
||||
// @Failure 401 {object} gin.H{message: string}
|
||||
// @Failure 404 {object} gin.H{message: string}
|
||||
// @Failure 500 {object} gin.H{message: string}
|
||||
// @Router /serials/{serialNumber}/qrcode [post]
|
||||
func (c *SerialsController) GenerateQRCode(ctx *gin.Context) {
|
||||
serialNumber := ctx.Param("serialNumber")
|
||||
|
||||
var qrCodeData models.QRCodeDTO
|
||||
if !BindJSON(ctx, &qrCodeData) {
|
||||
return
|
||||
}
|
||||
|
||||
protocol := "http"
|
||||
if ctx.Request.TLS != nil {
|
||||
protocol = "https"
|
||||
}
|
||||
|
||||
qrCodeBase64, queryUrl, err := c.serialsService.GenerateQRCode(
|
||||
serialNumber,
|
||||
qrCodeData.BaseUrl,
|
||||
ctx.Request.Host,
|
||||
protocol,
|
||||
)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "二维码生成成功", gin.H{
|
||||
"qrCodeData": qrCodeBase64,
|
||||
"queryUrl": queryUrl,
|
||||
})
|
||||
}
|
||||
|
||||
// Query 查询序列号信息
|
||||
// @Summary 查询序列号信息
|
||||
// @Description 查询指定序列号的详细信息
|
||||
// @Tags 序列号查询
|
||||
// @Produce json
|
||||
// @Param serialNumber path string true "序列号"
|
||||
// @Success 200 {object} gin.H{message: string, serial: models.Serial}
|
||||
// @Failure 400 {object} gin.H{message: string}
|
||||
// @Failure 404 {object} gin.H{message: string}
|
||||
// @Failure 500 {object} gin.H{message: string}
|
||||
// @Router /serials/{serialNumber}/query [get]
|
||||
func (c *SerialsController) Query(ctx *gin.Context) {
|
||||
serialNumber := ctx.Param("serialNumber")
|
||||
|
||||
serial, err := c.serialsService.Query(serialNumber)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "查询成功", gin.H{
|
||||
"serial": serial,
|
||||
})
|
||||
}
|
||||
|
||||
// FindAll 获取序列号列表
|
||||
// @Summary 获取序列号列表
|
||||
// @Description 获取序列号列表,支持分页和搜索
|
||||
// @Tags 序列号管理
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param page query int false "页码"
|
||||
// @Param limit query int false "每页数量"
|
||||
// @Param search query string false "搜索关键词"
|
||||
// @Success 200 {object} gin.H{message: string, data: []models.Serial, pagination: gin.H{page: int, limit: int, total: int, totalPages: int}}
|
||||
// @Failure 401 {object} gin.H{message: string}
|
||||
// @Failure 500 {object} gin.H{message: string}
|
||||
// @Router /serials [get]
|
||||
func (c *SerialsController) FindAll(ctx *gin.Context) {
|
||||
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
|
||||
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "20"))
|
||||
search := ctx.DefaultQuery("search", "")
|
||||
|
||||
serials, total, totalPages, err := c.serialsService.FindAll(page, limit, search)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "获取序列号列表成功", gin.H{
|
||||
"data": serials,
|
||||
"pagination": gin.H{
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
"total": total,
|
||||
"totalPages": totalPages,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Update 更新序列号信息
|
||||
// @Summary 更新序列号信息
|
||||
// @Description 更新指定序列号的信息
|
||||
// @Tags 序列号管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param serialNumber path string true "序列号"
|
||||
// @Param updateData body models.UpdateSerialDTO true "更新数据"
|
||||
// @Success 200 {object} gin.H{message: string, serial: models.Serial}
|
||||
// @Failure 400 {object} gin.H{message: string}
|
||||
// @Failure 401 {object} gin.H{message: string}
|
||||
// @Failure 404 {object} gin.H{message: string}
|
||||
// @Failure 500 {object} gin.H{message: string}
|
||||
// @Router /serials/{serialNumber} [put]
|
||||
func (c *SerialsController) Update(ctx *gin.Context) {
|
||||
serialNumber := ctx.Param("serialNumber")
|
||||
|
||||
var updateData models.UpdateSerialDTO
|
||||
if !BindJSON(ctx, &updateData) {
|
||||
return
|
||||
}
|
||||
|
||||
serial, err := c.serialsService.Update(serialNumber, updateData)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "序列号信息更新成功", gin.H{
|
||||
"serial": serial,
|
||||
})
|
||||
}
|
||||
|
||||
// Revoke 吊销序列号
|
||||
// @Summary 吊销序列号
|
||||
// @Description 吊销指定序列号
|
||||
// @Tags 序列号管理
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param serialNumber path string true "序列号"
|
||||
// @Success 200 {object} gin.H{message: string}
|
||||
// @Failure 400 {object} gin.H{message: string}
|
||||
// @Failure 401 {object} gin.H{message: string}
|
||||
// @Failure 404 {object} gin.H{message: string}
|
||||
// @Failure 500 {object} gin.H{message: string}
|
||||
// @Router /serials/{serialNumber}/revoke [post]
|
||||
func (c *SerialsController) Revoke(ctx *gin.Context) {
|
||||
serialNumber := ctx.Param("serialNumber")
|
||||
|
||||
err := c.serialsService.Revoke(serialNumber)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(ctx, "序列号吊销成功")
|
||||
}
|
||||
Reference in New Issue
Block a user