员工权限相关接口
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package com.czg.annotation;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import cn.dev33.satoken.annotation.SaMode;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 权限认证(User版):必须具有指定权限才能进入该方法
|
||||
* <p> 可标注在函数、类上(效果等同于标注在此类的所有方法上)
|
||||
* @author click33
|
||||
*
|
||||
*/
|
||||
//@SaCheckPermission(type = "user")
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE})
|
||||
public @interface SaStaffCheckPermission {
|
||||
|
||||
/**
|
||||
* 需要校验的权限码
|
||||
* @return 需要校验的权限码
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.czg.handler;
|
||||
|
||||
import cn.dev33.satoken.annotation.handler.SaAnnotationHandlerInterface;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.czg.annotation.SaStaffCheckPermission;
|
||||
import com.czg.sa.StpKit;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* 员工权限校验
|
||||
* @author Administrator
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class SaStaffCheckPermissionHandler implements SaAnnotationHandlerInterface<SaStaffCheckPermission> {
|
||||
|
||||
@Override
|
||||
public Class<SaStaffCheckPermission> getHandlerAnnotationClass() {
|
||||
return SaStaffCheckPermission.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkMethod(SaStaffCheckPermission at, Method method) {
|
||||
if (at != null && StrUtil.isNotBlank(at.value()) && StpKit.USER.isStaff()) {
|
||||
StpKit.USER.checkPermission(at.value());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,10 @@ public class MyStpLogic {
|
||||
public static final StpLogic CLIENT_LOGIC = new StpLogic("client");
|
||||
public static final StpLogic ADMIN_LOGIC = new StpLogic("admin");
|
||||
|
||||
public enum LoginType {
|
||||
MANAGER, USER, STAFF
|
||||
}
|
||||
|
||||
private StpLogic getLogic() {
|
||||
boolean hit = SaRouter.match("/admin/**").isHit();
|
||||
if (hit) {
|
||||
@@ -37,16 +41,16 @@ public class MyStpLogic {
|
||||
/**
|
||||
* @param id 登录账号id
|
||||
* @param shopId 店铺id
|
||||
* @param isManager true 管理端 false 用户端
|
||||
* @param loginType 登录类型枚举
|
||||
* @param isAdmin 是否为管理员账号
|
||||
*/
|
||||
public void login(Long id, Long shopId, boolean isManager, boolean isAdmin) {
|
||||
public void login(Long id, Long shopId, LoginType loginType, boolean isAdmin) {
|
||||
StpLogic logic = getLogic();
|
||||
logic.login(id);
|
||||
if (isManager && shopId == null) {
|
||||
if (loginType.equals(LoginType.MANAGER) && shopId == null) {
|
||||
throw new ApiNotPrintException("管理端登录必须传递店铺id");
|
||||
}
|
||||
SaSession session = logic.getSession().set("userId", id).set("isAdmin", isAdmin).set("isManager", isManager);
|
||||
SaSession session = logic.getSession().set("userId", id).set("isAdmin", isAdmin).set("isManager", loginType.equals(LoginType.MANAGER)).set("loginType", loginType);
|
||||
if (shopId != null) {
|
||||
session.set("shopId", shopId);
|
||||
}
|
||||
@@ -104,6 +108,28 @@ public class MyStpLogic {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为管理端登录
|
||||
*
|
||||
* @return 布尔值
|
||||
*/
|
||||
public boolean isStaff() {
|
||||
StpLogic logic = getLogic();
|
||||
Object object = logic.getSession().get("loginType");
|
||||
return object instanceof LoginType t && t.equals(LoginType.STAFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为管理端登录
|
||||
*
|
||||
* @return 布尔值
|
||||
*/
|
||||
public boolean isManager() {
|
||||
StpLogic logic = getLogic();
|
||||
Object object = logic.getSession().get("isManager");
|
||||
return object instanceof Boolean t && t;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为管理员
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user