first commit
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// AdminController operations for Admin
|
||||
type AdminController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
func (c *AdminController) Get() {
|
||||
c.TplName = "admin/index.tpl"
|
||||
}
|
||||
@@ -0,0 +1,388 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"server/models"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// 用于签名的密钥
|
||||
var jwtSecret = []byte("yunzer_jwt_secret_key")
|
||||
|
||||
// AuthController 处理认证相关请求
|
||||
type AuthController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// Login 处理登录请求
|
||||
func (c *AuthController) Login() {
|
||||
var username, password string
|
||||
|
||||
// 优先尝试从URL参数获取(Apifox测试方式)
|
||||
username = c.GetString("username")
|
||||
password = c.GetString("password")
|
||||
|
||||
// 如果URL参数为空,尝试从JSON请求体获取(前端方式)
|
||||
if username == "" || password == "" {
|
||||
var loginData struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &loginData)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "请求参数格式错误",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
username = loginData.Username
|
||||
password = loginData.Password
|
||||
}
|
||||
|
||||
// 添加日志调试
|
||||
fmt.Println("接收到的登录请求:")
|
||||
fmt.Println("用户名:", username)
|
||||
fmt.Println("密码:", password)
|
||||
|
||||
// 验证用户
|
||||
fmt.Println("开始验证用户:", username)
|
||||
user, err := models.ValidateUser(username, password)
|
||||
fmt.Println("验证结果:", err)
|
||||
if user != nil {
|
||||
fmt.Println("用户信息:ID=", user.Id, "Username=", user.Username, "Salt=", user.Salt)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
// 登录失败
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "用户名或密码错误",
|
||||
"data": nil,
|
||||
}
|
||||
} else {
|
||||
// 使用models包中的GenerateToken函数生成token
|
||||
tokenString, err := models.GenerateToken(user.Id, user.Username)
|
||||
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "生成token失败",
|
||||
"data": nil,
|
||||
}
|
||||
} else {
|
||||
// 登录成功
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "登录成功",
|
||||
"data": map[string]interface{}{
|
||||
"accessToken": tokenString,
|
||||
"token": tokenString, // 兼容性
|
||||
"user": map[string]interface{}{
|
||||
"id": user.Id,
|
||||
"username": user.Username,
|
||||
"email": user.Email,
|
||||
"avatar": user.Avatar,
|
||||
"nickname": user.Nickname,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// ResetPassword 重置用户密码
|
||||
func (c *AuthController) ResetPassword() {
|
||||
// 获取请求参数
|
||||
username := c.GetString("username")
|
||||
superPassword := c.GetString("superPassword")
|
||||
|
||||
// 调用模型方法
|
||||
err := models.ResetPassword(username, superPassword)
|
||||
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"success": false, "message": err.Error()}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{"success": true, "message": "密码重置成功"}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// ChangePassword 修改用户密码
|
||||
func (c *AuthController) ChangePassword() {
|
||||
// 获取请求参数
|
||||
username := c.GetString("username")
|
||||
oldPassword := c.GetString("oldPassword")
|
||||
newPassword := c.GetString("newPassword")
|
||||
|
||||
// 调用模型方法
|
||||
err := models.ChangePassword(username, oldPassword, newPassword)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"success": false, "message": err.Error()}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{"success": true, "message": "密码修改成功"}
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// Logout 处理登出请求
|
||||
func (c *AuthController) Logout() {
|
||||
// 在实际应用中,这里需要处理JWT或Session的清除
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "登出成功",
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// FindAllUsers 获取所有用户
|
||||
func (c *AuthController) FindAllUsers() {
|
||||
users := models.FindAllUsers()
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取用户列表成功",
|
||||
"data": users,
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetUserByUsername 通过用户名查询用户信息
|
||||
func (c *AuthController) GetUserByUsername() {
|
||||
// 获取请求参数中的用户名
|
||||
username := c.GetString("username")
|
||||
if username == "" {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "用户名不能为空",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 调用模型层方法查询用户
|
||||
user, err := models.GetUserByUsername(username) // 假设models层有这个方法
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "查询用户失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
} else if user == nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "用户不存在",
|
||||
"data": nil,
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "查询成功",
|
||||
"data": map[string]interface{}{
|
||||
"id": user.Id,
|
||||
"username": user.Username,
|
||||
"email": user.Email,
|
||||
"avatar": user.Avatar,
|
||||
"nickname": user.Nickname,
|
||||
// 其他需要返回的用户字段
|
||||
},
|
||||
}
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// AddUser 添加新用户
|
||||
func (c *AuthController) AddUser() {
|
||||
// 定义接收用户数据的结构体(与JSON请求体对应)
|
||||
var userData struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Email string `json:"email"`
|
||||
Nickname string `json:"nickname"`
|
||||
Avatar string `json:"avatar"`
|
||||
}
|
||||
|
||||
// 解析请求体JSON数据
|
||||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &userData)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "请求参数格式错误: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 校验必要参数
|
||||
if userData.Username == "" {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "用户名不能为空",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
if userData.Password == "" {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "密码不能为空",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 调用模型层方法添加用户(传递参数,接收新用户对象)
|
||||
newUser, err := models.AddUser(
|
||||
userData.Username,
|
||||
userData.Password,
|
||||
userData.Email,
|
||||
userData.Nickname,
|
||||
userData.Avatar,
|
||||
)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "添加用户失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "用户添加成功",
|
||||
"data": map[string]interface{}{
|
||||
"id": newUser.Id,
|
||||
"username": newUser.Username,
|
||||
"email": newUser.Email,
|
||||
"nickname": newUser.Nickname,
|
||||
"avatar": newUser.Avatar,
|
||||
},
|
||||
}
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// UpdateUser 更新用户信息
|
||||
func (c *AuthController) UpdateUser() {
|
||||
// 定义接收更新数据的结构体
|
||||
var updateData struct {
|
||||
Id int `json:"id"` // 必须包含用户ID,用于定位要更新的用户
|
||||
Username string `json:"username"` // 可选更新字段
|
||||
Email string `json:"email"` // 可选更新字段
|
||||
Nickname string `json:"nickname"` // 可选更新字段
|
||||
Avatar string `json:"avatar"` // 可选更新字段
|
||||
}
|
||||
|
||||
// 解析请求体JSON
|
||||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &updateData)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "请求参数格式错误: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 校验必要参数(用户ID不能为空)
|
||||
if updateData.Id == 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "用户ID不能为空",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 调用模型层方法更新用户
|
||||
updatedUser, err := models.UpdateUser(
|
||||
updateData.Id,
|
||||
updateData.Username,
|
||||
updateData.Email,
|
||||
updateData.Nickname,
|
||||
updateData.Avatar,
|
||||
)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "更新用户失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "用户更新成功",
|
||||
"data": map[string]interface{}{
|
||||
"id": updatedUser.Id,
|
||||
"username": updatedUser.Username,
|
||||
"email": updatedUser.Email,
|
||||
"nickname": updatedUser.Nickname,
|
||||
"avatar": updatedUser.Avatar,
|
||||
},
|
||||
}
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// DeleteUser 删除用户
|
||||
func (c *AuthController) DeleteUser() {
|
||||
// 获取要删除的用户ID(从URL参数或请求体中获取)
|
||||
userId, err := c.GetInt("id") // 从URL参数获取,如 /user?id=1
|
||||
if err != nil {
|
||||
// 若URL参数获取失败,尝试从JSON请求体获取
|
||||
var deleteData struct {
|
||||
Id int `json:"id"`
|
||||
}
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &deleteData); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "用户ID获取失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
userId = deleteData.Id
|
||||
}
|
||||
|
||||
// 校验用户ID
|
||||
if userId <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "无效的用户ID",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 调用模型层方法删除用户
|
||||
err = models.DeleteUser(userId)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "删除用户失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "用户删除成功",
|
||||
"data": nil,
|
||||
}
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
type MainController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// Hello 处理 /api/hello 请求
|
||||
func (c *MainController) Hello() {
|
||||
c.Data["json"] = map[string]string{"message": "Hello from Beego backend!"}
|
||||
c.ServeJSON()
|
||||
}
|
||||
@@ -0,0 +1,432 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"server/models"
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// FileController 处理文件相关请求
|
||||
type FileController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// GetAllFiles 获取所有文件信息
|
||||
func (c *FileController) GetAllFiles() {
|
||||
files, err := models.GetAllFiles()
|
||||
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "获取文件列表失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取文件列表成功",
|
||||
"data": files,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetFileById 根据ID获取文件信息
|
||||
func (c *FileController) GetFileById() {
|
||||
idStr := c.Ctx.Input.Param(":id")
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "参数错误",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
file, err := models.GetFileById(id)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "文件不存在",
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取文件成功",
|
||||
"data": file,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetFilesByTenant 根据租户ID获取文件信息
|
||||
func (c *FileController) GetFilesByTenant() {
|
||||
tenantID := c.GetString("tenant_id")
|
||||
if tenantID == "" {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "租户ID不能为空",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
files, err := models.GetFilesByTenant(tenantID)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "获取文件列表失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取文件列表成功",
|
||||
"data": files,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetFilesByCategory 根据分类获取文件信息
|
||||
func (c *FileController) GetFilesByCategory() {
|
||||
category := c.GetString("category")
|
||||
if category == "" {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "分类不能为空",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
files, err := models.GetFilesByCategory(category)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "获取文件列表失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取文件列表成功",
|
||||
"data": files,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetFilesByStatus 根据状态获取文件信息
|
||||
func (c *FileController) GetFilesByStatus() {
|
||||
statusStr := c.GetString("status")
|
||||
status, err := strconv.Atoi(statusStr)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "状态参数错误",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
files, err := models.GetFilesByStatus(int8(status))
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "获取文件列表失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取文件列表成功",
|
||||
"data": files,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// CreateFile 创建新文件信息
|
||||
func (c *FileController) CreateFile() {
|
||||
var file models.FileInfo
|
||||
|
||||
// 解析请求体
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &file); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "请求参数错误",
|
||||
"error": err.Error(),
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 从JWT中间件获取用户信息
|
||||
if userID, ok := c.Ctx.Input.GetData("userId").(int); ok && userID > 0 {
|
||||
file.UserID = userID
|
||||
}
|
||||
if username, ok := c.Ctx.Input.GetData("username").(string); ok && username != "" {
|
||||
file.UploadBy = username
|
||||
}
|
||||
|
||||
// 验证必填字段
|
||||
if file.TenantID == "" || file.FileName == "" || file.OriginalName == "" ||
|
||||
file.FilePath == "" || file.FileType == "" || file.FileExt == "" ||
|
||||
file.Category == "" || file.UploadBy == "" {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "必填字段不能为空",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 添加文件信息
|
||||
if _, err := models.AddFile(&file); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "创建文件失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "创建文件成功",
|
||||
"data": file,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// UpdateFile 更新文件信息
|
||||
func (c *FileController) UpdateFile() {
|
||||
idStr := c.Ctx.Input.Param(":id")
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "参数错误",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
var file models.FileInfo
|
||||
file.ID = id
|
||||
|
||||
// 解析请求体
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &file); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "请求参数错误",
|
||||
"error": err.Error(),
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 更新文件信息
|
||||
if err := models.UpdateFile(&file); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "更新文件失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "更新文件成功",
|
||||
"data": file,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// DeleteFile 删除文件信息(软删除)
|
||||
func (c *FileController) DeleteFile() {
|
||||
idStr := c.Ctx.Input.Param(":id")
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "参数错误",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
if err := models.DeleteFile(id); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "删除文件失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "删除文件成功",
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// HardDeleteFile 硬删除文件信息
|
||||
func (c *FileController) HardDeleteFile() {
|
||||
idStr := c.Ctx.Input.Param(":id")
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "参数错误",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
if err := models.HardDeleteFile(id); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "删除文件失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "删除文件成功",
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetFileStatistics 获取文件统计信息
|
||||
func (c *FileController) GetFileStatistics() {
|
||||
tenantID := c.GetString("tenant_id")
|
||||
if tenantID == "" {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "租户ID不能为空",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
stats, err := models.GetFileStatistics(tenantID)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "获取统计信息失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取统计信息成功",
|
||||
"data": stats,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// SearchFiles 搜索文件
|
||||
func (c *FileController) SearchFiles() {
|
||||
keyword := c.GetString("keyword")
|
||||
tenantID := c.GetString("tenant_id")
|
||||
|
||||
if keyword == "" {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "搜索关键词不能为空",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
if tenantID == "" {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "租户ID不能为空",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
files, err := models.SearchFiles(keyword, tenantID)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "搜索文件失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "搜索文件成功",
|
||||
"data": files,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetMyFiles 获取当前用户的文件列表
|
||||
func (c *FileController) GetMyFiles() {
|
||||
// 从JWT中间件获取用户信息
|
||||
userID, ok := c.Ctx.Input.GetData("userId").(int)
|
||||
if !ok || userID <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "用户未登录或登录已过期",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
files, err := models.GetFilesByUserID(userID)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "获取文件列表失败: " + err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取成功",
|
||||
"data": files,
|
||||
}
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetFilesPublic 不需要认证的获取文件信息接口
|
||||
func (c *FileController) GetFilesPublic() {
|
||||
files, err := models.GetAllFiles()
|
||||
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "获取文件列表失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取文件列表成功",
|
||||
"data": files,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"server/models"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// MenuController 处理菜单相关请求
|
||||
type MenuController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// GetAllMenus 获取所有菜单
|
||||
func (c *MenuController) GetAllMenus() {
|
||||
menus, err := models.GetAllMenus()
|
||||
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "获取菜单失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取菜单成功",
|
||||
"data": menus,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// 获取顶级菜单
|
||||
// GetTopLevelMenus 获取顶级菜单
|
||||
func (c *MenuController) GetTopLevelMenus() {
|
||||
menus, err := models.GetTopLevelMenus()
|
||||
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "获取顶级菜单失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取顶级菜单成功",
|
||||
"data": menus,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetActiveMenus 获取启用的菜单(用于前端路由生成)
|
||||
func (c *MenuController) GetActiveMenus() {
|
||||
menus, err := models.GetActiveMenus()
|
||||
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "获取菜单失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取菜单成功",
|
||||
"data": menus,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetMenusByParentId 根据ParentID获取菜单
|
||||
func (c *MenuController) GetMenusByParentId() {
|
||||
parentId, err := c.GetInt(":parentId")
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "参数错误",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
menus, err := models.GetMenusByParentId(parentId)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "获取子菜单失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取子菜单成功",
|
||||
"data": menus,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// CreateMenu 创建新菜单
|
||||
func (c *MenuController) CreateMenu() {
|
||||
var menu models.Menu
|
||||
|
||||
// 解析请求体
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &menu); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "请求参数错误",
|
||||
"error": err.Error(),
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 添加菜单
|
||||
if _, err := models.AddMenu(&menu); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "创建菜单失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "创建菜单成功",
|
||||
"data": menu,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// UpdateMenu 更新菜单
|
||||
func (c *MenuController) UpdateMenu() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "参数错误",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 直接使用 Beego 的 BindJSON 方法
|
||||
var menu models.Menu
|
||||
if err := c.BindJSON(&menu); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "请求参数错误",
|
||||
"error": err.Error(),
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 设置 ID
|
||||
menu.Id = id
|
||||
|
||||
// 更新菜单
|
||||
if err := models.UpdateMenu(&menu); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "更新菜单失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "更新菜单成功",
|
||||
"data": menu,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// DeleteMenu 删除菜单
|
||||
func (c *MenuController) DeleteMenu() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "参数错误",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 删除菜单
|
||||
if err := models.DeleteMenu(id); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "删除菜单失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "删除菜单成功",
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
@@ -0,0 +1,408 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"server/models"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// ProgramCategoryController 处理程序分类相关请求
|
||||
type ProgramCategoryController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// GetProgramCategoriesPublic 不需要认证的获取所有程序分类接口
|
||||
func (c *ProgramCategoryController) GetProgramCategoriesPublic() {
|
||||
// fmt.Println("调用了不需要认证的GetProgramCategoriesPublic接口")
|
||||
categories, err := models.GetAllProgramCategories()
|
||||
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "获取程序分类失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取程序分类成功",
|
||||
"data": categories,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// ProgramInfoController 处理程序信息相关请求
|
||||
type ProgramInfoController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// GetProgramInfosPublic 不需要认证的获取所有程序信息接口
|
||||
func (c *ProgramInfoController) GetProgramInfosPublic() {
|
||||
// fmt.Println("调用了不需要认证的GetProgramInfosPublic接口")
|
||||
programs, err := models.GetAllProgramInfos()
|
||||
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "获取程序信息失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取程序信息成功",
|
||||
"data": programs,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// ==================== 程序分类相关接口 ====================
|
||||
|
||||
// GetAllProgramCategories 获取所有程序分类
|
||||
func (c *ProgramCategoryController) GetAllProgramCategories() {
|
||||
categories, err := models.GetAllProgramCategories()
|
||||
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "获取程序分类失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取程序分类成功",
|
||||
"data": categories,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetProgramCategoryById 根据ID获取程序分类
|
||||
func (c *ProgramCategoryController) GetProgramCategoryById() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "参数错误",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
category, err := models.GetProgramCategoryById(id)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "程序分类不存在",
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取程序分类成功",
|
||||
"data": category,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// CreateProgramCategory 创建新程序分类
|
||||
func (c *ProgramCategoryController) CreateProgramCategory() {
|
||||
var category models.ProgramCategory
|
||||
|
||||
// 解析请求体
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &category); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "请求参数错误",
|
||||
"error": err.Error(),
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 添加程序分类
|
||||
if _, err := models.AddProgramCategory(&category); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "创建程序分类失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "创建程序分类成功",
|
||||
"data": category,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// UpdateProgramCategory 更新程序分类
|
||||
func (c *ProgramCategoryController) UpdateProgramCategory() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "参数错误",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
var category models.ProgramCategory
|
||||
category.CategoryId = id
|
||||
|
||||
// 解析请求体
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &category); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "请求参数错误",
|
||||
"error": err.Error(),
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 更新程序分类
|
||||
if err := models.UpdateProgramCategory(&category); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "更新程序分类失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "更新程序分类成功",
|
||||
"data": category,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// DeleteProgramCategory 删除程序分类
|
||||
func (c *ProgramCategoryController) DeleteProgramCategory() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "参数错误",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 删除程序分类
|
||||
if err := models.DeleteProgramCategory(id); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "删除程序分类失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "删除程序分类成功",
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// ==================== 程序信息相关接口 ====================
|
||||
|
||||
// GetAllProgramInfos 获取所有程序信息
|
||||
func (c *ProgramInfoController) GetAllProgramInfos() {
|
||||
programs, err := models.GetAllProgramInfos()
|
||||
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "获取程序信息失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取程序信息成功",
|
||||
"data": programs,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetProgramInfoById 根据ID获取程序信息
|
||||
func (c *ProgramInfoController) GetProgramInfoById() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "参数错误",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
program, err := models.GetProgramInfoById(id)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "程序信息不存在",
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取程序信息成功",
|
||||
"data": program,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// CreateProgramInfo 创建新程序信息
|
||||
func (c *ProgramInfoController) CreateProgramInfo() {
|
||||
var program models.ProgramInfo
|
||||
|
||||
// 解析请求体
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &program); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "请求参数错误",
|
||||
"error": err.Error(),
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 添加程序信息
|
||||
if _, err := models.AddProgramInfo(&program); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "创建程序信息失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "创建程序信息成功",
|
||||
"data": program,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// UpdateProgramInfo 更新程序信息
|
||||
func (c *ProgramInfoController) UpdateProgramInfo() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "参数错误",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
var program models.ProgramInfo
|
||||
program.ProgramId = id
|
||||
|
||||
// 解析请求体
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &program); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "请求参数错误",
|
||||
"error": err.Error(),
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 更新程序信息
|
||||
if err := models.UpdateProgramInfo(&program); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "更新程序信息失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "更新程序信息成功",
|
||||
"data": program,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// DeleteProgramInfo 删除程序信息
|
||||
func (c *ProgramInfoController) DeleteProgramInfo() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "参数错误",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 删除程序信息
|
||||
if err := models.DeleteProgramInfo(id); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "删除程序信息失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "删除程序信息成功",
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetProgramInfosByCategoryId 根据分类ID获取程序信息
|
||||
func (c *ProgramInfoController) GetProgramInfosByCategoryId() {
|
||||
categoryId, err := c.GetInt(":category_id")
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "参数错误",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
programs, err := models.GetProgramInfosByCategoryId(categoryId)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "获取程序信息失败",
|
||||
"error": err.Error(),
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取程序信息成功",
|
||||
"data": programs,
|
||||
}
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
Reference in New Issue
Block a user