diff --git a/ebike-staff/src/main/java/com/cdzy/staff/component/EbikeTenantFactory.java b/ebike-staff/src/main/java/com/cdzy/staff/component/EbikeTenantFactory.java new file mode 100644 index 0000000..2328752 --- /dev/null +++ b/ebike-staff/src/main/java/com/cdzy/staff/component/EbikeTenantFactory.java @@ -0,0 +1,21 @@ +package com.cdzy.staff.component; + +import cn.dev33.satoken.stp.StpUtil; +import com.mybatisflex.core.tenant.TenantFactory; +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.context.request.RequestContextHolder; + +public class EbikeTenantFactory implements TenantFactory { + + public Object[] getTenantIds() { + if (StpUtil.isLogin()) { + RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); + Object attribute = attributes.getAttribute("tenantId", RequestAttributes.SCOPE_REQUEST); + if (attribute != null) { + Long tenantId = Long.valueOf(attribute.toString()); + return new Object[]{tenantId}; + } + } + return null; + } +} \ No newline at end of file diff --git a/ebike-staff/src/main/java/com/cdzy/staff/component/TenantInterceptor.java b/ebike-staff/src/main/java/com/cdzy/staff/component/TenantInterceptor.java new file mode 100644 index 0000000..7f41cd2 --- /dev/null +++ b/ebike-staff/src/main/java/com/cdzy/staff/component/TenantInterceptor.java @@ -0,0 +1,33 @@ +package com.cdzy.staff.component; + +import cn.dev33.satoken.stp.StpUtil; +import com.cdzy.staff.model.dto.StaffInfo; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.jetbrains.annotations.NotNull; +import org.springframework.web.servlet.HandlerInterceptor; + + +public class TenantInterceptor implements HandlerInterceptor { + + @Override + public boolean preHandle(@NotNull HttpServletRequest request + , @NotNull HttpServletResponse response, @NotNull Object handler) throws Exception { + + //通过 request 去获取租户 ID + if (StpUtil.isLogin()) { + Long tenantId = getTenantIdByReuqest(request); + //设置租户ID到 request 的 attribute + request.setAttribute("tenantId", tenantId); + return true; + } + return true; + } + + Long getTenantIdByReuqest(HttpServletRequest request) { + String token = request.getHeader("Authorization"); + String id = (String) StpUtil.getLoginIdByToken(token); + StaffInfo staffDto = (StaffInfo) StpUtil.getSessionByLoginId(id).get(id); + return staffDto.getStaff().getOperatorId(); + } +} \ No newline at end of file diff --git a/ebike-staff/src/main/java/com/cdzy/staff/config/WebConfig.java b/ebike-staff/src/main/java/com/cdzy/staff/config/WebConfig.java new file mode 100644 index 0000000..d5a102a --- /dev/null +++ b/ebike-staff/src/main/java/com/cdzy/staff/config/WebConfig.java @@ -0,0 +1,23 @@ +package com.cdzy.staff.config; + +import com.cdzy.staff.component.EbikeTenantFactory; +import com.cdzy.staff.component.TenantInterceptor; +import com.mybatisflex.core.tenant.TenantFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebConfig implements WebMvcConfigurer { + + @Bean + public TenantFactory tenantFactory(){ + return new EbikeTenantFactory(); + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new TenantInterceptor()); + } +} \ No newline at end of file diff --git a/ebike-staff/src/main/java/com/cdzy/staff/model/entity/EbikeOperatorStaff.java b/ebike-staff/src/main/java/com/cdzy/staff/model/entity/EbikeOperatorStaff.java index 82e4840..a7111ea 100644 --- a/ebike-staff/src/main/java/com/cdzy/staff/model/entity/EbikeOperatorStaff.java +++ b/ebike-staff/src/main/java/com/cdzy/staff/model/entity/EbikeOperatorStaff.java @@ -29,7 +29,7 @@ public class EbikeOperatorStaff implements Serializable { @Serial private static final long serialVersionUID = 1L; - @Id(keyType = KeyType.Auto) + @Id private Long staffId; private String username;