From 3f38072356ab9cd11e3a8a15c9fefcd32fbea160 Mon Sep 17 00:00:00 2001 From: yanyang Date: Thu, 2 Mar 2023 15:12:54 +0800 Subject: [PATCH] common --- ym-admin/pom.xml | 5 ++ .../java/com/cnbm/admin/base/BaseSupport.java | 62 +++++++++++------- .../java/com/cnbm/admin/dto/LoginUserDTO.java | 65 +++++++++++++++++++ 3 files changed, 107 insertions(+), 25 deletions(-) create mode 100644 ym-admin/src/main/java/com/cnbm/admin/dto/LoginUserDTO.java diff --git a/ym-admin/pom.xml b/ym-admin/pom.xml index 132653d..54a505d 100644 --- a/ym-admin/pom.xml +++ b/ym-admin/pom.xml @@ -43,6 +43,11 @@ ym-common 1.0-SNAPSHOT + + com.alibaba + fastjson + 1.2.75 + diff --git a/ym-admin/src/main/java/com/cnbm/admin/base/BaseSupport.java b/ym-admin/src/main/java/com/cnbm/admin/base/BaseSupport.java index 64738e3..6779778 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/base/BaseSupport.java +++ b/ym-admin/src/main/java/com/cnbm/admin/base/BaseSupport.java @@ -5,18 +5,13 @@ package com.cnbm.admin.base; -import com.cnbm.admin.entity.LoginUser; +import com.alibaba.fastjson.JSON; +import com.cnbm.admin.dto.LoginUserDTO; import com.cnbm.admin.enums.WhetherEnum; import lombok.Builder; import lombok.Data; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.NamedThreadLocal; -import org.springframework.security.authentication.AuthenticationManager; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; @@ -26,10 +21,6 @@ import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.Serializable; import java.time.LocalDateTime; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import java.util.concurrent.TimeUnit; /** * 接口支持基类 @@ -41,19 +32,40 @@ import java.util.concurrent.TimeUnit; @Service public class BaseSupport { + protected LoginUserDTO getLoginUser() { + // 后续完善拦截器再使用该方式 +// LoginUser loginUser = loginUserHolder.get(); +// if (loginUser != null) { +// return loginUser; +// } + HttpSession session = getHttpServletRequest().getSession(false); + LoginUserDTO loginUser = null; + if (session != null) { + String loginUserJson = (String) session.getAttribute(LoginUserDTO.HTTP_HEADER_NAME); + if (StringUtils.isNotBlank(loginUserJson)) { + loginUser = JSON.parseObject(loginUserJson, LoginUserDTO.class); + return loginUser; + } + } + return null; + } + /** - * 获取当前登录用户信息 + * 获取当前http请求对象 * * @return */ - protected LoginUser getLoginUser() { - UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); + protected HttpServletRequest getHttpServletRequest() { + return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); + } - if (Objects.isNull(authentication)) { - LoginUser loginUser = (LoginUser) authentication.getPrincipal(); - return loginUser; - } - return null; + /** + * 获取当前http响应对象 + * + * @return + */ + protected HttpServletResponse getHttpServletResponse() { + return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); } @@ -81,11 +93,11 @@ public class BaseSupport { .enabled(WhetherEnum.YES.getValue()) .valid(WhetherEnum.YES.getValue()) .createTime(LocalDateTime.now()) - .creatorId(getLoginUser().getSysUserEntity().getId()) - .creatorName(getLoginUser().getUsername()) + .creatorId(getLoginUser().getUserId()) + .creatorName(getLoginUser().getUserName()) .updateTime(LocalDateTime.now()) - .updaterId(getLoginUser().getSysUserEntity().getId()) - .updaterName(getLoginUser().getUsername()) + .updaterId(getLoginUser().getUserId()) + .updaterName(getLoginUser().getUserName()) .build(); BeanUtils.copyProperties(commonField, t, ignoreProperties); return t; @@ -105,8 +117,8 @@ public class BaseSupport { */ public T setUpdateCommonField(T t) { CommonField commonField = CommonField.builder() - .updaterId(getLoginUser().getSysUserEntity().getId()) - .updaterName(getLoginUser().getUsername()) + .updaterId(getLoginUser().getUserId()) + .updaterName(getLoginUser().getUserName()) .updateTime(LocalDateTime.now()) .build(); BeanUtils.copyProperties(commonField, t, "enabled", "valid"); diff --git a/ym-admin/src/main/java/com/cnbm/admin/dto/LoginUserDTO.java b/ym-admin/src/main/java/com/cnbm/admin/dto/LoginUserDTO.java new file mode 100644 index 0000000..46f4177 --- /dev/null +++ b/ym-admin/src/main/java/com/cnbm/admin/dto/LoginUserDTO.java @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2018. + * http://www.ulabcare.com + */ + +package com.cnbm.admin.dto; + +import lombok.Builder; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +import java.io.Serializable; + +/** + * @author jiff + * @date 2018/11/1 + * @since 1.0 + */ +@EqualsAndHashCode(callSuper = false) +@Builder +@Data +@Accessors(chain = true) +public class LoginUserDTO implements Serializable { + public static final String HTTP_HEADER_NAME = "loginUser"; + public static final int USER_TYPE_PLATFORM = 1; + public static final int USER_TYPE_PARTNER = 2; + public static final int USER_TYPE_HOSPITAL = 3; + public static final int USER_TYPE_PATIENT = 4; + /** + * 会话ID + */ + private String sessionId; + private Long passportUserId; + private Long userId; + private Long partnerId; + private Long hospitalId; + private Long orgId; + private String mobile; + private String userName; + /** + * 用户类型 + */ + private int userType; + /** + * 应用类型 + */ + private int appType; + /** + * 应用代码 + */ + private int appCode; + /** + * 登录类型:1、自主登录,2、漫游登录 + */ + private int loginType; + /** + * 微信appId + */ + private String wechatAppId; + /** + * 微信openId + */ + private String openId; +}