作者 yangfu

运营用户管理

  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
  6 + "reflect"
  7 + "strings"
  8 +
  9 + "github.com/beego/beego/v2/core/validation"
  10 +)
  11 +
  12 +type EnableUserCommand struct {
  13 + Operator domain.Operator `json:"-"`
  14 + UserBaseId int64 `cname:"用户id列表" json:"userBaseId" valid:"Required"`
  15 + // 启用状态(启用:1 禁用:2 注销:3)
  16 + EnableStatus int `cname:"启用状态(启用:1 禁用:2 注销:3)" json:"enableStatus" valid:"Required"`
  17 +}
  18 +
  19 +func (batchEnableCommand *EnableUserCommand) Valid(validation *validation.Validation) {
  20 +}
  21 +
  22 +func (batchEnableCommand *EnableUserCommand) ValidateCommand() error {
  23 + valid := validation.Validation{}
  24 + b, err := valid.Valid(batchEnableCommand)
  25 + if err != nil {
  26 + return err
  27 + }
  28 + if !b {
  29 + elem := reflect.TypeOf(batchEnableCommand).Elem()
  30 + for _, validErr := range valid.Errors {
  31 + field, isExist := elem.FieldByName(validErr.Field)
  32 + if isExist {
  33 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  34 + } else {
  35 + return fmt.Errorf(validErr.Message)
  36 + }
  37 + }
  38 + }
  39 + return nil
  40 +}
  1 +package command
  2 +
  3 +import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
  4 +
  5 +type ResetPasswordCommand struct {
  6 + Operator domain.Operator `json:"-"`
  7 + Account string `json:"account"`
  8 +}
  1 +package dto
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
  5 + "strings"
  6 +)
  7 +
  8 +type UserBaseDto struct {
  9 + // 用户基础数据id
  10 + UserBaseId int `json:"userBaseId,omitempty"`
  11 + //姓名
  12 + UserName string `json:"userName"`
  13 + // 账号
  14 + Account string `json:"account"`
  15 + // 用户关联的组织
  16 + Organizations string `json:"organizations"`
  17 + // 账号状态 1:正常 2.禁用 3:注销
  18 + Status int `json:"status,omitempty"`
  19 + // 推荐人
  20 + Referer string `json:"referer"`
  21 + // 创建时间
  22 + RegistrationDate string `json:"registrationDate,omitempty"`
  23 + // 最后登录时间
  24 + LastLogIn string `json:"lastLogIn"`
  25 +}
  26 +
  27 +func (dto *UserBaseDto) LoadDto(detail allied_creation_user.UserBaseDetail) {
  28 + dto.UserBaseId = detail.UserBaseId
  29 + dto.UserName = detail.UserInfo.UserName
  30 + dto.Account = detail.UserInfo.Phone
  31 + dto.Status = detail.Status
  32 + dto.Referer = detail.Referer
  33 + dto.RegistrationDate = detail.RegistrationDate
  34 + dto.LastLogIn = detail.LastLogIn
  35 + var organizations []string
  36 + for i := range detail.UserOrg {
  37 + organizations = append(organizations, detail.UserOrg[i].OrgName)
  38 + }
  39 + dto.Organizations = strings.Join(organizations, ",")
  40 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
  6 + "reflect"
  7 + "strings"
  8 +
  9 + "github.com/beego/beego/v2/core/validation"
  10 +)
  11 +
  12 +type ListUserQuery struct {
  13 + Operator domain.Operator `json:"-"`
  14 + // 查询偏离量
  15 + PageNumber int `cname:"查询偏离量" json:"pageNumber"`
  16 + // 查询限制
  17 + PageSize int `cname:"查询限制" json:"pageSize"`
  18 + // 用户姓名
  19 + UserName string `cname:"用户姓名" json:"userName,omitempty"`
  20 + // 在用户列表内
  21 + //InUserBaseIds []int64 `cname:"用户姓名" json:"inUserBaseIds,omitempty"`
  22 + // 所属组织
  23 + OrgName string `cname:"所属组织" json:"orgName,omitempty"`
  24 + // 关闭查询限制
  25 + //DisableLimit bool `cname:"关闭查询限制" json:"disableLimit,omitempty"`
  26 +
  27 + // 获取组织
  28 + //FetchOrgBelong bool `cname:"获取组织" json:"fetchOrgBelong,omitempty"`
  29 +}
  30 +
  31 +func (listUserQuery *ListUserQuery) Valid(validation *validation.Validation) {
  32 + //validation.SetError("CustomValid", "未实现的自定义认证")
  33 +}
  34 +
  35 +func (listUserQuery *ListUserQuery) ValidateQuery() error {
  36 + valid := validation.Validation{}
  37 + b, err := valid.Valid(listUserQuery)
  38 + if err != nil {
  39 + return err
  40 + }
  41 + if !b {
  42 + elem := reflect.TypeOf(listUserQuery).Elem()
  43 + for _, validErr := range valid.Errors {
  44 + field, isExist := elem.FieldByName(validErr.Field)
  45 + if isExist {
  46 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  47 + } else {
  48 + return fmt.Errorf(validErr.Message)
  49 + }
  50 + }
  51 + }
  52 + return nil
  53 +}
  1 +package service
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/core/application"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/background/user/command"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/background/user/dto"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/background/user/query"
  8 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
  9 +)
  10 +
  11 +type UserService struct {
  12 +}
  13 +
  14 +// 创建菜单服务
  15 +func (userService *UserService) SearchUser(cmd *query.ListUserQuery) (int64, interface{}, error) {
  16 + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(cmd.Operator)
  17 + result, err := creationUserGateway.UserBaseSearch(allied_creation_user.ReqUserBaseSearch{
  18 + Offset: (cmd.PageNumber - 1) * cmd.PageSize,
  19 + Limit: cmd.PageSize,
  20 + UserName: cmd.UserName,
  21 + OrgName: cmd.OrgName,
  22 + FetchOrgBelong: true,
  23 + })
  24 + if err != nil {
  25 + return 0, nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  26 + }
  27 + var users []dto.UserBaseDto
  28 + for i := range result.Users {
  29 + item := &dto.UserBaseDto{}
  30 + item.LoadDto(result.Users[i])
  31 + users = append(users, *item)
  32 + }
  33 + return result.Count, users, nil
  34 +}
  35 +
  36 +func (userService *UserService) EnableUser(cmd *command.EnableUserCommand) (interface{}, error) {
  37 + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(cmd.Operator)
  38 + _, err := creationUserGateway.EnableUserBase(allied_creation_user.ReqEnableUserBase{
  39 + UserBaseIds: []int64{cmd.UserBaseId},
  40 + EnableStatus: cmd.EnableStatus,
  41 + })
  42 + if err != nil {
  43 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  44 + }
  45 + return struct{}{}, nil
  46 +}
  47 +
  48 +func (userService *UserService) ResetPassword(cmd *command.ResetPasswordCommand) (interface{}, error) {
  49 + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(cmd.Operator)
  50 + _, err := creationUserGateway.AuthResetPassword(allied_creation_user.ReqAuthResetPassword{
  51 + Phone: cmd.Account,
  52 + Password: "4a693460c4cf078ea5b6b5a9e2cf382064a6f810", //TL123456!
  53 + })
  54 + if err != nil {
  55 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  56 + }
  57 + return struct{}{}, nil
  58 +}
  59 +
  60 +func NewUserService(options map[string]interface{}) *UserService {
  61 + newUserService := &UserService{}
  62 + return newUserService
  63 +}
@@ -430,3 +430,68 @@ func (gateway HttplibAlliedCreationUser) UserAccessMenus(param ReqUserAccessMenu @@ -430,3 +430,68 @@ func (gateway HttplibAlliedCreationUser) UserAccessMenus(param ReqUserAccessMenu
430 err = gateway.GetResponseData(result, &data) 430 err = gateway.GetResponseData(result, &data)
431 return &data, err 431 return &data, err
432 } 432 }
  433 +
  434 +//UserSearch 搜索用户列表
  435 +func (gateway HttplibAlliedCreationUser) UserBaseSearch(param ReqUserBaseSearch) (*DataUserBaseSearch, error) {
  436 + url := gateway.baseUrL + "/user-base/search"
  437 + method := "post"
  438 + req := gateway.CreateRequest(url, method)
  439 + //TODO traceID
  440 + log.Logger.Debug("向用户模块请求数据:搜索用户列表。", map[string]interface{}{
  441 + "api": method + ":" + url,
  442 + "param": param,
  443 + })
  444 + req, err := req.JSONBody(param)
  445 + if err != nil {
  446 + return nil, fmt.Errorf("搜索用户列表失败:%w", err)
  447 + }
  448 +
  449 + byteResult, err := req.Bytes()
  450 + if err != nil {
  451 + return nil, fmt.Errorf("获取搜索用户列表失败:%w", err)
  452 + }
  453 + if param.Limit > 50 {
  454 + //太多就不打印了
  455 + log.Logger.Debug("获取用户模块请求数据:搜索用户列表。", map[string]interface{}{
  456 + "result": string(byteResult),
  457 + })
  458 + }
  459 +
  460 + var result service_gateway.GatewayResponse
  461 + err = json.Unmarshal(byteResult, &result)
  462 + if err != nil {
  463 + return nil, fmt.Errorf("解析搜索用户列表:%w", err)
  464 + }
  465 + var data DataUserBaseSearch
  466 + err = gateway.GetResponseData(result, &data)
  467 + return &data, err
  468 +}
  469 +
  470 +func (gateway HttplibAlliedCreationUser) EnableUserBase(param ReqEnableUserBase) (interface{}, error) {
  471 + url := gateway.baseUrL + "/user-base/batch-enable"
  472 + method := "post"
  473 + req := gateway.CreateRequest(url, method)
  474 + //TODO traceID
  475 + log.Logger.Debug("向用户模块请求数据:搜索用户列表。", map[string]interface{}{
  476 + "api": method + ":" + url,
  477 + "param": param,
  478 + })
  479 + req, err := req.JSONBody(param)
  480 + if err != nil {
  481 + return nil, fmt.Errorf("搜索用户列表失败:%w", err)
  482 + }
  483 +
  484 + byteResult, err := req.Bytes()
  485 + if err != nil {
  486 + return nil, fmt.Errorf("获取搜索用户列表失败:%w", err)
  487 + }
  488 +
  489 + var result service_gateway.GatewayResponse
  490 + err = json.Unmarshal(byteResult, &result)
  491 + if err != nil {
  492 + return nil, fmt.Errorf("解析搜索用户列表:%w", err)
  493 + }
  494 + //var data DataCompanyEnable
  495 + //err = gateway.GetResponseData(result, &data)
  496 + return nil, err
  497 +}
@@ -411,3 +411,57 @@ type ( @@ -411,3 +411,57 @@ type (
411 FailReason string `json:"failReason"` 411 FailReason string `json:"failReason"`
412 } 412 }
413 ) 413 )
  414 +
  415 +//搜索用户列表
  416 +type (
  417 + ReqUserBaseSearch struct {
  418 + // 查询偏离量
  419 + Offset int `json:"offset"`
  420 + // 查询限制
  421 + Limit int `json:"limit"`
  422 + // 用户姓名
  423 + UserName string `cname:"用户姓名" json:"userName,omitempty"`
  424 + // 在用户列表内
  425 + InUserBaseIds []int64 `cname:"用户姓名" json:"inUserBaseIds,omitempty"`
  426 + // 所属组织
  427 + OrgName string `cname:"所属组织" json:"orgName,omitempty"`
  428 + // 关闭查询限制
  429 + DisableLimit bool `cname:"关闭查询限制" json:"disableLimit,omitempty"`
  430 + // 获取组织
  431 + FetchOrgBelong bool `cname:"获取组织" json:"fetchOrgBelong,omitempty"`
  432 + }
  433 +
  434 + //DataUserSearch 搜索用户列表
  435 + DataUserBaseSearch struct {
  436 + Count int64 `json:"count"`
  437 + Users []UserBaseDetail `json:"users"`
  438 + }
  439 +
  440 + UserBaseDetail struct {
  441 + UserBaseId int `json:"userBaseId"`
  442 + LastLogIn string `json:"lastLogIn"`
  443 + UserInfo struct {
  444 + UserName string `json:"userName"`
  445 + Phone string `json:"phone"`
  446 + Avatar string `json:"avatar"`
  447 + } `json:"userInfo"`
  448 + Status int `json:"status"`
  449 + Referer string `json:"referer"`
  450 + UserOrg []struct {
  451 + OrgId int `json:"orgId"`
  452 + OrgName string `json:"orgName"`
  453 + } `json:"userOrg"`
  454 + RegistrationDate string `json:"registrationDate"`
  455 + }
  456 +)
  457 +
  458 +type (
  459 + ReqEnableUserBase struct {
  460 + UserBaseIds []int64 `cname:"用户id列表" json:"userBaseIds" valid:"Required"`
  461 + // 启用状态(启用:1 禁用:2 注销:3)
  462 + EnableStatus int `cname:"启用状态(启用:1 禁用:2 注销:3)" json:"enableStatus" valid:"Required"`
  463 + }
  464 +
  465 + //DataUserSearch 搜索用户列表
  466 + DataEnableUserBase = interface{}
  467 +)
  1 +package backgroud_client
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/background/user/command"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/background/user/query"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/background/user/service"
  7 +)
  8 +
  9 +type UserController struct {
  10 + baseController
  11 +}
  12 +
  13 +func (controller *UserController) SearchUser() {
  14 + companyService := service.NewUserService(nil)
  15 + listCompanyQuery := &query.ListUserQuery{}
  16 + controller.Unmarshal(listCompanyQuery)
  17 + cnt, data, err := companyService.SearchUser(listCompanyQuery)
  18 + controller.returnPageListData(cnt, data, err, listCompanyQuery.PageNumber)
  19 +}
  20 +
  21 +func (controller *UserController) EnableUser() {
  22 + companyService := service.NewUserService(nil)
  23 + cmd := &command.EnableUserCommand{}
  24 + controller.Unmarshal(cmd)
  25 + data, err := companyService.EnableUser(cmd)
  26 + controller.Response(data, err)
  27 +}
  28 +
  29 +func (controller *UserController) ResetPassword() {
  30 + companyService := service.NewUserService(nil)
  31 + cmd := &command.ResetPasswordCommand{}
  32 + controller.Unmarshal(cmd)
  33 + data, err := companyService.ResetPassword(cmd)
  34 + controller.Response(data, err)
  35 +}
@@ -28,4 +28,9 @@ func init() { @@ -28,4 +28,9 @@ func init() {
28 web.Router("/v1/background/company/search", &backgroud_client.CompanyController{}, "Post:SearchCompany") 28 web.Router("/v1/background/company/search", &backgroud_client.CompanyController{}, "Post:SearchCompany")
29 web.Router("/v1/background/company/:companyId/audit", &backgroud_client.CompanyController{}, "Post:AuditCompany") 29 web.Router("/v1/background/company/:companyId/audit", &backgroud_client.CompanyController{}, "Post:AuditCompany")
30 web.Router("/v1/background/company/:companyId/enable", &backgroud_client.CompanyController{}, "Post:EnableCompany") 30 web.Router("/v1/background/company/:companyId/enable", &backgroud_client.CompanyController{}, "Post:EnableCompany")
  31 +
  32 + // user
  33 + web.Router("/v1/background/user/search", &backgroud_client.UserController{}, "Post:SearchUser")
  34 + web.Router("/v1/background/user/enable", &backgroud_client.UserController{}, "Post:EnableUser")
  35 + web.Router("/v1/background/user/reset-password", &backgroud_client.UserController{}, "Post:ResetPassword")
31 } 36 }