44323
2024-04-23 16b704d18a875d1fb63827aaa507790ba2bef5be
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
package com.stylefeng.guns.modular.system.service.impl;
 
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.AppUserMapper;
import com.stylefeng.guns.modular.system.dto.AppUserQuery;
import com.stylefeng.guns.modular.system.dto.TeamQuery;
import com.stylefeng.guns.modular.system.model.AppUser;
import com.stylefeng.guns.modular.system.service.IAppUserService;
import com.stylefeng.guns.modular.system.util.RedisUtil;
import com.stylefeng.guns.modular.system.vo.InviteUserVO;
import com.stylefeng.guns.modular.system.vo.TeamDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
import java.util.List;
 
/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 无关风月
 * @since 2024-02-06
 */
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements IAppUserService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private AppUserMapper appUserMapper;
    /**
     * 通过token获取用户信息
     * @return
     */
    @Override
    public AppUser getAppUser() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String authorization = request.getHeader("Authorization");
        // todo 这里注释了一段代码
        // && authorization.contains("Bearer")
        if(ToolUtil.isNotEmpty(authorization) && authorization.contains("Bearer ") ){
            String token = authorization.substring(7);
            //通过token获取用户id
            Integer appUserId = getAppUserIdFromToken(token);
            return this.selectById(appUserId);
        }
        return null;
    }
 
    @Override
    public List<InviteUserVO> inviteUserList(AppUserQuery query) {
        return appUserMapper.inviteUserList(query);
    }
 
    @Override
    public List<TeamDetailVO> teamList(TeamQuery query) {
        return appUserMapper.teamList(query);
    }
 
    @Override
    public List<AppUser> selectList1(String name, String phone) {
        return appUserMapper.selectList1(name,phone);
 
    }
 
 
    /**
     * 通过token获取用户id
     * @param token
     * @return
     */
    private Integer getAppUserIdFromToken(String token){
        String key = token;
        int length = token.length();
        if(length > 32){
            key = token.substring(token.length() - 32);
        }
        String value = redisUtil.getValue(key);
        if(ToolUtil.isEmpty(value)){
            return 0;
        }
        return Integer.valueOf(value);
    }
}