From 481529614e32ee4e18261787730d09d80376167e Mon Sep 17 00:00:00 2001 From: yangduo Date: Thu, 29 Aug 2024 15:05:42 +0800 Subject: [PATCH] active code --- .../adminserver/api/v1/system/activecode.go | 180 ++++++++++++++++++ server/adminserver/api/v1/system/enter.go | 1 + server/adminserver/model/system/activecode.go | 12 ++ server/adminserver/router/routermgr.go | 1 + .../adminserver/router/system/activecode.go | 21 ++ server/adminserver/router/system/enter.go | 1 + 6 files changed, 216 insertions(+) create mode 100644 server/adminserver/api/v1/system/activecode.go create mode 100644 server/adminserver/model/system/activecode.go create mode 100644 server/adminserver/router/system/activecode.go diff --git a/server/adminserver/api/v1/system/activecode.go b/server/adminserver/api/v1/system/activecode.go new file mode 100644 index 00000000..bef693aa --- /dev/null +++ b/server/adminserver/api/v1/system/activecode.go @@ -0,0 +1,180 @@ +package system + +import ( + "f5" + "fmt" + "main/constant" + "main/model/system" + "net/http" + "q5" + "strings" + + "math/rand" + + "github.com/gin-gonic/gin" +) + +type ActiveCodeApi struct { +} + +func (aca *ActiveCodeApi) GenCode(c *gin.Context) { + var batchid int32 = 0 + var maxbatchid int32 = 0 + db := f5.GetApp().GetOrmDb(constant.ACCOUNT_DB).Table("t_activation_code").Select("max(batch_id)") + err := db.Error + if err != nil { + if !f5.IsOrmErrRecordNotFound(err) { + f5.RspErr2(c, 1, err.Error()) + return + } + } else { + var count int64 = 0 + if db.Count(&count); count > 0 { + db.Scan(&maxbatchid) + } + } + + if maxbatchid >= 0xFFF { + f5.RspErr2(c, 1, "batchid overflow") + return + } + + batchid = maxbatchid + 1 + + count := q5.SafeToInt32(c.DefaultQuery("count", "1")) + if count < 1 { + count = 1 + } + if count > 5000 { + count = 5000 + } + + tryCount := 0 + postfix := fmt.Sprintf("%03x", batchid) + codelist := map[string]int{} + nowsecs := int32(f5.GetApp().GetRealSeconds()) + batchcodeinfo := []*system.ActiveCode{} + for i := 0; i < int(count); { + if tryCount > 10000*10 { + f5.RspErr2(c, 1, "internal error") + return + } + + code := aca.genCode(7) + postfix + _, exist := codelist[code] + if exist { + tryCount++ + continue + } + + codelist[code] = 1 + i++ + p := new(system.ActiveCode) + p.Batch_id = batchid + p.Code = code + p.CreateTime = nowsecs + p.ModifyTime = nowsecs + batchcodeinfo = append(batchcodeinfo, p) + } + + err = f5.GetApp().GetOrmDb(constant.ACCOUNT_DB).Table("t_activation_code").Create(batchcodeinfo).Error + if err != nil { + f5.RspErr2(c, 1, err.Error()) + return + } + + c.JSON(http.StatusOK, gin.H{ + "code": 0, + "message": "成功生成", + "batchid": batchid, + }) +} + +const strpol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz" + +func (this *ActiveCodeApi) genCode(n int) string { + var sb strings.Builder + k := len(strpol) + + for i := 0; i < n; i++ { + c := strpol[rand.Intn(k)] + sb.WriteByte(c) + } + + return sb.String() +} + +func (this *ActiveCodeApi) List(c *gin.Context) { + req := struct { + BatchId int32 `json:"batch_id"` + PageDto system.PageDto `json:"page_dto"` + }{} + if err := c.ShouldBindJSON(&req); err != nil { + f5.RspErr2(c, 1, err.Error()) + return + } + sql := fmt.Sprintf("SELECT * FROM t_activation_code WHERE 1=1 AND batch_id = %d", req.BatchId) + params := []string{} + result := []*system.ActiveCode{} + f5.GetGoStyleDb().PageQuery( + constant.ACCOUNT_DB, + q5.ToInt32(req.PageDto.PageSize), + q5.ToInt32(req.PageDto.Page), + sql, + params, + f5.GetDbFilter().Comp([]f5.DbQueryFilter{}...), + "", + func(err error, pg *f5.Pagination) { + if err != nil { + f5.RspErr2(c, 1, err.Error()) + return + } + + f5.UnmarshalModelList(pg.Rows, &result) + + c.JSON(http.StatusOK, gin.H{ + "code": 0, + "message": "获取成功", + "data": result, + "total": pg.Total, + "total_page": pg.TotalPages, + "cur_page": pg.CurrentPage, + }) + }) +} + +func (this *ActiveCodeApi) DownloadFile(c *gin.Context) { + batchid := q5.SafeToInt32(c.DefaultQuery("batchid", "")) + if batchid < 1 { + f5.RspErr2(c, 1, "batchid error") + return + } + + sql := fmt.Sprintf("SELECT * FROM t_activation_code WHERE 1=1 AND batch_id = %d", batchid) + f5.GetGoStyleDb().RawQuery( + constant.ACCOUNT_DB, + sql, + []string{}, + func(err error, ds *f5.DataSet) { + if err != nil { + f5.RspErr2(c, 1, err.Error()) + return + } + + data := []struct { + BatchId int32 `json:"batch_id"` + Code string `json:"activation_code"` + }{} + for ds.Next() { + p := q5.NewSliceElement(&data) + p.BatchId = q5.SafeToInt32(ds.GetByName("batch_id")) + p.Code = ds.GetByName("activation_code") + } + + c.JSON(http.StatusOK, gin.H{ + "code": 0, + "message": "", + "data": data, + }) + }) +} diff --git a/server/adminserver/api/v1/system/enter.go b/server/adminserver/api/v1/system/enter.go index a3781d1e..110464ab 100644 --- a/server/adminserver/api/v1/system/enter.go +++ b/server/adminserver/api/v1/system/enter.go @@ -13,4 +13,5 @@ type ApiGroup struct { GameSwitchApi BlockPlayerApi WhiteListApi + ActiveCodeApi } diff --git a/server/adminserver/model/system/activecode.go b/server/adminserver/model/system/activecode.go new file mode 100644 index 00000000..59105fbc --- /dev/null +++ b/server/adminserver/model/system/activecode.go @@ -0,0 +1,12 @@ +package system + +type ActiveCode struct { + Code string `gorm:"column:activation_code" json:"activation_code"` + Batch_id int32 `gorm:"column:batch_id" json:"batch_id"` + CreateTime int32 `gorm:"column:createtime" json:"-"` + ModifyTime int32 `gorm:"column:modifytime" json:"-"` +} + +func (ac *ActiveCode) TableName() string { + return "t_activation_code" +} diff --git a/server/adminserver/router/routermgr.go b/server/adminserver/router/routermgr.go index dd22a7e7..be8fa175 100644 --- a/server/adminserver/router/routermgr.go +++ b/server/adminserver/router/routermgr.go @@ -29,6 +29,7 @@ func (this *routerMgr) Init() { this.system.InitGameSwitchRouter(priGroup) this.system.InitBlockPlayerRouter(priGroup) this.system.InitWhiteListRouter(priGroup) + this.system.InitActiveCodeRouter(priGroup) f5.GetSysLog().Info("routerMgr.init") } diff --git a/server/adminserver/router/system/activecode.go b/server/adminserver/router/system/activecode.go new file mode 100644 index 00000000..412ddb8b --- /dev/null +++ b/server/adminserver/router/system/activecode.go @@ -0,0 +1,21 @@ +package system + +import ( + v1 "main/api/v1" + "main/middleware" + + "github.com/gin-gonic/gin" +) + +type ActiveCodeRoute struct { +} + +func (this *ActiveCodeRoute) InitActiveCodeRouter(priRouter *gin.RouterGroup) { + group := priRouter.Group("active_code") + api := v1.ApiGroupApp.SystemApiGroup.ActiveCodeApi + { + group.GET("gen", middleware.Permission("api/v1/active_code/gen", api.GenCode)) + group.POST("list", middleware.Permission("api/v1/active_code/list", api.List)) + group.GET("download", middleware.Permission("api/v1/active_code/download", api.DownloadFile)) + } +} \ No newline at end of file diff --git a/server/adminserver/router/system/enter.go b/server/adminserver/router/system/enter.go index 5a467cbf..f6acc66f 100644 --- a/server/adminserver/router/system/enter.go +++ b/server/adminserver/router/system/enter.go @@ -13,4 +13,5 @@ type RouterGroup struct { GameSwitchRoute BlockPlayerRoute WhiteListRoute + ActiveCodeRoute }