feat: RBAC 基本完成
This commit is contained in:
+11
-5
@@ -1,15 +1,16 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/mojocn/base64Captcha"
|
|
||||||
"go.uber.org/zap"
|
|
||||||
"sundynix-go/global"
|
"sundynix-go/global"
|
||||||
"sundynix-go/model/commom/response"
|
"sundynix-go/model/commom/response"
|
||||||
"sundynix-go/model/system"
|
"sundynix-go/model/system"
|
||||||
systemReq "sundynix-go/model/system/request"
|
systemReq "sundynix-go/model/system/request"
|
||||||
systemRes "sundynix-go/model/system/response"
|
systemRes "sundynix-go/model/system/response"
|
||||||
"sundynix-go/utils"
|
"sundynix-go/utils"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/mojocn/base64Captcha"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
var store = base64Captcha.DefaultMemStore
|
var store = base64Captcha.DefaultMemStore
|
||||||
@@ -26,7 +27,7 @@ func (a *AuthApi) Login(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
if l.CaptchaId != "" && l.Captcha != "" && store.Verify(l.CaptchaId, l.Captcha, true) {
|
if l.CaptchaId != "" && l.Captcha != "" && store.Verify(l.CaptchaId, l.Captcha, true) {
|
||||||
u := &system.User{Account: l.Account, Password: l.Password}
|
u := &system.User{Account: l.Account, Password: l.Password}
|
||||||
user, err := UserService.Login(u)
|
user, err := userService.Login(u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.Logger.Error("登录失败! 用户名不存在或者密码错误!", zap.Error(err))
|
global.Logger.Error("登录失败! 用户名不存在或者密码错误!", zap.Error(err))
|
||||||
response.FailWithMsg("用户名不存在或者密码错误", c)
|
response.FailWithMsg("用户名不存在或者密码错误", c)
|
||||||
@@ -38,6 +39,11 @@ func (a *AuthApi) Login(c *gin.Context) {
|
|||||||
response.FailWithMsg("验证码错误", c)
|
response.FailWithMsg("验证码错误", c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 登出
|
||||||
|
func (a *AuthApi) Logout(c *gin.Context) {
|
||||||
|
utils.ClearToken(c)
|
||||||
|
}
|
||||||
|
|
||||||
// Captcha api 生成验证码
|
// Captcha api 生成验证码
|
||||||
func (u *AuthApi) Captcha(c *gin.Context) {
|
func (u *AuthApi) Captcha(c *gin.Context) {
|
||||||
var driver = base64Captcha.DriverString{
|
var driver = base64Captcha.DriverString{
|
||||||
@@ -45,7 +51,7 @@ func (u *AuthApi) Captcha(c *gin.Context) {
|
|||||||
Width: 240,
|
Width: 240,
|
||||||
NoiseCount: 2,
|
NoiseCount: 2,
|
||||||
ShowLineOptions: 4,
|
ShowLineOptions: 4,
|
||||||
Length: 6,
|
Length: 4,
|
||||||
Source: "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM",
|
Source: "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,10 +7,12 @@ type ApiGroup struct {
|
|||||||
UserApi
|
UserApi
|
||||||
ClientApi
|
ClientApi
|
||||||
RoleApi
|
RoleApi
|
||||||
|
MenuApi
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
UserService = service.ServiceGroupApp.SystemServiceGroup.UserService
|
userService = service.ServiceGroupApp.SystemServiceGroup.UserService
|
||||||
ClientService = service.ServiceGroupApp.SystemServiceGroup.ClientService
|
clientService = service.ServiceGroupApp.SystemServiceGroup.ClientService
|
||||||
RoleService = service.ServiceGroupApp.SystemServiceGroup.RoleService
|
roleService = service.ServiceGroupApp.SystemServiceGroup.RoleService
|
||||||
|
menuService = service.ServiceGroupApp.SystemServiceGroup.MenuService
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ func (s *ClientApi) SaveClient(c *gin.Context) {
|
|||||||
response.FailWithMsg(err.Error(), c)
|
response.FailWithMsg(err.Error(), c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = ClientService.SaveClient(client)
|
err = clientService.SaveClient(client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.Logger.Error("保存客户端失败!", zap.Error(err))
|
global.Logger.Error("保存客户端失败!", zap.Error(err))
|
||||||
response.FailWithMsg(err.Error(), c)
|
response.FailWithMsg(err.Error(), c)
|
||||||
@@ -36,7 +36,7 @@ func (s *ClientApi) UpdateClient(c *gin.Context) {
|
|||||||
response.FailWithMsg(err.Error(), c)
|
response.FailWithMsg(err.Error(), c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = ClientService.UpdateClient(client)
|
err = clientService.UpdateClient(client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.Logger.Error("更新客户端失败!", zap.Error(err))
|
global.Logger.Error("更新客户端失败!", zap.Error(err))
|
||||||
response.FailWithMsg(err.Error(), c)
|
response.FailWithMsg(err.Error(), c)
|
||||||
@@ -52,7 +52,7 @@ func (s *ClientApi) GetClientList(c *gin.Context) {
|
|||||||
response.FailWithMsg(err.Error(), c)
|
response.FailWithMsg(err.Error(), c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
list, total, err := ClientService.GetClientList(pageInfo)
|
list, total, err := clientService.GetClientList(pageInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.Logger.Error("获取客户端列表失败!", zap.Error(err))
|
global.Logger.Error("获取客户端列表失败!", zap.Error(err))
|
||||||
response.FailWithMsg(err.Error(), c)
|
response.FailWithMsg(err.Error(), c)
|
||||||
@@ -73,7 +73,7 @@ func (s *ClientApi) Delete(c *gin.Context) {
|
|||||||
response.FailWithMsg(err.Error(), c)
|
response.FailWithMsg(err.Error(), c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = ClientService.DeleteClientByIds(ids)
|
err = clientService.DeleteClientByIds(ids)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.Logger.Error("删除客户端失败!", zap.Error(err))
|
global.Logger.Error("删除客户端失败!", zap.Error(err))
|
||||||
response.FailWithMsg(err.Error(), c)
|
response.FailWithMsg(err.Error(), c)
|
||||||
@@ -89,7 +89,7 @@ func (s *ClientApi) Detail(c *gin.Context) {
|
|||||||
response.FailWithMsg(err.Error(), c)
|
response.FailWithMsg(err.Error(), c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
client, err := ClientService.GetClientById(idInfo.ID)
|
client, err := clientService.GetClientById(idInfo.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.Logger.Error("获取客户端详情失败!", zap.Error(err))
|
global.Logger.Error("获取客户端详情失败!", zap.Error(err))
|
||||||
response.FailWithMsg(err.Error(), c)
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
|||||||
@@ -0,0 +1,100 @@
|
|||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sundynix-go/global"
|
||||||
|
"sundynix-go/model/commom/response"
|
||||||
|
"sundynix-go/model/system"
|
||||||
|
systemReq "sundynix-go/model/system/request"
|
||||||
|
"sundynix-go/utils"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MenuApi struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MenuApi) SaveMenu(c *gin.Context) {
|
||||||
|
var menu system.Menu
|
||||||
|
err := c.ShouldBindJSON(&menu)
|
||||||
|
if err != nil {
|
||||||
|
response.FailWithMsg("参数错误:"+err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = menuService.SaveMenu(menu); err != nil {
|
||||||
|
global.Logger.Error("保存菜单失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
response.OkWithMsg("保存菜单成功!", c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MenuApi) UpdateMenu(c *gin.Context) {
|
||||||
|
var menu system.Menu
|
||||||
|
err := c.ShouldBindJSON(&menu)
|
||||||
|
if err != nil {
|
||||||
|
response.FailWithMsg("参数错误:"+err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = menuService.UpdateMenu(&menu); err != nil {
|
||||||
|
global.Logger.Error("更新菜单失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
response.OkWithMsg("更新菜单成功!", c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MenuApi) DeleteMenu(c *gin.Context) {
|
||||||
|
id := c.Query("id")
|
||||||
|
err := menuService.DeleteMenu(id)
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Error("删除菜单失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response.OkWithMsg("删除菜单成功!", c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MenuApi) Detail(c *gin.Context) {
|
||||||
|
id := c.Query("id")
|
||||||
|
menu, err := menuService.GetMenuById(id)
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Error("获取菜单详情失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response.OkWithData(menu, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MenuApi) GetAllMenuTree(c *gin.Context) {
|
||||||
|
var param systemReq.GetMenuTree
|
||||||
|
err := c.ShouldBindJSON(¶m)
|
||||||
|
if err != nil {
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
menus, err := menuService.GetAllMenuTree(param.Category, param.ParentId)
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Error("获取菜单树结构失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response.OkWithData(menus, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MenuApi) GetUserMenuTree(c *gin.Context) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MenuApi) Route(c *gin.Context) {
|
||||||
|
userId := utils.GetUserId(c)
|
||||||
|
routes, err := menuService.GetUserRoutes(userId)
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Error("获取用户菜单失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response.OkWithData(routes, c)
|
||||||
|
}
|
||||||
+26
-14
@@ -1,13 +1,14 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"go.uber.org/zap"
|
|
||||||
"sundynix-go/global"
|
"sundynix-go/global"
|
||||||
"sundynix-go/model/commom/request"
|
"sundynix-go/model/commom/request"
|
||||||
"sundynix-go/model/commom/response"
|
"sundynix-go/model/commom/response"
|
||||||
"sundynix-go/model/system"
|
"sundynix-go/model/system"
|
||||||
systemreq "sundynix-go/model/system/request"
|
systemreq "sundynix-go/model/system/request"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RoleApi struct {
|
type RoleApi struct {
|
||||||
@@ -17,10 +18,10 @@ func (a *RoleApi) SaveRole(context *gin.Context) {
|
|||||||
var role system.Role
|
var role system.Role
|
||||||
err := context.ShouldBindJSON(&role)
|
err := context.ShouldBindJSON(&role)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.FailWithMsg(err.Error(), context)
|
response.FailWithMsg("参数错误"+err.Error(), context)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = RoleService.SaveRole(role)
|
err = roleService.SaveRole(role)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.Logger.Error("保存角色失败!", zap.Error(err))
|
global.Logger.Error("保存角色失败!", zap.Error(err))
|
||||||
response.FailWithMsg(err.Error(), context)
|
response.FailWithMsg(err.Error(), context)
|
||||||
@@ -36,7 +37,7 @@ func (a *RoleApi) UpdateRole(context *gin.Context) {
|
|||||||
response.FailWithMsg(err.Error(), context)
|
response.FailWithMsg(err.Error(), context)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = RoleService.UpdateRole(role)
|
err = roleService.UpdateRole(role)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.Logger.Error("更新角色失败!", zap.Error(err))
|
global.Logger.Error("更新角色失败!", zap.Error(err))
|
||||||
response.FailWithMsg(err.Error(), context)
|
response.FailWithMsg(err.Error(), context)
|
||||||
@@ -52,7 +53,7 @@ func (a *RoleApi) GetRoleList(c *gin.Context) {
|
|||||||
response.FailWithMsg(err.Error(), c)
|
response.FailWithMsg(err.Error(), c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
list, total, err := RoleService.GetRoleList(pageInfo)
|
list, total, err := roleService.GetRoleList(pageInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.Logger.Error("获取角色列表失败!", zap.Error(err))
|
global.Logger.Error("获取角色列表失败!", zap.Error(err))
|
||||||
response.FailWithMsg(err.Error(), c)
|
response.FailWithMsg(err.Error(), c)
|
||||||
@@ -72,7 +73,7 @@ func (a *RoleApi) Delete(context *gin.Context) {
|
|||||||
response.FailWithMsg(err.Error(), context)
|
response.FailWithMsg(err.Error(), context)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = RoleService.DeleteRoleByIds(ids)
|
err = roleService.DeleteRoleByIds(ids)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.Logger.Error("删除角色失败!", zap.Error(err))
|
global.Logger.Error("删除角色失败!", zap.Error(err))
|
||||||
response.FailWithMsg(err.Error(), context)
|
response.FailWithMsg(err.Error(), context)
|
||||||
@@ -82,13 +83,8 @@ func (a *RoleApi) Delete(context *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *RoleApi) Detail(context *gin.Context) {
|
func (a *RoleApi) Detail(context *gin.Context) {
|
||||||
var idInfo request.GetById
|
id := context.Query("id")
|
||||||
err := context.ShouldBindJSON(&idInfo)
|
role, err := roleService.GetRoleById(id)
|
||||||
if err != nil {
|
|
||||||
response.FailWithMsg(err.Error(), context)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
role, err := RoleService.GetRoleById(idInfo.ID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.Logger.Error("获取角色详情失败!", zap.Error(err))
|
global.Logger.Error("获取角色详情失败!", zap.Error(err))
|
||||||
response.FailWithMsg(err.Error(), context)
|
response.FailWithMsg(err.Error(), context)
|
||||||
@@ -96,3 +92,19 @@ func (a *RoleApi) Detail(context *gin.Context) {
|
|||||||
}
|
}
|
||||||
response.OkWithData(role, context)
|
response.OkWithData(role, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *RoleApi) GrantMenu(c *gin.Context) {
|
||||||
|
var grantMenu systemreq.GrantMenu
|
||||||
|
err := c.ShouldBindJSON(&grantMenu)
|
||||||
|
if err != nil {
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = roleService.GrantMenu(grantMenu.RoleId, grantMenu.MenuIds)
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Error("授权菜单失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response.OkWithMsg("授权菜单成功!", c)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,16 +1,49 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sundynix-go/global"
|
||||||
|
"sundynix-go/model/commom/request"
|
||||||
|
"sundynix-go/model/commom/response"
|
||||||
|
"sundynix-go/model/system"
|
||||||
|
systemReq "sundynix-go/model/system/request"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"sundynix-go/global"
|
|
||||||
"sundynix-go/model/commom/response"
|
|
||||||
systemReq "sundynix-go/model/system/request"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserApi struct {
|
type UserApi struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *UserApi) SaveUser(c *gin.Context) {
|
||||||
|
var user system.User
|
||||||
|
err := c.ShouldBindJSON(&user)
|
||||||
|
if err != nil {
|
||||||
|
response.FailWithMsg("参数错误:"+err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = userService.SaveUser(user); err != nil {
|
||||||
|
global.Logger.Error("保存用户失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
} else {
|
||||||
|
response.OkWithMsg("保存用户成功!", c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserApi) UpdateUser(c *gin.Context) {
|
||||||
|
var user system.User
|
||||||
|
err := c.ShouldBindJSON(&user)
|
||||||
|
if err != nil {
|
||||||
|
response.FailWithMsg("参数错误:"+err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = userService.UpdateUser(&user); err != nil {
|
||||||
|
global.Logger.Error("更新用户失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
} else {
|
||||||
|
response.OkWithMsg("更新用户成功!", c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (u *UserApi) GetUserList(c *gin.Context) {
|
func (u *UserApi) GetUserList(c *gin.Context) {
|
||||||
var pageInfo systemReq.GetUserList
|
var pageInfo systemReq.GetUserList
|
||||||
err := c.ShouldBindJSON(&pageInfo)
|
err := c.ShouldBindJSON(&pageInfo)
|
||||||
@@ -18,7 +51,7 @@ func (u *UserApi) GetUserList(c *gin.Context) {
|
|||||||
response.FailWithMsg(err.Error(), c)
|
response.FailWithMsg(err.Error(), c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
list, total, err := UserService.GetUserList(pageInfo)
|
list, total, err := userService.GetUserList(pageInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.Logger.Error("获取用户列表失败!", zap.Error(err))
|
global.Logger.Error("获取用户列表失败!", zap.Error(err))
|
||||||
response.FailWithMsg(err.Error(), c)
|
response.FailWithMsg(err.Error(), c)
|
||||||
@@ -31,3 +64,62 @@ func (u *UserApi) GetUserList(c *gin.Context) {
|
|||||||
PageSize: pageInfo.PageSize,
|
PageSize: pageInfo.PageSize,
|
||||||
}, c)
|
}, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *UserApi) Delete(c *gin.Context) {
|
||||||
|
var ids request.IdsReq
|
||||||
|
err := c.ShouldBindJSON(&ids)
|
||||||
|
if err != nil {
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = userService.DeleteUserByIds(ids)
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Error("删除用户失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response.OkWithMsg("删除用户成功!", c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserApi) Detail(c *gin.Context) {
|
||||||
|
id := c.Query("id")
|
||||||
|
user, err := userService.GetUserById(id)
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Error("获取用户详情失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response.OkWithData(user, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserApi) ChangePassword(c *gin.Context) {
|
||||||
|
var changePwd systemReq.ChangePwd
|
||||||
|
err := c.ShouldBindJSON(&changePwd)
|
||||||
|
if err != nil {
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = userService.ChangePassword(changePwd.Id, changePwd.NewPwd)
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Error("修改密码失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response.OkWithMsg("修改密码成功", c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserApi) GrantRole(c *gin.Context) {
|
||||||
|
var grantRole systemReq.GrantRole
|
||||||
|
err := c.ShouldBindJSON(&grantRole)
|
||||||
|
if err != nil {
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = roleService.GrantRole(grantRole.UserId, grantRole.RoleIds)
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Error("授权角色失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response.OkWithMsg("授权角色成功!", c)
|
||||||
|
}
|
||||||
|
|||||||
+1
-1
@@ -22,7 +22,7 @@ mysql:
|
|||||||
max-open-conns: 100
|
max-open-conns: 100
|
||||||
host: 127.0.0.1
|
host: 127.0.0.1
|
||||||
port: "3306"
|
port: "3306"
|
||||||
prefix: "sundynix-"
|
prefix: "sundynix_"
|
||||||
singular: true
|
singular: true
|
||||||
user: root
|
user: root
|
||||||
password: root
|
password: root
|
||||||
|
|||||||
+6
-2
@@ -1,12 +1,14 @@
|
|||||||
package global
|
package global
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"gorm.io/gorm"
|
"sundynix-go/utils/uniqueid"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BaseModel struct {
|
type BaseModel struct {
|
||||||
Id uint `gorm:"primarykey" json:"id"` // 主键ID
|
Id string `gorm:"size:50;primaryKey" json:"id"` // 主键ID
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
UpdatedAt time.Time `json:"updatedAt"`
|
UpdatedAt time.Time `json:"updatedAt"`
|
||||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"` // 删除时间
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"` // 删除时间
|
||||||
@@ -14,6 +16,8 @@ type BaseModel struct {
|
|||||||
|
|
||||||
// BeforeCreate 定义一个钩子,在创建之前执行自动插入字段
|
// BeforeCreate 定义一个钩子,在创建之前执行自动插入字段
|
||||||
func (model BaseModel) BeforeCreate(db *gorm.DB) (err error) {
|
func (model BaseModel) BeforeCreate(db *gorm.DB) (err error) {
|
||||||
|
//生成主键的string uniqueid
|
||||||
|
db.Statement.SetColumn("id", uniqueid.GenerateId())
|
||||||
db.Statement.SetColumn("created_at", time.Now())
|
db.Statement.SetColumn("created_at", time.Now())
|
||||||
db.Statement.SetColumn("updated_at", time.Now())
|
db.Statement.SetColumn("updated_at", time.Now())
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -32,6 +32,8 @@ func MigrateTable() {
|
|||||||
err := db.AutoMigrate(
|
err := db.AutoMigrate(
|
||||||
system.User{},
|
system.User{},
|
||||||
system.Client{},
|
system.Client{},
|
||||||
|
system.Role{},
|
||||||
|
system.Menu{},
|
||||||
system.SysOperationRecord{},
|
system.SysOperationRecord{},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ func Routers() {
|
|||||||
{
|
{
|
||||||
//无须鉴权的路由
|
//无须鉴权的路由
|
||||||
systemRouter.InitAuthRouter(PublicGroup) //登录和验证码不需要鉴权
|
systemRouter.InitAuthRouter(PublicGroup) //登录和验证码不需要鉴权
|
||||||
|
//systemRouter.InitMenuRouter(PublicGroup)
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -36,6 +37,7 @@ func Routers() {
|
|||||||
systemRouter.InitUserRouter(NeedAuthGroup) //用户相关
|
systemRouter.InitUserRouter(NeedAuthGroup) //用户相关
|
||||||
systemRouter.InitClientRouter(NeedAuthGroup) //客户端相关
|
systemRouter.InitClientRouter(NeedAuthGroup) //客户端相关
|
||||||
systemRouter.InitRoleRouter(NeedAuthGroup) //角色相关
|
systemRouter.InitRoleRouter(NeedAuthGroup) //角色相关
|
||||||
|
systemRouter.InitMenuRouter(NeedAuthGroup) //菜单相关
|
||||||
}
|
}
|
||||||
|
|
||||||
address := fmt.Sprintf(":%d", global.Config.System.Addr)
|
address := fmt.Sprintf(":%d", global.Config.System.Addr)
|
||||||
|
|||||||
@@ -29,20 +29,20 @@ func (r *PageInfo) Paginate() func(db *gorm.DB) *gorm.DB {
|
|||||||
|
|
||||||
// GetById Find by id structure
|
// GetById Find by id structure
|
||||||
type GetById struct {
|
type GetById struct {
|
||||||
ID int `json:"id" form:"id"` // 主键ID
|
ID string `json:"id" form:"id"` // 主键ID
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *GetById) Uint() uint {
|
func (r *GetById) Uint() string {
|
||||||
return uint(r.ID)
|
return string(r.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
type IdsReq struct {
|
type IdsReq struct {
|
||||||
Ids []int `json:"ids" form:"ids"`
|
Ids []string `json:"ids" form:"ids"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAuthorityId Get role by id structure
|
// GetAuthorityId Get role by id structure
|
||||||
type GetAuthorityId struct {
|
type GetAuthorityId struct {
|
||||||
AuthorityId uint `json:"authorityId" form:"authorityId"` // 角色ID
|
AuthorityId string `json:"authorityId" form:"authorityId"` // 角色ID
|
||||||
}
|
}
|
||||||
|
|
||||||
type Empty struct{}
|
type Empty struct{}
|
||||||
|
|||||||
@@ -13,6 +13,6 @@ type CustomClaims struct {
|
|||||||
|
|
||||||
type BaseClaims struct {
|
type BaseClaims struct {
|
||||||
UUID uuid.UUID
|
UUID uuid.UUID
|
||||||
ID uint
|
ID string
|
||||||
Account string
|
Account string
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package request
|
||||||
|
|
||||||
|
type GetMenuTree struct {
|
||||||
|
Category int `json:"category" form:"category"`
|
||||||
|
ParentId string `json:"parentId" form:"parentId"`
|
||||||
|
}
|
||||||
@@ -7,3 +7,8 @@ type GetRoleList struct {
|
|||||||
Code string `json:"code" form:"code"`
|
Code string `json:"code" form:"code"`
|
||||||
Name string `json:"name" form:"name"`
|
Name string `json:"name" form:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GrantMenu struct {
|
||||||
|
RoleId string `json:"roleId"`
|
||||||
|
MenuIds []string `json:"menuIds"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,3 +14,13 @@ type GetUserList struct {
|
|||||||
Account string `json:"account" form:"account"`
|
Account string `json:"account" form:"account"`
|
||||||
Phone string `json:"phone" form:"phone"`
|
Phone string `json:"phone" form:"phone"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ChangePwd struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
NewPwd string `json:"newPwd"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GrantRole struct {
|
||||||
|
UserId string `json:"userId"`
|
||||||
|
RoleIds []string `json:"roleIds"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,10 +2,6 @@ package response
|
|||||||
|
|
||||||
import "sundynix-go/model/system"
|
import "sundynix-go/model/system"
|
||||||
|
|
||||||
type SysUserResponse struct {
|
|
||||||
User system.User `json:"user"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type LoginResponse struct {
|
type LoginResponse struct {
|
||||||
User system.User `json:"user"`
|
User system.User `json:"user"`
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
|
|||||||
@@ -8,5 +8,5 @@ type Client struct {
|
|||||||
Name string `gorm:"size:50;" json:"name"`
|
Name string `gorm:"size:50;" json:"name"`
|
||||||
GrantType string `gorm:"size:50;" json:"grantType"`
|
GrantType string `gorm:"size:50;" json:"grantType"`
|
||||||
AdditionalInfo string `gorm:"type:text" json:"additionalInfo"`
|
AdditionalInfo string `gorm:"type:text" json:"additionalInfo"`
|
||||||
ActiveTimeout uint `json:"activeTimeout"`
|
ActiveTimeout int64 `json:"activeTimeout"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package system
|
||||||
|
|
||||||
|
import "sundynix-go/global"
|
||||||
|
|
||||||
|
type Menu struct {
|
||||||
|
global.BaseModel
|
||||||
|
ParentId string `gorm:"size:100;default:'0'" json:"parentId" form:"parentId"`
|
||||||
|
Category int `json:"category" form:"category"`
|
||||||
|
Name string `gorm:"size:20" json:"name" form:"name"`
|
||||||
|
Title string `gorm:"size:20" json:"title" form:"title"`
|
||||||
|
Code string `gorm:"size:20" json:"code" form:"code"`
|
||||||
|
Permission string `gorm:"size:20" json:"permission" form:"permission"`
|
||||||
|
Locale string `gorm:"size:50" json:"locale" form:"locale"`
|
||||||
|
Icon string `gorm:"size:20" json:"icon" form:"icon"`
|
||||||
|
Sort int `json:"sort" form:"sort"`
|
||||||
|
Children []*Menu `json:"children" gorm:"-"`
|
||||||
|
}
|
||||||
@@ -4,7 +4,8 @@ import "sundynix-go/global"
|
|||||||
|
|
||||||
type Role struct {
|
type Role struct {
|
||||||
global.BaseModel
|
global.BaseModel
|
||||||
Name string `json:"name" form:"name"`
|
Name string `gorm:"size:20" json:"name" form:"name"`
|
||||||
Code string `json:"code" form:"code"`
|
Code string `gorm:"size:20" json:"code" form:"code"`
|
||||||
Sort int `json:"sort" form:"sort"`
|
Sort int `json:"sort" form:"sort"`
|
||||||
|
Menus []Menu `gorm:"many2many:role_menu;"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package system
|
||||||
|
|
||||||
|
type RoleMenu struct {
|
||||||
|
RoleId string `json:"roleId" gorm:"size:100;column:role_id;comment:角色id"`
|
||||||
|
MenuId string `json:"menuId" gorm:"size:100;column:menu_id;comment:菜单id"`
|
||||||
|
}
|
||||||
@@ -6,21 +6,23 @@ import (
|
|||||||
|
|
||||||
type Login interface {
|
type Login interface {
|
||||||
GetAccount() string
|
GetAccount() string
|
||||||
GetUserId() uint
|
GetUserId() string
|
||||||
GetUserInfo() any
|
GetUserInfo() any
|
||||||
}
|
}
|
||||||
type User struct {
|
type User struct {
|
||||||
global.BaseModel
|
global.BaseModel
|
||||||
|
TenantId string `gorm:"size:20;" json:"tenantId" form:"tenantId"`
|
||||||
ClientId string `gorm:"size:20;" json:"clientId"`
|
ClientId string `gorm:"size:20;" json:"clientId"`
|
||||||
Account string `gorm:"size:11;unique;" json:"account" form:"account"`
|
Account string `gorm:"size:11;unique;" json:"account" form:"account"`
|
||||||
Password string `gorm:"size:100;" json:"-" form:"password"`
|
Password string `gorm:"size:100;" json:"-" form:"password"`
|
||||||
Phone string `gorm:"size:11;" json:"phone" form:"phone"`
|
Phone string `gorm:"size:11;" json:"phone" form:"phone"`
|
||||||
|
Roles []Role `gorm:"many2many:user_role;" json:"roles"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *User) GetAccount() string {
|
func (u *User) GetAccount() string {
|
||||||
return u.Account
|
return u.Account
|
||||||
}
|
}
|
||||||
func (u *User) GetUserId() uint {
|
func (u *User) GetUserId() string {
|
||||||
return u.Id
|
return u.Id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package system
|
||||||
|
|
||||||
|
type UserRole struct {
|
||||||
|
UserId string `json:"userId" gorm:"size:100;column:user_id;comment:用户id"`
|
||||||
|
RoleId string `json:"roleId" gorm:"size:100;column:role_id;comment:角色id"`
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ type RouterGroup struct {
|
|||||||
UserRouter
|
UserRouter
|
||||||
ClientRouter
|
ClientRouter
|
||||||
RoleRouter
|
RoleRouter
|
||||||
|
MenuRouter
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化路由
|
// 初始化路由
|
||||||
@@ -15,4 +16,5 @@ var (
|
|||||||
userApi = v1.ApiGroupApp.SystemApiGroup.UserApi
|
userApi = v1.ApiGroupApp.SystemApiGroup.UserApi
|
||||||
clientApi = v1.ApiGroupApp.SystemApiGroup.ClientApi
|
clientApi = v1.ApiGroupApp.SystemApiGroup.ClientApi
|
||||||
roleApi = v1.ApiGroupApp.SystemApiGroup.RoleApi
|
roleApi = v1.ApiGroupApp.SystemApiGroup.RoleApi
|
||||||
|
menuApi = v1.ApiGroupApp.SystemApiGroup.MenuApi
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package system
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
type MenuRouter struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m MenuRouter) InitMenuRouter(Router *gin.RouterGroup) {
|
||||||
|
menuRouter := Router.Group("menu")
|
||||||
|
{
|
||||||
|
menuRouter.GET("route", menuApi.Route)
|
||||||
|
menuRouter.POST("getAllMenuTree", menuApi.GetAllMenuTree)
|
||||||
|
menuRouter.POST("getUserMenuTree", menuApi.GetUserMenuTree)
|
||||||
|
menuRouter.POST("save", menuApi.SaveMenu)
|
||||||
|
menuRouter.POST("update", menuApi.UpdateMenu)
|
||||||
|
menuRouter.GET("delete", menuApi.DeleteMenu)
|
||||||
|
menuRouter.GET("detail", menuApi.Detail)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,7 +11,8 @@ func (r *RoleRouter) InitRoleRouter(router *gin.RouterGroup) {
|
|||||||
roleRouter.POST("save", roleApi.SaveRole)
|
roleRouter.POST("save", roleApi.SaveRole)
|
||||||
roleRouter.POST("update", roleApi.UpdateRole)
|
roleRouter.POST("update", roleApi.UpdateRole)
|
||||||
roleRouter.POST("getRoleList", roleApi.GetRoleList)
|
roleRouter.POST("getRoleList", roleApi.GetRoleList)
|
||||||
roleRouter.GET("delete", roleApi.Delete)
|
roleRouter.POST("delete", roleApi.Delete)
|
||||||
roleRouter.GET("detail", roleApi.Detail)
|
roleRouter.GET("detail", roleApi.Detail)
|
||||||
|
roleRouter.POST("grantMenu", roleApi.GrantMenu)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,12 @@ type UserRouter struct {
|
|||||||
func (s *UserRouter) InitUserRouter(Router *gin.RouterGroup) {
|
func (s *UserRouter) InitUserRouter(Router *gin.RouterGroup) {
|
||||||
userRouter := Router.Group("user")
|
userRouter := Router.Group("user")
|
||||||
{
|
{
|
||||||
|
userRouter.POST("save", userApi.SaveUser)
|
||||||
|
userRouter.POST("update", userApi.UpdateUser)
|
||||||
userRouter.POST("getUserList", userApi.GetUserList)
|
userRouter.POST("getUserList", userApi.GetUserList)
|
||||||
|
userRouter.POST("delete", userApi.Delete)
|
||||||
|
userRouter.GET("detail", userApi.Detail)
|
||||||
|
userRouter.POST("changePassword", userApi.ChangePassword)
|
||||||
|
userRouter.POST("grantRole", userApi.GrantRole)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,4 +4,5 @@ type ServiceGroup struct {
|
|||||||
UserService
|
UserService
|
||||||
ClientService
|
ClientService
|
||||||
RoleService
|
RoleService
|
||||||
|
MenuService
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,12 @@ package system
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"gorm.io/gorm"
|
|
||||||
"sundynix-go/global"
|
"sundynix-go/global"
|
||||||
common "sundynix-go/model/commom/request"
|
common "sundynix-go/model/commom/request"
|
||||||
"sundynix-go/model/system"
|
"sundynix-go/model/system"
|
||||||
systemReq "sundynix-go/model/system/request"
|
systemReq "sundynix-go/model/system/request"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ClientService struct{}
|
type ClientService struct{}
|
||||||
@@ -44,10 +45,17 @@ func (s *ClientService) GetClientList(info systemReq.GetClientList) (list interf
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *ClientService) DeleteClientByIds(ids common.IdsReq) (err error) {
|
func (s *ClientService) DeleteClientByIds(ids common.IdsReq) (err error) {
|
||||||
return global.DB.Delete(&system.Client{}, "id IN ?", ids.Ids).Error
|
return global.DB.Where("id IN (?)", ids.Ids).Delete(&system.Client{}).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ClientService) GetClientById(id int) (client system.Client, err error) {
|
func (s *ClientService) GetClientById(id string) (client *system.Client, err error) {
|
||||||
err = global.DB.Where("id = ?", id).First(&client).Error
|
var c system.Client
|
||||||
return client, err
|
err = global.DB.Where("id = ?", id).First(&c).Error
|
||||||
|
return &c, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ClientService) GetClientByClientId(clientId string) (client *system.Client, err error) {
|
||||||
|
var c system.Client
|
||||||
|
err = global.DB.Where("client_id = ?", clientId).First(&c).Error
|
||||||
|
return &c, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,126 @@
|
|||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"sundynix-go/global"
|
||||||
|
"sundynix-go/model/system"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MenuService struct{}
|
||||||
|
|
||||||
|
var MenuServiceApp = new(MenuService)
|
||||||
|
|
||||||
|
func (s *MenuService) SaveMenu(menu system.Menu) error {
|
||||||
|
//1.根据code和name查询是否存在重名
|
||||||
|
if err := global.DB.Where("code = ? or name = ?", menu.Code, menu.Name).First(&system.Menu{}).Error; err == nil {
|
||||||
|
return errors.New("菜单已存在")
|
||||||
|
}
|
||||||
|
return global.DB.Create(&menu).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *MenuService) UpdateMenu(menu *system.Menu) (err error) {
|
||||||
|
var sysMenu system.Menu
|
||||||
|
menuMap := map[string]interface{}{
|
||||||
|
"Category": menu.Category,
|
||||||
|
"Name": menu.Name,
|
||||||
|
"Title": menu.Title,
|
||||||
|
"Code": menu.Code,
|
||||||
|
"Permission": menu.Permission,
|
||||||
|
"Locale": menu.Locale,
|
||||||
|
"Icon": menu.Icon,
|
||||||
|
"Sort": menu.Sort,
|
||||||
|
}
|
||||||
|
err = global.DB.Where("id = ?", menu.Id).First(&sysMenu).Error
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Debug(err.Error())
|
||||||
|
return errors.New("查询菜单失败")
|
||||||
|
}
|
||||||
|
err = global.DB.Model(&sysMenu).Updates(menuMap).Error
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *MenuService) DeleteMenu(id string) (err error) {
|
||||||
|
err = global.DB.First(&system.Menu{}, "parent_id = ?", id).Error
|
||||||
|
if err == nil {
|
||||||
|
return errors.New("请先删除子菜单")
|
||||||
|
}
|
||||||
|
var menu system.Menu
|
||||||
|
err = global.DB.Where("id = ?", id).First(&menu).Error
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("菜单记录不存在")
|
||||||
|
}
|
||||||
|
// 同步删除menu表和role-menu表数据
|
||||||
|
return global.DB.Transaction(func(tx *gorm.DB) error {
|
||||||
|
if err = tx.Where("id = ?", id).Delete(&system.Menu{}).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = tx.Where("menu_id = ?", id).Delete(&system.RoleMenu{}).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *MenuService) GetMenuById(id string) (menu *system.Menu, err error) {
|
||||||
|
var m system.Menu
|
||||||
|
err = global.DB.Where("id = ?", id).First(&m).Error
|
||||||
|
return &m, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *MenuService) GetAllMenuTree(category int, parentId string) (menus []*system.Menu, err error) {
|
||||||
|
//1,先根据category和parentId获取所有菜单 category默认为0,parentId默认为0
|
||||||
|
//2.讲查询出的列表构建为树结构
|
||||||
|
var menuList []*system.Menu
|
||||||
|
db := global.DB.Model(&system.Menu{})
|
||||||
|
if category != 0 {
|
||||||
|
db.Where("category = ?", category)
|
||||||
|
}
|
||||||
|
if parentId != "0" {
|
||||||
|
db.Where("parent_id = ?", parentId)
|
||||||
|
}
|
||||||
|
err = db.Order("sort asc").Find(&menuList).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
tree := buildMenuTree(menuList)
|
||||||
|
return tree, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *MenuService) GetUserRoutes(userId string) (menus []*system.Menu, err error) {
|
||||||
|
//1.根据userId 查询角色 根据角色查询菜单 去重
|
||||||
|
//2.构建树结构
|
||||||
|
var roleIds []string
|
||||||
|
err = global.DB.Model(&system.UserRole{}).Where("user_id = ?", userId).Pluck("role_id", &roleIds).Error
|
||||||
|
var menuIds []string
|
||||||
|
err = global.DB.Model(&system.RoleMenu{}).Where("role_id in ?", roleIds).Pluck("menu_id", &menuIds).Error
|
||||||
|
var menuList []*system.Menu
|
||||||
|
err = global.DB.Model(&system.Menu{}).Where("id in ?", menuIds).Find(&menuList).Error
|
||||||
|
return buildMenuTree(menuList), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildMenuTree(list []*system.Menu) []*system.Menu {
|
||||||
|
//1.定义一个map
|
||||||
|
menuMap := make(map[string]*system.Menu)
|
||||||
|
for _, item := range list {
|
||||||
|
menuMap[item.Id] = item
|
||||||
|
}
|
||||||
|
//构建树结构
|
||||||
|
var treeList []*system.Menu
|
||||||
|
for _, item := range list {
|
||||||
|
if item.ParentId == "0" {
|
||||||
|
// 如果没有父节点,直接添加到树中
|
||||||
|
treeList = append(treeList, item)
|
||||||
|
} else {
|
||||||
|
if parent, exists := menuMap[item.ParentId]; exists {
|
||||||
|
// 如果有父节点,将当前节点添加到父节点的Children中
|
||||||
|
parent.Children = append(parent.Children, item)
|
||||||
|
} else {
|
||||||
|
// 如果没有父节点,将当前节点添加到树中
|
||||||
|
treeList = append(treeList, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return treeList
|
||||||
|
}
|
||||||
@@ -2,11 +2,12 @@ package system
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"gorm.io/gorm"
|
|
||||||
"sundynix-go/global"
|
"sundynix-go/global"
|
||||||
common "sundynix-go/model/commom/request"
|
common "sundynix-go/model/commom/request"
|
||||||
"sundynix-go/model/system"
|
"sundynix-go/model/system"
|
||||||
systemReq "sundynix-go/model/system/request"
|
systemReq "sundynix-go/model/system/request"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RoleService struct {
|
type RoleService struct {
|
||||||
@@ -48,7 +49,41 @@ func (s *RoleService) DeleteRoleByIds(ids common.IdsReq) error {
|
|||||||
return global.DB.Where("id in ?", ids.Ids).Delete(&system.Role{}).Error
|
return global.DB.Where("id in ?", ids.Ids).Delete(&system.Role{}).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *RoleService) GetRoleById(id int) (role system.Role, err error) {
|
func (s *RoleService) GetRoleById(id string) (role *system.Role, err error) {
|
||||||
err = global.DB.Where("id = ?", id).First(&role).Error
|
var r system.Role
|
||||||
return role, err
|
err = global.DB.Where("id = ?", id).First(&r).Error
|
||||||
|
return &r, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *RoleService) GrantRole(userId string, roleIds []string) error {
|
||||||
|
//1. 检查是否存在userid的授权记录 存在就删除 不存在就插入
|
||||||
|
//2. 插入新的数据
|
||||||
|
return global.DB.Transaction(func(tx *gorm.DB) error {
|
||||||
|
if err := tx.Where("user_id = ?", userId).Delete(&system.UserRole{}).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, roleId := range roleIds {
|
||||||
|
if err := tx.Create(&system.UserRole{UserId: userId, RoleId: roleId}).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *RoleService) GrantMenu(roleId string, menuIds []string) error {
|
||||||
|
//1. 检查是否存在userid的授权记录 存在就删除 不存在就插入
|
||||||
|
//2. 插入新的数据
|
||||||
|
return global.DB.Transaction(func(tx *gorm.DB) error {
|
||||||
|
if err := tx.Where("role_id = ?", roleId).Delete(&system.RoleMenu{}).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, menuId := range menuIds {
|
||||||
|
if err := tx.Create(&system.RoleMenu{RoleId: roleId, MenuId: menuId}).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,12 @@ package system
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"sundynix-go/global"
|
"sundynix-go/global"
|
||||||
|
common "sundynix-go/model/commom/request"
|
||||||
"sundynix-go/model/system"
|
"sundynix-go/model/system"
|
||||||
systemReq "sundynix-go/model/system/request"
|
systemReq "sundynix-go/model/system/request"
|
||||||
"sundynix-go/utils"
|
"sundynix-go/utils"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserService struct{}
|
type UserService struct{}
|
||||||
@@ -14,7 +17,8 @@ var UserServiceApp = new(UserService)
|
|||||||
|
|
||||||
func (userService *UserService) Login(u *system.User) (userInfo *system.User, err error) {
|
func (userService *UserService) Login(u *system.User) (userInfo *system.User, err error) {
|
||||||
var user system.User
|
var user system.User
|
||||||
err = global.DB.Where("account = ?", u.Account).First(&user).Error
|
// 查询出用户信息的同时查询出角色信息
|
||||||
|
err = global.DB.Model(&system.User{}).Preload("Roles").Where("account = ?", u.Account).First(&user).Error
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if ok := utils.BcryptCheck(u.Password, user.Password); !ok {
|
if ok := utils.BcryptCheck(u.Password, user.Password); !ok {
|
||||||
return nil, errors.New("密码错误")
|
return nil, errors.New("密码错误")
|
||||||
@@ -23,6 +27,29 @@ func (userService *UserService) Login(u *system.User) (userInfo *system.User, er
|
|||||||
return &user, err
|
return &user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (userService *UserService) SaveUser(user system.User) error {
|
||||||
|
if !errors.Is(global.DB.Where("account = ?", user.Account).First(&system.User{}).Error, gorm.ErrRecordNotFound) {
|
||||||
|
return errors.New("存在重复Account,请修改Account")
|
||||||
|
}
|
||||||
|
user.Password = utils.BcryptHash(user.Password)
|
||||||
|
return global.DB.Create(&user).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (userService *UserService) UpdateUser(user *system.User) (err error) {
|
||||||
|
var sysUser system.User
|
||||||
|
userMap := map[string]interface{}{
|
||||||
|
"Account": user.Account,
|
||||||
|
"Phone": user.Phone,
|
||||||
|
}
|
||||||
|
err = global.DB.Where("id = ?", user.Id).First(&sysUser).Error
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Debug(err.Error())
|
||||||
|
return errors.New("查询用户失败")
|
||||||
|
}
|
||||||
|
err = global.DB.Model(&sysUser).Updates(userMap).Error
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (userService *UserService) GetUserList(info systemReq.GetUserList) (list interface{}, total int64, err error) {
|
func (userService *UserService) GetUserList(info systemReq.GetUserList) (list interface{}, total int64, err error) {
|
||||||
limit := info.PageSize
|
limit := info.PageSize
|
||||||
offset := info.PageSize * (info.Current - 1)
|
offset := info.PageSize * (info.Current - 1)
|
||||||
@@ -42,3 +69,17 @@ func (userService *UserService) GetUserList(info systemReq.GetUserList) (list in
|
|||||||
err = db.Limit(limit).Offset(offset).Find(&userList).Error
|
err = db.Limit(limit).Offset(offset).Find(&userList).Error
|
||||||
return userList, total, err
|
return userList, total, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (userService *UserService) DeleteUserByIds(ids common.IdsReq) error {
|
||||||
|
return global.DB.Where("id IN (?)", ids.Ids).Delete(&system.User{}).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (userService *UserService) GetUserById(id string) (user *system.User, err error) {
|
||||||
|
var u system.User
|
||||||
|
err = global.DB.Where("id = ?", id).Preload("Roles").First(&u).Error
|
||||||
|
return &u, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (userService *UserService) ChangePassword(id string, pwd string) (err error) {
|
||||||
|
return global.DB.Model(&system.User{}).Where("id = ?", id).Update("password", utils.BcryptHash(pwd)).Error
|
||||||
|
}
|
||||||
|
|||||||
+20
-20
@@ -1,12 +1,12 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"net"
|
"net"
|
||||||
"sundynix-go/global"
|
"sundynix-go/global"
|
||||||
"sundynix-go/model/system"
|
"sundynix-go/model/system"
|
||||||
systemReq "sundynix-go/model/system/request"
|
systemReq "sundynix-go/model/system/request"
|
||||||
"time"
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetLoginToken 获取登录token
|
// GetLoginToken 获取登录token
|
||||||
@@ -57,22 +57,22 @@ func GetToken(c *gin.Context) string {
|
|||||||
// 从请求头中获取Authorization字段的值
|
// 从请求头中获取Authorization字段的值
|
||||||
token := c.Request.Header.Get("Authorization")
|
token := c.Request.Header.Get("Authorization")
|
||||||
|
|
||||||
// 如果请求头中没有Authorization字段,则尝试从Cookie中获取token
|
//// 如果请求头中没有Authorization字段,则尝试从Cookie中获取token
|
||||||
if token == "" {
|
//if token == "" {
|
||||||
j := NewJWT()
|
// j := NewJWT()
|
||||||
token, _ = c.Cookie("x-token")
|
// token, _ = c.Cookie("x-token")
|
||||||
|
//
|
||||||
// 解析并验证token的有效性
|
// // 解析并验证token的有效性
|
||||||
claims, err := j.ParseToken(token)
|
// claims, err := j.ParseToken(token)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
// 如果解析失败,记录错误日志并返回当前token
|
// // 如果解析失败,记录错误日志并返回当前token
|
||||||
global.Logger.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在x-token且claims是否为规定结构")
|
// global.Logger.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在x-token且claims是否为规定结构")
|
||||||
return token
|
// return token
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// 如果token有效,则将其重新写入Cookie,并设置过期时间
|
// // 如果token有效,则将其重新写入Cookie,并设置过期时间
|
||||||
SetToken(c, token, int((claims.ExpiresAt.Unix()-time.Now().Unix())/60))
|
// SetToken(c, token, int((claims.ExpiresAt.Unix()-time.Now().Unix())/60))
|
||||||
}
|
//}
|
||||||
|
|
||||||
// 返回获取到的token
|
// 返回获取到的token
|
||||||
return token
|
return token
|
||||||
@@ -127,10 +127,10 @@ func GetUserInfo(c *gin.Context) *systemReq.CustomClaims {
|
|||||||
//
|
//
|
||||||
// 返回值:
|
// 返回值:
|
||||||
// - uint: 返回用户的 ID,如果获取失败则返回 0。
|
// - uint: 返回用户的 ID,如果获取失败则返回 0。
|
||||||
func GetUserId(c *gin.Context) uint {
|
func GetUserId(c *gin.Context) string {
|
||||||
if claims, exists := c.Get("claims"); !exists {
|
if claims, exists := c.Get("claims"); !exists {
|
||||||
if cl, err := GetClaims(c); err != nil {
|
if cl, err := GetClaims(c); err != nil {
|
||||||
return 0
|
return "0"
|
||||||
} else {
|
} else {
|
||||||
return cl.BaseClaims.ID
|
return cl.BaseClaims.ID
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-5
@@ -1,14 +1,20 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sundynix-go/utils/uniqueid"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHashPwd(t *testing.T) {
|
func TestHashPwd(t *testing.T) {
|
||||||
//hash := BcryptHash("admin")
|
hash := BcryptHash("sundynix")
|
||||||
//fmt.Println(hash) // $2a$10$QC/zkQ/ohPmvjF/goDyicu7cHgAEj8gHg6OTDHWhbYQMHHn4dwxX2
|
fmt.Println(hash) // $2a$10$QC/zkQ/ohPmvjF/goDyicu7cHgAEj8gHg6OTDHWhbYQMHHn4dwxX2
|
||||||
//
|
|
||||||
//check := BcryptCheck("admin", "$2a$10$QC/zkQ/ohPmvjF/goDyicu7cHgAEj8gHg6OTDHWhbYQMHHn4dwxX2")
|
|
||||||
//fmt.Println(check)
|
|
||||||
|
|
||||||
|
check := BcryptCheck("admin", "$2a$10$QC/zkQ/ohPmvjF/goDyicu7cHgAEj8gHg6OTDHWhbYQMHHn4dwxX2")
|
||||||
|
fmt.Println(check)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUuid(t *testing.T) {
|
||||||
|
id := uniqueid.GenerateId()
|
||||||
|
fmt.Println(id)
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -2,10 +2,11 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/golang-jwt/jwt/v5"
|
|
||||||
"sundynix-go/global"
|
"sundynix-go/global"
|
||||||
"sundynix-go/model/system/request"
|
"sundynix-go/model/system/request"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/golang-jwt/jwt/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
type JWT struct {
|
type JWT struct {
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package uniqueid
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GenerateId() string {
|
||||||
|
uuidV1, err := uuid.NewUUID()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return uuidV1.String()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user