gin-vue-admin 2.0代码重构

This commit is contained in:
QM303176530
2020-04-04 21:39:07 +08:00
parent 5ed41ddd8c
commit 1b9adea284
202 changed files with 1240 additions and 1299 deletions
+118
View File
@@ -0,0 +1,118 @@
package v1
import (
"fmt"
"gin-vue-admin/global/response"
"gin-vue-admin/model"
"gin-vue-admin/utils"
"github.com/gin-gonic/gin"
"io/ioutil"
"strconv"
)
// @Tags ExaFileUploadAndDownload
// @Summary 断点续传到服务器
// @Security ApiKeyAuth
// @accept multipart/form-data
// @Produce application/json
// @Param file formData file true "断点续传示例"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"上传成功"}"
// @Router /fileUploadAndDownload/breakpointContinue [post]
func BreakpointContinue(c *gin.Context) {
fileMd5 := c.Request.FormValue("fileMd5")
fileName := c.Request.FormValue("fileName")
chunkMd5 := c.Request.FormValue("chunkMd5")
chunkNumber, _ := strconv.Atoi(c.Request.FormValue("chunkNumber"))
chunkTotal, _ := strconv.Atoi(c.Request.FormValue("chunkTotal"))
_, FileHeader, err := c.Request.FormFile("file")
if err != nil {
response.Result(response.SUCCESS, nil, fmt.Sprintf("%v", err), c)
} else {
f, err := FileHeader.Open()
if err != nil {
response.Result(response.ERROR, nil, fmt.Sprintf("%v", err), c)
} else {
cen, _ := ioutil.ReadAll(f)
defer f.Close()
if flag := utils.CheckMd5(cen, chunkMd5); flag {
err, file := new(model.ExaFile).FindOrCreateFile(fileMd5, fileName, chunkTotal)
if err != nil {
response.Result(response.ERROR, nil, fmt.Sprintf("%v", err), c)
} else {
err, pathc := utils.BreakPointContinue(cen, fileName, chunkNumber, chunkTotal, fileMd5)
if err != nil {
response.Result(response.ERROR, nil, fmt.Sprintf("%v", err), c)
} else {
err = file.CreateFileChunk(pathc, chunkNumber)
if err != nil {
response.Result(response.ERROR, nil, fmt.Sprintf("%v", err), c)
} else {
response.Result(response.SUCCESS, nil, "切片创建成功", c)
}
}
}
} else {
}
}
}
}
// @Tags ExaFileUploadAndDownload
// @Summary 查找文件
// @Security ApiKeyAuth
// @accept multipart/form-data
// @Produce application/json
// @Param file formData file true "查找文件"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查找成功"}"
// @Router /fileUploadAndDownload/findFile [post]
func FindFile(c *gin.Context) {
fileMd5 := c.Query("fileMd5")
fileName := c.Query("fileName")
chunkTotal, _ := strconv.Atoi(c.Query("chunkTotal"))
err, file := new(model.ExaFile).FindOrCreateFile(fileMd5, fileName, chunkTotal)
if err != nil {
response.Result(response.ERROR, nil, fmt.Sprintf("查找失败:%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{"file": file}, "查找成功", c)
}
}
// @Tags ExaFileUploadAndDownload
// @Summary 查找文件
// @Security ApiKeyAuth
// @accept multipart/form-data
// @Produce application/json
// @Param file formData file true "查找文件"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查找成功"}"
// @Router /fileUploadAndDownload/findFile [post]
func BreakpointContinueFinish(c *gin.Context) {
fileMd5 := c.Query("fileMd5")
fileName := c.Query("fileName")
err, filePath := utils.MakeFile(fileName, fileMd5)
if err != nil {
response.Result(response.ERROR, gin.H{"filePath": filePath}, fmt.Sprintf("文件创建失败:%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{"filePath": filePath}, "文件创建成功", c)
}
}
// @Tags ExaFileUploadAndDownload
// @Summary 删除切片
// @Security ApiKeyAuth
// @accept multipart/form-data
// @Produce application/json
// @Param file formData file true "查找文件"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查找成功"}"
// @Router /fileUploadAndDownload/removeChunk [post]
func RemoveChunk(c *gin.Context) {
fileMd5 := c.Query("fileMd5")
fileName := c.Query("fileName")
filePath := c.Query("filePath")
err := utils.RemoveChunk(fileMd5)
err = new(model.ExaFile).DeleteFileChunk(fileMd5, fileName, filePath)
if err != nil {
response.Result(response.ERROR, gin.H{"filePath": filePath}, fmt.Sprintf("缓存切片删除失败:%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{"filePath": filePath}, "缓存切片删除成功", c)
}
}
+119
View File
@@ -0,0 +1,119 @@
package v1
import (
"fmt"
"gin-vue-admin/global/response"
"gin-vue-admin/middleware"
"gin-vue-admin/model"
"github.com/gin-gonic/gin"
)
// @Tags SysApi
// @Summary 创建客户
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body dbModel.ExaCustomer true "创建客户"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /customer/createExaCustomer [post]
func CreateExaCustomer(c *gin.Context) {
var cu model.ExaCustomer
_ = c.ShouldBindJSON(&cu)
claims, _ := c.Get("claims")
waitUse := claims.(*middleware.CustomClaims)
cu.SysUserID = waitUse.ID
cu.SysUserAuthorityID = waitUse.AuthorityId
err := cu.CreateExaCustomer()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("创建失败:%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{}, "创建成功", c)
}
}
// @Tags SysApi
// @Summary 删除客户
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body dbModel.ExaCustomer true "删除客户"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /customer/deleteExaCustomer [post]
func DeleteExaCustomer(c *gin.Context) {
var cu model.ExaCustomer
_ = c.ShouldBindJSON(&cu)
err := cu.DeleteExaCustomer()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("删除失败:%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{}, "删除成功", c)
}
}
// @Tags SysApi
// @Summary 更新客户信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body dbModel.ExaCustomer true "创建客户"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /customer/updateExaCustomer [post]
func UpdateExaCustomer(c *gin.Context) {
var cu model.ExaCustomer
_ = c.ShouldBindJSON(&cu)
err := cu.UpdateExaCustomer()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("更新失败:%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{}, "更新成功", c)
}
}
// @Tags SysApi
// @Summary 获取单一客户信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body dbModel.ExaCustomer true "获取单一客户信息"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /customer/getExaCustomer [post]
func GetExaCustomer(c *gin.Context) {
var cu model.ExaCustomer
_ = c.ShouldBindJSON(&cu)
err, customer := cu.GetExaCustomer()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("获取失败:%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{
"customer": customer,
}, "创建", c)
}
}
// @Tags SysApi
// @Summary 获取权限客户列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.PageInfo true "获取权限客户列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /customer/getExaCustomerList [post]
func GetExaCustomerList(c *gin.Context) {
claims, _ := c.Get("claims")
waitUse := claims.(*middleware.CustomClaims)
var cu model.ExaCustomer
cu.SysUserAuthorityID = waitUse.AuthorityId
var pageInfo model.PageInfo
_ = c.ShouldBindJSON(&pageInfo)
err, customerList, total := cu.GetInfoList(pageInfo)
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("创建失败:%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{
"customer": customerList,
"total": total,
"page": pageInfo.Page,
"pageSize": pageInfo.PageSize,
}, "创建成功", c)
}
}
+102
View File
@@ -0,0 +1,102 @@
package v1
import (
"fmt"
"gin-vue-admin/global/response"
"gin-vue-admin/model"
"gin-vue-admin/utils"
"github.com/gin-gonic/gin"
"strings"
)
// @Tags ExaFileUploadAndDownload
// @Summary 上传文件示例
// @Security ApiKeyAuth
// @accept multipart/form-data
// @Produce application/json
// @Param file formData file true "上传文件示例"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"上传成功"}"
// @Router /fileUploadAndDownload/upload [post]
func UploadFile(c *gin.Context) {
noSave := c.DefaultQuery("noSave", "0")
_, header, err := c.Request.FormFile("file")
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("上传文件失败,%v", err), c)
} else {
//文件上传后拿到文件路径
err, filePath, key := utils.Upload(header, USER_HEADER_BUCKET, USER_HEADER_IMG_PATH)
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("接收返回值失败,%v", err), c)
} else {
//修改数据库后得到修改后的user并且返回供前端使用
var file model.ExaFileUploadAndDownload
file.Url = filePath
file.Name = header.Filename
s := strings.Split(file.Name, ".")
file.Tag = s[len(s)-1]
file.Key = key
if noSave == "0" {
err = file.Upload()
}
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("修改数据库链接失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{"file": file}, "上传成功", c)
}
}
}
}
// @Tags ExaFileUploadAndDownload
// @Summary 删除文件
// @Security ApiKeyAuth
// @Produce application/json
// @Param data body dbModel.ExaFileUploadAndDownload true "传入文件里面id即可"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
// @Router /fileUploadAndDownload/deleteFile [post]
func DeleteFile(c *gin.Context) {
var file model.ExaFileUploadAndDownload
_ = c.ShouldBindJSON(&file)
err, f := file.FindFile()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("删除失败,%v", err), c)
} else {
err = utils.DeleteFile(USER_HEADER_BUCKET, f.Key)
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("删除失败,%v", err), c)
} else {
err = f.DeleteFile()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("删除失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{}, "删除成功", c)
}
}
}
}
// @Tags ExaFileUploadAndDownload
// @Summary 分页文件列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.PageInfo true "分页获取文件户列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /fileUploadAndDownload/getFileList [post]
func GetFileList(c *gin.Context) {
var pageInfo model.PageInfo
_ = c.ShouldBindJSON(&pageInfo)
err, list, total := new(model.ExaFileUploadAndDownload).GetInfoList(pageInfo)
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("获取数据失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{
"list": list,
"total": total,
"page": pageInfo.Page,
"pageSize": pageInfo.PageSize,
}, "获取数据成功", c)
}
}
+150
View File
@@ -0,0 +1,150 @@
package v1
import (
"fmt"
"gin-vue-admin/global/response"
"gin-vue-admin/model"
"github.com/gin-gonic/gin"
)
type CreateApiParams struct {
Path string `json:"path"`
Description string `json:"description"`
}
type DeleteApiParams struct {
ID uint `json:"id"`
}
// @Tags SysApi
// @Summary 创建基础api
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body api.CreateApiParams true "创建api"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/createApi [post]
func CreateApi(c *gin.Context) {
var api model.SysApi
_ = c.ShouldBindJSON(&api)
err := api.CreateApi()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("创建失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{}, "创建成功", c)
}
}
// @Tags SysApi
// @Summary 删除指定api
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body sysModel.SysApi true "删除api"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/deleteApi [post]
func DeleteApi(c *gin.Context) {
var a model.SysApi
_ = c.ShouldBindJSON(&a)
err := a.DeleteApi()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("删除失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{}, "删除成功", c)
}
}
type AuthAndPathIn struct {
AuthorityId string `json:"authorityId"`
ApiIds []uint `json:"apiIds"`
}
//条件搜索后端看此api
// @Tags SysApi
// @Summary 分页获取API列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.PageInfo true "分页获取API列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/getApiList [post]
func GetApiList(c *gin.Context) {
// 此结构体仅本方法使用
type searchParams struct {
model.SysApi
model.PageInfo
}
var sp searchParams
_ = c.ShouldBindJSON(&sp)
err, list, total := sp.SysApi.GetInfoList(sp.PageInfo)
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("获取数据失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{
"list": list,
"total": total,
"page": sp.PageInfo.Page,
"pageSize": sp.PageInfo.PageSize,
}, "删除成功", c)
}
}
// @Tags SysApi
// @Summary 根据id获取api
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.PageInfo true "分页获取用户列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/getApiById [post]
func GetApiById(c *gin.Context) {
var idInfo GetById
_ = c.ShouldBindJSON(&idInfo)
err, api := new(model.SysApi).GetApiById(idInfo.Id)
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("获取数据失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{
"api": api,
}, "获取数据成功", c)
}
}
// @Tags SysApi
// @Summary 创建基础api
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body api.CreateApiParams true "创建api"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/updateApi [post]
func UpdateApi(c *gin.Context) {
var api model.SysApi
_ = c.ShouldBindJSON(&api)
err := api.UpdateApi()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("修改数据失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{}, "修改数据成功", c)
}
}
// @Tags SysApi
// @Summary 获取所有的Api 不分页
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/getAllApis [post]
func GetAllApis(c *gin.Context) {
err, apis := new(model.SysApi).GetAllApis()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("获取数据失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{
"apis": apis,
}, "获取数据成功", c)
}
}
+92
View File
@@ -0,0 +1,92 @@
package v1
import (
"fmt"
"gin-vue-admin/global/response"
"gin-vue-admin/model"
"github.com/gin-gonic/gin"
)
// @Tags authority
// @Summary 创建角色
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body sysModel.SysAuthority true "创建角色"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /authority/createAuthority [post]
func CreateAuthority(c *gin.Context) {
var auth model.SysAuthority
_ = c.ShouldBindJSON(&auth)
err, authBack := auth.CreateAuthority()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("创建失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{
"authority": authBack,
}, fmt.Sprintf("创建成功,%v", err), c)
}
}
// @Tags authority
// @Summary 删除角色
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body sysModel.SysAuthority true "删除角色"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /authority/deleteAuthority [post]
func DeleteAuthority(c *gin.Context) {
var a model.SysAuthority
_ = c.ShouldBindJSON(&a)
//删除角色之前需要判断是否有用户正在使用此角色
err := a.DeleteAuthority()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("删除失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{}, "删除失败", c)
}
}
// @Tags authority
// @Summary 分页获取角色列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.PageInfo true "分页获取用户列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /authority/getAuthorityList [post]
func GetAuthorityList(c *gin.Context) {
var pageInfo model.PageInfo
_ = c.ShouldBindJSON(&pageInfo)
err, list, total := new(model.SysAuthority).GetInfoList(pageInfo)
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("获取数据失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{
"list": list,
"total": total,
"page": pageInfo.Page,
"pageSize": pageInfo.PageSize,
}, "获取数据成功", c)
}
}
// @Tags authority
// @Summary 设置角色资源权限
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body sysModel.SysAuthority true "设置角色资源权限"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"设置成功"}"
// @Router /authority/setDataAuthority [post]
func SetDataAuthority(c *gin.Context) {
var auth model.SysAuthority
_ = c.ShouldBindJSON(&auth)
err := auth.SetDataAuthority()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("设置关联失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{}, "获取数据成功", c)
}
}
+33
View File
@@ -0,0 +1,33 @@
package v1
import (
"fmt"
"gin-vue-admin/global/response"
"gin-vue-admin/model"
"github.com/gin-gonic/gin"
"os"
)
// @Tags SysApi
// @Summary 自动代码模板
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autoCodeModel.AutoCodeStruct true "创建自动代码"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /autoCode/createTemp [post]
func CreateTemp(c *gin.Context) {
var a model.AutoCodeStruct
_ = c.ShouldBindJSON(&a)
err := a.CreateTemp()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("创建失败,%v", err), c)
os.Remove("./ginvueadmin.zip")
} else {
c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "ginvueadmin.zip")) //fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
c.Writer.Header().Add("Content-Type", "application/json")
c.Writer.Header().Add("success", "true")
c.File("./ginvueadmin.zip")
os.Remove("./ginvueadmin.zip")
}
}
+35
View File
@@ -0,0 +1,35 @@
package api
import (
"gin-vue-admin/config"
"gin-vue-admin/controller/servers"
"github.com/dchest/captcha"
"github.com/gin-gonic/gin"
)
// 获取图片验证码id
// @Tags base
// @Summary 生成验证码
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /base/captcha [post]
func Captcha(c *gin.Context) {
captchaId := captcha.NewLen(config.GinVueAdminconfig.Captcha.KeyLong)
servers.ReportFormat(c, true, "验证码获取成功", gin.H{
"captchaId": captchaId,
"picPath": "/base/captcha/" + captchaId + ".png",
})
}
// @Tags base
// @Summary 生成验证码图片路径
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /base/captcha/:captchaId [get]
func CaptchaImg(c *gin.Context) {
servers.GinCapthcaServeHTTP(c.Writer, c.Request)
}
+57
View File
@@ -0,0 +1,57 @@
package v1
import (
"fmt"
"gin-vue-admin/global/response"
"gin-vue-admin/model"
"github.com/gin-gonic/gin"
)
// @Tags casbin
// @Summary 更改角色api权限
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body sysModel.CasbinInReceive true "更改角色api权限"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /casbin/casbinPUpdate [post]
func CasbinPUpdate(c *gin.Context) {
var cmr model.CasbinInReceive
_ = c.ShouldBindJSON(&cmr)
err := new(model.CasbinModel).CasbinPUpdate(cmr.AuthorityId, cmr.CasbinInfos)
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("添加规则失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{}, "添加规则成功", c)
}
}
// @Tags casbin
// @Summary 获取权限列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body sysModel.CasbinInReceive true "获取权限列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /casbin/getPolicyPathByAuthorityId [post]
func GetPolicyPathByAuthorityId(c *gin.Context) {
var cmr model.CasbinInReceive
_ = c.ShouldBindJSON(&cmr)
paths := new(model.CasbinModel).GetPolicyPathByAuthorityId(cmr.AuthorityId)
response.Result(response.SUCCESS, gin.H{"paths": paths}, "获取规则成功", c)
}
// @Tags casbin
// @Summary casb RBAC RESTFUL测试路由
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body sysModel.CasbinInReceive true "获取权限列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /casbin/CasbinTest [get]
func CasbinTest(c *gin.Context) {
// 测试restful以及占位符代码 随意书写
pathParam := c.Param("pathParam")
query := c.Query("query")
response.Result(response.SUCCESS, gin.H{"pathParam": pathParam, "query": query}, "获取规则成功", c)
}
+28
View File
@@ -0,0 +1,28 @@
package v1
import (
"fmt"
"gin-vue-admin/global/response"
"gin-vue-admin/model"
"github.com/gin-gonic/gin"
)
// @Tags jwt
// @Summary jwt加入黑名单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"拉黑成功"}"
// @Router /jwt/jsonInBlacklist [post]
func JsonInBlacklist(c *gin.Context) {
token := c.Request.Header.Get("x-token")
ModelJwt := model.JwtBlacklist{
Jwt: token,
}
err := ModelJwt.JsonInBlacklist()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("jwt作废失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{}, "jwt作废成功", c)
}
}
+198
View File
@@ -0,0 +1,198 @@
package v1
import (
"fmt"
"gin-vue-admin/middleware"
"gin-vue-admin/model"
"github.com/gin-gonic/gin"
)
// @Tags authorityAndMenu
// @Summary 获取用户动态路由
// @Security ApiKeyAuth
// @Produce application/json
// @Param data body api.RegisterAndLoginStuct true "可以什么都不填"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
// @Router /menu/getMenu [post]
func GetMenu(c *gin.Context) {
claims, _ := c.Get("claims")
waitUse := claims.(*middleware.CustomClaims)
err, menus := new(model.SysMenu).GetMenuTree(waitUse.AuthorityId)
if err != nil {
servers.ReportFormat(c, false, fmt.Sprintf("获取失败:%v", err), gin.H{"menus": menus})
} else {
servers.ReportFormat(c, true, "获取成功", gin.H{"menus": menus})
}
}
// @Tags menu
// @Summary 分页获取基础menu列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.PageInfo true "分页获取基础menu列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /menu/getMenuList [post]
func GetMenuList(c *gin.Context) {
var pageInfo model.PageInfo
_ = c.ShouldBindJSON(&pageInfo)
err, menuList, total := new(model.SysBaseMenu).GetInfoList(pageInfo)
if err != nil {
servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
} else {
servers.ReportFormat(c, true, "获取数据成功", gin.H{
"list": menuList,
"total": total,
"page": pageInfo.Page,
"pageSize": pageInfo.PageSize,
})
}
}
// @Tags menu
// @Summary 新增菜单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body sysModel.SysBaseMenu true "新增菜单"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /menu/addBaseMenu [post]
func AddBaseMenu(c *gin.Context) {
var addMenu model.SysBaseMenu
_ = c.ShouldBindJSON(&addMenu)
err := addMenu.AddBaseMenu()
if err != nil {
servers.ReportFormat(c, false, fmt.Sprintf("添加失败,%v", err), gin.H{})
} else {
servers.ReportFormat(c, true, fmt.Sprintf("添加成功,%v", err), gin.H{})
}
}
// @Tags authorityAndMenu
// @Summary 获取用户动态路由
// @Security ApiKeyAuth
// @Produce application/json
// @Param data body api.RegisterAndLoginStuct true "可以什么都不填"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
// @Router /menu/getBaseMenuTree [post]
func GetBaseMenuTree(c *gin.Context) {
err, menus := new(model.SysBaseMenu).GetBaseMenuTree()
if err != nil {
servers.ReportFormat(c, false, fmt.Sprintf("获取失败:%v", err), gin.H{"menus": menus})
} else {
servers.ReportFormat(c, true, "获取成功", gin.H{"menus": menus})
}
}
type AddMenuAuthorityInfo struct {
Menus []model.SysBaseMenu
AuthorityId string
}
// @Tags authorityAndMenu
// @Summary 增加menu和角色关联关系
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body api.AddMenuAuthorityInfo true "增加menu和角色关联关系"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /menu/addMenuAuthority [post]
func AddMenuAuthority(c *gin.Context) {
var addMenuAuthorityInfo AddMenuAuthorityInfo
_ = c.ShouldBindJSON(&addMenuAuthorityInfo)
err := new(model.SysMenu).AddMenuAuthority(addMenuAuthorityInfo.Menus, addMenuAuthorityInfo.AuthorityId)
if err != nil {
servers.ReportFormat(c, false, fmt.Sprintf("添加失败,%v", err), gin.H{})
} else {
servers.ReportFormat(c, true, fmt.Sprintf("添加成功,%v", err), gin.H{})
}
}
type AuthorityIdInfo struct {
AuthorityId string
}
// @Tags authorityAndMenu
// @Summary 获取指定角色menu
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body api.AuthorityIdInfo true "增加menu和角色关联关系"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /menu/addMenuAuthority [post]
func GetMenuAuthority(c *gin.Context) {
var authorityIdInfo AuthorityIdInfo
_ = c.ShouldBindJSON(&authorityIdInfo)
err, menus := new(model.SysMenu).GetMenuAuthority(authorityIdInfo.AuthorityId)
if err != nil {
servers.ReportFormat(c, false, fmt.Sprintf("获取失败:%v", err), gin.H{"menus": menus})
} else {
servers.ReportFormat(c, true, "获取成功", gin.H{"menus": menus})
}
}
type IdInfo struct {
Id float64
}
// @Tags menu
// @Summary 删除菜单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body api.IdInfo true "删除菜单"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /menu/deleteBaseMenu [post]
func DeleteBaseMenu(c *gin.Context) {
var idInfo IdInfo
_ = c.ShouldBindJSON(&idInfo)
err := new(model.SysBaseMenu).DeleteBaseMenu(idInfo.Id)
if err != nil {
servers.ReportFormat(c, false, fmt.Sprintf("删除失败:%v", err), gin.H{})
} else {
servers.ReportFormat(c, true, "删除成功", gin.H{})
}
}
// @Tags menu
// @Summary 更新菜单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body sysModel.SysBaseMenu true "更新菜单"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /menu/updateBaseMenu [post]
func UpdateBaseMenu(c *gin.Context) {
var menu model.SysBaseMenu
_ = c.ShouldBindJSON(&menu)
err := menu.UpdateBaseMenu()
if err != nil {
servers.ReportFormat(c, false, fmt.Sprintf("修改失败:%v", err), gin.H{})
} else {
servers.ReportFormat(c, true, "修改成功", gin.H{})
}
}
type GetById struct {
Id float64 `json:"id"`
}
// @Tags menu
// @Summary 根据id获取菜单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body api.GetById true "根据id获取菜单"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /menu/getBaseMenuById [post]
func GetBaseMenuById(c *gin.Context) {
var idInfo GetById
_ = c.ShouldBindJSON(&idInfo)
err, menu := new(model.SysBaseMenu).GetBaseMenuById(idInfo.Id)
if err != nil {
servers.ReportFormat(c, false, fmt.Sprintf("查询失败:%v", err), gin.H{})
} else {
servers.ReportFormat(c, true, "查询成功", gin.H{"menu": menu})
}
}
+60
View File
@@ -0,0 +1,60 @@
package v1
import (
"fmt"
"gin-vue-admin/global/response"
"gin-vue-admin/model"
"github.com/gin-gonic/gin"
)
// @Tags system
// @Summary 获取配置文件内容
// @Security ApiKeyAuth
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
// @Router /system/getSystemConfig [post]
func GetSystemConfig(c *gin.Context) {
err, config := new(model.System).GetSystemConfig()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("获取失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{"config": config}, "获取成功", c)
}
}
// @Tags system
// @Summary 设置配置文件内容
// @Security ApiKeyAuth
// @Produce application/json
// @Param data body sysModel.System true "设置配置文件内容"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
// @Router /system/setSystemConfig [post]
func SetSystemConfig(c *gin.Context) {
var sys model.System
_ = c.ShouldBindJSON(&sys)
err := sys.SetSystemConfig()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("设置失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{}, "设置成功", c)
}
}
//本方法开发中 开发者windows系统 缺少linux系统所需的包 因此搁置
// @Tags system
// @Summary 设置配置文件内容
// @Security ApiKeyAuth
// @Produce application/json
// @Param data body sysModel.System true "设置配置文件内容"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
// @Router /system/ReloadSystem [post]
func ReloadSystem(c *gin.Context) {
var sys model.System
_ = c.ShouldBindJSON(&sys)
err := sys.SetSystemConfig()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("设置失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{}, "设置成功", c)
}
}
+247
View File
@@ -0,0 +1,247 @@
package v1
import (
"fmt"
"gin-vue-admin/global"
"gin-vue-admin/global/response"
"gin-vue-admin/middleware"
"gin-vue-admin/model"
"gin-vue-admin/utils"
"github.com/dchest/captcha"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis"
uuid "github.com/satori/go.uuid"
"mime/multipart"
"time"
)
var (
USER_HEADER_IMG_PATH string = "http://qmplusimg.henrongyi.top"
USER_HEADER_BUCKET string = "qm-plus-img"
)
type RegisterAndLoginStuct struct {
Username string `json:"username"`
Password string `json:"password"`
Captcha string `json:"captcha"`
CaptchaId string `json:"captchaId"`
}
type RegestStuct struct {
Username string `json:"userName"`
Password string `json:"passWord"`
NickName string `json:"nickName" gorm:"default:'QMPlusUser'"`
HeaderImg string `json:"headerImg" gorm:"default:'http://www.henrongyi.top/avatar/lufu.jpg'"`
AuthorityId string `json:"authorityId" gorm:"default:888"`
}
// @Tags Base
// @Summary 用户注册账号
// @Produce application/json
// @Param data body sysModel.SysUser true "用户注册接口"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"注册成功"}"
// @Router /base/register [post]
func Register(c *gin.Context) {
var R RegestStuct
_ = c.ShouldBindJSON(&R)
user := &model.SysUser{Username: R.Username, NickName: R.NickName, Password: R.Password, HeaderImg: R.HeaderImg, AuthorityId: R.AuthorityId}
err, user := user.Register()
if err != nil {
response.Result(response.ERROR, gin.H{
"user": user,
}, fmt.Sprintf("%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{
"user": user,
}, "注册成功", c)
}
}
// @Tags Base
// @Summary 用户登录
// @Produce application/json
// @Param data body api.RegisterAndLoginStuct true "用户登录接口"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"登陆成功"}"
// @Router /base/login [post]
func Login(c *gin.Context) {
var L RegisterAndLoginStuct
_ = c.ShouldBindJSON(&L)
if captcha.VerifyString(L.CaptchaId, L.Captcha) {
U := &model.SysUser{Username: L.Username, Password: L.Password}
if err, user := U.Login(); err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("用户名密码错误或%v", err), c)
} else {
tokenNext(c, *user)
}
} else {
response.Result(response.ERROR, gin.H{}, "验证码错误", c)
}
}
//登录以后签发jwt
func tokenNext(c *gin.Context, user model.SysUser) {
j := &middleware.JWT{
[]byte(global.GVA_CONFIG.JWT.SigningKey), // 唯一签名
}
clams := middleware.CustomClaims{
UUID: user.UUID,
ID: user.ID,
NickName: user.NickName,
AuthorityId: user.AuthorityId,
StandardClaims: jwt.StandardClaims{
NotBefore: int64(time.Now().Unix() - 1000), // 签名生效时间
ExpiresAt: int64(time.Now().Unix() + 60*60*24*7), // 过期时间 一周
Issuer: "qmPlus", //签名的发行者
},
}
token, err := j.CreateToken(clams)
if err != nil {
response.Result(response.ERROR, gin.H{}, "获取token失败", c)
} else {
if global.GVA_CONFIG.System.UseMultipoint {
var loginJwt model.JwtBlacklist
loginJwt.Jwt = token
err, jwtStr := loginJwt.GetRedisJWT(user.Username)
if err == redis.Nil {
err2 := loginJwt.SetRedisJWT(user.Username)
if err2 != nil {
response.Result(response.ERROR, gin.H{}, "设置登录状态失败", c)
} else {
response.Result(response.SUCCESS, gin.H{"user": user, "token": token, "expiresAt": clams.StandardClaims.ExpiresAt * 1000}, "登录成功", c)
}
} else if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("%v", err), c)
} else {
var blackjWT model.JwtBlacklist
blackjWT.Jwt = jwtStr
err3 := blackjWT.JsonInBlacklist()
if err3 != nil {
response.Result(response.ERROR, gin.H{}, "jwt作废失败", c)
} else {
err2 := loginJwt.SetRedisJWT(user.Username)
if err2 != nil {
response.Result(response.ERROR, gin.H{}, "设置登录状态失败", c)
} else {
response.Result(response.SUCCESS, gin.H{"user": user, "token": token, "expiresAt": clams.StandardClaims.ExpiresAt * 1000}, "登录成功", c)
}
}
}
} else {
response.Result(response.SUCCESS, gin.H{"user": user, "token": token, "expiresAt": clams.StandardClaims.ExpiresAt * 1000}, "登录成功", c)
}
}
}
type ChangePasswordStutrc struct {
Username string `json:"username"`
Password string `json:"password"`
NewPassword string `json:"newPassword"`
}
// @Tags SysUser
// @Summary 用户修改密码
// @Security ApiKeyAuth
// @Produce application/json
// @Param data body api.ChangePasswordStutrc true "用户修改密码"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
// @Router /user/changePassword [put]
func ChangePassword(c *gin.Context) {
var params ChangePasswordStutrc
_ = c.ShouldBindJSON(&params)
U := &model.SysUser{Username: params.Username, Password: params.Password}
if err, _ := U.ChangePassword(params.NewPassword); err != nil {
response.Result(response.ERROR, gin.H{}, "修改失败,请检查用户名密码", c)
} else {
response.Result(response.SUCCESS, gin.H{}, "修改成功", c)
}
}
type UserHeaderImg struct {
HeaderImg multipart.File `json:"headerImg"`
}
// @Tags SysUser
// @Summary 用户上传头像
// @Security ApiKeyAuth
// @accept multipart/form-data
// @Produce application/json
// @Param headerImg formData file true "用户上传头像"
// @Param username formData string true "用户上传头像"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"上传成功"}"
// @Router /user/uploadHeaderImg [post]
func UploadHeaderImg(c *gin.Context) {
claims, _ := c.Get("claims")
//获取头像文件
// 这里我们通过断言获取 claims内的所有内容
waitUse := claims.(*middleware.CustomClaims)
uuid := waitUse.UUID
_, header, err := c.Request.FormFile("headerImg")
//便于找到用户 以后从jwt中取
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("上传文件失败,%v", err), c)
} else {
//文件上传后拿到文件路径
err, filePath, _ := utils.Upload(header, USER_HEADER_BUCKET, USER_HEADER_IMG_PATH)
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("接收返回值失败,%v", err), c)
} else {
//修改数据库后得到修改后的user并且返回供前端使用
err, user := new(model.SysUser).UploadHeaderImg(uuid, filePath)
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("修改数据库链接失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{"user": user}, "上传成功", c)
}
}
}
}
// @Tags SysUser
// @Summary 分页获取用户列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.PageInfo true "分页获取用户列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /user/getUserList [post]
func GetUserList(c *gin.Context) {
var pageInfo model.PageInfo
_ = c.ShouldBindJSON(&pageInfo)
err, list, total := new(model.SysUser).GetInfoList(pageInfo)
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("获取数据失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{
"userList": list,
"total": total,
"page": pageInfo.Page,
"pageSize": pageInfo.PageSize,
}, "获取数据成功", c)
}
}
type SetUserAuth struct {
UUID uuid.UUID `json:"uuid"`
AuthorityId string `json:"authorityId"`
}
// @Tags SysUser
// @Summary 设置用户权限
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body api.SetUserAuth true "设置用户权限"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
// @Router /user/setUserAuthority [post]
func SetUserAuthority(c *gin.Context) {
var sua SetUserAuth
_ = c.ShouldBindJSON(&sua)
err := new(model.SysUser).SetUserAuthority(sua.UUID, sua.AuthorityId)
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("修改失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{}, "修改成功", c)
}
}
+25
View File
@@ -0,0 +1,25 @@
package v1
import (
"fmt"
"gin-vue-admin/global/response"
"gin-vue-admin/model"
"github.com/gin-gonic/gin"
)
// @Tags workflow
// @Summary 注册工作流
// @Produce application/json
// @Param data body sysModel.SysWorkflow true "注册工作流接口"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"注册成功"}"
// @Router /workflow/createWorkFlow [post]
func CreateWorkFlow(c *gin.Context) {
var wk model.SysWorkflow
_ = c.ShouldBindJSON(&wk)
err := wk.Create()
if err != nil {
response.Result(response.ERROR, gin.H{}, fmt.Sprintf("获取失败:%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{}, "获取成功", c)
}
}