| package com.doumee.api.web; | 
|   | 
| 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.dao.business.model.Member; | 
| import com.doumee.dao.business.model.Users; | 
| import com.doumee.service.business.MemberService; | 
| import com.doumee.service.business.UsersService; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.apache.commons.lang3.StringUtils; | 
| import org.apache.poi.ss.formula.ptg.MemAreaPtg; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.beans.factory.annotation.Value; | 
| import org.springframework.web.context.request.RequestContextHolder; | 
| import org.springframework.web.context.request.ServletRequestAttributes; | 
|   | 
| import javax.servlet.http.HttpServletRequest; | 
|   | 
| /** | 
|  * Controller基类 | 
|  * @author Eva.Caesar Liu | 
|  * @date 2022/03/15 09:54 | 
|  */ | 
| @Slf4j | 
| public class ApiController { | 
|   | 
|     @Autowired | 
|     UsersService usersService; | 
|     @Autowired | 
|     MemberService memberService; | 
|     /** | 
|      * 是否开发者 | 
|      */ | 
|     @Value("${debug_model}") | 
|     private Boolean isDebug; | 
|   | 
|     /** | 
|      * 得到request对象 | 
|      * | 
|      * @return | 
|      */ | 
|     public HttpServletRequest getRequest() { | 
|         HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | 
|         return request; | 
|     } | 
|   | 
|     /** | 
|      * 获取用户ID | 
|      * | 
|      * @return | 
|      */ | 
|     protected Long getMemberId() { | 
|         Object obj = this.getRequest().getAttribute(JwtTokenUtil.UserId_Name); | 
|         return obj != null ? (Long) obj : null; | 
|     } | 
|   | 
|     protected String getToken() { | 
|         Object obj = this.getRequest().getAttribute(JwtTokenUtil.HEADER_KEY); | 
|         return obj != null ? (String) obj : null; | 
|     } | 
|   | 
|   | 
|     protected Long getUserId() { | 
| //        if(isDebug){ | 
| //            return 2L; | 
| //        } | 
|         Object obj = this.getRequest().getAttribute(JwtTokenUtil.UserId_Name); | 
|         return obj != null ? (Long) obj : null; | 
|     } | 
|     protected Users getLoginUserInfo() { | 
|         Long userId = getUserId(); | 
|         if(userId== null){ | 
|             throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"未登录"); | 
|         } | 
|         Object obj = this.getRequest().getAttribute(JwtTokenUtil.UserInfo); | 
|         String userInfo = obj != null ? (String) obj : null; | 
|         Users user = new Users(); | 
|         if(StringUtils.isNotBlank(userInfo)){ | 
|             user = JSONObject.toJavaObject(JSONObject.parseObject(userInfo),Users.class); | 
|         } | 
|         return user; | 
|     } | 
|     protected Member getLoginMemberInfo() { | 
|       Long userId = getMemberId(); | 
|       if(userId== null){ | 
|           return null; | 
|       } | 
|         Object obj = this.getRequest().getAttribute(JwtTokenUtil.UserInfo); | 
|         String userInfo = obj != null ? (String) obj : null; | 
|         Member member = new Member(); | 
|         if(StringUtils.isNotBlank(userInfo)){ | 
|             member = JSONObject.toJavaObject(JSONObject.parseObject(userInfo),Member.class); | 
|         } | 
|       return member; | 
|     } | 
|   | 
|     protected String getUserType() { | 
|         Object obj = this.getRequest().getAttribute(JwtTokenUtil.UserType); | 
|         return obj != null ? (String) obj : null; | 
|     } | 
|   | 
| } |