Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 41s
新增随机种子生成与验证逻辑,包括: 1. 添加随机承诺生成接口 2. 实现抽奖执行与验证流程 3. 新增批量用户创建与删除功能 4. 添加抽奖收据记录表 5. 完善配置管理与错误码 新增测试用例验证随机算法正确性
82 lines
2.4 KiB
Go
82 lines
2.4 KiB
Go
package admin
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"bindbox-game/internal/code"
|
|
"bindbox-game/internal/pkg/core"
|
|
"bindbox-game/internal/pkg/validation"
|
|
"bindbox-game/internal/service/user"
|
|
)
|
|
|
|
type batchUsersRequest struct {
|
|
Count int `json:"count" binding:"min=1,max=1000"`
|
|
}
|
|
|
|
type batchUsersResponse struct {
|
|
Users []batchUserItem `json:"users"`
|
|
}
|
|
|
|
type batchUserItem struct {
|
|
ID int64 `json:"id"`
|
|
Nickname string `json:"nickname"`
|
|
OpenID string `json:"open_id"`
|
|
Avatar string `json:"avatar"`
|
|
}
|
|
|
|
func (h *handler) BatchCreateUsers() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
var req batchUsersRequest
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
|
|
return
|
|
}
|
|
users := make([]batchUserItem, 0, req.Count)
|
|
for i := 0; i < req.Count; i++ {
|
|
nickname := "用户" + strconv.Itoa(i+1) + "_" + strconv.FormatInt(time.Now().UnixNano()%10000, 10)
|
|
openID := "batch_" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
|
avatar := "https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=avatar&image_size=square"
|
|
u, err := h.user.CreateUser(ctx.RequestContext(), user.CreateUserInput{
|
|
Nickname: nickname,
|
|
OpenID: openID,
|
|
Avatar: avatar,
|
|
})
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateUserError, err.Error()))
|
|
return
|
|
}
|
|
users = append(users, batchUserItem{
|
|
ID: u.ID,
|
|
Nickname: u.Nickname,
|
|
OpenID: u.Openid,
|
|
Avatar: u.Avatar,
|
|
})
|
|
}
|
|
ctx.Payload(&batchUsersResponse{Users: users})
|
|
}
|
|
}
|
|
|
|
func (h *handler) BatchDeleteUsers() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
var req struct {
|
|
UserIDs []int64 `json:"user_ids" binding:"required,min=1,max=1000,dive,min=1"`
|
|
}
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
|
|
return
|
|
}
|
|
if ctx.SessionUserInfo().IsSuper != 1 {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.DeleteUserError, "禁止操作"))
|
|
return
|
|
}
|
|
for _, uid := range req.UserIDs {
|
|
if err := h.user.DeleteUser(ctx.RequestContext(), uid); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.DeleteUserError, err.Error()))
|
|
return
|
|
}
|
|
}
|
|
ctx.Payload(&simpleMessageResponse{Message: "操作成功"})
|
|
}
|
|
} |