This commit is contained in:
aozhiwei 2024-08-14 11:30:45 +08:00
commit 02833fc89c
4 changed files with 149 additions and 18 deletions

View File

@ -2,9 +2,11 @@ package system
import (
"f5"
"fmt"
"main/constant"
"main/model/system"
"net/http"
"q5"
"strconv"
"github.com/gin-gonic/gin"
@ -13,8 +15,65 @@ import (
type AnncApi struct {
}
type anncJson struct {
AnncType int32 `json:"annc_type" binding:"required" `
PlatForm int32 `json:"platform" binding:"required" `
AnncDesc string `json:"annc_desc" binding:"required" `
}
var anncCfg []anncJson
func (aa *AnncApi) Init() {
loginanncs := []anncJson{}
if jsonStr, err := f5.ReadJsonFile("../config/login_annc.json"); err == nil {
if err := q5.DecodeJson(jsonStr, &loginanncs); err != nil {
panic(fmt.Sprintf("parse metafile error %s %s", "login_annc.json", err))
}
} else {
panic(fmt.Sprintf("load metafile error %s %s", "login_annc.json", err))
}
nowDaySeconds := int32(f5.GetApp().GetRealSeconds())
for _, item := range loginanncs {
if item.AnncType != constant.LOGIN_ANNC_TYPE_LEAST_VERSION &&
item.AnncType != constant.LOGIN_ANNC_TYPE_WHITE_VERSION &&
item.AnncType != constant.LOGIN_ANNC_TYPE_CURRENT_VERSION {
panic(fmt.Sprintf("annc type 配置错误:%d, %d", item.AnncType, item.PlatForm))
}
if item.PlatForm != constant.PLATFORM_ANDROID &&
item.PlatForm != constant.PLATFORM_IOS {
panic(fmt.Sprintf("annc platform 配置错误:%d, %d", item.AnncType, item.PlatForm))
}
annc := new(system.Annc)
var count int64 = 0
f5.GetApp().GetOrmDb(constant.CONF_DB).Table(annc.TableName()).Where("annc_type = ?", item.AnncType).Where("platform = ?", item.PlatForm).Count(&count)
if count > 0 {
continue
}
annc.AnncType = item.AnncType
annc.Version = "1.0.0"
annc.ClickType = 0
annc.PlatForm = item.PlatForm
annc.CreateTime = nowDaySeconds
annc.ModifyTime = nowDaySeconds
if err := f5.GetApp().GetOrmDb(constant.CONF_DB).Create(annc).Error; err != nil {
f5.GetSysLog().Error("添加annc 失败:%d, %d", item.AnncType, item.PlatForm)
}
}
anncCfg = loginanncs
}
func (this *AnncApi) AnncList(c *gin.Context) {
var anncList []system.Annc
var anncList []*struct {
system.Annc
Desc string `json:"desc"`
}
err := f5.GetApp().GetOrmDb(constant.CONF_DB).Find(&anncList).Error
if err != nil {
c.JSON(http.StatusOK, gin.H{
@ -23,6 +82,16 @@ func (this *AnncApi) AnncList(c *gin.Context) {
})
return
}
for _, item := range anncList {
for _, cfg := range anncCfg {
if item.AnncType == cfg.AnncType && item.PlatForm == cfg.PlatForm {
item.Desc = cfg.AnncDesc
break
}
}
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "success",
@ -39,9 +108,14 @@ func (this *AnncApi) AddAnnc(c *gin.Context) {
})
return
}
if !this.CheckLoginAnnc(annc, c) {
return
}
var count int64
db := f5.GetApp().GetOrmDb(constant.CONF_DB)
db.Table(annc.TableName()).Where("model = ?", annc.Model).Where("type = ?", annc.Type).Count(&count)
db.Table(annc.TableName()).Where("annc_type = ?", annc.AnncType).Where("platform = ?", annc.PlatForm).Count(&count)
if count > 0 {
c.JSON(http.StatusOK, gin.H{
"code": 1,
@ -76,6 +150,10 @@ func (this *AnncApi) UpdateAnnc(c *gin.Context) {
return
}
if !this.CheckLoginAnnc(annc, c) {
return
}
db := f5.GetApp().GetOrmDb(constant.CONF_DB)
db.Table(annc.TableName()).Where("idx = ?", idx).Count(&count)
if count < 1 {
@ -86,7 +164,7 @@ func (this *AnncApi) UpdateAnnc(c *gin.Context) {
return
}
db.Table(annc.TableName()).Where("idx != ?", idx).Where("model = ?", annc.Model).Where("type = ?", annc.Type).Count(&count)
db.Table(annc.TableName()).Where("idx != ?", idx).Where("annc_type = ?", annc.AnncType).Where("platform = ?", annc.PlatForm).Count(&count)
if count > 0 {
c.JSON(http.StatusOK, gin.H{
"code": 1,
@ -95,7 +173,8 @@ func (this *AnncApi) UpdateAnnc(c *gin.Context) {
return
}
err := db.Select("*").Omit("idx").Where("idx = ?", idx).Updates(&annc).Error
annc.ModifyTime = int32(f5.GetApp().GetRealSeconds())
err := db.Select("*").Omit("idx", "createtime").Where("idx = ?", idx).Updates(&annc).Error
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": 1,
@ -136,12 +215,16 @@ func (this *AnncApi) AddIngameAnnc(c *gin.Context) {
return
}
if !this.CheckIngameAnnu(annc, c) {
if !this.CheckIngameAnnc(annc, c) {
return
}
annc.Uniid = uint64(f5.GetApp().NewNodeUuid())
nowDaySeconds := int32(f5.GetApp().GetRealSeconds())
annc.BeginTime = getTimeSeconds(annc.BeginTime)
annc.EndTime = getTimeSeconds(annc.EndTime)
annc.BeginDate = getUtcDaysSeconds(annc.BeginDate)
annc.EndDate = getUtcDaysSeconds(annc.EndDate)
annc.CreateTime = nowDaySeconds
annc.ModifyTime = nowDaySeconds
err := f5.GetApp().GetOrmDb(constant.CONF_DB).Create(&annc).Error
@ -169,7 +252,7 @@ func (this *AnncApi) UpdateIngameAnnc(c *gin.Context) {
return
}
if !this.CheckIngameAnnu(annc, c) {
if !this.CheckIngameAnnc(annc, c) {
return
}
@ -199,7 +282,7 @@ func (this *AnncApi) UpdateIngameAnnc(c *gin.Context) {
})
}
func (this *AnncApi) CheckIngameAnnu(annc system.IngameAnnc, c *gin.Context) bool {
func (this *AnncApi) CheckLoginAnnc(annc system.Annc, c *gin.Context) bool {
if len(annc.Title) > 0xFF {
c.JSON(http.StatusOK, gin.H{
"code": 2,
@ -216,13 +299,50 @@ func (this *AnncApi) CheckIngameAnnu(annc system.IngameAnnc, c *gin.Context) boo
return false
}
if annc.EndTime < annc.BeginTime || annc.EndTime < int32(f5.GetApp().GetRealSeconds()) {
return true
}
func (this *AnncApi) CheckIngameAnnc(annc system.IngameAnnc, c *gin.Context) bool {
if len(annc.Title) > 0xFF {
c.JSON(http.StatusOK, gin.H{
"code": 2,
"message": "发送时间不对",
"message": "标题过长",
})
return false
}
if len(annc.Content) > 0xFF {
c.JSON(http.StatusOK, gin.H{
"code": 2,
"message": "内容过长",
})
return false
}
notsecs := f5.GetApp().GetRealSeconds()
// if annc.EndTime < annc.BeginTime || annc.EndTime < int32(notsecs) {
// c.JSON(http.StatusOK, gin.H{
// "code": 2,
// "message": "发送时间不对",
// })
// return false
// }
if getUtcDaysSeconds(annc.EndDate) < getUtcDaysSeconds(annc.BeginDate) || getUtcDaysSeconds(annc.EndDate) < getUtcDaysSeconds(int32(notsecs)) {
c.JSON(http.StatusOK, gin.H{
"code": 2,
"message": "发送日期不对",
})
return false
}
return true
}
func getUtcDaysSeconds(ts int32) int32 {
return ts / 3600 / 24 * 3600 * 24
}
func getTimeSeconds(ts int32) int32 {
return ts - ts/3600/24*3600*24
}

View File

@ -14,6 +14,8 @@ import (
type GameSwitchApi struct {
}
var namescrpt = map[string]string{}
func (gs *GameSwitchApi) Init() {
switches := []struct {
Name string `json:"switch_name"`
@ -30,6 +32,7 @@ func (gs *GameSwitchApi) Init() {
nowDaySeconds := int32(f5.GetApp().GetRealSeconds())
for _, item := range switches {
namescrpt[item.Name] = item.Desc
if err := f5.GetApp().GetOrmDb(constant.CONF_DB).Where("switch_name = ?", item.Name).Take(new(system.GameSwitch)).Error; err == nil {
continue
}
@ -37,7 +40,6 @@ func (gs *GameSwitchApi) Init() {
gswitch := new(system.GameSwitch)
gswitch.Name = item.Name
gswitch.Opened = 0
gswitch.Remark = item.Desc
gswitch.CreateTime = nowDaySeconds
gswitch.ModifyTime = nowDaySeconds
if err := f5.GetApp().GetOrmDb(constant.CONF_DB).Create(gswitch).Error; err != nil {
@ -72,7 +74,10 @@ func (this *GameSwitchApi) List(c *gin.Context) {
p.Idx = q5.SafeToInt64(pg.Rows.GetByName("idx"))
p.Name = pg.Rows.GetByName("switch_name")
p.Opened = q5.SafeToInt32(pg.Rows.GetByName("is_open"))
p.Remark = pg.Rows.GetByName("remark")
desc, exist := namescrpt[p.Name]
if exist {
p.Remark = desc
}
p.CreateTime = q5.SafeToInt32(pg.Rows.GetByName("createtime"))
p.ModifyTime = q5.SafeToInt32(pg.Rows.GetByName("modifytime"))
q5.AppendSlice(&result, p)

View File

@ -1,13 +1,16 @@
package system
type Annc struct {
Idx uint64 `json:"idx" `
Title string `json:"title" binding:"required" `
Version string `json:"version" binding:"required" `
Model uint `json:"model" binding:"required" `
Type uint `json:"type" binding:"required"`
IsEffect uint `json:"is_effect"`
Content string `json:"content" binding:"required" `
Idx int64 `json:"idx"`
AnncType int32 `json:"annc_type" binding:"required"`
Title string `json:"title" binding:"required" `
Version string `json:"version" binding:"required" `
PlatForm int32 `gorm:"column:platform" json:"platform" binding:"required"`
ClickType int32 `json:"click_type"`
Enable int32 `json:"enable"`
Content string `json:"content" binding:"required" `
CreateTime int32 `gorm:"column:createtime" json:"-"`
ModifyTime int32 `gorm:"column:modifytime" json:"-"`
}
func (this Annc) TableName() string {
@ -21,6 +24,8 @@ type IngameAnnc struct {
Content string `json:"content" binding:"required"`
BeginTime int32 `json:"begin_time" binding:"required"`
EndTime int32 `json:"end_time" binding:"required"`
BeginDate int32 `json:"begin_date" binding:"required"`
EndDate int32 `json:"end_date" binding:"required"`
IsOpen uint `json:"is_open"`
CreateTime int32 `gorm:"column:createtime" json:"-"`
ModifyTime int32 `gorm:"column:modifytime" json:"-"`

View File

@ -12,6 +12,7 @@ type AnncRouter struct{}
func (this *AnncRouter) InitAnncRouter(priRouter *gin.RouterGroup) {
priUserRouter := priRouter.Group("annc")
anncApi := v1.ApiGroupApp.SystemApiGroup.AnncApi
anncApi.Init()
{
priUserRouter.GET("anncList", middleware.Permission("api/v1/annc/anncList", anncApi.AnncList))
priUserRouter.POST("addAnnc", middleware.Permission("api/v1/annc/addAnnc", anncApi.AddAnnc))