作者 yangfu

运营用户管理

package command
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type EnableUserCommand struct {
Operator domain.Operator `json:"-"`
UserBaseId int64 `cname:"用户id列表" json:"userBaseId" valid:"Required"`
// 启用状态(启用:1 禁用:2 注销:3)
EnableStatus int `cname:"启用状态(启用:1 禁用:2 注销:3)" json:"enableStatus" valid:"Required"`
}
func (batchEnableCommand *EnableUserCommand) Valid(validation *validation.Validation) {
}
func (batchEnableCommand *EnableUserCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(batchEnableCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(batchEnableCommand).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
}
... ...
package command
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
type ResetPasswordCommand struct {
Operator domain.Operator `json:"-"`
Account string `json:"account"`
}
... ...
package dto
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
"strings"
)
type UserBaseDto struct {
// 用户基础数据id
UserBaseId int `json:"userBaseId,omitempty"`
//姓名
UserName string `json:"userName"`
// 账号
Account string `json:"account"`
// 用户关联的组织
Organizations string `json:"organizations"`
// 账号状态 1:正常 2.禁用 3:注销
Status int `json:"status,omitempty"`
// 推荐人
Referer string `json:"referer"`
// 创建时间
RegistrationDate string `json:"registrationDate,omitempty"`
// 最后登录时间
LastLogIn string `json:"lastLogIn"`
}
func (dto *UserBaseDto) LoadDto(detail allied_creation_user.UserBaseDetail) {
dto.UserBaseId = detail.UserBaseId
dto.UserName = detail.UserInfo.UserName
dto.Account = detail.UserInfo.Phone
dto.Status = detail.Status
dto.Referer = detail.Referer
dto.RegistrationDate = detail.RegistrationDate
dto.LastLogIn = detail.LastLogIn
var organizations []string
for i := range detail.UserOrg {
organizations = append(organizations, detail.UserOrg[i].OrgName)
}
dto.Organizations = strings.Join(organizations, ",")
}
... ...
package query
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type ListUserQuery struct {
Operator domain.Operator `json:"-"`
// 查询偏离量
PageNumber int `cname:"查询偏离量" json:"pageNumber"`
// 查询限制
PageSize int `cname:"查询限制" json:"pageSize"`
// 用户姓名
UserName string `cname:"用户姓名" json:"userName,omitempty"`
// 在用户列表内
//InUserBaseIds []int64 `cname:"用户姓名" json:"inUserBaseIds,omitempty"`
// 所属组织
OrgName string `cname:"所属组织" json:"orgName,omitempty"`
// 关闭查询限制
//DisableLimit bool `cname:"关闭查询限制" json:"disableLimit,omitempty"`
// 获取组织
//FetchOrgBelong bool `cname:"获取组织" json:"fetchOrgBelong,omitempty"`
}
func (listUserQuery *ListUserQuery) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (listUserQuery *ListUserQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(listUserQuery)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(listUserQuery).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
}
... ...
package service
import (
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/background/user/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/background/user/dto"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/background/user/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
)
type UserService struct {
}
// 创建菜单服务
func (userService *UserService) SearchUser(cmd *query.ListUserQuery) (int64, interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(cmd.Operator)
result, err := creationUserGateway.UserBaseSearch(allied_creation_user.ReqUserBaseSearch{
Offset: (cmd.PageNumber - 1) * cmd.PageSize,
Limit: cmd.PageSize,
UserName: cmd.UserName,
OrgName: cmd.OrgName,
FetchOrgBelong: true,
})
if err != nil {
return 0, nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
var users []dto.UserBaseDto
for i := range result.Users {
item := &dto.UserBaseDto{}
item.LoadDto(result.Users[i])
users = append(users, *item)
}
return result.Count, users, nil
}
func (userService *UserService) EnableUser(cmd *command.EnableUserCommand) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(cmd.Operator)
_, err := creationUserGateway.EnableUserBase(allied_creation_user.ReqEnableUserBase{
UserBaseIds: []int64{cmd.UserBaseId},
EnableStatus: cmd.EnableStatus,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return struct{}{}, nil
}
func (userService *UserService) ResetPassword(cmd *command.ResetPasswordCommand) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(cmd.Operator)
_, err := creationUserGateway.AuthResetPassword(allied_creation_user.ReqAuthResetPassword{
Phone: cmd.Account,
Password: "4a693460c4cf078ea5b6b5a9e2cf382064a6f810", //TL123456!
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return struct{}{}, nil
}
func NewUserService(options map[string]interface{}) *UserService {
newUserService := &UserService{}
return newUserService
}
... ...
... ... @@ -430,3 +430,68 @@ func (gateway HttplibAlliedCreationUser) UserAccessMenus(param ReqUserAccessMenu
err = gateway.GetResponseData(result, &data)
return &data, err
}
//UserSearch 搜索用户列表
func (gateway HttplibAlliedCreationUser) UserBaseSearch(param ReqUserBaseSearch) (*DataUserBaseSearch, error) {
url := gateway.baseUrL + "/user-base/search"
method := "post"
req := gateway.CreateRequest(url, method)
//TODO traceID
log.Logger.Debug("向用户模块请求数据:搜索用户列表。", map[string]interface{}{
"api": method + ":" + url,
"param": param,
})
req, err := req.JSONBody(param)
if err != nil {
return nil, fmt.Errorf("搜索用户列表失败:%w", err)
}
byteResult, err := req.Bytes()
if err != nil {
return nil, fmt.Errorf("获取搜索用户列表失败:%w", err)
}
if param.Limit > 50 {
//太多就不打印了
log.Logger.Debug("获取用户模块请求数据:搜索用户列表。", map[string]interface{}{
"result": string(byteResult),
})
}
var result service_gateway.GatewayResponse
err = json.Unmarshal(byteResult, &result)
if err != nil {
return nil, fmt.Errorf("解析搜索用户列表:%w", err)
}
var data DataUserBaseSearch
err = gateway.GetResponseData(result, &data)
return &data, err
}
func (gateway HttplibAlliedCreationUser) EnableUserBase(param ReqEnableUserBase) (interface{}, error) {
url := gateway.baseUrL + "/user-base/batch-enable"
method := "post"
req := gateway.CreateRequest(url, method)
//TODO traceID
log.Logger.Debug("向用户模块请求数据:搜索用户列表。", map[string]interface{}{
"api": method + ":" + url,
"param": param,
})
req, err := req.JSONBody(param)
if err != nil {
return nil, fmt.Errorf("搜索用户列表失败:%w", err)
}
byteResult, err := req.Bytes()
if err != nil {
return nil, fmt.Errorf("获取搜索用户列表失败:%w", err)
}
var result service_gateway.GatewayResponse
err = json.Unmarshal(byteResult, &result)
if err != nil {
return nil, fmt.Errorf("解析搜索用户列表:%w", err)
}
//var data DataCompanyEnable
//err = gateway.GetResponseData(result, &data)
return nil, err
}
... ...
... ... @@ -411,3 +411,57 @@ type (
FailReason string `json:"failReason"`
}
)
//搜索用户列表
type (
ReqUserBaseSearch struct {
// 查询偏离量
Offset int `json:"offset"`
// 查询限制
Limit int `json:"limit"`
// 用户姓名
UserName string `cname:"用户姓名" json:"userName,omitempty"`
// 在用户列表内
InUserBaseIds []int64 `cname:"用户姓名" json:"inUserBaseIds,omitempty"`
// 所属组织
OrgName string `cname:"所属组织" json:"orgName,omitempty"`
// 关闭查询限制
DisableLimit bool `cname:"关闭查询限制" json:"disableLimit,omitempty"`
// 获取组织
FetchOrgBelong bool `cname:"获取组织" json:"fetchOrgBelong,omitempty"`
}
//DataUserSearch 搜索用户列表
DataUserBaseSearch struct {
Count int64 `json:"count"`
Users []UserBaseDetail `json:"users"`
}
UserBaseDetail struct {
UserBaseId int `json:"userBaseId"`
LastLogIn string `json:"lastLogIn"`
UserInfo struct {
UserName string `json:"userName"`
Phone string `json:"phone"`
Avatar string `json:"avatar"`
} `json:"userInfo"`
Status int `json:"status"`
Referer string `json:"referer"`
UserOrg []struct {
OrgId int `json:"orgId"`
OrgName string `json:"orgName"`
} `json:"userOrg"`
RegistrationDate string `json:"registrationDate"`
}
)
type (
ReqEnableUserBase struct {
UserBaseIds []int64 `cname:"用户id列表" json:"userBaseIds" valid:"Required"`
// 启用状态(启用:1 禁用:2 注销:3)
EnableStatus int `cname:"启用状态(启用:1 禁用:2 注销:3)" json:"enableStatus" valid:"Required"`
}
//DataUserSearch 搜索用户列表
DataEnableUserBase = interface{}
)
... ...
package backgroud_client
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/background/user/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/background/user/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/background/user/service"
)
type UserController struct {
baseController
}
func (controller *UserController) SearchUser() {
companyService := service.NewUserService(nil)
listCompanyQuery := &query.ListUserQuery{}
controller.Unmarshal(listCompanyQuery)
cnt, data, err := companyService.SearchUser(listCompanyQuery)
controller.returnPageListData(cnt, data, err, listCompanyQuery.PageNumber)
}
func (controller *UserController) EnableUser() {
companyService := service.NewUserService(nil)
cmd := &command.EnableUserCommand{}
controller.Unmarshal(cmd)
data, err := companyService.EnableUser(cmd)
controller.Response(data, err)
}
func (controller *UserController) ResetPassword() {
companyService := service.NewUserService(nil)
cmd := &command.ResetPasswordCommand{}
controller.Unmarshal(cmd)
data, err := companyService.ResetPassword(cmd)
controller.Response(data, err)
}
... ...
... ... @@ -28,4 +28,9 @@ func init() {
web.Router("/v1/background/company/search", &backgroud_client.CompanyController{}, "Post:SearchCompany")
web.Router("/v1/background/company/:companyId/audit", &backgroud_client.CompanyController{}, "Post:AuditCompany")
web.Router("/v1/background/company/:companyId/enable", &backgroud_client.CompanyController{}, "Post:EnableCompany")
// user
web.Router("/v1/background/user/search", &backgroud_client.UserController{}, "Post:SearchUser")
web.Router("/v1/background/user/enable", &backgroud_client.UserController{}, "Post:EnableUser")
web.Router("/v1/background/user/reset-password", &backgroud_client.UserController{}, "Post:ResetPassword")
}
... ...