|
@@ -0,0 +1,252 @@
|
|
|
|
|
+package shop.alien.store.util;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+import shop.alien.entity.store.StoreUser;
|
|
|
|
|
+import shop.alien.mapper.StoreUserMapper;
|
|
|
|
|
+import shop.alien.util.common.JwtUtil;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 登录用户工具类
|
|
|
|
|
+ * 用于获取当前登录用户的信息
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author dujian
|
|
|
|
|
+ * @since 2025-11-17
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Component
|
|
|
|
|
+public class LoginUserUtil {
|
|
|
|
|
+
|
|
|
|
|
+ private static StoreUserMapper storeUserMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ public void setStoreUserMapper(StoreUserMapper storeUserMapper) {
|
|
|
|
|
+ LoginUserUtil.storeUserMapper = storeUserMapper;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取当前登录用户的ID
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return 用户ID,如果未登录返回null
|
|
|
|
|
+ */
|
|
|
|
|
+ public static Integer getCurrentUserId() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ JSONObject userInfo = JwtUtil.getCurrentUserInfo();
|
|
|
|
|
+ if (userInfo != null) {
|
|
|
|
|
+ return userInfo.getInteger("userId");
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("LoginUserUtil.getCurrentUserId - 获取用户ID失败: {}", e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取当前登录用户的手机号
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return 手机号,如果未登录返回null
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String getCurrentUserPhone() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ JSONObject userInfo = JwtUtil.getCurrentUserInfo();
|
|
|
|
|
+ if (userInfo != null) {
|
|
|
|
|
+ return userInfo.getString("phone");
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("LoginUserUtil.getCurrentUserPhone - 获取用户手机号失败: {}", e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取当前登录用户的用户类型
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return 用户类型,如果未登录返回null
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String getCurrentUserType() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ JSONObject userInfo = JwtUtil.getCurrentUserInfo();
|
|
|
|
|
+ if (userInfo != null) {
|
|
|
|
|
+ return userInfo.getString("userType");
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("LoginUserUtil.getCurrentUserType - 获取用户类型失败: {}", e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取当前登录用户的Token信息(JSONObject格式)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return 用户信息JSONObject,包含userId、phone、userType等字段,如果未登录返回null
|
|
|
|
|
+ */
|
|
|
|
|
+ public static JSONObject getCurrentUserInfo() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ JSONObject userInfo = JwtUtil.getCurrentUserInfo();
|
|
|
|
|
+ if (userInfo != null) {
|
|
|
|
|
+ log.debug("LoginUserUtil.getCurrentUserInfo - 获取用户信息成功: userId={}, phone={}",
|
|
|
|
|
+ userInfo.getString("userId"), userInfo.getString("phone"));
|
|
|
|
|
+ return userInfo;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("LoginUserUtil.getCurrentUserInfo - 获取用户信息失败: {}", e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取当前登录用户的完整商户用户信息(从数据库查询)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return StoreUser对象,如果未登录或查询失败返回null
|
|
|
|
|
+ */
|
|
|
|
|
+ public static StoreUser getCurrentStoreUser() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Integer userId = getCurrentUserId();
|
|
|
|
|
+ if (userId == null) {
|
|
|
|
|
+ log.warn("LoginUserUtil.getCurrentStoreUser - 用户未登录");
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 从数据库查询完整用户信息
|
|
|
|
|
+ StoreUser storeUser = storeUserMapper.selectById(userId);
|
|
|
|
|
+ if (storeUser != null) {
|
|
|
|
|
+ log.debug("LoginUserUtil.getCurrentStoreUser - 查询用户信息成功: userId={}, phone={}",
|
|
|
|
|
+ storeUser.getId(), storeUser.getPhone());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.warn("LoginUserUtil.getCurrentStoreUser - 用户不存在: userId={}", userId);
|
|
|
|
|
+ }
|
|
|
|
|
+ return storeUser;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("LoginUserUtil.getCurrentStoreUser - 查询用户信息失败: {}", e.getMessage(), e);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据手机号获取商户用户信息
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return StoreUser对象,如果未登录或查询失败返回null
|
|
|
|
|
+ */
|
|
|
|
|
+ public static StoreUser getCurrentStoreUserByPhone() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ String phone = getCurrentUserPhone();
|
|
|
|
|
+ if (phone == null) {
|
|
|
|
|
+ log.warn("LoginUserUtil.getCurrentStoreUserByPhone - 用户未登录");
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 从数据库根据手机号查询用户信息
|
|
|
|
|
+ LambdaQueryWrapper<StoreUser> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ queryWrapper.eq(StoreUser::getPhone, phone);
|
|
|
|
|
+ StoreUser storeUser = storeUserMapper.selectOne(queryWrapper);
|
|
|
|
|
+
|
|
|
|
|
+ if (storeUser != null) {
|
|
|
|
|
+ log.debug("LoginUserUtil.getCurrentStoreUserByPhone - 查询用户信息成功: userId={}, phone={}",
|
|
|
|
|
+ storeUser.getId(), storeUser.getPhone());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.warn("LoginUserUtil.getCurrentStoreUserByPhone - 用户不存在: phone={}", phone);
|
|
|
|
|
+ }
|
|
|
|
|
+ return storeUser;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("LoginUserUtil.getCurrentStoreUserByPhone - 查询用户信息失败: {}", e.getMessage(), e);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 验证当前登录用户是否为指定用户
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param userId 要验证的用户ID
|
|
|
|
|
+ * @return true-是当前用户, false-不是当前用户
|
|
|
|
|
+ */
|
|
|
|
|
+ public static boolean isCurrentUser(Integer userId) {
|
|
|
|
|
+ if (userId == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer currentUserId = getCurrentUserId();
|
|
|
|
|
+ return userId.equals(currentUserId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 验证当前用户是否已登录
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return true-已登录, false-未登录
|
|
|
|
|
+ */
|
|
|
|
|
+ public static boolean isLogin() {
|
|
|
|
|
+ return getCurrentUserId() != null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 验证当前用户类型是否为web端商户平台
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return true-是商户平台用户, false-不是
|
|
|
|
|
+ */
|
|
|
|
|
+ public static boolean isStorePlatformUser() {
|
|
|
|
|
+ String userType = getCurrentUserType();
|
|
|
|
|
+ return "storePlatform".equals(userType);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取当前登录用户的门店ID
|
|
|
|
|
+ * 注意:需要先查询数据库获取完整用户信息
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return 门店ID,如果未登录或用户无门店返回null
|
|
|
|
|
+ */
|
|
|
|
|
+ public static Integer getCurrentStoreId() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ StoreUser storeUser = getCurrentStoreUser();
|
|
|
|
|
+ if (storeUser != null) {
|
|
|
|
|
+ return storeUser.getStoreId();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("LoginUserUtil.getCurrentStoreId - 获取门店ID失败: {}", e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取当前登录用户的昵称
|
|
|
|
|
+ * 注意:需要先查询数据库获取完整用户信息
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return 昵称,如果未登录返回null
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String getCurrentNickName() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ StoreUser storeUser = getCurrentStoreUser();
|
|
|
|
|
+ if (storeUser != null) {
|
|
|
|
|
+ return storeUser.getNickName();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("LoginUserUtil.getCurrentNickName - 获取昵称失败: {}", e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取当前登录用户的真实姓名
|
|
|
|
|
+ * 注意:需要先查询数据库获取完整用户信息
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return 真实姓名,如果未登录返回null
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String getCurrentName() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ StoreUser storeUser = getCurrentStoreUser();
|
|
|
|
|
+ if (storeUser != null) {
|
|
|
|
|
+ return storeUser.getName();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("LoginUserUtil.getCurrentName - 获取姓名失败: {}", e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|