Add GetUserInfo

This commit is contained in:
2023-10-27 18:20:15 +08:00
parent 7aa2212976
commit 11ceb410c8
13 changed files with 181 additions and 7 deletions

View File

@@ -3,7 +3,10 @@ package top.fatweb.api.controller.permission
import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RestController
import top.fatweb.api.converter.UserConverter
import top.fatweb.api.entity.common.ResponseResult
import top.fatweb.api.service.permission.IUserService import top.fatweb.api.service.permission.IUserService
import top.fatweb.api.vo.authentication.UserInfoVo
/** /**
* <p> * <p>
@@ -19,7 +22,10 @@ class UserController(
private val userService: IUserService private val userService: IUserService
) { ) {
@GetMapping("info") @GetMapping("info")
fun getInfo() { fun getInfo(): ResponseResult<UserInfoVo> {
userService.getInfo()?.let {
return ResponseResult.databaseSuccess(data = UserConverter.userToUserInfoVo(it))
} ?: let { return ResponseResult.databaseFail() }
} }
} }

View File

@@ -6,7 +6,7 @@ import top.fatweb.api.vo.PageVo
import top.fatweb.api.vo.system.SysLogGetVo import top.fatweb.api.vo.system.SysLogGetVo
object SysLogConverter { object SysLogConverter {
fun sysLogPageToSysLogPageVo(syslogPage: IPage<SysLog>): PageVo<SysLogGetVo> = PageVo<SysLogGetVo>( fun sysLogPageToSysLogPageVo(syslogPage: IPage<SysLog>): PageVo<SysLogGetVo> = PageVo(
syslogPage.total, syslogPage.total,
syslogPage.pages, syslogPage.pages,
syslogPage.size, syslogPage.size,

View File

@@ -2,6 +2,10 @@ package top.fatweb.api.converter
import top.fatweb.api.entity.permission.User import top.fatweb.api.entity.permission.User
import top.fatweb.api.param.authentication.LoginParam import top.fatweb.api.param.authentication.LoginParam
import top.fatweb.api.vo.authentication.ElementVo
import top.fatweb.api.vo.authentication.MenuVo
import top.fatweb.api.vo.authentication.OperationVo
import top.fatweb.api.vo.authentication.UserInfoVo
object UserConverter { object UserConverter {
fun loginParamToUser(loginParam: LoginParam): User { fun loginParamToUser(loginParam: LoginParam): User {
@@ -12,4 +16,45 @@ object UserConverter {
return user return user
} }
fun userToUserInfoVo(user: User) = UserInfoVo(
id = user.id,
username = user.username,
locking = user.locking?.let { it == 1 },
expiration = user.expiration,
credentialsExpiration = user.credentialsExpiration,
enable = user.enable?.let { it == 1 },
lastLoginTime = user.lastLoginTime,
lastLoginIp = user.lastLoginIp,
createTime = user.createTime,
updateTime = user.updateTime,
menus = user.menus?.map {
MenuVo(
id = it.id,
name = it.name,
url = it.url,
powerId = it.powerId,
parentId = it.parentId
)
},
elements = user.elements?.map {
ElementVo(
id = it.id,
name = it.name,
powerId = it.powerId,
parentId = it.parentId,
menuId = it.menuId
)
},
operations = user.operations?.map {
OperationVo(
id = it.id,
name = it.name,
code = it.code,
powerId = it.powerId,
parentId = it.parentId,
elementId = it.elementId
)
}
)
} }

View File

@@ -31,6 +31,12 @@ class Element : Serializable {
@TableField("power_id") @TableField("power_id")
var powerId: Long? = null var powerId: Long? = null
/**
* 父ID
*/
@TableField("parent_id")
var parentId: Long? = null
/** /**
* 菜单ID * 菜单ID
*/ */

View File

@@ -14,5 +14,5 @@ import top.fatweb.api.entity.permission.User
interface IUserService : IService<User> { interface IUserService : IService<User> {
fun getUserWithPower(username: String): User? fun getUserWithPower(username: String): User?
fun getInfo(): User fun getInfo(): User?
} }

View File

@@ -8,6 +8,7 @@ import top.fatweb.api.service.permission.IElementService
import top.fatweb.api.service.permission.IMenuService import top.fatweb.api.service.permission.IMenuService
import top.fatweb.api.service.permission.IOperationService import top.fatweb.api.service.permission.IOperationService
import top.fatweb.api.service.permission.IUserService import top.fatweb.api.service.permission.IUserService
import top.fatweb.api.util.WebUtil
/** /**
* <p> * <p>
@@ -35,4 +36,7 @@ class UserServiceImpl(
return user return user
} }
override fun getInfo() = WebUtil.getLoginUsername()?.let { getUserWithPower(it) } ?: let { null }
} }

View File

@@ -11,6 +11,8 @@ object WebUtil {
fun getLoginUserId() = getLoginUser()?.user?.id fun getLoginUserId() = getLoginUser()?.user?.id
fun getLoginUsername() = getLoginUser()?.user?.username
fun getToken(tokenWithPrefix: String) = tokenWithPrefix.removePrefix(SecurityConstants.tokenPrefix) fun getToken(tokenWithPrefix: String) = tokenWithPrefix.removePrefix(SecurityConstants.tokenPrefix)
fun getToken(request: HttpServletRequest) = getToken(request.getHeader(SecurityConstants.headerString)) fun getToken(request: HttpServletRequest) = getToken(request.getHeader(SecurityConstants.headerString))

View File

@@ -0,0 +1,20 @@
package top.fatweb.api.vo.authentication
import io.swagger.v3.oas.annotations.media.Schema
@Schema(description = "页面元素返回参数")
data class ElementVo(
val id: Long?,
@Schema(description = "元素名", example = "AddButton")
val name: String?,
@Schema(description = "权限 ID")
val powerId: Long?,
@Schema(description = "父 ID")
val parentId: Long?,
@Schema(description = "菜单 ID")
val menuId: Long?
)

View File

@@ -0,0 +1,20 @@
package top.fatweb.api.vo.authentication
import io.swagger.v3.oas.annotations.media.Schema
@Schema(description = "菜单返回参数")
data class MenuVo(
val id: Long?,
@Schema(description = "菜单名", example = "System")
val name: String?,
@Schema(description = "URL", example = "/system")
val url: String?,
@Schema(description = "权限 ID")
val powerId: Long?,
@Schema(description = "父 ID")
val parentId: Long?
)

View File

@@ -0,0 +1,23 @@
package top.fatweb.api.vo.authentication
import io.swagger.v3.oas.annotations.media.Schema
@Schema(description = "功能返回参数")
data class OperationVo(
val id: Long?,
@Schema(description = "功能名", example = "Add User")
val name: String?,
@Schema(description = "功能编码", example = "system:user:add")
val code: String?,
@Schema(description = "权限 ID")
val powerId: Long?,
@Schema(description = "父 ID")
val parentId: Long?,
@Schema(description = "页面元素 ID")
val elementId: Long?
)

View File

@@ -0,0 +1,45 @@
package top.fatweb.api.vo.authentication
import io.swagger.v3.oas.annotations.media.Schema
import java.time.LocalDateTime
@Schema(description = "获取用户信息返回参数")
data class UserInfoVo(
val id: Long?,
@Schema(description = "用户名", example = "User")
val username: String?,
@Schema(description = "是否锁定", example = "false")
val locking: Boolean?,
@Schema(description = "过期时间", example = "1900-01-01T00:00:00.000Z")
val expiration: LocalDateTime?,
@Schema(description = "认证过期时间", example = "1900-01-01T00:00:00.000Z")
val credentialsExpiration: LocalDateTime?,
@Schema(description = "是否启用", example = "true")
val enable: Boolean?,
@Schema(description = "最后登录时间", example = "1900-01-01T00:00:00.000Z")
val lastLoginTime: LocalDateTime?,
@Schema(description = "最后登录 IP", example = "1.1.1.1")
val lastLoginIp: String?,
@Schema(description = "创建时间", example = "1900-01-01T00:00:00.000Z")
val createTime: LocalDateTime?,
@Schema(description = "修改时间", example = "1900-01-01T00:00:00.000Z")
val updateTime: LocalDateTime?,
@Schema(description = "菜单列表")
val menus: List<MenuVo>?,
@Schema(description = "页面元素列表")
val elements: List<ElementVo>?,
@Schema(description = "功能列表")
val operations: List<OperationVo>?
)

View File

@@ -5,5 +5,6 @@ create table if not exists t_element
id bigint not null primary key, id bigint not null primary key,
name varchar(100) not null comment '元素名', name varchar(100) not null comment '元素名',
power_id bigint not null comment '权限ID', power_id bigint not null comment '权限ID',
parent_id bigint not null comment '父ID',
menu_id bigint not null comment '菜单ID' menu_id bigint not null comment '菜单ID'
) comment '页面元素'; ) comment '页面元素';

View File

@@ -23,6 +23,7 @@
te.id as element_id, te.id as element_id,
te.name as element_name, te.name as element_name,
te.power_id as element_power_id, te.power_id as element_power_id,
te.parent_id as element_parent_id,
te.menu_id as element_menu_id, te.menu_id as element_menu_id,
t.id as operation_id, t.id as operation_id,
t.name as operation_name, t.name as operation_name,
@@ -74,6 +75,7 @@
<id property="id" column="element_id"/> <id property="id" column="element_id"/>
<result property="name" column="element_name"/> <result property="name" column="element_name"/>
<result property="powerId" column="element_power_id"/> <result property="powerId" column="element_power_id"/>
<result property="parentId" column="element_parent_id"/>
<result property="menuId" column="element_menu_id"/> <result property="menuId" column="element_menu_id"/>
</collection> </collection>
<collection property="operations" ofType="operation"> <collection property="operations" ofType="operation">