Browse Source

修复swagger

master
hejl 7 days ago
parent
commit
552db62497
  1. 1
      .gitignore
  2. 532
      gofaster/backend/docs/docs.go
  3. 532
      gofaster/backend/docs/swagger.json
  4. 355
      gofaster/backend/docs/swagger.yaml
  5. 55
      gofaster/backend/internal/auth/controller/user_controller.go
  6. 2
      gofaster/backend/internal/auth/routes/auth_routes.go
  7. 2
      gofaster/backend/internal/auth/routes/role_routes.go
  8. 7
      gofaster/backend/internal/auth/service/user_service.go
  9. 2
      gofaster/backend/internal/shared/middleware/jwt_middleware.go
  10. BIN
      gofaster/backend/main.exe
  11. BIN
      gofaster/backend/tmp/main.exe

1
.gitignore vendored

@ -15,3 +15,4 @@ @@ -15,3 +15,4 @@
/gofaster/tmp/
/gofaster/app/dist/*/*/
/gofaster/backend/tmp
/gofaster/backend/tmp/main.exe

532
gofaster/backend/docs/docs.go

@ -24,6 +24,243 @@ const docTemplate = `{ @@ -24,6 +24,243 @@ const docTemplate = `{
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/auth/admin/users": {
"get": {
"description": "获取分页用户列表",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "获取用户列表",
"parameters": [
{
"type": "integer",
"default": 1,
"description": "页码",
"name": "page",
"in": "query"
},
{
"type": "integer",
"default": 10,
"description": "每页数量",
"name": "pageSize",
"in": "query"
}
],
"responses": {
"200": {
"description": "用户列表",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"400": {
"description": "请求参数错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
},
"/auth/admin/users/{id}": {
"get": {
"description": "根据ID获取用户详情",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "获取用户详情",
"parameters": [
{
"type": "integer",
"description": "用户ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "用户信息",
"schema": {
"$ref": "#/definitions/model.User"
}
},
"400": {
"description": "无效的用户ID",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"404": {
"description": "用户不存在",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
},
"put": {
"description": "根据ID更新用户信息",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "更新用户信息",
"parameters": [
{
"type": "integer",
"description": "用户ID",
"name": "id",
"in": "path",
"required": true
},
{
"description": "用户信息",
"name": "user",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/model.User"
}
}
],
"responses": {
"200": {
"description": "更新后的用户信息",
"schema": {
"$ref": "#/definitions/model.User"
}
},
"400": {
"description": "无效的用户ID或请求参数",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"404": {
"description": "用户不存在",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
},
"delete": {
"description": "根据ID删除用户",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "删除用户",
"parameters": [
{
"type": "integer",
"description": "用户ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"204": {
"description": "删除成功"
},
"400": {
"description": "无效的用户ID",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"404": {
"description": "用户不存在",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
},
"/auth/captcha": {
"get": {
"description": "生成图形验证码,用于登录验证",
@ -573,301 +810,6 @@ const docTemplate = `{ @@ -573,301 +810,6 @@ const docTemplate = `{
}
}
}
},
"/roles": {
"get": {
"description": "获取分页角色列表",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "获取角色列表",
"parameters": [
{
"type": "integer",
"default": 1,
"description": "页码",
"name": "page",
"in": "query"
},
{
"type": "integer",
"default": 100,
"description": "每页数量",
"name": "pageSize",
"in": "query"
}
],
"responses": {
"200": {
"description": "角色列表",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"400": {
"description": "请求参数错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
},
"/users": {
"get": {
"description": "获取分页用户列表",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "获取用户列表",
"parameters": [
{
"type": "integer",
"default": 1,
"description": "页码",
"name": "page",
"in": "query"
},
{
"type": "integer",
"default": 10,
"description": "每页数量",
"name": "pageSize",
"in": "query"
}
],
"responses": {
"200": {
"description": "用户列表",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"400": {
"description": "请求参数错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
},
"/users/{id}": {
"get": {
"description": "根据ID获取用户详情",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "获取用户详情",
"parameters": [
{
"type": "integer",
"description": "用户ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "用户信息",
"schema": {
"$ref": "#/definitions/model.User"
}
},
"400": {
"description": "无效的用户ID",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"404": {
"description": "用户不存在",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
},
"put": {
"description": "根据ID更新用户信息",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "更新用户信息",
"parameters": [
{
"type": "integer",
"description": "用户ID",
"name": "id",
"in": "path",
"required": true
},
{
"description": "用户信息",
"name": "user",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/model.User"
}
}
],
"responses": {
"200": {
"description": "更新后的用户信息",
"schema": {
"$ref": "#/definitions/model.User"
}
},
"400": {
"description": "无效的用户ID或请求参数",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"404": {
"description": "用户不存在",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
},
"delete": {
"description": "根据ID删除用户",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "删除用户",
"parameters": [
{
"type": "integer",
"description": "用户ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"204": {
"description": "删除成功"
},
"400": {
"description": "无效的用户ID",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"404": {
"description": "用户不存在",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
}
},
"definitions": {

532
gofaster/backend/docs/swagger.json

@ -21,6 +21,243 @@ @@ -21,6 +21,243 @@
"host": "localhost:8080",
"basePath": "/api",
"paths": {
"/auth/admin/users": {
"get": {
"description": "获取分页用户列表",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "获取用户列表",
"parameters": [
{
"type": "integer",
"default": 1,
"description": "页码",
"name": "page",
"in": "query"
},
{
"type": "integer",
"default": 10,
"description": "每页数量",
"name": "pageSize",
"in": "query"
}
],
"responses": {
"200": {
"description": "用户列表",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"400": {
"description": "请求参数错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
},
"/auth/admin/users/{id}": {
"get": {
"description": "根据ID获取用户详情",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "获取用户详情",
"parameters": [
{
"type": "integer",
"description": "用户ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "用户信息",
"schema": {
"$ref": "#/definitions/model.User"
}
},
"400": {
"description": "无效的用户ID",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"404": {
"description": "用户不存在",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
},
"put": {
"description": "根据ID更新用户信息",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "更新用户信息",
"parameters": [
{
"type": "integer",
"description": "用户ID",
"name": "id",
"in": "path",
"required": true
},
{
"description": "用户信息",
"name": "user",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/model.User"
}
}
],
"responses": {
"200": {
"description": "更新后的用户信息",
"schema": {
"$ref": "#/definitions/model.User"
}
},
"400": {
"description": "无效的用户ID或请求参数",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"404": {
"description": "用户不存在",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
},
"delete": {
"description": "根据ID删除用户",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "删除用户",
"parameters": [
{
"type": "integer",
"description": "用户ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"204": {
"description": "删除成功"
},
"400": {
"description": "无效的用户ID",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"404": {
"description": "用户不存在",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
},
"/auth/captcha": {
"get": {
"description": "生成图形验证码,用于登录验证",
@ -570,301 +807,6 @@ @@ -570,301 +807,6 @@
}
}
}
},
"/roles": {
"get": {
"description": "获取分页角色列表",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "获取角色列表",
"parameters": [
{
"type": "integer",
"default": 1,
"description": "页码",
"name": "page",
"in": "query"
},
{
"type": "integer",
"default": 100,
"description": "每页数量",
"name": "pageSize",
"in": "query"
}
],
"responses": {
"200": {
"description": "角色列表",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"400": {
"description": "请求参数错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
},
"/users": {
"get": {
"description": "获取分页用户列表",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "获取用户列表",
"parameters": [
{
"type": "integer",
"default": 1,
"description": "页码",
"name": "page",
"in": "query"
},
{
"type": "integer",
"default": 10,
"description": "每页数量",
"name": "pageSize",
"in": "query"
}
],
"responses": {
"200": {
"description": "用户列表",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"400": {
"description": "请求参数错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
},
"/users/{id}": {
"get": {
"description": "根据ID获取用户详情",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "获取用户详情",
"parameters": [
{
"type": "integer",
"description": "用户ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "用户信息",
"schema": {
"$ref": "#/definitions/model.User"
}
},
"400": {
"description": "无效的用户ID",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"404": {
"description": "用户不存在",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
},
"put": {
"description": "根据ID更新用户信息",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "更新用户信息",
"parameters": [
{
"type": "integer",
"description": "用户ID",
"name": "id",
"in": "path",
"required": true
},
{
"description": "用户信息",
"name": "user",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/model.User"
}
}
],
"responses": {
"200": {
"description": "更新后的用户信息",
"schema": {
"$ref": "#/definitions/model.User"
}
},
"400": {
"description": "无效的用户ID或请求参数",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"404": {
"description": "用户不存在",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
},
"delete": {
"description": "根据ID删除用户",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"用户管理"
],
"summary": "删除用户",
"parameters": [
{
"type": "integer",
"description": "用户ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"204": {
"description": "删除成功"
},
"400": {
"description": "无效的用户ID",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"404": {
"description": "用户不存在",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "服务器内部错误",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
}
},
"definitions": {

355
gofaster/backend/docs/swagger.yaml

@ -213,6 +213,164 @@ info: @@ -213,6 +213,164 @@ info:
title: GoFaster API
version: "1.0"
paths:
/auth/admin/users:
get:
consumes:
- application/json
description: 获取分页用户列表
parameters:
- default: 1
description: 页码
in: query
name: page
type: integer
- default: 10
description: 每页数量
in: query
name: pageSize
type: integer
produces:
- application/json
responses:
"200":
description: 用户列表
schema:
additionalProperties: true
type: object
"400":
description: 请求参数错误
schema:
additionalProperties:
type: string
type: object
"500":
description: 服务器内部错误
schema:
additionalProperties:
type: string
type: object
summary: 获取用户列表
tags:
- 用户管理
/auth/admin/users/{id}:
delete:
consumes:
- application/json
description: 根据ID删除用户
parameters:
- description: 用户ID
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"204":
description: 删除成功
"400":
description: 无效的用户ID
schema:
additionalProperties:
type: string
type: object
"404":
description: 用户不存在
schema:
additionalProperties:
type: string
type: object
"500":
description: 服务器内部错误
schema:
additionalProperties:
type: string
type: object
summary: 删除用户
tags:
- 用户管理
get:
consumes:
- application/json
description: 根据ID获取用户详情
parameters:
- description: 用户ID
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: 用户信息
schema:
$ref: '#/definitions/model.User'
"400":
description: 无效的用户ID
schema:
additionalProperties:
type: string
type: object
"404":
description: 用户不存在
schema:
additionalProperties:
type: string
type: object
"500":
description: 服务器内部错误
schema:
additionalProperties:
type: string
type: object
summary: 获取用户详情
tags:
- 用户管理
put:
consumes:
- application/json
description: 根据ID更新用户信息
parameters:
- description: 用户ID
in: path
name: id
required: true
type: integer
- description: 用户信息
in: body
name: user
required: true
schema:
$ref: '#/definitions/model.User'
produces:
- application/json
responses:
"200":
description: 更新后的用户信息
schema:
$ref: '#/definitions/model.User'
"400":
description: 无效的用户ID或请求参数
schema:
additionalProperties:
type: string
type: object
"404":
description: 用户不存在
schema:
additionalProperties:
type: string
type: object
"500":
description: 服务器内部错误
schema:
additionalProperties:
type: string
type: object
summary: 更新用户信息
tags:
- 用户管理
/auth/captcha:
get:
description: 生成图形验证码,用于登录验证
@ -544,203 +702,6 @@ paths: @@ -544,203 +702,6 @@ paths:
summary: 获取用户信息
tags:
- 认证
/roles:
get:
consumes:
- application/json
description: 获取分页角色列表
parameters:
- default: 1
description: 页码
in: query
name: page
type: integer
- default: 100
description: 每页数量
in: query
name: pageSize
type: integer
produces:
- application/json
responses:
"200":
description: 角色列表
schema:
additionalProperties: true
type: object
"400":
description: 请求参数错误
schema:
additionalProperties:
type: string
type: object
"500":
description: 服务器内部错误
schema:
additionalProperties:
type: string
type: object
summary: 获取角色列表
tags:
- 用户管理
/users:
get:
consumes:
- application/json
description: 获取分页用户列表
parameters:
- default: 1
description: 页码
in: query
name: page
type: integer
- default: 10
description: 每页数量
in: query
name: pageSize
type: integer
produces:
- application/json
responses:
"200":
description: 用户列表
schema:
additionalProperties: true
type: object
"400":
description: 请求参数错误
schema:
additionalProperties:
type: string
type: object
"500":
description: 服务器内部错误
schema:
additionalProperties:
type: string
type: object
summary: 获取用户列表
tags:
- 用户管理
/users/{id}:
delete:
consumes:
- application/json
description: 根据ID删除用户
parameters:
- description: 用户ID
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"204":
description: 删除成功
"400":
description: 无效的用户ID
schema:
additionalProperties:
type: string
type: object
"404":
description: 用户不存在
schema:
additionalProperties:
type: string
type: object
"500":
description: 服务器内部错误
schema:
additionalProperties:
type: string
type: object
summary: 删除用户
tags:
- 用户管理
get:
consumes:
- application/json
description: 根据ID获取用户详情
parameters:
- description: 用户ID
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: 用户信息
schema:
$ref: '#/definitions/model.User'
"400":
description: 无效的用户ID
schema:
additionalProperties:
type: string
type: object
"404":
description: 用户不存在
schema:
additionalProperties:
type: string
type: object
"500":
description: 服务器内部错误
schema:
additionalProperties:
type: string
type: object
summary: 获取用户详情
tags:
- 用户管理
put:
consumes:
- application/json
description: 根据ID更新用户信息
parameters:
- description: 用户ID
in: path
name: id
required: true
type: integer
- description: 用户信息
in: body
name: user
required: true
schema:
$ref: '#/definitions/model.User'
produces:
- application/json
responses:
"200":
description: 更新后的用户信息
schema:
$ref: '#/definitions/model.User'
"400":
description: 无效的用户ID或请求参数
schema:
additionalProperties:
type: string
type: object
"404":
description: 用户不存在
schema:
additionalProperties:
type: string
type: object
"500":
description: 服务器内部错误
schema:
additionalProperties:
type: string
type: object
summary: 更新用户信息
tags:
- 用户管理
schemes:
- http
securityDefinitions:

55
gofaster/backend/internal/auth/controller/user_controller.go

@ -33,7 +33,7 @@ func NewUserController(userService *service.UserService) *UserController { @@ -33,7 +33,7 @@ func NewUserController(userService *service.UserService) *UserController {
// @Success 200 {object} map[string]interface{} "用户列表"
// @Failure 400 {object} map[string]string "请求参数错误"
// @Failure 500 {object} map[string]string "服务器内部错误"
// @Router /users [get]
// @Router /auth/admin/users [get]
func (c *UserController) ListUsers(ctx *gin.Context) {
// 获取分页参数,默认值 page=1, pageSize=10
pageStr := ctx.DefaultQuery("page", "1")
@ -77,7 +77,7 @@ func (c *UserController) ListUsers(ctx *gin.Context) { @@ -77,7 +77,7 @@ func (c *UserController) ListUsers(ctx *gin.Context) {
// @Success 201 {object} model.User "创建的用户信息"
// @Failure 400 {object} map[string]string "请求参数错误"
// @Failure 500 {object} map[string]string "服务器内部错误"
// @Router /users [post]
// @Router /auth/admin/users [post]
func (c *UserController) CreateUser(ctx *gin.Context) {
var user model.User
@ -105,7 +105,7 @@ func (c *UserController) CreateUser(ctx *gin.Context) { @@ -105,7 +105,7 @@ func (c *UserController) CreateUser(ctx *gin.Context) {
// @Failure 400 {object} map[string]string "无效的用户ID"
// @Failure 404 {object} map[string]string "用户不存在"
// @Failure 500 {object} map[string]string "服务器内部错误"
// @Router /users/{id} [get]
// @Router /auth/admin/users/{id} [get]
func (c *UserController) GetUser(ctx *gin.Context) {
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
@ -139,7 +139,7 @@ func (c *UserController) GetUser(ctx *gin.Context) { @@ -139,7 +139,7 @@ func (c *UserController) GetUser(ctx *gin.Context) {
// @Failure 400 {object} map[string]string "无效的用户ID或请求参数"
// @Failure 404 {object} map[string]string "用户不存在"
// @Failure 500 {object} map[string]string "服务器内部错误"
// @Router /users/{id} [put]
// @Router /auth/admin/users/{id} [put]
func (c *UserController) UpdateUser(ctx *gin.Context) {
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
@ -183,7 +183,7 @@ func (c *UserController) UpdateUser(ctx *gin.Context) { @@ -183,7 +183,7 @@ func (c *UserController) UpdateUser(ctx *gin.Context) {
// @Failure 400 {object} map[string]string "无效的用户ID"
// @Failure 404 {object} map[string]string "用户不存在"
// @Failure 500 {object} map[string]string "服务器内部错误"
// @Router /users/{id} [delete]
// @Router /auth/admin/users/{id} [delete]
func (c *UserController) DeleteUser(ctx *gin.Context) {
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
@ -203,48 +203,3 @@ func (c *UserController) DeleteUser(ctx *gin.Context) { @@ -203,48 +203,3 @@ func (c *UserController) DeleteUser(ctx *gin.Context) {
response.Success(ctx, "用户删除成功", nil)
}
// GetRoles godoc
// @Summary 获取角色列表
// @Description 获取分页角色列表
// @Tags 用户管理
// @Accept json
// @Produce json
// @Param page query int false "页码" default(1)
// @Param pageSize query int false "每页数量" default(100)
// @Success 200 {object} map[string]interface{} "角色列表"
// @Failure 400 {object} map[string]string "请求参数错误"
// @Failure 500 {object} map[string]string "服务器内部错误"
// @Router /roles [get]
func (c *UserController) GetRoles(ctx *gin.Context) {
// 获取分页参数,默认值 page=1, pageSize=100
pageStr := ctx.DefaultQuery("page", "1")
pageSizeStr := ctx.DefaultQuery("pageSize", "100")
page, err := strconv.Atoi(pageStr)
if err != nil || page < 1 {
response.Error(ctx, http.StatusBadRequest, "请求参数错误", "无效的页码参数")
return
}
pageSize, err := strconv.Atoi(pageSizeStr)
if err != nil || pageSize < 1 || pageSize > 1000 {
response.Error(ctx, http.StatusBadRequest, "请求参数错误", "无效的每页大小参数,范围1-1000")
return
}
// 调用服务层获取角色列表
roles, total, err := c.userService.GetRoles(ctx.Request.Context(), page, pageSize)
if err != nil {
response.Error(ctx, http.StatusInternalServerError, "获取角色列表失败", err.Error())
return
}
// 返回分页结果
response.Success(ctx, "获取角色列表成功", gin.H{
"data": roles,
"total": total,
"page": page,
"size": pageSize,
})
}

2
gofaster/backend/internal/auth/routes/auth_routes.go

@ -68,7 +68,6 @@ func RegisterAuthRoutes(router *gin.RouterGroup, db *gorm.DB, jwtSecret string) @@ -68,7 +68,6 @@ func RegisterAuthRoutes(router *gin.RouterGroup, db *gorm.DB, jwtSecret string)
admin.GET("/users/:id", userController.GetUser)
admin.PUT("/users/:id", userController.UpdateUser)
admin.DELETE("/users/:id", userController.DeleteUser)
admin.GET("/roles", userController.GetRoles)
admin.POST("/users/:id/reset-password", passwordController.ResetPassword)
admin.PUT("/password-policy", passwordController.UpdatePasswordPolicy)
}
@ -78,7 +77,6 @@ func RegisterAuthRoutes(router *gin.RouterGroup, db *gorm.DB, jwtSecret string) @@ -78,7 +77,6 @@ func RegisterAuthRoutes(router *gin.RouterGroup, db *gorm.DB, jwtSecret string)
testAdmin.Use(middleware.JWTAuth()) // 只检查JWT,不检查权限
{
testAdmin.GET("/users", userController.ListUsers)
testAdmin.GET("/roles", userController.GetRoles)
}
// 注册资源管理路由

2
gofaster/backend/internal/auth/routes/role_routes.go

@ -30,7 +30,7 @@ func RegisterRoleRoutes(router *gin.RouterGroup, db *gorm.DB, jwtSecret string) @@ -30,7 +30,7 @@ func RegisterRoleRoutes(router *gin.RouterGroup, db *gorm.DB, jwtSecret string)
}
// 需要权限验证的路由
roleGroup.Use(middleware.AuthMiddleware(jwtSecret))
roleGroup.Use(middleware.JWTAuth())
{
// 角色CRUD操作
roleGroup.GET("", roleController.ListRoles) // 获取角色列表

7
gofaster/backend/internal/auth/service/user_service.go

@ -53,10 +53,3 @@ func (s *UserService) Update(user *model.User) error { @@ -53,10 +53,3 @@ func (s *UserService) Update(user *model.User) error {
ctx := context.Background()
return s.repo.Update(ctx, user)
}
// GetRoles 获取角色列表
func (s *UserService) GetRoles(ctx context.Context, page, pageSize int) ([]*model.Role, int64, error) {
roleRepo := repository.NewRoleRepository(s.db)
offset := (page - 1) * pageSize
return roleRepo.List(ctx, offset, pageSize)
}

2
gofaster/backend/internal/shared/middleware/jwt_middleware.go

@ -37,7 +37,7 @@ func JWTAuth() gin.HandlerFunc { @@ -37,7 +37,7 @@ func JWTAuth() gin.HandlerFunc {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
// 返回密钥
// 返回密钥 - 使用配置中的密钥
return []byte("your-secret-key"), nil
})

BIN
gofaster/backend/main.exe

Binary file not shown.

BIN
gofaster/backend/tmp/main.exe

Binary file not shown.
Loading…
Cancel
Save