active code

This commit is contained in:
yangduo 2024-08-29 15:05:42 +08:00
parent f3e2165269
commit 481529614e
6 changed files with 216 additions and 0 deletions

View File

@ -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,
})
})
}

View File

@ -13,4 +13,5 @@ type ApiGroup struct {
GameSwitchApi
BlockPlayerApi
WhiteListApi
ActiveCodeApi
}

View File

@ -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"
}

View File

@ -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")
}

View File

@ -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))
}
}

View File

@ -13,4 +13,5 @@ type RouterGroup struct {
GameSwitchRoute
BlockPlayerRoute
WhiteListRoute
ActiveCodeRoute
}