bindbox-game/internal/api/admin/user_token_admin.go
邹方成 45815bfb7d chore: 清理无用文件与优化代码结构
refactor(utils): 修复密码哈希比较逻辑错误
feat(user): 新增按状态筛选优惠券接口
docs: 添加虚拟发货与任务中心相关文档
fix(wechat): 修正Code2Session上下文传递问题
test: 补充订单折扣与积分转换测试用例
build: 更新配置文件与构建脚本
style: 清理多余的空行与注释
2025-12-18 17:35:55 +08:00

60 lines
2.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package admin
import (
"net/http"
"strconv"
"time"
"bindbox-game/configs"
"bindbox-game/internal/code"
"bindbox-game/internal/pkg/core"
"bindbox-game/internal/pkg/jwtoken"
"bindbox-game/internal/pkg/validation"
"bindbox-game/internal/proposal"
)
type issueUserTokenResponse struct {
Token string `json:"token"`
ExpiresIn int64 `json:"expires_in"`
}
// IssueUserToken 为指定APP用户签发访问令牌
// @Summary 管理端为APP用户签发令牌
// @Description 仅超级管理员可用用于测试或紧急场景下为指定用户签发APP令牌
// @Tags 管理端.用户
// @Accept json
// @Produce json
// @Param user_id path integer true "用户ID"
// @Success 200 {object} issueUserTokenResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/users/{user_id}/token [post]
// @Security LoginVerifyToken
func (h *handler) IssueUserToken() core.HandlerFunc {
return func(ctx core.Context) {
if ctx.SessionUserInfo().IsSuper != 1 {
ctx.AbortWithError(core.Error(http.StatusBadRequest, 10101, "禁止操作"))
return
}
idStr := ctx.Param("user_id")
userID, err := strconv.ParseInt(idStr, 10, 64)
if err != nil || userID <= 0 {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
return
}
u, e := h.readDB.Users.WithContext(ctx.RequestContext()).Where(h.readDB.Users.ID.Eq(userID)).First()
if e != nil || u == nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, 20101, "用户不存在"))
return
}
su := proposal.SessionUserInfo{Id: int32(u.ID), UserName: u.Nickname, NickName: u.Nickname, IsSuper: 0, Platform: "APP"}
exp := int64(30 * 24 * 3600)
token, sErr := jwtoken.New(configs.Get().JWT.PatientSecret).Sign(su, 30*24*time.Hour)
if sErr != nil {
ctx.AbortWithError(core.Error(http.StatusInternalServerError, 20102, sErr.Error()))
return
}
ctx.Payload(&issueUserTokenResponse{Token: token, ExpiresIn: exp})
}
}