You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.1 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package scripts
import (
"fmt"
"mygo/dbhelper"
"mygo/libs"
"mygo/model"
"mygo/response"
"mygo/say"
"mygo/validator"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
)
//获取角色授权信息
func GetRoutesByRoleId(c echo.Context) error {
fmt.Println("获取角色授权信息 ")
id := c.QueryParam("id")
role_id := c.QueryParam("role_id")
data := response.RoleRoutesResponse{}
if id == "" || role_id == "" {
return say.Success(c, 10001, data)
}
db := dbhelper.GetMySqlDB()
//获取角色对应路由列表
ids, _ := model.GetRouteIdsByRoleId(db, role_id)
routes, err := model.GetRoleRoutes(db, ids)
if err != nil {
return say.Success(c, 10000, data)
}
data.Routes = routes
return say.Success(c, 0, data)
}
//角色授权
func RoleBindRoute(c echo.Context) error {
id := c.QueryParam("id")
data := response.NoneResponse{}
body := &validator.RoleBindRouteBody{}
err := c.Bind(body)
if err != nil {
return say.Success(c, 10000, data)
}
if id == "" || len(body.Routes) <= 0 || body.RoleID == "" {
return say.Success(c, 10001, data)
}
db := dbhelper.GetMySqlDB()
role_id := body.RoleID
role, err := model.GetRoleById(db, role_id)
if err != nil || role.ID == "" {
return say.Success(c, 10006, data)
}
//启动事务
txerr := db.Transaction(func(tx *gorm.DB) error {
// 在事务中执行一些 db 操作(从这里开始,您应该使用 'tx' 而不是 'db'
//批量删除存在的授权
rr := model.RoleRoute{}
if err := tx.Debug().Where("role_id = ? and deleted_at is NULL", role_id).Delete(&rr).Error; err != nil {
// 返回任何错误都会回滚事务
return err
}
for _, route_id := range body.Routes {
r, err := model.GetRoleRoute(tx, role_id, route_id)
if err != nil {
return err
}
if r.ID != "" {
continue
}
id := libs.GenId()
route := model.RoleRoute{
ID: id,
RouteID: route_id,
RoleID: role_id,
}
if err := tx.Create(&route).Error; err != nil {
// 返回任何错误都会回滚事务
return err
}
}
// 返回 nil 提交事务
return nil
})
if txerr != nil {
return say.Success(c, 10000, data)
}
return say.Success(c, 0, data)
}