- 新增「简记memo」一体化小程序产品原型设计文档 - 新增简记memo完整版UI视觉设计规范和界面细节 - 添加IDEA项目配置文件.gitignore - 创建404页面HTML文件,包含响应式布局和错误提示 - 添加关于页面HTML文件,展示品牌介绍和团队信息 - 实现AES加解密工具函数,支持请求体加密 - 添加用户协议页面基础框架
96 lines
2.2 KiB
Go
96 lines
2.2 KiB
Go
// 预算接口
|
|
package app
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
"simple-memo/global"
|
|
"simple-memo/models"
|
|
"simple-memo/utils"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// BudgetHandler 预算模块接口
|
|
type BudgetHandler interface {
|
|
GetBudget(c *gin.Context) // 获取当前/指定月份预算
|
|
SetBudget(c *gin.Context) // 设置/更新月度预算
|
|
DeleteBudget(c *gin.Context) // 删除月度预算
|
|
}
|
|
|
|
type budgetHandler struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewBudgetHandler() BudgetHandler {
|
|
return &budgetHandler{db: global.DB}
|
|
}
|
|
|
|
// SetBudgetReq 设置预算请求参数
|
|
type SetBudgetReq struct {
|
|
Month string `json:"month"` // 月份 2025-12
|
|
Amount float64 `json:"amount"` // 金额
|
|
}
|
|
|
|
// GetBudget 获取月度预算
|
|
func (h *budgetHandler) GetBudget(c *gin.Context) {
|
|
userID := c.GetUint("userID")
|
|
month := c.DefaultQuery("month", time.Now().Format("2006-01"))
|
|
|
|
var budget models.Budget
|
|
err := h.db.Where("user_id = ? AND month = ?", userID, month).First(&budget).Error
|
|
|
|
// 没查到 → 返回 0
|
|
if err != nil {
|
|
utils.Ok(c, gin.H{"amount": 0})
|
|
return
|
|
}
|
|
|
|
utils.Ok(c, gin.H{"amount": budget.Amount})
|
|
}
|
|
|
|
// SetBudget 设置/更新预算(不存在则创建,存在则更新)
|
|
func (h *budgetHandler) SetBudget(c *gin.Context) {
|
|
userID := c.GetUint("userID")
|
|
var req SetBudgetReq
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
utils.Fail(c, "参数解析失败")
|
|
return
|
|
}
|
|
|
|
// 不传月份默认当前月
|
|
month := req.Month
|
|
if month == "" {
|
|
month = time.Now().Format("2006-01")
|
|
}
|
|
|
|
var budget models.Budget
|
|
// 查询是否已存在
|
|
err := h.db.Where("user_id = ? AND month = ?", userID, month).First(&budget).Error
|
|
|
|
if err != nil {
|
|
// 不存在 → 新建
|
|
h.db.Create(&models.Budget{
|
|
UserID: userID,
|
|
Month: month,
|
|
Amount: req.Amount,
|
|
})
|
|
utils.Ok(c, "预算设置成功")
|
|
return
|
|
}
|
|
|
|
// 存在 → 更新金额
|
|
h.db.Model(&budget).Update("amount", req.Amount)
|
|
utils.Ok(c, "预算更新成功")
|
|
}
|
|
|
|
// DeleteBudget 删除预算
|
|
func (h *budgetHandler) DeleteBudget(c *gin.Context) {
|
|
userID := c.GetUint("userID")
|
|
month := c.DefaultQuery("month", time.Now().Format("2006-01"))
|
|
|
|
h.db.Where("user_id = ? AND month = ?", userID, month).Delete(&models.Budget{})
|
|
utils.Ok(c, "删除成功")
|
|
}
|