bindbox-game/internal/api/admin/auth_refresh.go
邹方成 2a89a1ab9d
Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 39s
feat(admin): 更新前端资源文件及修复相关功能
refactor(service): 修改banner和guild删除逻辑为软删除
fix(service): 修复删除操作使用软删除而非物理删除

build: 添加SQLite测试仓库实现
docs: 新增奖励管理字段拆分和批量抽奖UI改造文档

ci: 更新CI忽略文件
style: 清理无用资源文件
2025-11-19 01:35:55 +08:00

50 lines
1.6 KiB
Go

package admin
import (
"net/http"
"time"
"bindbox-game/configs"
"bindbox-game/internal/code"
"bindbox-game/internal/pkg/core"
"bindbox-game/internal/pkg/jwtoken"
"bindbox-game/internal/pkg/utils"
)
type refreshResponse struct {
Token string `json:"token"`
ExpiresIn int64 `json:"expires_in"`
}
// RefreshToken 刷新管理员访问令牌
// @Summary 管理端令牌刷新
// @Tags 管理端.登录
// @Accept json
// @Produce json
// @Success 200 {object} refreshResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/auth/refresh [post]
// @Security LoginVerifyToken
func (h *handler) RefreshToken() core.HandlerFunc {
return func(ctx core.Context) {
auth := ctx.Request().Header.Get("Authorization")
if auth == "" {
ctx.AbortWithError(core.Error(http.StatusUnauthorized, code.AdminLoginError, "未携带令牌"))
return
}
newToken, err := jwtoken.New(configs.Get().JWT.AdminSecret).Refresh(auth)
if err != nil || newToken == "" {
ctx.AbortWithError(core.Error(http.StatusUnauthorized, code.AdminLoginError, "令牌刷新失败"))
return
}
info := ctx.SessionUserInfo()
if info.Id > 0 {
_, _ = h.writeDB.Admin.WithContext(ctx.RequestContext()).Where(h.writeDB.Admin.ID.Eq(int32(info.Id))).Updates(map[string]any{
"last_login_time": time.Now(),
"last_login_ip": utils.GetIP(ctx.Request()),
"last_login_hash": utils.MD5(newToken),
})
}
ctx.Payload(refreshResponse{Token: newToken, ExpiresIn: int64(24 * 3600)})
}
}