bindbox-game/internal/api/admin/user_token_admin.go

60 lines
2.1 KiB
Go
Executable File
Raw Permalink 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})
}
}