liukangdong
2024-08-05 b9b6de6fc6cf1d586bd43b2c72b447da03cb01f3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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;
    }
 
}