|
|
@@ -1,5 +1,6 @@ |
|
|
|
package com.tuoheng.system.utils; |
|
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil; |
|
|
|
import com.tuoheng.common.exception.ServiceException; |
|
|
|
import com.tuoheng.common.utils.SpringUtils; |
|
|
|
import com.tuoheng.system.entity.User; |
|
|
@@ -45,7 +46,11 @@ public class ShiroUtils { |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static User getUserInfo() { |
|
|
|
return (User) SecurityUtils.getSubject().getPrincipal(); |
|
|
|
User user = (User) SecurityUtils.getSubject().getPrincipal(); |
|
|
|
if(ObjectUtil.isEmpty(user)){ |
|
|
|
throw new ServiceException(HttpStatus.UNAUTHORIZED.value(), "请先登录"); |
|
|
|
} |
|
|
|
return user; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@@ -53,8 +58,11 @@ public class ShiroUtils { |
|
|
|
* |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static Integer getUserId() { |
|
|
|
public static Integer getUserId() throws ServiceException { |
|
|
|
User user = getUserInfo(); |
|
|
|
if(ObjectUtil.isEmpty(user)){ |
|
|
|
throw new ServiceException(HttpStatus.UNAUTHORIZED.value(), "请先登录"); |
|
|
|
} |
|
|
|
return Optional.ofNullable(user.getId()).orElseThrow(() -> |
|
|
|
new ServiceException(HttpStatus.BAD_REQUEST.value(), "未获取到当前用户信息!")); |
|
|
|
} |
|
|
@@ -66,6 +74,9 @@ public class ShiroUtils { |
|
|
|
*/ |
|
|
|
public static Integer getTenantId() { |
|
|
|
User user = getUserInfo(); |
|
|
|
if(ObjectUtil.isEmpty(user)){ |
|
|
|
throw new ServiceException(HttpStatus.UNAUTHORIZED.value(), "请先登录"); |
|
|
|
} |
|
|
|
return Optional.ofNullable(user.getTenantId()).orElseThrow(() -> |
|
|
|
new ServiceException(HttpStatus.BAD_REQUEST.value(), "未获取到当前租户信息!")); |
|
|
|
} |