Skip to content

Commit

Permalink
aa
Browse files Browse the repository at this point in the history
  • Loading branch information
xufqing committed Nov 6, 2020
1 parent b53abe4 commit 04843ff
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 41 deletions.
19 changes: 15 additions & 4 deletions api/v1/system/sys_dept.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@ import (

// 查询所有部门
func GetDepts(c *gin.Context) {
// 绑定参数
var req request.DeptListReq
err := c.Bind(&req)
if err != nil {
response.FailWithCode(response.ParmError)
return
}
// 创建服务
s := service.New(c)
depts := s.GetDepts()
var resp []response.DeptTreeResp
resp = service.GenDeptTree(nil,depts)
response.SuccessWithData(resp)
depts := s.GetDepts(&req)
if (req.Name != "" || req.Status != nil){
response.SuccessWithData(depts)
} else {
var resp []response.DeptTreeResp
resp = service.GenDeptTree(nil,depts)
response.SuccessWithData(resp)
}
}

// 创建部门
Expand Down
2 changes: 0 additions & 2 deletions api/v1/system/sys_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ func GetRoles(c *gin.Context) {
for _, role := range roles {
var item response.RoleListResp
item.Id = role.Id
item.Key = fmt.Sprintf("%d", role.Id)
item.Title = role.Name
item.Name = role.Name
item.Keyword = role.Keyword
item.Desc = role.Desc
Expand Down
8 changes: 4 additions & 4 deletions api/v1/system/sys_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,23 +253,23 @@ func UserAvatarUpload(c *gin.Context) {
response.FailWithMsg("无法读取文件")
return
}
fileName := utils.CreateRandomString(8) + path.Ext(file.Filename)
user, _ := GetCurrentUser(c)
fileName := user.Username+ "_avatar" + path.Ext(file.Filename)
imgPath := common.Conf.Upload.SaveDir + "/avatar/" + fileName
err = c.SaveUploadedFile(file, imgPath)
if err != nil {
response.FailWithMsg(err.Error())
return
}
// 将头像url保存到数据库
user, _ := GetCurrentUser(c)
query := common.Mysql.Where("username = ?", user.Username).First(&user)
err = query.Update("avatar", imgPath).Error
err = query.Update("avatar", "/" + imgPath).Error
if err != nil {
response.FailWithMsg(err.Error())
}
resp := map[string]string{
"name": fileName,
"url": imgPath,
"url": "/" + imgPath,
}

response.SuccessWithData(resp)
Expand Down
5 changes: 5 additions & 0 deletions dto/request/sys_dept.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ type CreateDeptReq struct {
Creator string `json:"creator"`
}

type DeptListReq struct {
Name string `json:"name" form:"name"`
Status *bool `json:"status" form:"status"`
}

// 翻译需要校验的字段名称
func (s CreateDeptReq) FieldTrans() map[string]string {
m := make(map[string]string, 0)
Expand Down
6 changes: 2 additions & 4 deletions dto/request/sys_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type CreateUserReq struct {
Email string `json:"email"`
Status *bool `json:"status"`
DeptId uint `json:"deptId"`
Roles []string `json:"roles" validate:"required"` // 可绑定多个角色
Roles []uint `json:"roles" validate:"required"` // 可绑定多个角色
Creator string `json:"creator"`
}

Expand All @@ -52,12 +52,11 @@ type UpdateUserBaseInfoReq struct {

// 修改用户结构体
type UpdateUserReq struct {
Username string `json:"username"`
Mobile string `json:"mobile"`
Name string `json:"name" validate:"required"`
Email string `json:"email"`
Password string `json:"password"`
Roles []string `json:"roles"` // 可绑定多个角色
Roles []uint `json:"roles"` // 可绑定多个角色
DeptId uint `json:"deptId"`
Status *bool `json:"status"`
}
Expand All @@ -80,7 +79,6 @@ func (s ChangePwdReq) FieldTrans() map[string]string {

func (s UpdateUserReq) FieldTrans() map[string]string {
m := make(map[string]string, 0)
m["Username"] = "用户名"
m["Name"] = "姓名"
return m
}
Expand Down
3 changes: 0 additions & 3 deletions dto/response/sys_dept.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ package response
type DeptTreeResp struct {
Id uint `json:"id"`
ParentId uint `json:"parentId"`
Key string `json:"key"`
Value string `json:"value"`
Name string `json:"name"`
Title string `json:"title"`
Creator string `json:"creator"`
Status bool `json:"status"`
Sort int `json:"sort"`
Expand Down
2 changes: 0 additions & 2 deletions dto/response/sys_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ type RolePermsStrResp struct {
// 角色信息响应, 字段含义见models
type RoleListResp struct {
Id uint `json:"id"`
Key string `json:"key"`
Name string `json:"name"`
Title string `json:"title"`
Keyword string `json:"keyword"`
Desc string `json:"desc"`
Status *bool `json:"status"`
Expand Down
2 changes: 0 additions & 2 deletions dto/response/sys_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ type LoginResp struct {
type UserRolesResp struct {
Id uint `json:"id"`
Name string `json:"name"`
Key string `json:"key"`
Title string `json:"title"`
Desc string `json:"desc"`
Keyword string `json:"keyword"`
Status *bool `json:"status"`
Expand Down
33 changes: 19 additions & 14 deletions dto/service/sys_dept.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,32 @@ import (
"anew-server/dto/request"
"anew-server/dto/response"
"anew-server/models"
"anew-server/pkg/common"
"anew-server/pkg/utils"
"github.com/gin-gonic/gin"
"sort"
"fmt"
"errors"
"strings"
)

// 获取所有部门信息
func (s *MysqlService) GetDepts() []models.SysDept {
//tree := make([]response.DeptTreeResp , 0)
depts := s.getAllDept()
func (s *MysqlService) GetDepts(req *request.DeptListReq) []models.SysDept {
depts := make([]models.SysDept, 0)
db := common.Mysql
name := strings.TrimSpace(req.Name)
if name != "" {
db = db.Where("name LIKE ?", fmt.Sprintf("%%%s%%", name))
}
status := req.Status
if status != nil {
if *status {
db = db.Where("status = ?", 1)
} else {
db = db.Where("status = ?", 0)
}
}
db.Order("sort").Find(&depts)
// 生成菜单树
//tree = GenDeptTree(nil, depts)
return depts
Expand All @@ -31,10 +46,6 @@ func GenDeptTree(parent *response.DeptTreeResp, depts []models.SysDept) []respon
parentId = parent.Id
}
for _, dept := range resp {
// 增加key值
dept.Key = fmt.Sprintf("%d",dept.Id)
dept.Value = fmt.Sprintf("%d",dept.Id)
dept.Title = dept.Name
// 父菜单编号一致
if dept.ParentId == parentId {
// 递归获取子菜单
Expand All @@ -48,12 +59,6 @@ func GenDeptTree(parent *response.DeptTreeResp, depts []models.SysDept) []respon
return tree
}

// 获取部门信息,非树列表
func (s *MysqlService) getAllDept() []models.SysDept {
depts := make([]models.SysDept, 0)
s.tx.Order("sort").Find(&depts)
return depts
}

// 创建部门
func (s *MysqlService) CreateDept(req *request.CreateDeptReq) (err error) {
Expand Down Expand Up @@ -81,7 +86,7 @@ func (s *MysqlService) UpdateDeptById(id uint, req gin.H) (err error) {
return
}

// 批量删除菜单
// 批量删除部门
func (s *MysqlService) DeleteDeptByIds(ids []uint) (err error) {
var dept models.SysDept
// 先解除父级关联
Expand Down
8 changes: 4 additions & 4 deletions middleware/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func PermsMiddleware(c *gin.Context) {
for _,role := range roleId{
roleData,_ := s.GetPermsByRoleId(role)
//如果是管理员直接放行
//if roleData.Keyword == "admin" {
// c.Next()
// return
//}
if roleData.Keyword == "admin" {
c.Next()
return
}
for _,api := range roleData.Apis{
permsList = append(permsList,api)
}
Expand Down
4 changes: 2 additions & 2 deletions models/sys_dept.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ type SysDept struct {
Name string `gorm:"comment:'部门名称';size:128" json:"name"`
Status *bool `gorm:"type:tinyint(1);default:1;comment:'菜单状态(正常/禁用, 默认正常)'" json:"status"` // 由于设置了默认值, 这里使用ptr, 可避免赋值失败
Creator string `gorm:"comment:'创建人';size:128" json:"creator"`
Sort int `gorm:"type:int(3);comment:'菜单顺序(同级菜单, 从0开始, 越小显示越靠前)'" json:"sort"`
ParentId uint `gorm:"default:0;comment:'父菜单编号(编号为0时表示根菜单)'" json:"parentId"`
Sort int `gorm:"type:int(3);comment:'排序'" json:"sort"`
ParentId uint `gorm:"default:0;comment:'父级部门(编号为0时表示根)'" json:"parentId"`
Children []SysDept `gorm:"-" json:"children"` // 下属部门集合
Users []SysUser `gorm:"foreignkey:DeptId"` // 一个部门有多个user
}
Expand Down

0 comments on commit 04843ff

Please sign in to comment.