gongchunyi
昨天 e9b4ceeea014f9d81263672f3d9c46a567c39591
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
package com.ruoyi.energymeter.util;
 
import com.ruoyi.common.utils.SecurityUtils;
 
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
 
/**
 * 登录态与时间工具:系统级定时任务不会携带登录态,这里统一兜底。
 */
public final class EnergyMeterSupport {
 
    private EnergyMeterSupport() {
    }
 
    public static Long currentTenantId() {
        try {
            return SecurityUtils.getLoginUser().getTenantId();
        } catch (Exception e) {
            return null;
        }
    }
 
    public static Long currentUserId() {
        try {
            return SecurityUtils.getUserId();
        } catch (Exception e) {
            return null;
        }
    }
 
    public static boolean isAdmin() {
        try {
            return SecurityUtils.getLoginUser().getUser().isAdmin();
        } catch (Exception e) {
            return false;
        }
    }
 
    public static LocalDate today() {
        return LocalDate.now(ZoneId.of("Asia/Shanghai"));
    }
 
    public static LocalDateTime now() {
        return LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
    }
 
    public static String truncate(String text, int maxLength) {
        if (text == null) {
            return null;
        }
        return text.length() <= maxLength ? text : text.substring(0, maxLength);
    }
}