From 70834b64d14f1701995d7806261e2f6e3ab5a9a20cff4cbf19799abc9e79712b Mon Sep 17 00:00:00 2001 From: attiya <2413103649@qq.com> Date: Mon, 1 Sep 2025 14:26:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E7=A7=9F=E6=88=B7=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../staff/component/EbikeTenantFactory.java | 21 ++++++++++++ .../staff/component/TenantInterceptor.java | 33 +++++++++++++++++++ .../java/com/cdzy/staff/config/WebConfig.java | 23 +++++++++++++ .../model/entity/EbikeOperatorStaff.java | 2 +- 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 ebike-staff/src/main/java/com/cdzy/staff/component/EbikeTenantFactory.java create mode 100644 ebike-staff/src/main/java/com/cdzy/staff/component/TenantInterceptor.java create mode 100644 ebike-staff/src/main/java/com/cdzy/staff/config/WebConfig.java 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;