lzq 2025-12-09 18:05:34 +08:00
parent 2f8f3eb381
commit e625468fe4
12 changed files with 4086 additions and 1363 deletions

View File

@ -0,0 +1,57 @@
package com.njzscloud.common.core.mitt;
import com.njzscloud.common.core.thread.ThreadPool;
import lombok.extern.slf4j.Slf4j;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
public class Mitt {
private static final Map<MittEvent<?>, List<MittHandler<?>>> POOL = new ConcurrentHashMap<>();
private static final Map<MittVoidEvent, List<MittVoidHandler>> VOID_POOL = new ConcurrentHashMap<>();
public static <T> void on(MittEvent<T> mittEvent, MittHandler<T> handlerIns) {
POOL.compute(mittEvent, (k, v) -> {
if (v == null) v = new LinkedList<>();
v.add(handlerIns);
return v;
});
}
public static <T> void emit(MittEvent<T> mittEvent, T t) {
POOL.computeIfPresent(mittEvent, (k, v) -> {
for (MittHandler<?> handlerIns : v) {
ThreadPool.runAsync(() -> handlerIns.on0(t))
.exceptionally(e -> {
log.error("执行失败,事件名称:{}", mittEvent, e);
return null;
});
}
return v;
});
}
public static void on(MittVoidEvent mittEvent, MittVoidHandler handlerIns) {
VOID_POOL.compute(mittEvent, (k, v) -> {
if (v == null) v = new LinkedList<>();
v.add(handlerIns);
return v;
});
}
public static void emit(MittVoidEvent mittEvent) {
VOID_POOL.computeIfPresent(mittEvent, (k, v) -> {
for (MittVoidHandler handlerIns : v) {
ThreadPool.runAsync(handlerIns::on0)
.exceptionally(e -> {
log.error("执行失败,事件名称:{}", mittEvent, e);
return null;
});
}
return v;
});
}
}

View File

@ -0,0 +1,13 @@
package com.njzscloud.common.core.mitt;
public interface MittEvent<T> {
default void emit(T t) {
Mitt.emit(this, t);
}
default void on(MittHandler<T> handlerIns) {
Mitt.on(this, handlerIns);
}
void event();
}

View File

@ -0,0 +1,11 @@
package com.njzscloud.common.core.mitt;
@FunctionalInterface
public interface MittHandler<T> {
void on(T msg);
@SuppressWarnings("unchecked")
default void on0(Object msg) {
on((T) msg);
}
}

View File

@ -0,0 +1,14 @@
package com.njzscloud.common.core.mitt;
public interface MittVoidEvent {
default void emit() {
Mitt.emit(this);
}
default void on(MittVoidHandler handlerIns) {
Mitt.on(this, handlerIns);
}
void event();
}

View File

@ -0,0 +1,6 @@
package com.njzscloud.common.core.mitt;
public interface MittVoidHandler {
void on0();
}

View File

@ -1,13 +1,19 @@
package com.njzscloud.supervisory.config;
import com.njzscloud.common.security.util.SecurityUtil;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import java.util.List;
import static com.njzscloud.supervisory.event.SysMittEvent.COERCE_LOGOUT;
@Component
public class SpringReady {
@EventListener(ApplicationReadyEvent.class)
public void onApplicationReady() {
COERCE_LOGOUT.on((List<Long> ids) -> ids.forEach(SecurityUtil::removeToken));
}
}

View File

@ -0,0 +1,11 @@
package com.njzscloud.supervisory.event;
import com.njzscloud.common.core.mitt.MittEvent;
import java.util.List;
public class SysMittEvent {
public static MittEvent<List<Long>> COERCE_LOGOUT = () -> {
};
}

View File

@ -26,4 +26,6 @@ public interface RoleMapper extends BaseMapper<RoleEntity> {
List<RoleEntity> listUserRole(@Param("userId") Long userId);
Boolean candel(@Param("ew") QueryWrapper<Object> ew);
List<Long> users(@Param("id") Long id);
}

View File

@ -28,6 +28,7 @@ import java.util.Set;
import java.util.stream.Collectors;
import static com.njzscloud.common.security.contant.Constants.ROLE_ADMIN;
import static com.njzscloud.supervisory.event.SysMittEvent.COERCE_LOGOUT;
/**
*
@ -126,5 +127,7 @@ public class RoleService extends ServiceImpl<RoleMapper, RoleEntity> implements
Set<Long> res = bindResParam.getRes();
Assert.notEmpty(res, () -> Exceptions.clierr("未指定资源"));
roleResService.bindRoleRes(id, res);
List<Long> ids = baseMapper.users(id);
COERCE_LOGOUT.emit(ids);
}
}

View File

@ -26,10 +26,13 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static com.njzscloud.supervisory.event.SysMittEvent.COERCE_LOGOUT;
/**
*
*/
@ -152,6 +155,7 @@ public class UserService extends ServiceImpl<UserMapper, UserEntity> implements
public void disable(Long id, Boolean disable) {
userAccountService.update(Wrappers.<UserAccountEntity>lambdaUpdate().eq(UserAccountEntity::getUserId, id)
.set(UserAccountEntity::getDisabled, disable));
COERCE_LOGOUT.emit(Collections.singletonList(id));
}
public void bindRole(BindRoleParam bindRoleParam) {
@ -161,16 +165,18 @@ public class UserService extends ServiceImpl<UserMapper, UserEntity> implements
Set<Long> roles = bindRoleParam.getRoles();
Assert.notEmpty(roles, () -> Exceptions.clierr("未指定角色"));
userRoleService.bindUserRole(id, roles);
COERCE_LOGOUT.emit(Collections.singletonList(id));
}
public void bindClient(Long id, Integer clientCode) {
userAccountService.update(Wrappers.<UserAccountEntity>lambdaUpdate().eq(UserAccountEntity::getUserId, id)
.set(UserAccountEntity::getClientCode, clientCode));
COERCE_LOGOUT.emit(Collections.singletonList(id));
}
public void resetPasswd(Long id) {
userAccountService.update(Wrappers.<UserAccountEntity>lambdaUpdate().eq(UserAccountEntity::getUserId, id)
.set(UserAccountEntity::getSecret, EncryptUtil.encrypt("666666")));
COERCE_LOGOUT.emit(Collections.singletonList(id));
}
}

View File

@ -26,4 +26,10 @@
${ew.customSqlSegment}
</if>
</select>
<select id="users" resultType="java.lang.Long">
SELECT b.user_id
FROM sys_role a
INNER JOIN sys_user_role b ON b.role_id = a.id
WHERE a.id = #{id}
</select>
</mapper>

File diff suppressed because it is too large Load Diff