|  |  |  | 
|---|
|  |  |  | package com.doumee.api; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.doumee.core.model.LoginUserInfo; | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.doumee.config.jwt.JwtTokenUtil; | 
|---|
|  |  |  | import com.doumee.core.constants.ResponseStatus; | 
|---|
|  |  |  | import com.doumee.core.exception.BusinessException; | 
|---|
|  |  |  | import com.doumee.service.business.third.model.LoginUserInfo; | 
|---|
|  |  |  | import com.doumee.core.utils.Constants; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | import org.apache.shiro.SecurityUtils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
|---|
|  |  |  | import org.springframework.web.context.request.RequestContextHolder; | 
|---|
|  |  |  | import org.springframework.web.context.request.ServletRequestAttributes; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.servlet.http.HttpServletRequest; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Controller基类 | 
|---|
|  |  |  | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | public class BaseController { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RedisTemplate<String,Object> stringRedisTemplate; | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取当前登录用户 | 
|---|
|  |  |  | * @author Eva.Caesar Liu | 
|---|
|  |  |  | * @date 2023/03/21 14:49 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | protected LoginUserInfo getLoginUser () { | 
|---|
|  |  |  | return (LoginUserInfo)SecurityUtils.getSubject().getPrincipal(); | 
|---|
|  |  |  | public LoginUserInfo getLoginUser (String token) { | 
|---|
|  |  |  | if (token == null || token.isEmpty()) { | 
|---|
|  |  |  | return (LoginUserInfo)SecurityUtils.getSubject().getPrincipal(); | 
|---|
|  |  |  | //            throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String userinfo =(String) stringRedisTemplate.opsForValue().get(Constants.REDIS_TOKEN_KEY + token); | 
|---|
|  |  |  | if (StringUtils.isBlank(userinfo)) { | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | LoginUserInfo user = JSONObject.toJavaObject(JSONObject.parseObject(userinfo),LoginUserInfo.class ); | 
|---|
|  |  |  | if(user ==null ){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"用户登陆已失效,请重新登陆!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return  user; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | return request; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public List<Integer> getIdList(String ids){ | 
|---|
|  |  |  | if(StringUtils.isBlank(ids)){ | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String [] idArray = ids.split(","); | 
|---|
|  |  |  | List<Integer> idList = new ArrayList<>(); | 
|---|
|  |  |  | for (String id : idArray) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | idList.add(Integer.valueOf(id)); | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return idList; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|