feat: 新增平台客服角色与客服查询菜单
This commit is contained in:
@@ -28,6 +28,7 @@ type UserListQuery struct {
|
||||
Size int
|
||||
Keyword string
|
||||
Role string
|
||||
Roles []string
|
||||
Status *int
|
||||
}
|
||||
|
||||
@@ -55,7 +56,9 @@ func (s *UserService) List(q UserListQuery) ([]model.User, int64, error) {
|
||||
like := "%" + q.Keyword + "%"
|
||||
tx = tx.Where("users.username LIKE ? OR users.nickname LIKE ?", like, like)
|
||||
}
|
||||
if q.Role != "" {
|
||||
if len(q.Roles) > 0 {
|
||||
tx = tx.Where("users.role IN ?", q.Roles)
|
||||
} else if q.Role != "" {
|
||||
tx = tx.Where("users.role = ?", q.Role)
|
||||
}
|
||||
if q.Status != nil {
|
||||
@@ -79,7 +82,7 @@ func (s *UserService) Create(username, password, nickname, role string, merchant
|
||||
if role == "" {
|
||||
role = model.RoleMerchant
|
||||
}
|
||||
if role != model.RoleAdmin && role != model.RoleMerchant {
|
||||
if role != model.RoleAdmin && role != model.RoleMerchant && role != model.RolePlatformSupport {
|
||||
return nil, errors.New("无效的账号角色")
|
||||
}
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
@@ -142,10 +145,15 @@ type UpdatePlatformAdminInput struct {
|
||||
Nickname string
|
||||
Password string
|
||||
Status int
|
||||
Role string
|
||||
}
|
||||
|
||||
func (s *UserService) ListPlatformAdmins(page, size int) ([]model.User, int64, error) {
|
||||
list, total, err := s.List(UserListQuery{Page: page, Size: size, Role: model.RoleAdmin})
|
||||
list, total, err := s.List(UserListQuery{
|
||||
Page: page,
|
||||
Size: size,
|
||||
Roles: []string{model.RoleAdmin, model.RolePlatformSupport},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -213,10 +221,23 @@ func (s *UserService) ListMerchantAccountGroups(page, size int) ([]MerchantMembe
|
||||
return groups, total, nil
|
||||
}
|
||||
|
||||
// CreatePlatformUser creates a platform-level account. Platform admins get the
|
||||
// hidden self-operated owner membership; platform support accounts use the
|
||||
// synthetic tenant context in middleware.Tenant and do not become merchant members.
|
||||
func (s *UserService) CreatePlatformUser(username, password, nickname, role string) (*model.User, error) {
|
||||
if role == "" {
|
||||
role = model.RoleAdmin
|
||||
}
|
||||
if role != model.RoleAdmin && role != model.RolePlatformSupport {
|
||||
return nil, errors.New("无效的平台账号角色")
|
||||
}
|
||||
return s.Create(username, password, nickname, role, 0)
|
||||
}
|
||||
|
||||
// CreatePlatformAdmin creates an account with platform privileges and the
|
||||
// hidden self-operated merchant context required by merchant-side routes.
|
||||
func (s *UserService) CreatePlatformAdmin(username, password, nickname string) (*model.User, error) {
|
||||
return s.Create(username, password, nickname, model.RoleAdmin, 0)
|
||||
return s.CreatePlatformUser(username, password, nickname, model.RoleAdmin)
|
||||
}
|
||||
|
||||
func (s *UserService) UpdatePlatformAdmin(id uint, in UpdatePlatformAdminInput, actorUserID uint) (*model.User, error) {
|
||||
@@ -238,10 +259,18 @@ func (s *UserService) UpdatePlatformAdmin(id uint, in UpdatePlatformAdminInput,
|
||||
}
|
||||
return err
|
||||
}
|
||||
if user.Role != model.RoleAdmin {
|
||||
return errors.New("只能管理平台管理员账号")
|
||||
if user.Role != model.RoleAdmin && user.Role != model.RolePlatformSupport {
|
||||
return errors.New("只能管理平台账号")
|
||||
}
|
||||
if user.Status == 1 && in.Status == 0 {
|
||||
targetRole := user.Role
|
||||
if in.Role != "" {
|
||||
targetRole = in.Role
|
||||
}
|
||||
if targetRole != model.RoleAdmin && targetRole != model.RolePlatformSupport {
|
||||
return errors.New("无效的平台账号角色")
|
||||
}
|
||||
// 降级或停用最后一个启用的平台管理员会导致平台无法登录。
|
||||
if user.Role == model.RoleAdmin && user.Status == 1 && (targetRole != model.RoleAdmin || in.Status == 0) {
|
||||
var admins int64
|
||||
if err := tx.Model(&model.User{}).Where("role = ? AND status = ?", model.RoleAdmin, 1).Count(&admins).Error; err != nil {
|
||||
return err
|
||||
@@ -259,7 +288,40 @@ func (s *UserService) UpdatePlatformAdmin(id uint, in UpdatePlatformAdminInput,
|
||||
return errors.New("用户名已存在")
|
||||
}
|
||||
}
|
||||
updates := map[string]interface{}{"username": in.Username, "nickname": in.Nickname, "status": in.Status}
|
||||
if targetRole != user.Role {
|
||||
// 管理员需要自营商户负责人成员关系;平台客服使用 Tenant 合成上下文,
|
||||
// 不写入 merchant_members,避免出现在商户成员列表里。
|
||||
if err := tx.Where("user_id = ?", user.ID).Delete(&model.MerchantMember{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if targetRole == model.RoleAdmin {
|
||||
var selfMerchant model.Merchant
|
||||
if err := tx.Where("code = ?", model.MerchantCodeSelfOperated).First(&selfMerchant).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
member := model.MerchantMember{
|
||||
MerchantID: selfMerchant.ID,
|
||||
UserID: user.ID,
|
||||
Role: model.MemberRoleOwner,
|
||||
Status: 1,
|
||||
IsDefault: true,
|
||||
}
|
||||
if err := tx.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "merchant_id"}, {Name: "user_id"}},
|
||||
DoUpdates: clause.Assignments(map[string]interface{}{
|
||||
"role": model.MemberRoleOwner, "status": 1, "is_default": true,
|
||||
}),
|
||||
}).Create(&member).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
updates := map[string]interface{}{
|
||||
"username": in.Username,
|
||||
"nickname": in.Nickname,
|
||||
"status": in.Status,
|
||||
"role": targetRole,
|
||||
}
|
||||
if in.Password != "" {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(in.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
@@ -303,8 +365,9 @@ func (s *UserService) UpdateStatus(id uint, status int) error {
|
||||
})
|
||||
}
|
||||
|
||||
// DeletePlatformAdmin removes a platform-only account. It protects the current
|
||||
// account and the final enabled platform administrator so the platform cannot be locked out.
|
||||
// DeletePlatformAdmin removes a platform account (admin or support). It protects
|
||||
// the current account and the final enabled platform administrator so the platform
|
||||
// cannot be locked out.
|
||||
func (s *UserService) DeletePlatformAdmin(id, actorUserID uint) error {
|
||||
if id == 0 {
|
||||
return errors.New("用户不存在")
|
||||
@@ -320,8 +383,8 @@ func (s *UserService) DeletePlatformAdmin(id, actorUserID uint) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
if user.Role != model.RoleAdmin {
|
||||
return errors.New("只能删除平台管理员账号")
|
||||
if user.Role != model.RoleAdmin && user.Role != model.RolePlatformSupport {
|
||||
return errors.New("只能删除平台账号")
|
||||
}
|
||||
if user.Role == model.RoleAdmin && user.Status == 1 {
|
||||
var admins int64
|
||||
@@ -332,6 +395,9 @@ func (s *UserService) DeletePlatformAdmin(id, actorUserID uint) error {
|
||||
return errors.New("至少保留一个启用的平台管理员")
|
||||
}
|
||||
}
|
||||
if err := tx.Where("user_id = ?", user.ID).Delete(&model.MerchantMember{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Delete(&user).Error
|
||||
})
|
||||
}
|
||||
@@ -371,7 +437,7 @@ func (s *UserService) DeleteMerchantAccount(id, actorUserID uint) error {
|
||||
return err
|
||||
}
|
||||
if user.Role != model.RoleMerchant {
|
||||
return errors.New("平台管理员请在平台管理员列表中管理")
|
||||
return errors.New("平台账号请在平台账号列表中管理")
|
||||
}
|
||||
if err := tx.Where("user_id = ?", user.ID).Delete(&model.MerchantMember{}).Error; err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user