user_controller.go 1.5 KB
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
}