From a61566a3194840e389f649c4d5ceb65b8747f730 Mon Sep 17 00:00:00 2001 From: yxh Date: Tue, 12 Apr 2022 18:14:30 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=A2=9E=E5=88=A0=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/v1/common/req.go | 2 +- api/v1/system/sys_user.go | 77 +++++- internal/app/system/controller/sys_user.go | 35 ++- .../app/system/model/entity/sys_user_post.go | 11 + internal/app/system/model/sys_user.go | 22 +- .../internal/dao/internal/sys_user_post.go | 74 ++++++ .../service/internal/dao/sys_user_post.go | 27 ++ .../service/internal/do/sys_user_post.go | 16 ++ internal/app/system/service/sys_dept.go | 1 + internal/app/system/service/sys_post.go | 12 + internal/app/system/service/sys_user.go | 240 +++++++++++++++++- manifest/config/config.yaml | 2 +- 12 files changed, 493 insertions(+), 26 deletions(-) create mode 100644 internal/app/system/model/entity/sys_user_post.go create mode 100644 internal/app/system/service/internal/dao/internal/sys_user_post.go create mode 100644 internal/app/system/service/internal/dao/sys_user_post.go create mode 100644 internal/app/system/service/internal/do/sys_user_post.go diff --git a/api/v1/common/req.go b/api/v1/common/req.go index f944af5..45b25e5 100644 --- a/api/v1/common/req.go +++ b/api/v1/common/req.go @@ -7,7 +7,7 @@ package common -// +// PageReq 公共请求参数 type PageReq struct { BeginTime string `p:"beginTime"` //开始时间 EndTime string `p:"endTime"` //结束时间 diff --git a/api/v1/system/sys_user.go b/api/v1/system/sys_user.go index 15d9d7d..88ee32c 100644 --- a/api/v1/system/sys_user.go +++ b/api/v1/system/sys_user.go @@ -4,6 +4,7 @@ import ( "github.com/gogf/gf/v2/frame/g" commonApi "github.com/tiger1103/gfast/v3/api/v1/common" "github.com/tiger1103/gfast/v3/internal/app/system/model" + "github.com/tiger1103/gfast/v3/internal/app/system/model/entity" ) type UserLoginReq struct { @@ -36,10 +37,10 @@ type UserMenusRes struct { // UserSearchReq 用户搜索请求参数 type UserSearchReq struct { g.Meta `path:"/user/list" tags:"用户管理" method:"get" summary:"用户列表"` - DeptIds []int64 //所属部门id数据 - Mobile string `p:"mobile"` - Status string `p:"status"` - KeyWords string `p:"keyWords"` + DeptId string `p:"deptId"` //部门id + Mobile string `p:"mobile"` + Status string `p:"status"` + KeyWords string `p:"keyWords"` commonApi.PageReq } @@ -48,3 +49,71 @@ type UserSearchRes struct { UserList []*model.SysUserRoleDeptRes `json:"userList"` commonApi.ListRes } + +type UserGetParamsReq struct { + g.Meta `path:"/user/params" tags:"用户管理" method:"get" summary:"用户维护参数获取"` +} + +type UserGetParamsRes struct { + g.Meta `mime:"application/json"` + RoleList []*entity.SysRole `json:"roleList"` + Posts []*entity.SysPost `json:"posts"` +} + +// SetUserReq 添加修改用户公用请求字段 +type SetUserReq struct { + DeptId uint64 `p:"deptId" v:"required#用户部门不能为空"` //所属部门 + Email string `p:"email" v:"email#邮箱格式错误"` //邮箱 + NickName string `p:"nickName" v:"required#用户昵称不能为空"` + Mobile string `p:"mobile" v:"required|phone#手机号不能为空|手机号格式错误"` + PostIds []int64 `p:"postIds"` + Remark string `p:"remark"` + RoleIds []int64 `p:"roleIds"` + Sex int `p:"sex"` + Status uint `p:"status"` + IsAdmin int `p:"isAdmin"` // 是否后台管理员 1 是 0 否 +} + +// UserAddReq 添加用户参数 +type UserAddReq struct { + g.Meta `path:"/user/add" tags:"用户管理" method:"post" summary:"添加用户"` + *SetUserReq + UserName string `p:"userName" v:"required#用户账号不能为空"` + Password string `p:"password" v:"required|password#密码不能为空|密码以字母开头,只能包含字母、数字和下划线,长度在6~18之间"` + UserSalt string +} + +type UserAddRes struct { +} + +// UserEditReq 修改用户参数 +type UserEditReq struct { + g.Meta `path:"/user/edit" tags:"用户管理" method:"put" summary:"修改用户"` + *SetUserReq + UserId int64 `p:"userId" v:"required#用户id不能为空"` +} + +type UserEditRes struct { +} + +type UserGetEditReq struct { + g.Meta `path:"/user/getEdit" tags:"用户管理" method:"get" summary:"获取用户信息"` + Id uint64 `p:"id"` +} + +type UserGetEditRes struct { + g.Meta `mime:"application/json"` + User *entity.SysUser `json:"user"` + CheckedRoleIds []uint `json:"checkedRoleIds"` + CheckedPosts []int64 `json:"checkedPosts"` +} + +// UserResetPwdReq 重置用户密码状态参数 +type UserResetPwdReq struct { + g.Meta `path:"/user/resetPwd" tags:"用户管理" method:"put" summary:"重置用户密码"` + Id uint64 `p:"userId" v:"required#用户id不能为空"` + Password string `p:"password" v:"required|password#密码不能为空|密码以字母开头,只能包含字母、数字和下划线,长度在6~18之间"` +} + +type UserResetPwdRes struct { +} diff --git a/internal/app/system/controller/sys_user.go b/internal/app/system/controller/sys_user.go index 8455a79..55e8a86 100644 --- a/internal/app/system/controller/sys_user.go +++ b/internal/app/system/controller/sys_user.go @@ -110,12 +110,45 @@ func (c *userController) List(ctx context.Context, req *system.UserSearchReq) (r total int userList []*entity.SysUser ) + res = new(system.UserSearchRes) total, userList, err = service.User().List(ctx, req) if err != nil || total == 0 { return } - res = new(system.UserSearchRes) res.Total = total res.UserList, err = service.User().GetUsersRoleDept(ctx, userList) return } + +// GetParams 获取用户维护相关参数 +func (c *userController) GetParams(ctx context.Context, req *system.UserGetParamsReq) (res *system.UserGetParamsRes, err error) { + res = new(system.UserGetParamsRes) + res.RoleList, err = service.Role().GetRoleList(ctx) + if err != nil { + return + } + res.Posts, err = service.Post().GetUsedPost(ctx) + return +} + +// Add 添加用户 +func (c *userController) Add(ctx context.Context, req *system.UserAddReq) (res *system.UserAddRes, err error) { + err = service.User().Add(ctx, req) + return +} + +// GetEditUser 获取修改用户信息 +func (c *userController) GetEditUser(ctx context.Context, req *system.UserGetEditReq) (res *system.UserGetEditRes, err error) { + res, err = service.User().GetEditUser(ctx, req.Id) + return +} + +func (c *userController) Edit(ctx context.Context, req *system.UserEditReq) (res *system.UserEditRes, err error) { + err = service.User().Edit(ctx, req) + return +} + +func (c *userController) ResetPwd(ctx context.Context, req *system.UserResetPwdReq) (res *system.UserResetPwdRes, err error) { + err = service.User().ResetUserPwd(ctx, req) + return +} diff --git a/internal/app/system/model/entity/sys_user_post.go b/internal/app/system/model/entity/sys_user_post.go new file mode 100644 index 0000000..8de724c --- /dev/null +++ b/internal/app/system/model/entity/sys_user_post.go @@ -0,0 +1,11 @@ +// ================================================================================= +// Code generated by GoFrame CLI tool. DO NOT EDIT. +// ================================================================================= + +package entity + +// SysUserPost is the golang structure for table sys_user_post. +type SysUserPost struct { + UserId int64 `json:"userId" description:"用户ID"` + PostId int64 `json:"postId" description:"岗位ID"` +} diff --git a/internal/app/system/model/sys_user.go b/internal/app/system/model/sys_user.go index 07b3e92..f8ac245 100644 --- a/internal/app/system/model/sys_user.go +++ b/internal/app/system/model/sys_user.go @@ -25,13 +25,17 @@ type LoginUserRes struct { // SysUserRoleDeptRes 带有部门、角色、岗位信息的用户数据 type SysUserRoleDeptRes struct { *entity.SysUser - Dept *entity.SysDept `json:"dept"` - RoleInfo []*struct { - RoleId uint `json:"roleId"` - Name string `json:"name"` - } `json:"roleInfo"` - Post []*struct { - PostId int64 `json:"postId"` - PostName string `json:"postName"` - } `json:"post"` + Dept *entity.SysDept `json:"dept"` + RoleInfo []*SysUserRoleInfoRes `json:"roleInfo"` + Post []*SysUserPostInfoRes `json:"post"` +} + +type SysUserRoleInfoRes struct { + RoleId uint `json:"roleId"` + Name string `json:"name"` +} + +type SysUserPostInfoRes struct { + PostId int64 `json:"postId"` + PostName string `json:"postName"` } diff --git a/internal/app/system/service/internal/dao/internal/sys_user_post.go b/internal/app/system/service/internal/dao/internal/sys_user_post.go new file mode 100644 index 0000000..73e08ce --- /dev/null +++ b/internal/app/system/service/internal/dao/internal/sys_user_post.go @@ -0,0 +1,74 @@ +// ========================================================================== +// Code generated by GoFrame CLI tool. DO NOT EDIT. +// ========================================================================== + +package internal + +import ( + "context" + "github.com/gogf/gf/v2/database/gdb" + "github.com/gogf/gf/v2/frame/g" +) + +// SysUserPostDao is the data access object for table sys_user_post. +type SysUserPostDao struct { + table string // table is the underlying table name of the DAO. + group string // group is the database configuration group name of current DAO. + columns SysUserPostColumns // columns contains all the column names of Table for convenient usage. +} + +// SysUserPostColumns defines and stores column names for table sys_user_post. +type SysUserPostColumns struct { + UserId string // 用户ID + PostId string // 岗位ID +} + +// sysUserPostColumns holds the columns for table sys_user_post. +var sysUserPostColumns = SysUserPostColumns{ + UserId: "user_id", + PostId: "post_id", +} + +// NewSysUserPostDao creates and returns a new DAO object for table data access. +func NewSysUserPostDao() *SysUserPostDao { + return &SysUserPostDao{ + group: "default", + table: "sys_user_post", + columns: sysUserPostColumns, + } +} + +// DB retrieves and returns the underlying raw database management object of current DAO. +func (dao *SysUserPostDao) DB() gdb.DB { + return g.DB(dao.group) +} + +// Table returns the table name of current dao. +func (dao *SysUserPostDao) Table() string { + return dao.table +} + +// Columns returns all column names of current dao. +func (dao *SysUserPostDao) Columns() SysUserPostColumns { + return dao.columns +} + +// Group returns the configuration group name of database of current dao. +func (dao *SysUserPostDao) Group() string { + return dao.group +} + +// Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation. +func (dao *SysUserPostDao) Ctx(ctx context.Context) *gdb.Model { + return dao.DB().Model(dao.table).Safe().Ctx(ctx) +} + +// Transaction wraps the transaction logic using function f. +// It rollbacks the transaction and returns the error from function f if it returns non-nil error. +// It commits the transaction and returns nil if function f returns nil. +// +// Note that, you should not Commit or Rollback the transaction in function f +// as it is automatically handled by this function. +func (dao *SysUserPostDao) Transaction(ctx context.Context, f func(ctx context.Context, tx *gdb.TX) error) (err error) { + return dao.Ctx(ctx).Transaction(ctx, f) +} diff --git a/internal/app/system/service/internal/dao/sys_user_post.go b/internal/app/system/service/internal/dao/sys_user_post.go new file mode 100644 index 0000000..0a6574e --- /dev/null +++ b/internal/app/system/service/internal/dao/sys_user_post.go @@ -0,0 +1,27 @@ +// ================================================================================= +// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish. +// ================================================================================= + +package dao + +import ( + "github.com/tiger1103/gfast/v3/internal/app/system/service/internal/dao/internal" +) + +// internalSysUserPostDao is internal type for wrapping internal DAO implements. +type internalSysUserPostDao = *internal.SysUserPostDao + +// sysUserPostDao is the data access object for table sys_user_post. +// You can define custom methods on it to extend its functionality as you wish. +type sysUserPostDao struct { + internalSysUserPostDao +} + +var ( + // SysUserPost is globally public accessible object for table sys_user_post operations. + SysUserPost = sysUserPostDao{ + internal.NewSysUserPostDao(), + } +) + +// Fill with you ideas below. diff --git a/internal/app/system/service/internal/do/sys_user_post.go b/internal/app/system/service/internal/do/sys_user_post.go new file mode 100644 index 0000000..a06c02d --- /dev/null +++ b/internal/app/system/service/internal/do/sys_user_post.go @@ -0,0 +1,16 @@ +// ================================================================================= +// Code generated by GoFrame CLI tool. DO NOT EDIT. +// ================================================================================= + +package do + +import ( + "github.com/gogf/gf/v2/frame/g" +) + +// SysUserPost is the golang structure of table sys_user_post for DAO operations like Where/Data. +type SysUserPost struct { + g.Meta `orm:"table:sys_user_post, do:true"` + UserId interface{} // 用户ID + PostId interface{} // 岗位ID +} diff --git a/internal/app/system/service/sys_dept.go b/internal/app/system/service/sys_dept.go index d12213f..447ff3e 100644 --- a/internal/app/system/service/sys_dept.go +++ b/internal/app/system/service/sys_dept.go @@ -29,6 +29,7 @@ type IDept interface { GetFromCache(ctx context.Context) (list []*entity.SysDept, err error) Delete(ctx context.Context, id int64) (err error) GetListTree(pid int64, list []*entity.SysDept) (deptTree []*model.SysDeptTreeRes) + FindSonByParentId(deptList []*entity.SysDept, deptId int64) []*entity.SysDept } var deptService = deptImpl{} diff --git a/internal/app/system/service/sys_post.go b/internal/app/system/service/sys_post.go index 04bc550..f698d4b 100644 --- a/internal/app/system/service/sys_post.go +++ b/internal/app/system/service/sys_post.go @@ -13,6 +13,7 @@ import ( "github.com/gogf/gf/v2/util/gconv" "github.com/tiger1103/gfast/v3/api/v1/system" "github.com/tiger1103/gfast/v3/internal/app/system/consts" + "github.com/tiger1103/gfast/v3/internal/app/system/model/entity" "github.com/tiger1103/gfast/v3/internal/app/system/service/internal/dao" "github.com/tiger1103/gfast/v3/internal/app/system/service/internal/do" "github.com/tiger1103/gfast/v3/library/liberr" @@ -23,6 +24,7 @@ type IPost interface { Add(ctx context.Context, req *system.PostAddReq) (err error) Edit(ctx context.Context, req *system.PostEditReq) (err error) Delete(ctx context.Context, ids []int) (err error) + GetUsedPost(ctx context.Context) (list []*entity.SysPost, err error) } type postImpl struct { @@ -102,3 +104,13 @@ func (s *postImpl) Delete(ctx context.Context, ids []int) (err error) { }) return } + +// GetUsedPost 获取正常状态的岗位 +func (s *postImpl) GetUsedPost(ctx context.Context) (list []*entity.SysPost, err error) { + err = g.Try(func() { + err = dao.SysPost.Ctx(ctx).Where(dao.SysPost.Columns().Status, 1). + Order(dao.SysPost.Columns().PostSort + " ASC, " + dao.SysPost.Columns().PostId + " ASC ").Scan(&list) + liberr.ErrIsNil(ctx, err, "获取岗位数据失败") + }) + return +} diff --git a/internal/app/system/service/sys_user.go b/internal/app/system/service/sys_user.go index 2475f49..15a2bf4 100644 --- a/internal/app/system/service/sys_user.go +++ b/internal/app/system/service/sys_user.go @@ -11,14 +11,16 @@ import ( "context" "fmt" "github.com/gogf/gf/v2/container/gset" + "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/text/gstr" "github.com/gogf/gf/v2/util/gconv" + "github.com/gogf/gf/v2/util/grand" "github.com/mssola/user_agent" "github.com/tiger1103/gfast/v3/api/v1/system" - "github.com/tiger1103/gfast/v3/internal/app/common/service" + commonService "github.com/tiger1103/gfast/v3/internal/app/common/service" "github.com/tiger1103/gfast/v3/internal/app/system/consts" "github.com/tiger1103/gfast/v3/internal/app/system/model" "github.com/tiger1103/gfast/v3/internal/app/system/model/entity" @@ -36,6 +38,10 @@ type IUser interface { GetAdminRules(ctx context.Context, userId uint64) (menuList []*model.UserMenus, permissions []string, err error) List(ctx context.Context, req *system.UserSearchReq) (total int, userList []*entity.SysUser, err error) GetUsersRoleDept(ctx context.Context, userList []*entity.SysUser) (users []*model.SysUserRoleDeptRes, err error) + Add(ctx context.Context, req *system.UserAddReq) (err error) + GetEditUser(ctx context.Context, id uint64) (res *system.UserGetEditRes, err error) + Edit(ctx context.Context, req *system.UserEditReq) (err error) + ResetUserPwd(ctx context.Context, req *system.UserResetPwdReq) (err error) } type userImpl struct{} @@ -179,7 +185,7 @@ func (s *userImpl) GetAdminRole(ctx context.Context, userId uint64, allRoleList // GetAdminRoleIds 获取用户角色ids func (s *userImpl) GetAdminRoleIds(ctx context.Context, userId uint64) (roleIds []uint, err error) { - enforcer, e := service.CasbinEnforcer(ctx) + enforcer, e := commonService.CasbinEnforcer(ctx) if e != nil { err = e return @@ -216,7 +222,7 @@ func (s *userImpl) GetAllMenus(ctx context.Context) (menus []*model.UserMenus, e func (s *userImpl) GetAdminMenusByRoleIds(ctx context.Context, roleIds []uint) (menus []*model.UserMenus, err error) { //获取角色对应的菜单id err = g.Try(func() { - enforcer, e := service.CasbinEnforcer(ctx) + enforcer, e := commonService.CasbinEnforcer(ctx) liberr.ErrIsNil(ctx, e) menuIds := map[int64]int64{} for _, roleId := range roleIds { @@ -280,7 +286,7 @@ func (s *userImpl) setMenuData(menu *model.UserMenu, entity *model.SysAuthRuleIn func (s *userImpl) GetPermissions(ctx context.Context, roleIds []uint) (userButtons []string, err error) { err = g.Try(func() { //获取角色对应的菜单id - enforcer, err := service.CasbinEnforcer(ctx) + enforcer, err := commonService.CasbinEnforcer(ctx) liberr.ErrIsNil(ctx, err) menuIds := map[int64]int64{} for _, roleId := range roleIds { @@ -312,8 +318,10 @@ func (s *userImpl) List(ctx context.Context, req *system.UserSearchReq) (total i keyWords := "%" + req.KeyWords + "%" m = m.Where("user_name like ? or user_nickname like ?", keyWords, keyWords) } - if len(req.DeptIds) != 0 { - m = m.Where("dept_id in (?)", req.DeptIds) + if req.DeptId != "" { + deptIds, e := s.getSearchDeptIds(ctx, gconv.Int64(req.DeptId)) + liberr.ErrIsNil(ctx, e) + m = m.Where("dept_id in (?)", deptIds) } if req.Status != "" { m = m.Where("user_status", gconv.Int(req.Status)) @@ -364,12 +372,224 @@ func (s *userImpl) GetUsersRoleDept(ctx context.Context, userList []*entity.SysU roles, e := s.GetAdminRole(ctx, u.Id, allRoles) liberr.ErrIsNil(ctx, e) for _, r := range roles { - users[k].RoleInfo = append(users[k].RoleInfo, &struct { - RoleId uint `json:"roleId"` - Name string `json:"name"` - }{RoleId: r.Id, Name: r.Name}) + users[k].RoleInfo = append(users[k].RoleInfo, &model.SysUserRoleInfoRes{RoleId: r.Id, Name: r.Name}) } } }) return } + +func (s *userImpl) getSearchDeptIds(ctx context.Context, deptId int64) (deptIds []int64, err error) { + err = g.Try(func() { + deptAll, e := Dept().GetFromCache(ctx) + liberr.ErrIsNil(ctx, e) + deptWithChildren := Dept().FindSonByParentId(deptAll, gconv.Int64(deptId)) + deptIds = make([]int64, len(deptWithChildren)) + for k, v := range deptWithChildren { + deptIds[k] = v.DeptId + } + deptIds = append(deptIds, deptId) + }) + return +} + +func (s *userImpl) Add(ctx context.Context, req *system.UserAddReq) (err error) { + err = s.userNameOrMobileExists(ctx, req.UserName, req.Mobile) + if err != nil { + return + } + req.UserSalt = grand.S(10) + req.Password = libUtils.EncryptPassword(req.Password, req.UserSalt) + err = g.DB().Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error { + err = g.Try(func() { + userId, e := dao.SysUser.Ctx(ctx).TX(tx).InsertAndGetId(do.SysUser{ + UserName: req.UserName, + Mobile: req.Mobile, + UserNickname: req.NickName, + UserPassword: req.Password, + UserSalt: req.UserSalt, + UserStatus: req.Status, + UserEmail: req.Email, + Sex: req.Sex, + DeptId: req.DeptId, + Remark: req.Remark, + IsAdmin: req.IsAdmin, + }) + liberr.ErrIsNil(ctx, e, "添加用户失败") + e = s.addUserRole(ctx, req.RoleIds, userId) + liberr.ErrIsNil(ctx, e, "设置用户权限失败") + e = s.AddUserPost(ctx, tx, req.PostIds, userId) + liberr.ErrIsNil(ctx, e) + }) + return err + }) + return +} + +func (s *userImpl) Edit(ctx context.Context, req *system.UserEditReq) (err error) { + err = s.userNameOrMobileExists(ctx, "", req.Mobile, req.UserId) + if err != nil { + return + } + err = g.DB().Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error { + err = g.Try(func() { + _, err = dao.SysUser.Ctx(ctx).TX(tx).WherePri(req.UserId).Update(do.SysUser{ + Mobile: req.Mobile, + UserNickname: req.NickName, + UserStatus: req.Status, + UserEmail: req.Email, + Sex: req.Sex, + DeptId: req.DeptId, + Remark: req.Remark, + IsAdmin: req.IsAdmin, + }) + liberr.ErrIsNil(ctx, err, "修改用户信息失败") + //设置用户所属角色信息 + err = s.EditUserRole(ctx, req.RoleIds, req.UserId) + liberr.ErrIsNil(ctx, err, "设置用户权限失败") + err = s.AddUserPost(ctx, tx, req.PostIds, req.UserId) + liberr.ErrIsNil(ctx, err) + }) + return err + }) + return +} + +// AddUserPost 添加用户岗位信息 +func (s *userImpl) AddUserPost(ctx context.Context, tx *gdb.TX, postIds []int64, userId int64) (err error) { + err = g.Try(func() { + //删除旧岗位信息 + _, err = dao.SysUserPost.Ctx(ctx).TX(tx).Where(dao.SysUserPost.Columns().UserId, userId).Delete() + liberr.ErrIsNil(ctx, err, "设置用户岗位失败") + if len(postIds) == 0 { + return + } + //添加用户岗位信息 + data := g.List{} + for _, v := range postIds { + data = append(data, g.Map{ + dao.SysUserPost.Columns().UserId: userId, + dao.SysUserPost.Columns().PostId: v, + }) + } + _, err = dao.SysUserPost.Ctx(ctx).TX(tx).Data(data).Insert() + liberr.ErrIsNil(ctx, err, "设置用户岗位失败") + }) + return +} + +// AddUserRole 添加用户角色信息 +func (s *userImpl) addUserRole(ctx context.Context, roleIds []int64, userId int64) (err error) { + err = g.Try(func() { + enforcer, e := commonService.CasbinEnforcer(ctx) + liberr.ErrIsNil(ctx, e) + for _, v := range roleIds { + _, e = enforcer.AddGroupingPolicy(gconv.String(userId), gconv.String(v)) + liberr.ErrIsNil(ctx, e) + } + }) + return +} + +// EditUserRole 修改用户角色信息 +func (s *userImpl) EditUserRole(ctx context.Context, roleIds []int64, userId int64) (err error) { + err = g.Try(func() { + enforcer, e := commonService.CasbinEnforcer(ctx) + liberr.ErrIsNil(ctx, e) + + //删除用户旧角色信息 + enforcer.RemoveFilteredGroupingPolicy(0, gconv.String(userId)) + for _, v := range roleIds { + _, err = enforcer.AddGroupingPolicy(gconv.String(userId), gconv.String(v)) + liberr.ErrIsNil(ctx, err) + } + }) + return +} + +func (s *userImpl) userNameOrMobileExists(ctx context.Context, userName, mobile string, id ...int64) error { + user := (*entity.SysUser)(nil) + err := g.Try(func() { + m := dao.SysUser.Ctx(ctx) + if len(id) > 0 { + m = m.Where(dao.SysUser.Columns().Id+" != ", id) + } + m = m.Where(fmt.Sprintf("%s='%s' OR %s='%s'", + dao.SysUser.Columns().UserName, + userName, + dao.SysUser.Columns().Mobile, + mobile)) + err := m.Limit(1).Scan(&user) + liberr.ErrIsNil(ctx, err, "获取用户信息失败") + if user == nil { + return + } + if user.UserName == userName { + liberr.ErrIsNil(ctx, gerror.New("用户名已存在")) + } + if user.Mobile == mobile { + liberr.ErrIsNil(ctx, gerror.New("手机号已存在")) + } + }) + return err +} + +// GetEditUser 获取编辑用户信息 +func (s *userImpl) GetEditUser(ctx context.Context, id uint64) (res *system.UserGetEditRes, err error) { + res = new(system.UserGetEditRes) + err = g.Try(func() { + //获取用户信息 + res.User, err = s.GetUserInfoById(ctx, id) + liberr.ErrIsNil(ctx, err) + //获取已选择的角色信息 + res.CheckedRoleIds, err = s.GetAdminRoleIds(ctx, id) + liberr.ErrIsNil(ctx, err) + res.CheckedPosts, err = s.GetUserPostIds(ctx, id) + liberr.ErrIsNil(ctx, err) + }) + return +} + +// GetUserInfoById 通过Id获取用户信息 +func (s *userImpl) GetUserInfoById(ctx context.Context, id uint64, withPwd ...bool) (user *entity.SysUser, err error) { + err = g.Try(func() { + if len(withPwd) > 0 && withPwd[0] { + //用户用户信息 + err = dao.SysUser.Ctx(ctx).Where(dao.SysUser.Columns().Id, id).Scan(&user) + } else { + //用户用户信息 + err = dao.SysUser.Ctx(ctx).Where(dao.SysUser.Columns().Id, id). + FieldsEx(dao.SysUser.Columns().UserPassword, dao.SysUser.Columns().UserSalt).Scan(&user) + } + liberr.ErrIsNil(ctx, err, "获取用户数据失败") + }) + return +} + +// GetUserPostIds 获取用户岗位 +func (s *userImpl) GetUserPostIds(ctx context.Context, userId uint64) (postIds []int64, err error) { + err = g.Try(func() { + var list []*entity.SysUserPost + err = dao.SysUserPost.Ctx(ctx).Where(dao.SysUserPost.Columns().UserId, userId).Scan(&list) + liberr.ErrIsNil(ctx, err, "获取用户岗位信息失败") + postIds = make([]int64, 0) + for _, entity := range list { + postIds = append(postIds, entity.PostId) + } + }) + return +} + +// ResetUserPwd 重置用户密码 +func (s *userImpl) ResetUserPwd(ctx context.Context, req *system.UserResetPwdReq) (err error) { + salt := grand.S(10) + password := libUtils.EncryptPassword(req.Password, salt) + err = g.Try(func() { + _, err = dao.SysUser.Ctx(ctx).WherePri(req.Id).Update(g.Map{ + dao.SysUser.Columns().UserSalt: salt, + dao.SysUser.Columns().UserPassword: password, + }) + liberr.ErrIsNil(ctx, err, "重置用户密码失败") + }) + return +} diff --git a/manifest/config/config.yaml b/manifest/config/config.yaml index 73f7fa7..662ae12 100644 --- a/manifest/config/config.yaml +++ b/manifest/config/config.yaml @@ -76,7 +76,7 @@ gfcli: gen: dao: - link: "mysql:root:123456@tcp(127.0.0.1:3306)/gfast-v3" - tables: "sys_post" + tables: "sys_user_post" removePrefix: "gf_" descriptionTag: true noModelComment: true