“djh”
2024-12-04 d1549b8445c65a6775cf1ba093f5040ace0f87e7
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.gkhy.huataiFourierSpecialGasMonitor.application.account.service.impl;
 
import com.gkhy.huataiFourierSpecialGasMonitor.application.account.dto.respDto.TokenInfoDto;
import com.gkhy.huataiFourierSpecialGasMonitor.application.account.service.TokenAppService;
import com.gkhy.huataiFourierSpecialGasMonitor.commons.domain.Result;
import com.gkhy.huataiFourierSpecialGasMonitor.commons.enums.ResultCode;
import com.gkhy.huataiFourierSpecialGasMonitor.commons.enums.SystemCacheKeyEnum;
import com.google.common.collect.Range;
import com.google.common.hash.Hashing;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
 
@Service
public class TokenAppServiceImpl implements TokenAppService {
 
    @Autowired
    private RedissonClient redissonClient;
 
    @Override
    public Result<TokenInfoDto> setToken(Long uid) {
        Result result = new Result<>();
        if(uid == null){
            result.setCode(ResultCode.PARAM_ERROR_NULL.getCode());
            result.setMsg("参数缺失");
            return result;
        }
        String loginUserId = ""+uid;
        //生成Token
        String seed = loginUserId+ Range.atLeast(1) +System.nanoTime();
        String hash = String.valueOf(Hashing.hmacMd5(loginUserId.getBytes(StandardCharsets.UTF_8)).hashString(seed,
                StandardCharsets.UTF_8));
        //存入redis
        String accessTokenKey = SystemCacheKeyEnum.KEY_USER_TOKEN.getKey();
        try {
            RMapCache<String,Object> tokenCacheMap = redissonClient.getMapCache(accessTokenKey);
            tokenCacheMap.remove(loginUserId);
            tokenCacheMap.put(loginUserId,hash,120, TimeUnit.MINUTES);
            result.setSuccess();
            TokenInfoDto tokenInfoDto = new TokenInfoDto();
            tokenInfoDto.setTk(hash);
            tokenInfoDto.setUid(uid);
            tokenInfoDto.setRemainSecond(tokenCacheMap.remainTimeToLive(loginUserId));
            result.setData(tokenInfoDto);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
 
    @Override
    public Result resetTokenTime(Long uid) {
        Result result = new Result<>();
        if(uid == null){
            result.setCode(ResultCode.PARAM_ERROR_NULL.getCode());
            result.setMsg("参数缺失");
            return result;
        }
        String loginUserId = ""+uid;
        String accessTokenKey = SystemCacheKeyEnum.KEY_USER_TOKEN.getKey();
        try {
            RMapCache<String,Object> tokenCacheMap = redissonClient.getMapCache(accessTokenKey);
            String tk = (String) tokenCacheMap.get(loginUserId);
            tokenCacheMap.put(loginUserId,tk,120, TimeUnit.MINUTES);
            result.setSuccess();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
 
    @Override
    public Result removeToken(Long uid) {
        Result result = new Result<>();
        if(uid == null){
            result.setCode(ResultCode.PARAM_ERROR_NULL.getCode());
            result.setMsg("参数缺失");
            return result;
        }
        String loginUserId = ""+uid;
        String accessTokenKey = SystemCacheKeyEnum.KEY_USER_TOKEN.getKey();
        RMapCache<String,Object> tokenCacheMap = redissonClient.getMapCache(accessTokenKey);
        tokenCacheMap.remove(loginUserId);
        result.setSuccess();
        return result;
    }
 
    @Override
    public Result<TokenInfoDto> getToken(Long uid) {
        Result<TokenInfoDto> result = new Result<>();
        result.setCode(ResultCode.BUSINESS_ERROR_PERMISSION_DENIALED.getCode());
        result.setMsg("TOKEN不存在");
        if(uid == null){
            result.setCode(ResultCode.PARAM_ERROR_NULL.getCode());
            result.setMsg("参数缺失");
            return result;
        }
        String loginUserId = ""+uid;
        String accessTokenKey = SystemCacheKeyEnum.KEY_USER_TOKEN.getKey();
        RMapCache<String,Object> tokenCacheMap = redissonClient.getMapCache(accessTokenKey);
        Object tokenObject = tokenCacheMap.get(loginUserId);
        String cacheToken;
        if(tokenObject != null){
            cacheToken = (String) tokenObject;
            Long tokenRemainTimeToLive = tokenCacheMap.remainTimeToLive(loginUserId);
            if(cacheToken != null && !cacheToken.isEmpty()){
                result.setSuccess();
                TokenInfoDto tokenInfoDto = new TokenInfoDto();
                tokenInfoDto.setUid(uid);
                tokenInfoDto.setTk(cacheToken);
                tokenInfoDto.setRemainSecond(tokenRemainTimeToLive/1000);
                result.setData(tokenInfoDto);
            }
        }
        return result;
    }
}