This commit is contained in:
2026-05-06 17:03:57 +08:00
parent 3fcc98f6f6
commit 7369fe4117
+137 -107
View File
@@ -27,17 +27,17 @@ type TabCalendar struct {
// TabCalendarEvent 日历事件表 // TabCalendarEvent 日历事件表
type TabCalendarEvent struct { type TabCalendarEvent struct {
ID uint `gorm:"primarykey"` ID uint `gorm:"primarykey"`
CalendarID uint `gorm:"not null;index;comment:关联日历ID"` CalendarID uint `gorm:"not null;index;comment:关联日历ID"`
UserID uint `gorm:"not null;comment:创建人ID"` UserID uint `gorm:"not null;comment:创建人ID"`
UsersID []uint `gorm:"type:json; null;comment:其他关联用户ID"` //UsersID []uint `gorm:"type:json; null;comment:其他关联用户ID"`
Title string `gorm:"size:200;not null;comment:事件标题"` Title string `gorm:"size:200;not null;comment:事件标题"`
StartDate *time.Time `gorm:"size:10;not null;index;comment:开始日期 YYYY-MM-DD"` StartDate *time.Time `gorm:"size:10;not null;index;comment:开始日期 YYYY-MM-DD"`
EndDate *time.Time `gorm:"size:10;not null;index;comment:结束日期 YYYY-MM-DD"` EndDate *time.Time `gorm:"size:10;not null;index;comment:结束日期 YYYY-MM-DD"`
IsAllDay bool `gorm:"default:true;comment:是否全日事件"` IsAllDay bool `gorm:"default:true;comment:是否全日事件"`
BgColor string `gorm:"size:50;default:#3788d9;comment:背景颜色"` BgColor string `gorm:"size:50;default:#3788d9;comment:背景颜色"`
IsPublic bool `gorm:"default:false;comment:是否为公共日程"` IsPublic bool `gorm:"default:false;comment:是否为公共日程"`
Remark string `gorm:"type:text;comment:备注"` Remark string `gorm:"type:text;comment:备注"`
CreatedAt *time.Time `gorm:"type:datetime;autoCreateTime;comment:创建时间"` CreatedAt *time.Time `gorm:"type:datetime;autoCreateTime;comment:创建时间"`
UpdatedAt *time.Time `gorm:"type:datetime;autoUpdateTime;comment:最后修改时间"` UpdatedAt *time.Time `gorm:"type:datetime;autoUpdateTime;comment:最后修改时间"`
@@ -86,23 +86,25 @@ type fromGetCalendarEvents struct {
} }
type fromAddCalendarEvent struct { type fromAddCalendarEvent struct {
CalendarID uint `json:"calendar_id" binding:"required"` CalendarID uint `json:"calendar_id" binding:"required"`
Title string `json:"title" binding:"required"` Title string `json:"title" binding:"required"`
Start *time.Time `json:"start" binding:"required"` Start string `json:"start" binding:"required"`
End *time.Time `json:"end" binding:"required"` End string `json:"end" binding:"required"`
Color string `json:"color"` Color string `json:"color"`
Is_public bool `json:"is_public"` ScheduleType string `json:"schedule_type"`
Remark string `json:"remark"` Is_public bool `json:"is_public"`
Remark string `json:"remark"`
} }
type fromUpdateCalendarEvent struct { type fromUpdateCalendarEvent struct {
ID uint `json:"id" binding:"required"` ID uint `json:"id" binding:"required"`
Title string `json:"title" binding:"required"` Title string `json:"title" binding:"required"`
Start *time.Time `json:"start" binding:"required"` Start string `json:"start" binding:"required"`
End *time.Time `json:"end" binding:"required"` End string `json:"end" binding:"required"`
Color string `json:"color"` Color string `json:"color"`
Is_public bool `json:"is_public"` ScheduleType string `json:"schedule_type"`
Remark string `json:"remark"` Is_public bool `json:"is_public"`
Remark string `json:"remark"`
} }
type fromDeleteCalendarEvent struct { type fromDeleteCalendarEvent struct {
@@ -352,47 +354,61 @@ func ApiCalendar(r *gin.RouterGroup) {
r.POST("/calendar/addevent", func(ctx *gin.Context) { r.POST("/calendar/addevent", func(ctx *gin.Context) {
isAuth, user, data := AuthenticationAuthority(ctx) isAuth, user, data := AuthenticationAuthority(ctx)
if isAuth { if isAuth {
var from fromAddCalendarEvent // 先检查必需字段
if err := mapstructure.Decode(data, &from); err == nil { calendarIDRaw, ok := data["calendar_id"].(float64)
// 检查日历是否存在 if !ok || calendarIDRaw == 0 {
var calendar TabCalendar
if models.DB.Where("id = ? AND deleted_at IS NULL", from.CalendarID).First(&calendar).Error != nil {
ReturnJson(ctx, "calendar_not_find", nil)
return
}
event := TabCalendarEvent{
CalendarID: from.CalendarID,
UserID: user.ID,
Title: from.Title,
StartDate: from.Start,
EndDate: from.End,
BgColor: from.Color,
IsPublic: from.Is_public,
Remark: from.Remark,
}
if event.BgColor == "" {
event.BgColor = calendar.Color
}
if models.DB.Create(&event).Error == nil {
// 记录日志
newContent, _ := json.Marshal(event)
log := TabCalendarLog{
CalendarID: event.CalendarID,
EventID: event.ID,
UserID: user.ID,
ActionType: "create_event",
NewContent: string(newContent),
IP: ctx.ClientIP(),
}
models.DB.Create(&log)
ReturnJson(ctx, "apiOK", gin.H{"id": event.ID})
} else {
ReturnJson(ctx, "apiErr", nil)
}
} else {
ReturnJson(ctx, "jsonErr", nil) ReturnJson(ctx, "jsonErr", nil)
return
}
calendarID := uint(calendarIDRaw)
// 检查日历是否存在
var calendar TabCalendar
if models.DB.Where("id = ? AND deleted_at IS NULL", calendarID).First(&calendar).Error != nil {
ReturnJson(ctx, "calendar_not_find", nil)
return
}
// 解析日期
startStr, _ := data["start"].(string)
endStr, _ := data["end"].(string)
title, _ := data["title"].(string)
color, _ := data["color"].(string)
remark, _ := data["remark"].(string)
isPublic, _ := data["is_public"].(bool)
startDate, _ := time.Parse("2006-01-02 15:04:05", startStr)
endDate, _ := time.Parse("2006-01-02 15:04:05", endStr)
event := TabCalendarEvent{
CalendarID: calendarID,
UserID: user.ID,
Title: title,
StartDate: &startDate,
EndDate: &endDate,
BgColor: color,
IsPublic: isPublic,
Remark: remark,
}
if event.BgColor == "" {
event.BgColor = calendar.Color
}
if models.DB.Create(&event).Error == nil {
// 记录日志
newContent, _ := json.Marshal(event)
log := TabCalendarLog{
CalendarID: event.CalendarID,
EventID: event.ID,
UserID: user.ID,
ActionType: "create_event",
NewContent: string(newContent),
IP: ctx.ClientIP(),
}
models.DB.Create(&log)
ReturnJson(ctx, "apiOK", gin.H{"id": event.ID})
} else {
ReturnJson(ctx, "apiErr", nil)
} }
} else { } else {
ReturnJson(ctx, "userCookieError", nil) ReturnJson(ctx, "userCookieError", nil)
@@ -403,54 +419,68 @@ func ApiCalendar(r *gin.RouterGroup) {
r.POST("/calendar/updateevent", func(ctx *gin.Context) { r.POST("/calendar/updateevent", func(ctx *gin.Context) {
isAuth, user, data := AuthenticationAuthority(ctx) isAuth, user, data := AuthenticationAuthority(ctx)
if isAuth { if isAuth {
var from fromUpdateCalendarEvent // 先检查必需字段
if err := mapstructure.Decode(data, &from); err == nil { idRaw, ok := data["id"].(float64)
oldEvent := TabCalendarEvent{} if !ok || idRaw == 0 {
if models.DB.Where("id = ?", from.ID).First(&oldEvent).Error == nil { ReturnJson(ctx, "jsonErr", nil)
// 检查权限(只有创建人可以修改) return
if oldEvent.UserID != user.ID { }
ReturnJson(ctx, "permission_denied", nil) eventID := uint(idRaw)
return
}
newEvent := TabCalendarEvent{ oldEvent := TabCalendarEvent{}
Title: from.Title, if models.DB.Where("id = ?", eventID).First(&oldEvent).Error == nil {
StartDate: from.Start, // 检查权限(只有创建人可以修改)
EndDate: from.End, if oldEvent.UserID != user.ID {
BgColor: from.Color, ReturnJson(ctx, "permission_denied", nil)
IsPublic: from.Is_public, return
Remark: from.Remark, }
}
if newEvent.BgColor == "" {
// 获取日历颜色
var calendar TabCalendar
models.DB.Where("id = ?", oldEvent.CalendarID).First(&calendar)
newEvent.BgColor = calendar.Color
}
if models.DB.Model(&oldEvent).Updates(&newEvent).Error == nil { // 解析字段
// 记录日志 startStr, _ := data["start"].(string)
newContent, _ := json.Marshal(newEvent) endStr, _ := data["end"].(string)
oldContent, _ := json.Marshal(oldEvent) title, _ := data["title"].(string)
log := TabCalendarLog{ color, _ := data["color"].(string)
CalendarID: oldEvent.CalendarID, remark, _ := data["remark"].(string)
EventID: oldEvent.ID, isPublic, _ := data["is_public"].(bool)
UserID: user.ID,
ActionType: "update_event", startDate, _ := time.Parse("2006-01-02 15:04:05", startStr)
OldContent: string(oldContent), endDate, _ := time.Parse("2006-01-02 15:04:05", endStr)
NewContent: string(newContent),
IP: ctx.ClientIP(), newEvent := TabCalendarEvent{
} Title: title,
models.DB.Create(&log) StartDate: &startDate,
ReturnJson(ctx, "apiOK", nil) EndDate: &endDate,
} else { BgColor: color,
ReturnJson(ctx, "apiErr", nil) IsPublic: isPublic,
Remark: remark,
}
if newEvent.BgColor == "" {
// 获取日历颜色
var calendar TabCalendar
models.DB.Where("id = ?", oldEvent.CalendarID).First(&calendar)
newEvent.BgColor = calendar.Color
}
if models.DB.Model(&oldEvent).Updates(&newEvent).Error == nil {
// 记录日志
newContent, _ := json.Marshal(newEvent)
oldContent, _ := json.Marshal(oldEvent)
log := TabCalendarLog{
CalendarID: oldEvent.CalendarID,
EventID: oldEvent.ID,
UserID: user.ID,
ActionType: "update_event",
OldContent: string(oldContent),
NewContent: string(newContent),
IP: ctx.ClientIP(),
} }
models.DB.Create(&log)
ReturnJson(ctx, "apiOK", nil)
} else { } else {
ReturnJson(ctx, "event_not_find", nil) ReturnJson(ctx, "apiErr", nil)
} }
} else { } else {
ReturnJson(ctx, "jsonErr", nil) ReturnJson(ctx, "event_not_find", nil)
} }
} else { } else {
ReturnJson(ctx, "userCookieError", nil) ReturnJson(ctx, "userCookieError", nil)