前后端差不多

This commit is contained in:
2026-04-02 20:53:17 +08:00
parent 5e2b326838
commit 032d9bf383
9 changed files with 433 additions and 132 deletions
+1 -1
View File
@@ -54,7 +54,7 @@ func ApiRoot(r *gin.RouterGroup) {
ApiUser(r.Group("/users"))
ApiFiles(r.Group("/files"))
ApiPurchase(r.Group("/purchase"))
ApiSchedule(r.Group("/schedule"))
r.GET("/", func(ctx *gin.Context) {
ReturnJson(ctx, "apiOK", nil)
})
+134
View File
@@ -0,0 +1,134 @@
package routers
//2026-4-2开始每个功能的数据表在各自的api路由下初始化
import (
"encoding/json"
"fmt"
"ops/models"
"time"
"github.com/gin-gonic/gin"
"github.com/mitchellh/mapstructure"
"gorm.io/gorm"
)
type TabSchedule struct {
ID uint `gorm:"primarykey"`
UserID uint `gorm:"not null;comment:创建人ID"`
Title string `gorm:"size:200;not null;comment:日程标题"`
StartDate string `gorm:"size:10;not null;index;comment:开始日期 YYYY-MM-DD"`
EndDate string `gorm:"size:10;not null;index;comment:结束日期 YYYY-MM-DD"`
BgColor string `gorm:"size:50;default:#3788d9;comment:背景颜色"`
Remark string `gorm:"type:text;comment:备注"`
CreatedAt *time.Time `gorm:"type:datetime;autoCreateTime;comment:创建时间"`
UpdatedAt *time.Time `gorm:"type:datetime;autoUpdateTime;comment:最后修改时间"`
DeletedAt gorm.DeletedAt `gorm:"index"`
}
type TabScheduleLog struct {
ID uint `gorm:"primarykey"`
ScheduleID uint `gorm:"not null;index;comment:关联日程ID"`
UserID uint `gorm:"not null;comment:操作人ID"`
ActionType string `gorm:"size:50;not null;comment:操作类型: create-创建 update-修改 delete-删除 query-查询"`
OldContent string `gorm:"type:text;comment:修改前内容(JSON)"`
NewContent string `gorm:"type:text;comment:修改后内容(JSON)"`
IP string `gorm:"size:50;comment:操作IP"`
Remark string `gorm:"size:500;comment:备注/操作描述"`
CreatedAt *time.Time `gorm:"type:datetime;autoCreateTime;comment:操作时间"`
}
type fromAddEvent struct {
Title string `json:"title" binding:"required"` // 日程标题
Start string `json:"start" binding:"required"` // 开始日期
End string `json:"end" binding:"required"` // 结束日期
Color string `json:"color" binding:"required"` // 背景颜色
}
type fromGetEvents struct {
Start string `json:"start" binding:"required"` // 开始日期
End string `json:"end" binding:"required"` // 结束日期
}
var (
userGroup models.TabUserGroups_
)
func ApiScheduleInit() {
//先初始化数据表
models.DB.AutoMigrate(&TabSchedule{})
models.DB.AutoMigrate(&TabScheduleLog{})
//获取管理员用户组id
//先检查用户组有没有这个key
userGroup.Name = "schedule_admin"
if models.DB.Where(&userGroup).First(&userGroup).Error == nil {
} else {
userGroup.Type = "usergroup"
models.DB.Create(&userGroup)
}
}
func ApiSchedule(r *gin.RouterGroup) {
r.POST("/getevents", func(ctx *gin.Context) {
data, _ := SeparateData(ctx)
//fmt.Println(cookieval, data)
var from fromGetEvents
if err := mapstructure.Decode(data, &from); err == nil {
//fmt.Println(from)
//从数据库获取相关数据
var list []TabSchedule
models.DB.Where("start_date <= ? AND end_date >= ?", from.End, from.Start).Where("deleted_at IS NULL").Find(&list)
fmt.Println(list)
//ReturnJson(ctx, "ApiOK", list)
ReturnJson(ctx, "apiOK", gin.H{"list": list})
} else {
ReturnJson(ctx, "jsonErr", nil)
}
})
r.POST("/addevent", func(ctx *gin.Context) {
isAuth, user, data := AuthenticationAuthority(ctx)
if isAuth {
var from fromAddEvent
if err := mapstructure.Decode(data, &from); err == nil {
tosql := TabSchedule{
UserID: user.ID,
Title: from.Title,
StartDate: from.Start,
EndDate: from.End,
BgColor: from.Color,
}
if models.DB.Create(&tosql).Error == nil {
//记录日志
newContent, _ := json.Marshal(tosql) // 👈 转 JSON
tosqllog := TabScheduleLog{
UserID: user.ID,
ScheduleID: tosql.ID,
ActionType: "create",
NewContent: string(newContent), // 👈 直接赋值
OldContent: "",
IP: ctx.ClientIP(),
}
models.DB.Create(&tosqllog)
ReturnJson(ctx, "apiOK", nil)
} else {
ReturnJson(ctx, "apiErr", nil)
}
} else {
ReturnJson(ctx, "jsonErr", nil)
}
} else {
ReturnJson(ctx, "userCookieError", nil)
}
})
}
+22 -40
View File
@@ -12,7 +12,7 @@ import (
"github.com/mitchellh/mapstructure"
)
func ApiInit() {
func ApiUserInit() {
//用户模块初始化init
fmt.Println("users init")
@@ -110,33 +110,16 @@ func AuthenticationAuthorityFromCookie(c string) (*models.TabUser_, error) {
}
func AuthenticationAuthority(ctx *gin.Context) (bool, models.TabUser_, map[string]interface{}) {
var user models.TabUser_
data, cookieval := SeparateData(ctx)
//fmt.Println("cookieis" + cookieval)
var user models.TabUser_
if cookieval != "" {
cookie := models.TabCookie_{
Value: cookieval,
}
if models.DB.Where(&cookie).First(&cookie).Error == nil {
//找到cookie,验证cookie有效性,以及更新cookie
if models.CheckCookiesAndUpdate(&cookie) {
//cookie有效
//载入user
user := models.TabUser_{
ID: cookie.UserID,
}
models.DB.Where(&user).First(&user)
return true, user, data
} else {
ReturnJson(ctx, "userCookieExpired", nil)
return false, user, nil
}
user_, error := AuthenticationAuthorityFromCookie(cookieval)
if error == nil {
user = *user_
return true, user, data
} else {
ReturnJson(ctx, "userCookieNotFund", nil)
return false, user, nil
}
@@ -145,7 +128,6 @@ func AuthenticationAuthority(ctx *gin.Context) (bool, models.TabUser_, map[strin
return false, user, nil
}
//return false, user
}
func ApiUser(r *gin.RouterGroup) {
@@ -261,26 +243,26 @@ func ApiUser(r *gin.RouterGroup) {
}
}
if is_save_ok {
//修改数据库内容
var user_info_fund models.TabUserInfo_
user_info_fund.UserID = user.ID
if is_save_ok {
//修改数据库内容
var user_info_fund models.TabUserInfo_
user_info_fund.UserID = user.ID
var user_update_avatar models.TabUserInfo_
user_update_avatar.AvatarPath = file_hashi_name + file_extname
var user_update_avatar models.TabUserInfo_
user_update_avatar.AvatarPath = file_hashi_name + file_extname
//先查找是否有记录
if models.DB.Where(&user_info_fund).First(&user_info_fund).Error == nil {
//有记录,更新
models.DB.Model(&user_info_fund).Updates(&user_update_avatar)
} else {
//无记录,创建
user_update_avatar.UserID = user.ID
models.DB.Create(&user_update_avatar)
}
//先查找是否有记录
if models.DB.Where(&user_info_fund).First(&user_info_fund).Error == nil {
//有记录,更新
models.DB.Model(&user_info_fund).Updates(&user_update_avatar)
} else {
//无记录,创建
user_update_avatar.UserID = user.ID
models.DB.Create(&user_update_avatar)
}
}
} else {
ReturnJson(ctx, "postErr", nil)
}