作者 唐旭辉

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

... ... @@ -3,11 +3,11 @@ package command
import "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
type LoginBySecretKeyCommand struct {
Secret string `json:"secret"`
Code string `json:"code"`
}
func (login LoginBySecretKeyCommand) ValidateCommand() error {
if len(login.Secret) == 0 {
if len(login.Code) == 0 {
return lib.ThrowError(lib.ARG_ERROR, "登录参数错误")
}
return nil
... ...
package query
type UserListQuery struct {
//用户名称匹配
UserNameMatch string `json:"userNameMatch" `
// 查询偏离量
Offset int `json:"offset" `
// 查询限制
Limit int `json:"limit"`
}
... ...
... ... @@ -26,9 +26,9 @@ func (service UsersService) UserLoginBySecretKey(cmd command.LoginBySecretKeyCom
}
//向统一用户中心确认密钥信息并获取用户数据
ucenterService := serviceGateway.NewMmmUserCenterServiceGateway()
loginResp, err := ucenterService.RequestUCenterLoginBySecret(cmd.Secret)
loginResp, err := ucenterService.RequestUCenterLoginBySecret(cmd.Code)
if err != nil {
e := fmt.Sprintf("通过密钥(secret=%s)从统一用户中心获取数据失败:%s", cmd.Secret, err.Error())
e := fmt.Sprintf("通过密钥(code=%s)从统一用户中心获取数据失败:%s", cmd.Code, err.Error())
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
var (
... ... @@ -215,3 +215,83 @@ func (service UsersService) ValidateAdminpPermission(queryOption query.ValidateP
ok := usersData.HasPermissionByCode(queryOption.PermissionCode)
return ok, nil
}
//获取用户列表
func (service UsersService) GetUserList(queryOption query.UserListQuery) (int, []map[string]interface{}, error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return 0, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
userRespository domain.UsersRepository
usersData []domain.Users
cnt int
permissionRepository domain.AdminPermissionRepository
permissionList []domain.AdminPermission
)
if userRespository, err = factory.CreateUsersRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if permissionRepository, err = factory.CreateAdminPermissionRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
cnt, usersData, err = userRespository.Find(domain.UsersFindQuery{
UserNameMatch: queryOption.UserNameMatch,
Offset: queryOption.Offset,
Limit: queryOption.Limit,
})
var permissionQuery domain.PermissionFindOption
permissionList, err = permissionRepository.Find(permissionQuery)
if err != nil {
return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
result := service.buildGetUserList(usersData, permissionList)
return cnt, result, nil
}
//buildGetUserList 组装构建前端需要的用户列表数据
func (service UsersService) buildGetUserList(usersData []domain.Users, permissionData []domain.AdminPermission) []map[string]interface{} {
result := make([]map[string]interface{}, len(usersData))
permissionMap := map[int64]domain.AdminPermission{}
for i := range permissionData {
permissionMap[permissionData[i].Id] = permissionData[i]
}
for i := range usersData {
permissionTypes := []string{}
if usersData[i].IsSuperAdmin() {
for _, pp := range permissionData {
permissionTypes = append(permissionTypes, pp.Name)
}
} else {
for _, vv := range usersData[i].Permission {
if pm, ok := permissionMap[vv.Id]; ok {
permissionTypes = append(permissionTypes, pm.Name)
}
}
}
m := map[string]interface{}{
"id": usersData[i].Id,
"account": usersData[i].Phone,
"name": usersData[i].Name,
"permission": permissionTypes,
"isAdmin": 0,
"partnership": len(usersData[i].AccessPartners),
}
if usersData[i].IsSuperAdmin() {
m["isAdmin"] = 1
}
result = append(result, m)
}
return result
}
... ...
... ... @@ -137,12 +137,13 @@ type UsersFindOneQuery struct {
}
type UsersFindQuery struct {
AdminType int8
ChargeStatus int8
CompanyId int64
Ids []int64
Offset int
Limit int
AdminType int8
ChargeStatus int8
CompanyId int64
Ids []int64
Offset int
Limit int
UserNameMatch string
}
type UsersRepository interface {
Add(*Users) error
... ...
... ... @@ -151,6 +151,9 @@ func (reponsitory UsersRepository) Find(queryOption domain.UsersFindQuery) (int,
if queryOption.AdminType > 0 {
query = query.Where("admin_type=?", queryOption.AdminType)
}
if len(queryOption.UserNameMatch) > 0 {
query = query.Where("name like ?", "%"+queryOption.UserNameMatch+"%")
}
if queryOption.Offset >= 0 {
query = query.Offset(queryOption.Offset)
}
... ...
package controllers
import (
"errors"
"github.com/astaxie/beego/logs"
userQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/query"
userService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/service"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
)
type UserController struct {
BaseController
}
//Prepare 重写 BaseController 的Prepare方法
func (c *UserController) Prepare() {
c.BaseController.Prepare()
if ok := c.ValidJWTToken(); !ok {
return
}
if ok := c.ValidAdminPermission(domain.PERMINSSION_ADMIN_USER); !ok {
return
}
}
//ListUser 获取用户分页列表
func (c *AdminUserController) ListUser() {
//用与适配前端定义的数据结构
type Paramter struct {
SearchText string `json:"searchText"`
PageSize int `json::"pageSize"`
PageNumber int `json:"pageNumber"`
}
var (
param Paramter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
if param.PageSize == 0 {
param.PageSize = 20
}
if param.PageNumber == 0 {
param.PageNumber = 1
}
newUserService := userService.NewUsersService(nil)
queryOption := userQuery.UserListQuery{
UserNameMatch: param.SearchText,
Limit: param.PageSize,
Offset: param.PageSize * (param.PageNumber - 1),
}
cnt, usersData, err := newUserService.GetUserList(queryOption)
if err != nil {
c.ResponseError(err)
return
}
c.ResponsePageList(usersData, cnt, param.PageNumber)
return
}
... ...