12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package settings
- import (
- "errors"
- "fmt"
- "github.com/astaxie/beego/orm"
- "nginx-ui/server/models"
- "strings"
- )
- type RouteService struct {
- }
- // Save 保存或者修改
- func (c *RouteService) Save(body *models.SettingRoute) (*models.SettingRoute, error) {
- o := orm.NewOrm()
- _, err := models.InsertOrUpdate[models.SettingRoute](o, body)
- return body, err
- }
- // GetList 获取指定用户的全部路由
- // Uid为0表示系统菜单,大于0表示用户个人菜单
- func (c *RouteService) GetList(vo RouteListVo) ([]models.SettingRoute, error) {
- o := orm.NewOrm()
- qs := o.QueryTable(&models.SettingRoute{})
- if vo.Uid > -1 {
- qs = qs.FilterRaw("Uid", fmt.Sprintf("=%d", vo.Uid))
- }
- if vo.NonType != "" {
- qs = qs.FilterRaw("Type", fmt.Sprintf("<>'%s'", vo.NonType))
- }
- qs = qs.OrderBy("Uid", "-SortNum")
- var list []models.SettingRoute
- _, err := qs.All(&list)
- if err != nil {
- return nil, err
- }
- return list, nil
- }
- // GetUserRoutes 获取用户的菜单
- func (c *RouteService) GetUserRoutes(user *models.User) ([]models.SettingRoute, error) {
- o := orm.NewOrm()
- qs := o.QueryTable(&models.SettingRoute{})
- qs = qs.FilterRaw("Uid", fmt.Sprintf("=%d OR uid=0", user.Id))
- qs = qs.OrderBy("-SortNum")
- var list []models.SettingRoute
- _, err := qs.All(&list)
- if err != nil {
- return nil, err
- }
- if !user.IsAdmin() {
- userRoles := strings.Split(user.Roles, ",")
- var routes []models.SettingRoute
- for _, route := range list {
- if route.Roles == "" || strings.Contains(route.Roles, "USER") {
- routes = append(routes, route)
- continue
- }
- for _, role := range userRoles {
- if strings.Contains(route.Roles, role) {
- routes = append(routes, route)
- continue
- }
- }
- }
- return routes, nil
- }
- return list, nil
- }
- // Delete 获取全部组织
- func (c *RouteService) Delete(id string) error {
- o := orm.NewOrm()
- _, err := o.Delete(&models.SettingRoute{Id: id})
- return err
- }
- // InsertWhenNotExist 插入如果当ID不存在时
- func (c *RouteService) InsertWhenNotExist(route models.SettingRoute) error {
- o := orm.NewOrm()
- err := o.Read(&route, "Id")
- if err != nil && errors.Is(err, orm.ErrNoRows) {
- _, err = o.Insert(&route)
- }
- return err
- }
- var Route = new(RouteService)
|