Browse Source

权限初步模板

main
pixelqm 5 years ago
parent
commit
9f17f71ee2
  1. 56
      QMPlusServer/controller/api/api.go
  2. 60
      QMPlusServer/controller/api/authority.go
  3. 1
      QMPlusServer/controller/api/menu.go
  4. 189
      QMPlusServer/docs/docs.go
  5. 187
      QMPlusServer/docs/swagger.json
  6. 129
      QMPlusServer/docs/swagger.yaml
  7. 8
      QMPlusServer/init/initRouter/initRouter.go
  8. 2
      QMPlusServer/init/registTable/registTable.go
  9. 43
      QMPlusServer/model/dbModel/api.go
  10. 16
      QMPlusServer/model/dbModel/authority.go
  11. 1
      QMPlusServer/model/dbModel/menu.go
  12. 15
      QMPlusServer/router/api.go
  13. 15
      QMPlusServer/router/authority.go

56
QMPlusServer/controller/api/api.go

@ -0,0 +1,56 @@
package api
import (
"fmt"
"github.com/gin-gonic/gin"
"main/controller/servers"
"main/model/dbModel"
)
type CreateApiParams struct {
AuthorityId uint `json:"-"`
Path string `json:"path"`
Description string `json:"description"`
}
type DeleteApiParams struct {
AuthorityId uint `json:"-"`
}
// @Tags Api
// @Summary 为指定角色创建api
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body api.CreateApiParams true "创建api"
// @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/createApi [post]
func CreateApi(c *gin.Context) {
var api dbModel.Api
_ = c.BindJSON(&api)
err := api.CreateApi()
if err != nil {
servers.ReportFormat(c, false, fmt.Sprintf("创建失败:%v", err), gin.H{})
} else {
servers.ReportFormat(c, true, "创建成功", gin.H{})
}
}
// @Tags Api
// @Summary 删除指定角色api
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body api.DeleteApiParams true "删除api"
// @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/deleteApi [post]
func DeleteApi(c *gin.Context) {
var a dbModel.Api
_ = c.BindJSON(&a)
err := a.DeleteApi()
if err != nil {
servers.ReportFormat(c, false, fmt.Sprintf("删除失败:%v", err), gin.H{})
} else {
servers.ReportFormat(c, true, "删除成功", gin.H{})
}
}

60
QMPlusServer/controller/api/authority.go

@ -0,0 +1,60 @@
package api
import (
"fmt"
"github.com/gin-gonic/gin"
"main/controller/servers"
"main/model/dbModel"
)
type CreateAuthorityPatams struct {
AuthorityId uint `json:"authorityId"`
AuthorityName string `json:"authorityName"`
}
// @Tags authority
// @Summary 创建角色
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body api.CreateAuthorityPatams true "创建角色"
// @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /authority/createAuthority [post]
func CreateAuthority(c *gin.Context) {
var auth dbModel.Authority
_ = c.BindJSON(&auth)
err, authBack := auth.CreateAuthority()
if err != nil {
servers.ReportFormat(c, false, fmt.Sprintf("创建失败:%v", err), gin.H{
"authority": authBack,
})
} else {
servers.ReportFormat(c, true, "创建成功", gin.H{
"authority": authBack,
})
}
}
type DeleteAuthorityPatams struct {
AuthorityId uint `json:"authorityId"`
}
// @Tags authority
// @Summary 删除角色
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body api.DeleteAuthorityPatams true "删除角色"
// @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /authority/deleteAuthority [post]
func DeleteAuthority(c *gin.Context) {
var a dbModel.Authority
_ = c.BindJSON(&a)
//删除角色之前需要判断是否有用户正在使用此角色
err := a.DeleteAuthority()
if err != nil {
servers.ReportFormat(c, false, fmt.Sprintf("删除失败:%v", err), gin.H{})
} else {
servers.ReportFormat(c, true, "删除成功", gin.H{})
}
}

1
QMPlusServer/controller/api/menu.go

@ -15,6 +15,7 @@ import (
// @Param data body api.RegistAndLoginStuct true "可以什么都不填"
// @Success 200 {string} json "{"success":true,"data":{},"msg":"返回成功"}"
// @Router /menu/getMenu [post]
func GetMenu(c *gin.Context) {
claims, _ := c.Get("claims")
waitUse := claims.(*middleware.CustomClaims)

189
QMPlusServer/docs/docs.go

@ -1,6 +1,6 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// 2019-09-07 22:49:45.2497438 +0800 CST m=+0.040919801
// 2019-09-08 16:14:12.6903257 +0800 CST m=+0.106563301
package docs
@ -26,30 +26,38 @@ var doc = `{
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/base/login": {
"/api/createApi": {
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Base"
"Api"
],
"summary": "用户登录",
"summary": "为指定角色创建api",
"parameters": [
{
"description": "用户登录接口",
"description": "创建api",
"name": "data",
"in": "body",
"required": true,
"schema": {
"type": "object",
"$ref": "#/definitions/api.RegistAndLoginStuct"
"$ref": "#/definitions/api.CreateApiParams"
}
}
],
"responses": {
"200": {
"description": "{\"success\":true,\"data\":{},\"msg\":\"登陆成功\"}",
"description": "{\"success\":true,\"data\":{},\"msg\":\"获取成功\"}",
"schema": {
"type": "string"
}
@ -57,30 +65,77 @@ var doc = `{
}
}
},
"/base/regist": {
"/api/deleteApi": {
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Base"
"Api"
],
"summary": "用户注册账号",
"summary": "删除指定角色api",
"parameters": [
{
"description": "用户注册接口",
"description": "删除api",
"name": "data",
"in": "body",
"required": true,
"schema": {
"type": "object",
"$ref": "#/definitions/api.RegistAndLoginStuct"
"$ref": "#/definitions/api.DeleteApiParams"
}
}
],
"responses": {
"200": {
"description": "{\"success\":true,\"data\":{},\"msg\":\"注册成功\"}",
"description": "{\"success\":true,\"data\":{},\"msg\":\"获取成功\"}",
"schema": {
"type": "string"
}
}
}
}
},
"/authority/createAuthority": {
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"authority"
],
"summary": "创建角色",
"parameters": [
{
"description": "创建角色",
"name": "data",
"in": "body",
"required": true,
"schema": {
"type": "object",
"$ref": "#/definitions/api.CreateAuthorityPatams"
}
}
],
"responses": {
"200": {
"description": "{\"success\":true,\"data\":{},\"msg\":\"获取成功\"}",
"schema": {
"type": "string"
}
@ -88,23 +143,88 @@ var doc = `{
}
}
},
"/menu/getMenu": {
"/authority/deleteAuthority": {
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"authority"
],
"summary": "删除角色",
"parameters": [
{
"description": "删除角色",
"name": "data",
"in": "body",
"required": true,
"schema": {
"type": "object",
"$ref": "#/definitions/api.DeleteAuthorityPatams"
}
}
],
"responses": {
"200": {
"description": "{\"success\":true,\"data\":{},\"msg\":\"获取成功\"}",
"schema": {
"type": "string"
}
}
}
}
},
"/base/login": {
"post": {
"produces": [
"application/json"
],
"tags": [
"Base"
],
"summary": "用户登录",
"parameters": [
{
"description": "用户登录接口",
"name": "data",
"in": "body",
"required": true,
"schema": {
"type": "object",
"$ref": "#/definitions/api.RegistAndLoginStuct"
}
}
],
"responses": {
"200": {
"description": "{\"success\":true,\"data\":{},\"msg\":\"登陆成功\"}",
"schema": {
"type": "string"
}
}
}
}
},
"/base/regist": {
"post": {
"produces": [
"application/json"
],
"tags": [
"Menu"
"Base"
],
"summary": "获取用户动态路由",
"summary": "用户注册账号",
"parameters": [
{
"description": "可以什么都不填",
"description": "用户注册接口",
"name": "data",
"in": "body",
"required": true,
@ -116,7 +236,7 @@ var doc = `{
],
"responses": {
"200": {
"description": "{\"success\":true,\"data\":{},\"msg\":\"返回成功\"}",
"description": "{\"success\":true,\"data\":{},\"msg\":\"注册成功\"}",
"schema": {
"type": "string"
}
@ -258,6 +378,39 @@ var doc = `{
}
}
},
"api.CreateApiParams": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"path": {
"type": "string"
}
}
},
"api.CreateAuthorityPatams": {
"type": "object",
"properties": {
"authorityId": {
"type": "integer"
},
"authorityName": {
"type": "string"
}
}
},
"api.DeleteApiParams": {
"type": "object"
},
"api.DeleteAuthorityPatams": {
"type": "object",
"properties": {
"authorityId": {
"type": "integer"
}
}
},
"api.RegistAndLoginStuct": {
"type": "object",
"properties": {

187
QMPlusServer/docs/swagger.json

@ -9,30 +9,38 @@
},
"basePath": "/",
"paths": {
"/base/login": {
"/api/createApi": {
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Base"
"Api"
],
"summary": "用户登录",
"summary": "为指定角色创建api",
"parameters": [
{
"description": "用户登录接口",
"description": "创建api",
"name": "data",
"in": "body",
"required": true,
"schema": {
"type": "object",
"$ref": "#/definitions/api.RegistAndLoginStuct"
"$ref": "#/definitions/api.CreateApiParams"
}
}
],
"responses": {
"200": {
"description": "{\"success\":true,\"data\":{},\"msg\":\"登陆成功\"}",
"description": "{\"success\":true,\"data\":{},\"msg\":\"获取成功\"}",
"schema": {
"type": "string"
}
@ -40,30 +48,77 @@
}
}
},
"/base/regist": {
"/api/deleteApi": {
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Base"
"Api"
],
"summary": "用户注册账号",
"summary": "删除指定角色api",
"parameters": [
{
"description": "用户注册接口",
"description": "删除api",
"name": "data",
"in": "body",
"required": true,
"schema": {
"type": "object",
"$ref": "#/definitions/api.RegistAndLoginStuct"
"$ref": "#/definitions/api.DeleteApiParams"
}
}
],
"responses": {
"200": {
"description": "{\"success\":true,\"data\":{},\"msg\":\"注册成功\"}",
"description": "{\"success\":true,\"data\":{},\"msg\":\"获取成功\"}",
"schema": {
"type": "string"
}
}
}
}
},
"/authority/createAuthority": {
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"authority"
],
"summary": "创建角色",
"parameters": [
{
"description": "创建角色",
"name": "data",
"in": "body",
"required": true,
"schema": {
"type": "object",
"$ref": "#/definitions/api.CreateAuthorityPatams"
}
}
],
"responses": {
"200": {
"description": "{\"success\":true,\"data\":{},\"msg\":\"获取成功\"}",
"schema": {
"type": "string"
}
@ -71,23 +126,88 @@
}
}
},
"/menu/getMenu": {
"/authority/deleteAuthority": {
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"authority"
],
"summary": "删除角色",
"parameters": [
{
"description": "删除角色",
"name": "data",
"in": "body",
"required": true,
"schema": {
"type": "object",
"$ref": "#/definitions/api.DeleteAuthorityPatams"
}
}
],
"responses": {
"200": {
"description": "{\"success\":true,\"data\":{},\"msg\":\"获取成功\"}",
"schema": {
"type": "string"
}
}
}
}
},
"/base/login": {
"post": {
"produces": [
"application/json"
],
"tags": [
"Base"
],
"summary": "用户登录",
"parameters": [
{
"description": "用户登录接口",
"name": "data",
"in": "body",
"required": true,
"schema": {
"type": "object",
"$ref": "#/definitions/api.RegistAndLoginStuct"
}
}
],
"responses": {
"200": {
"description": "{\"success\":true,\"data\":{},\"msg\":\"登陆成功\"}",
"schema": {
"type": "string"
}
}
}
}
},
"/base/regist": {
"post": {
"produces": [
"application/json"
],
"tags": [
"Menu"
"Base"
],
"summary": "获取用户动态路由",
"summary": "用户注册账号",
"parameters": [
{
"description": "可以什么都不填",
"description": "用户注册接口",
"name": "data",
"in": "body",
"required": true,
@ -99,7 +219,7 @@
],
"responses": {
"200": {
"description": "{\"success\":true,\"data\":{},\"msg\":\"返回成功\"}",
"description": "{\"success\":true,\"data\":{},\"msg\":\"注册成功\"}",
"schema": {
"type": "string"
}
@ -241,6 +361,39 @@
}
}
},
"api.CreateApiParams": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"path": {
"type": "string"
}
}
},
"api.CreateAuthorityPatams": {
"type": "object",
"properties": {
"authorityId": {
"type": "integer"
},
"authorityName": {
"type": "string"
}
}
},
"api.DeleteApiParams": {
"type": "object"
},
"api.DeleteAuthorityPatams": {
"type": "object",
"properties": {
"authorityId": {
"type": "integer"
}
}
},
"api.RegistAndLoginStuct": {
"type": "object",
"properties": {

129
QMPlusServer/docs/swagger.yaml

@ -9,6 +9,27 @@ definitions:
userName:
type: string
type: object
api.CreateApiParams:
properties:
description:
type: string
path:
type: string
type: object
api.CreateAuthorityPatams:
properties:
authorityId:
type: integer
authorityName:
type: string
type: object
api.DeleteApiParams:
type: object
api.DeleteAuthorityPatams:
properties:
authorityId:
type: integer
type: object
api.RegistAndLoginStuct:
properties:
passWord:
@ -30,30 +51,106 @@ info:
title: Swagger Example API
version: 0.0.1
paths:
/base/login:
/api/createApi:
post:
consumes:
- application/json
parameters:
- description: 用户登录接口
- description: 创建api
in: body
name: data
required: true
schema:
$ref: '#/definitions/api.RegistAndLoginStuct'
$ref: '#/definitions/api.CreateApiParams'
type: object
produces:
- application/json
responses:
"200":
description: '{"success":true,"data":{},"msg":"登陆成功"}'
description: '{"success":true,"data":{},"msg":"获取成功"}'
schema:
type: string
summary: 用户登录
security:
- ApiKeyAuth: []
summary: 为指定角色创建api
tags:
- Base
/base/regist:
- Api
/api/deleteApi:
post:
consumes:
- application/json
parameters:
- description: 用户注册接口
- description: 删除api
in: body
name: data
required: true
schema:
$ref: '#/definitions/api.DeleteApiParams'
type: object
produces:
- application/json
responses:
"200":
description: '{"success":true,"data":{},"msg":"获取成功"}'
schema:
type: string
security:
- ApiKeyAuth: []
summary: 删除指定角色api
tags:
- Api
/authority/createAuthority:
post:
consumes:
- application/json
parameters:
- description: 创建角色
in: body
name: data
required: true
schema:
$ref: '#/definitions/api.CreateAuthorityPatams'
type: object
produces:
- application/json
responses:
"200":
description: '{"success":true,"data":{},"msg":"获取成功"}'
schema:
type: string
security:
- ApiKeyAuth: []
summary: 创建角色
tags:
- authority
/authority/deleteAuthority:
post:
consumes:
- application/json
parameters:
- description: 删除角色
in: body
name: data
required: true
schema:
$ref: '#/definitions/api.DeleteAuthorityPatams'
type: object
produces:
- application/json
responses:
"200":
description: '{"success":true,"data":{},"msg":"获取成功"}'
schema:
type: string
security:
- ApiKeyAuth: []
summary: 删除角色
tags:
- authority
/base/login:
post:
parameters:
- description: 用户登录接口
in: body
name: data
required: true
@ -64,16 +161,16 @@ paths:
- application/json
responses:
"200":
description: '{"success":true,"data":{},"msg":"注册成功"}'
description: '{"success":true,"data":{},"msg":"登陆成功"}'
schema:
type: string
summary: 用户注册账号
summary: 用户登录
tags:
- Base
/menu/getMenu:
/base/regist:
post:
parameters:
- description: 可以什么都不填
- description: 用户注册接口
in: body
name: data
required: true
@ -84,14 +181,12 @@ paths:
- application/json
responses:
"200":
description: '{"success":true,"data":{},"msg":"返回成功"}'
description: '{"success":true,"data":{},"msg":"注册成功"}'
schema:
type: string
security:
- ApiKeyAuth: []
summary: 获取用户动态路由
summary: 用户注册账号
tags:
- Menu
- Base
/user/changePassWord:
post:
parameters:

8
QMPlusServer/init/initRouter/initRouter.go

@ -13,8 +13,10 @@ func InitRouter() *gin.Engine {
var Router = gin.Default()
Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
//Router.Use(middleware.Logger())
router.InitUserRouter(Router)
router.InitBaseRouter(Router)
router.InitMenuRouter(Router)
router.InitUserRouter(Router) // 注册用户路由
router.InitBaseRouter(Router) // 注册基础功能路由
router.InitMenuRouter(Router) // 注册menu路由
router.InitAuthorityRouter(Router) // 注册角色路由
router.InitApiRouter(Router) // 注册功能api路由
return Router
}

2
QMPlusServer/init/registTable/registTable.go

@ -7,5 +7,5 @@ import (
//注册数据库表专用
func RegistTable(db *gorm.DB) {
db.AutoMigrate(dbModel.User{}, dbModel.Authority{}, dbModel.Menu{})
db.AutoMigrate(dbModel.User{}, dbModel.Authority{}, dbModel.Menu{}, dbModel.Api{})
}

43
QMPlusServer/model/dbModel/api.go

@ -0,0 +1,43 @@
package dbModel
import (
"github.com/jinzhu/gorm"
"main/controller/servers"
"main/init/qmsql"
"main/model/modelInterface"
)
type Api struct {
gorm.Model `json:"-"`
AuthorityId uint `json:"-"`
Path string `json:"path"`
Description string `json:"description"`
}
func (a *Api) CreateApi() (err error) {
err = qmsql.DEFAULTDB.Create(a).Error
return err
}
func (a *Api) DeleteApi() (err error) {
err = qmsql.DEFAULTDB.Where("id = ?", a.AuthorityId).Delete(a).Error
return err
}
func (a *Api) EditApi() (err error) {
err = qmsql.DEFAULTDB.Update(a).Error
return err
}
// 分页获取数据 需要分页实现这个接口即可
func (a *Api) GetInfoList(info modelInterface.PageInfo) (err error, list interface{}, total int) {
// 封装分页方法 调用即可 传入 当前的结构体和分页信息
err, db, total := servers.PagingServer(a, info)
if err != nil {
return
} else {
var apiList []Api
err = db.Find(&apiList).Error
return err, apiList, total
}
}

16
QMPlusServer/model/dbModel/authority.go

@ -2,17 +2,29 @@ package dbModel
import (
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
"main/init/qmsql"
)
type Authority struct {
gorm.Model `json:"-"`
AuthorityId uint `json:"authorityId"`
AuthorityId uint `json:"authorityId" gorm:"not null;unique"`
AuthorityName string `json:"authorityName"`
Menus []Menu `json:"_"`
}
// 创建角色
func (a *Authority) CreateAuthority() (err error, authority *Authority) {
err = qmsql.DEFAULTDB.Create(a).Error
return err, a
}
// 删除角色
func (a *Authority) DeleteAuthority() (err error) {
err = qmsql.DEFAULTDB.Where("authority_id = ?", a.AuthorityId).Find(&User{}).Error
if err != nil {
err = qmsql.DEFAULTDB.Where("authority_id = ?", a.AuthorityId).Delete(a).Error
} else {
err = errors.New("此角色有用户正在使用禁止删除")
}
return err
}

1
QMPlusServer/model/dbModel/menu.go

@ -23,6 +23,7 @@ type Meta struct {
Icon string `json:"icon"`
}
//获取动态路由树
func (m *Menu) GetMenuTree(authorityId float64) (err error, menus []Menu) {
err = qmsql.DEFAULTDB.Where("authority_id = ? AND parent_id = ?", authorityId, 0).Find(&menus).Error
for i := 0; i < len(menus); i++ {

15
QMPlusServer/router/api.go

@ -0,0 +1,15 @@
package router
import (
"github.com/gin-gonic/gin"
"main/controller/api"
"main/middleware"
)
func InitApiRouter(Router *gin.Engine) {
UserRouter := Router.Group("api").Use(middleware.JWTAuth())
{
UserRouter.POST("createApi", api.CreateApi)
UserRouter.POST("deleteApi", api.DeleteApi)
}
}

15
QMPlusServer/router/authority.go

@ -0,0 +1,15 @@
package router
import (
"github.com/gin-gonic/gin"
"main/controller/api"
"main/middleware"
)
func InitAuthorityRouter(Router *gin.Engine) {
AuthorityRouter := Router.Group("authority").Use(middleware.JWTAuth())
{
AuthorityRouter.POST("createAuthority", api.CreateAuthority)
AuthorityRouter.POST("deleteAuthority", api.DeleteAuthority)
}
}
Loading…
Cancel
Save