作者 唐旭辉

微调 以及 用户管理的部分功能

@@ -3,11 +3,11 @@ package command @@ -3,11 +3,11 @@ package command
3 import "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib" 3 import "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
4 4
5 type LoginBySecretKeyCommand struct { 5 type LoginBySecretKeyCommand struct {
6 - Secret string `json:"secret"` 6 + Code string `json:"code"`
7 } 7 }
8 8
9 func (login LoginBySecretKeyCommand) ValidateCommand() error { 9 func (login LoginBySecretKeyCommand) ValidateCommand() error {
10 - if len(login.Secret) == 0 { 10 + if len(login.Code) == 0 {
11 return lib.ThrowError(lib.ARG_ERROR, "登录参数错误") 11 return lib.ThrowError(lib.ARG_ERROR, "登录参数错误")
12 } 12 }
13 return nil 13 return nil
  1 +package query
  2 +
  3 +type UserListQuery struct {
  4 + //用户名称匹配
  5 + UserNameMatch string `json:"userNameMatch" `
  6 + // 查询偏离量
  7 + Offset int `json:"offset" `
  8 + // 查询限制
  9 + Limit int `json:"limit"`
  10 +}
@@ -26,9 +26,9 @@ func (service UsersService) UserLoginBySecretKey(cmd command.LoginBySecretKeyCom @@ -26,9 +26,9 @@ func (service UsersService) UserLoginBySecretKey(cmd command.LoginBySecretKeyCom
26 } 26 }
27 //向统一用户中心确认密钥信息并获取用户数据 27 //向统一用户中心确认密钥信息并获取用户数据
28 ucenterService := serviceGateway.NewMmmUserCenterServiceGateway() 28 ucenterService := serviceGateway.NewMmmUserCenterServiceGateway()
29 - loginResp, err := ucenterService.RequestUCenterLoginBySecret(cmd.Secret) 29 + loginResp, err := ucenterService.RequestUCenterLoginBySecret(cmd.Code)
30 if err != nil { 30 if err != nil {
31 - e := fmt.Sprintf("通过密钥(secret=%s)从统一用户中心获取数据失败:%s", cmd.Secret, err.Error()) 31 + e := fmt.Sprintf("通过密钥(code=%s)从统一用户中心获取数据失败:%s", cmd.Code, err.Error())
32 return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e) 32 return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
33 } 33 }
34 var ( 34 var (
@@ -215,3 +215,83 @@ func (service UsersService) ValidateAdminpPermission(queryOption query.ValidateP @@ -215,3 +215,83 @@ func (service UsersService) ValidateAdminpPermission(queryOption query.ValidateP
215 ok := usersData.HasPermissionByCode(queryOption.PermissionCode) 215 ok := usersData.HasPermissionByCode(queryOption.PermissionCode)
216 return ok, nil 216 return ok, nil
217 } 217 }
  218 +
  219 +//获取用户列表
  220 +func (service UsersService) GetUserList(queryOption query.UserListQuery) (int, []map[string]interface{}, error) {
  221 + var (
  222 + transactionContext, _ = factory.CreateTransactionContext(nil)
  223 + err error
  224 + )
  225 + if err = transactionContext.StartTransaction(); err != nil {
  226 + return 0, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  227 + }
  228 + defer func() {
  229 + transactionContext.RollbackTransaction()
  230 + }()
  231 + var (
  232 + userRespository domain.UsersRepository
  233 + usersData []domain.Users
  234 + cnt int
  235 + permissionRepository domain.AdminPermissionRepository
  236 + permissionList []domain.AdminPermission
  237 + )
  238 + if userRespository, err = factory.CreateUsersRepository(map[string]interface{}{
  239 + "transactionContext": transactionContext,
  240 + }); err != nil {
  241 + return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  242 + }
  243 + if permissionRepository, err = factory.CreateAdminPermissionRepository(map[string]interface{}{
  244 + "transactionContext": transactionContext,
  245 + }); err != nil {
  246 + return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  247 + }
  248 + cnt, usersData, err = userRespository.Find(domain.UsersFindQuery{
  249 + UserNameMatch: queryOption.UserNameMatch,
  250 + Offset: queryOption.Offset,
  251 + Limit: queryOption.Limit,
  252 + })
  253 + var permissionQuery domain.PermissionFindOption
  254 + permissionList, err = permissionRepository.Find(permissionQuery)
  255 + if err != nil {
  256 + return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  257 + }
  258 + err = transactionContext.CommitTransaction()
  259 + result := service.buildGetUserList(usersData, permissionList)
  260 + return cnt, result, nil
  261 +}
  262 +
  263 +//buildGetUserList 组装构建前端需要的用户列表数据
  264 +func (service UsersService) buildGetUserList(usersData []domain.Users, permissionData []domain.AdminPermission) []map[string]interface{} {
  265 + result := make([]map[string]interface{}, len(usersData))
  266 + permissionMap := map[int64]domain.AdminPermission{}
  267 + for i := range permissionData {
  268 + permissionMap[permissionData[i].Id] = permissionData[i]
  269 + }
  270 + for i := range usersData {
  271 + permissionTypes := []string{}
  272 + if usersData[i].IsSuperAdmin() {
  273 + for _, pp := range permissionData {
  274 + permissionTypes = append(permissionTypes, pp.Name)
  275 + }
  276 + } else {
  277 + for _, vv := range usersData[i].Permission {
  278 + if pm, ok := permissionMap[vv.Id]; ok {
  279 + permissionTypes = append(permissionTypes, pm.Name)
  280 + }
  281 + }
  282 + }
  283 + m := map[string]interface{}{
  284 + "id": usersData[i].Id,
  285 + "account": usersData[i].Phone,
  286 + "name": usersData[i].Name,
  287 + "permission": permissionTypes,
  288 + "isAdmin": 0,
  289 + "partnership": len(usersData[i].AccessPartners),
  290 + }
  291 + if usersData[i].IsSuperAdmin() {
  292 + m["isAdmin"] = 1
  293 + }
  294 + result = append(result, m)
  295 + }
  296 + return result
  297 +}
@@ -137,12 +137,13 @@ type UsersFindOneQuery struct { @@ -137,12 +137,13 @@ type UsersFindOneQuery struct {
137 } 137 }
138 138
139 type UsersFindQuery struct { 139 type UsersFindQuery struct {
140 - AdminType int8  
141 - ChargeStatus int8  
142 - CompanyId int64  
143 - Ids []int64  
144 - Offset int  
145 - Limit int 140 + AdminType int8
  141 + ChargeStatus int8
  142 + CompanyId int64
  143 + Ids []int64
  144 + Offset int
  145 + Limit int
  146 + UserNameMatch string
146 } 147 }
147 type UsersRepository interface { 148 type UsersRepository interface {
148 Add(*Users) error 149 Add(*Users) error
@@ -151,6 +151,9 @@ func (reponsitory UsersRepository) Find(queryOption domain.UsersFindQuery) (int, @@ -151,6 +151,9 @@ func (reponsitory UsersRepository) Find(queryOption domain.UsersFindQuery) (int,
151 if queryOption.AdminType > 0 { 151 if queryOption.AdminType > 0 {
152 query = query.Where("admin_type=?", queryOption.AdminType) 152 query = query.Where("admin_type=?", queryOption.AdminType)
153 } 153 }
  154 + if len(queryOption.UserNameMatch) > 0 {
  155 + query = query.Where("name like ?", "%"+queryOption.UserNameMatch+"%")
  156 + }
154 if queryOption.Offset >= 0 { 157 if queryOption.Offset >= 0 {
155 query = query.Offset(queryOption.Offset) 158 query = query.Offset(queryOption.Offset)
156 } 159 }
  1 +package controllers
  2 +
  3 +import (
  4 + "errors"
  5 +
  6 + "github.com/astaxie/beego/logs"
  7 + userQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/query"
  8 + userService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/service"
  9 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
  10 +)
  11 +
  12 +type UserController struct {
  13 + BaseController
  14 +}
  15 +
  16 +//Prepare 重写 BaseController 的Prepare方法
  17 +func (c *UserController) Prepare() {
  18 + c.BaseController.Prepare()
  19 + if ok := c.ValidJWTToken(); !ok {
  20 + return
  21 + }
  22 + if ok := c.ValidAdminPermission(domain.PERMINSSION_ADMIN_USER); !ok {
  23 + return
  24 + }
  25 +}
  26 +
  27 +//ListUser 获取用户分页列表
  28 +func (c *AdminUserController) ListUser() {
  29 + //用与适配前端定义的数据结构
  30 + type Paramter struct {
  31 + SearchText string `json:"searchText"`
  32 + PageSize int `json::"pageSize"`
  33 + PageNumber int `json:"pageNumber"`
  34 + }
  35 + var (
  36 + param Paramter
  37 + err error
  38 + )
  39 + if err = c.BindJsonData(&param); err != nil {
  40 + logs.Error(err)
  41 + c.ResponseError(errors.New("json数据解析失败"))
  42 + return
  43 + }
  44 + if param.PageSize == 0 {
  45 + param.PageSize = 20
  46 + }
  47 + if param.PageNumber == 0 {
  48 + param.PageNumber = 1
  49 + }
  50 + newUserService := userService.NewUsersService(nil)
  51 + queryOption := userQuery.UserListQuery{
  52 + UserNameMatch: param.SearchText,
  53 + Limit: param.PageSize,
  54 + Offset: param.PageSize * (param.PageNumber - 1),
  55 + }
  56 + cnt, usersData, err := newUserService.GetUserList(queryOption)
  57 + if err != nil {
  58 + c.ResponseError(err)
  59 + return
  60 + }
  61 + c.ResponsePageList(usersData, cnt, param.PageNumber)
  62 + return
  63 +}