作者 yangfu

管理员 菜单列表修改

package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"reflect"
"strings"
"time"
)
type CreateAdminUserCommand struct {
OperateInfo *domain.OperateInfo `json:"-"`
// 企业id
CompanyId int64 `cname:"企业id" json:"companyId"`
// 用户类型 1:企业内部用户(内部添加) 2:共创用户 1024:企业注册用户(注册添加)
UserType int `cname:"用户类型 1:企业内部用户(内部添加) 2:共创用户 1024:企业注册用户(注册添加)" json:"userType" valid:"Required"`
// 用户编号 企业内标识
UserCode string `cname:"用户编号 企业内标识" json:"userCode"`
// 组织机构
OrganizationId int64 `cname:"组织机构" json:"organizationId,omitempty"`
// 所属部门
DepartmentId int64 `cname:"所属部门" json:"departmentId,omitempty"`
// 用户关联的组织
UserOrg []int64 `cname:"用户关联的组织" json:"userOrg,omitempty"`
// 用户关联的角色
UserRole []int64 `cname:"用户关联的角色" json:"userRole,omitempty"`
// 共创公司
CooperationCompany string `cname:"共创公司" json:"cooperationCompany,omitempty"`
// 共创到期时间 (yyyy-MM-dd)
CooperationDeadline time.Time `cname:"共创到期时间 (yyyy-MM-dd)" json:"cooperationDeadline,omitempty"`
// 启用状态(启用:1 禁用:2)
EnableStatus int `cname:"启用状态(启用:1 禁用:2)" json:"enableStatus,omitempty" valid:"Required"`
// 密码
Password string `cname:"密码" json:"password" valid:"Required"`
// 用户姓名
UserName string `cname:"用户姓名" json:"userName" valid:"Required"`
// 手机号码
Phone string `cname:"手机号码" json:"phone" valid:"Required"`
// 头像
Avatar string `cname:"头像" json:"avatar"`
// 邮箱
Email string `cname:"邮箱" json:"email"`
// 运营管理扩展
// 部门
DepartmentName string `cname:"部门" json:"departmentName"`
}
func (createUserCommand *CreateAdminUserCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (createUserCommand *CreateAdminUserCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(createUserCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(createUserCommand).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
... ... @@ -41,6 +41,10 @@ type CreateUserCommand struct {
Avatar string `cname:"头像" json:"avatar"`
// 邮箱
Email string `cname:"邮箱" json:"email"`
// 运营管理扩展
// 部门
DepartmentName string `cname:"部门" json:"departmentName"`
}
func (createUserCommand *CreateUserCommand) Valid(validation *validation.Validation) {
... ...
package command
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"reflect"
"strings"
"time"
"github.com/beego/beego/v2/core/validation"
)
type UpdateAdminUserCommand struct {
OperateInfo *domain.OperateInfo `json:"-"`
// 用户Id 用户唯一标识
UserId int64 `cname:"用户Id 用户唯一标识" json:"userId" valid:"Required"`
// 组织机构
OrganizationId int64 `cname:"组织机构" json:"organizationId,omitempty"`
// 所属部门
DepartmentId int64 `cname:"所属部门" json:"departmentId,omitempty"`
// 用户关联的组织
UserOrg []int64 `cname:"用户关联的组织" json:"userOrg,omitempty"`
// 用户关联的角色
UserRole []int64 `cname:"用户关联的角色" json:"userRole,omitempty"`
// 共创公司
CooperationCompany string `cname:"共创公司" json:"cooperationCompany,omitempty"`
// 共创到期时间 (yyyy-MM-dd)
CooperationDeadline time.Time `cname:"共创到期时间 (yyyy-MM-dd)" json:"cooperationDeadline,omitempty"`
// 启用状态(启用:1 禁用:2)
EnableStatus int `cname:"启用状态(启用:1 禁用:2)" json:"enableStatus,omitempty"`
// 用户姓名
UserName string `cname:"用户姓名" json:"userName" valid:"Required"`
// 手机号码
Phone string `cname:"手机号码" json:"phone" valid:"Required"`
// 头像
Avatar string `cname:"头像" json:"avatar"`
// 邮箱
Email string `cname:"邮箱" json:"email"`
// 运营管理扩展
// 部门
DepartmentName string `cname:"部门" json:"departmentName"`
}
func (updateUserCommand *UpdateAdminUserCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (updateUserCommand *UpdateAdminUserCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(updateUserCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(updateUserCommand).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
... ... @@ -36,6 +36,10 @@ type UpdateUserCommand struct {
Avatar string `cname:"头像" json:"avatar"`
// 邮箱
Email string `cname:"邮箱" json:"email"`
// 运营管理扩展
// 部门
DepartmentName string `cname:"部门" json:"departmentName"`
}
func (updateUserCommand *UpdateUserCommand) Valid(validation *validation.Validation) {
... ...
... ... @@ -273,6 +273,7 @@ func (userService *UserService) CreateUser(createUserCommand *command.CreateUser
Ext: &domain.Ext{
Phone: createUserCommand.Phone,
UserName: createUserCommand.UserName,
DepName: createUserCommand.DepartmentName,
},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
... ... @@ -536,10 +537,12 @@ func (userService *UserService) ListUser(listUserQuery *query.ListUserQuery) (in
_, dep, _ := factory.FastPgOrg(transactionContext, user.DepartmentId)
_, org, _ := factory.FastPgOrg(transactionContext, user.OrganizationId)
_, userBase, _ := factory.FastPgUserBase(transactionContext, user.UserBaseId)
if dep != nil && org != nil && company != nil && userBase != nil {
if dep != nil && org != nil && company != nil {
user.Department = dep.ConvDep()
user.Organization = org.CloneSample()
user.Company = company.CloneSample()
}
if userBase != nil {
user.UserInfo = userBase.UserInfo
user.Favorite = userBase.Favorite
}
... ... @@ -724,6 +727,7 @@ func (userService *UserService) UpdateUser(updateUserCommand *command.UpdateUser
Phone: updateUserCommand.Phone,
Avatar: updateUserCommand.Avatar,
Email: updateUserCommand.Email,
DepartmentName: updateUserCommand.DepartmentName,
}
updateUserService, _ := factory.CreatePgUpdateUserService(map[string]interface{}{
... ... @@ -853,6 +857,123 @@ func (userService *UserService) UpdateFavorite(updateFavoriteCommand *command.Up
return struct{}{}, nil
}
// 创建
func (userService *UserService) CreateAdminUser(createUserCommand *command.CreateAdminUserCommand) (interface{}, error) {
if err := createUserCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
nweUserInfo := &domain.UserInfo{
UserName: createUserCommand.UserName,
Phone: createUserCommand.Phone,
Avatar: createUserCommand.Avatar,
Email: createUserCommand.Email,
DepartmentName: createUserCommand.DepartmentName,
}
var sampleUserOrg = make([]*domain.Org, 0)
var sampleUserRole = make([]*domain.Role, 0)
newUser := &domain.User{
CompanyId: createUserCommand.CompanyId,
UserType: createUserCommand.UserType,
UserCode: createUserCommand.UserCode,
OrganizationId: createUserCommand.OrganizationId,
DepartmentId: createUserCommand.DepartmentId,
UserOrg: sampleUserOrg,
UserRole: sampleUserRole,
FavoriteMenus: []string{},
//CooperationInfo: &domain.CooperationInfo{},
UserInfo: nweUserInfo,
EnableStatus: createUserCommand.EnableStatus,
Ext: &domain.Ext{
Phone: createUserCommand.Phone,
UserName: createUserCommand.UserName,
DepName: createUserCommand.DepartmentName,
},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
var user *domain.User
createUserService, _ := factory.CreatePgCreateUserService(map[string]interface{}{
"transactionContext": transactionContext,
})
if user, err = createUserService.CreateUser(nil, newUser, createUserCommand.Password); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
userDto := &dto.UserDto{}
if err := userDto.LoadDto(user, nil); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return userDto, nil
}
// 更新
func (userService *UserService) UpdateAdminUser(updateUserCommand *command.UpdateAdminUserCommand) (interface{}, error) {
if err := updateUserCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
_, user, err := factory.FastPgUser(transactionContext, updateUserCommand.UserId, factory.WithDataAuthRequired(), factory.WithOperator(updateUserCommand.OperateInfo))
if err != nil {
return nil, err
}
user.DepartmentId = updateUserCommand.DepartmentId
user.OrganizationId = updateUserCommand.OrganizationId
var userOrg = make([]*domain.Org, 0)
for i := range updateUserCommand.UserOrg {
userOrg = append(userOrg, &domain.Org{OrgId: updateUserCommand.UserOrg[i]})
}
user.UserOrg = userOrg
var userRole = make([]*domain.Role, 0)
for i := range updateUserCommand.UserRole {
userRole = append(userRole, &domain.Role{RoleId: updateUserCommand.UserRole[i]})
}
user.UserRole = userRole
userInfo := &domain.UserInfo{
UserName: updateUserCommand.UserName,
Phone: updateUserCommand.Phone,
Avatar: updateUserCommand.Avatar,
Email: updateUserCommand.Email,
DepartmentName: updateUserCommand.DepartmentName,
}
updateUserService, _ := factory.CreatePgUpdateUserService(map[string]interface{}{
"transactionContext": transactionContext,
})
if user, err = updateUserService.UpdateUser(nil, user, userInfo, user.EnableStatus); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
userDto := &dto.UserDto{}
if err := userDto.LoadDto(user, nil); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return userDto, nil
}
func NewUserService(options map[string]interface{}) *UserService {
newUserService := &UserService{}
return newUserService
... ...
... ... @@ -15,6 +15,7 @@ const (
UserTypeEmployee = 1
UserTypeCooperation = 2
UserTypeVisitor = 4 // 游客
UserTypeOperationAdmin = 8 // 运营管理员
UserTypeCompanyAdmin = 1024
)
... ...
... ... @@ -12,4 +12,6 @@ type UserInfo struct {
Email string `json:"email,omitempty"`
Referer string `json:"-"`
// 部门
DepartmentName string `cname:"部门" json:"-"`
}
... ...
... ... @@ -57,7 +57,7 @@ func (ptr *PgCreateUserService) CreateUser(optUser *domain.User, newUser *domain
// 普通账号需要验证
// 1.用户编号唯一验证 用户编号在该企业内已存在,请重新输入
// 2.当前企业内手机号唯一 手机号在该企业内已存在,请重新输入
if newUser.UserType&(domain.UserTypeCompanyAdmin|domain.UserTypeVisitor) == 0 {
if newUser.UserType&(domain.UserTypeCompanyAdmin|domain.UserTypeVisitor|domain.UserTypeOperationAdmin) == 0 {
if user, err = userRepository.FindOne(map[string]interface{}{"companyId": newUser.CompanyId, "organizationId": newUser.OrganizationId, "userCode": newUser.UserCode}); err == nil && user != nil {
return nil, fmt.Errorf("用户编号在该企业内已存在,请重新输入")
}
... ...
... ... @@ -19,19 +19,19 @@ type PgRoleAccessMenusService struct {
//
// options 数据参数
// data 需要验证权限的数据
func (ptr *PgRoleAccessMenusService) AccessMenus(options *domain.OperateInfo, roleId []int64, option domain.AccessMenusOptions) ([]*domain.Menu, error) {
func (ptr *PgRoleAccessMenusService) AccessMenus(options *domain.OperateInfo, roleIds []int64, option domain.AccessMenusOptions) ([]*domain.Menu, error) {
var err error
var menus []*domain.Menu
menuIdSet := hashset.New()
if len(roleId) == 0 {
if len(roleIds) == 0 {
return menus, nil
}
// 1.角色有权限的菜单列表
roleRepository, _ := repository.NewRoleRepository(ptr.transactionContext)
for i := range roleId {
for i := range roleIds {
var role *domain.Role
if role, _ = roleRepository.FindOne(map[string]interface{}{"roleId": roleId[i]}); role == nil {
if role, _ = roleRepository.FindOne(map[string]interface{}{"roleId": roleIds[i]}); role == nil {
continue
}
// 只要当前登录组织的有权限菜单
... ...
... ... @@ -87,6 +87,8 @@ func (ptr *PgUpdateUserService) UpdateUser(optUser *domain.OperateInfo, user *do
}
if dep != nil {
user.Ext.DepName = dep.OrgName
} else {
user.Ext.DepName = userInfo.DepartmentName
}
user.Ext.Phone = userBase.UserInfo.Phone
user.Ext.UserName = userBase.UserInfo.UserName
... ...
... ... @@ -159,3 +159,23 @@ func (controller *UserController) SearchUser() {
data, err := userService.ListUser(listUserQuery)
controller.Response(data, err)
}
func (controller *UserController) CreateAdminUser() {
userService := service.NewUserService(nil)
createUserCommand := &command.CreateAdminUserCommand{}
Must(controller.Unmarshal(createUserCommand))
createUserCommand.OperateInfo = ParseOperateInfo(controller.BaseController)
data, err := userService.CreateAdminUser(createUserCommand)
controller.Response(data, err)
}
func (controller *UserController) UpdateAdminUser() {
userService := service.NewUserService(nil)
updateUserCommand := &command.UpdateAdminUserCommand{}
Must(controller.Unmarshal(updateUserCommand))
userId, _ := controller.GetInt64(":userId")
updateUserCommand.UserId = userId
updateUserCommand.OperateInfo = ParseOperateInfo(controller.BaseController)
data, err := userService.UpdateAdminUser(updateUserCommand)
controller.Response(data, err)
}
... ...
... ... @@ -20,4 +20,7 @@ func init() {
web.Router("/user/:userId/base-info", &controllers.UserController{}, "Put:UpdateUsersBase")
web.Router("/user/cooperator", &controllers.UserController{}, "Post:CreateCooperator")
web.Router("/user/cooperator/:userId", &controllers.UserController{}, "Put:UpdateCooperator")
web.Router("/admin-user/", &controllers.UserController{}, "Post:CreateAdminUser")
web.Router("/admin-user/:userId", &controllers.UserController{}, "Put:UpdateAdminUser")
}
... ...