1
zj
2025-03-20 697c08d94a3c26aaa970c467775989bb548fb6c2
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
package project.web.admin.impl.email;
 
import java.text.MessageFormat;
import java.util.Date;
 
import kernel.exception.BusinessException;
import kernel.util.DateUtils;
import project.Constants;
import project.log.Log;
import project.log.LogService;
import project.syspara.SysparaService;
import project.user.googleauth.GoogleAuthService;
import project.web.admin.service.email.AdminEmailCodeService;
import security.SecUser;
import security.internal.SecUserService;
 
public class AdminEmailCodeServiceImpl implements AdminEmailCodeService {
 
    private SecUserService secUserService;
    protected LogService logService;
//    protected IdentifyingCodeService identifyingCodeService;
//    protected IdentifyingCodeTimeWindowService identifyingCodeTimeWindowService;
    private SysparaService sysparaService;
    private GoogleAuthService googleAuthService;
//    private Map<String,Date> cacheDate = new ConcurrentHashMap<String, Date>();
    /**
     * 发送验证码
     * @param ip
     * @param operatorUsername    操作人
     * @param context            操作内容
     */
    public void sendCode(String ip,String operatorUsername,String context,boolean isSuper) {
////        if(cacheDate.get(context)!=null&&DateUtils.addMinute(cacheDate.get(context), 1).after(new Date())) {
////            throw new BusinessException("验证码已经发送,请于"+DateUtils.calcTimeBetweenInSecond(new Date(), DateUtils.addMinute(cacheDate.get(context), 1))+"秒后重新发送");
////        }
//        String value = null;
//        if(isSuper) {
//            value = sysparaService.find("admin_verify_email").getValue();
//        }else {
//            value = secUserService.findUserByLoginName(operatorUsername).getEmail();
//        }
//        if(StringUtils.isEmptyString(value)) {
//            throw new BusinessException("管理员尚未配置邮箱");
//        }
//        if(!RegexUtil.isEmail(value)) {
//            throw new BusinessException("管理员邮箱格式错误");
//        }
//        identifyingCodeService.send(value, ip);
//        SecUser sec = this.secUserService.findUserByLoginName(operatorUsername);
//        saveLog(sec,operatorUsername,String.format("ip:{%s},操作:{%s},邮箱:{%s},发送验证码", ip,context,value));
////        cacheDate.put(context, new Date());
    }
    
    public void saveLog(SecUser secUser, String operator,String context) {
        Log log = new Log();
        log.setCategory(Constants.LOG_CATEGORY_OPERATION);
        log.setOperator(operator);
        log.setUsername(secUser.getUsername());
        log.setPartyId(secUser.getPartyId());
        log.setLog(context);
        log.setCreateTime(new Date());
        logService.saveSync(log);
    }
 
    public void checkCode(String ip,String operatorUsername,String code,String uri) {
        SecUser user = secUserService.findUserByLoginName(operatorUsername);
        // checkEmailCode(user.getEmail(),code);
        String context = MessageFormat.format("user:{0},opera time:{1},opera ip:{2},request uri:{3},"
                + "last login ip:{4},last login time:{5},验证码:["+code+"]",
                new Object[]{user.getUsername(),DateUtils.dateToStr(new Date(), DateUtils.DF_yyyyMMddHHmmss),ip,uri,
                        user.getLogin_ip(),DateUtils.dateToStr(user.getLast_loginTime(), DateUtils.DF_yyyyMMddHHmmss)});
        user.setLogin_ip(ip);
        user.setLast_loginTime(new Date());
        secUserService.update(user);//ip切换相当于重新登录
        this.saveLog(user, operatorUsername, context);
    }
    public void checkGoogleAuthCode(String ip,String operatorUsername,String googleAuthCode,String uri) {
        SecUser user = secUserService.findUserByLoginName(operatorUsername);
        googleAuthService.checkGoogleAuthCode(user,googleAuthCode);
        String context = MessageFormat.format("user:{0},opera time:{1},opera ip:{2},request uri:{3},"
                + "last login ip:{4},last login time:{5}",
                new Object[]{user.getUsername(),DateUtils.dateToStr(new Date(), DateUtils.DF_yyyyMMddHHmmss),ip,uri,
                        user.getLogin_ip(),DateUtils.dateToStr(user.getLast_loginTime(), DateUtils.DF_yyyyMMddHHmmss)});
        user.setLogin_ip(ip);
        user.setLast_loginTime(new Date());
        secUserService.update(user);//ip切换相当于重新登录
        this.saveLog(user, operatorUsername, context);
    }
 
    public void setSecUserService(SecUserService secUserService) {
        this.secUserService = secUserService;
    }
 
    public void setLogService(LogService logService) {
        this.logService = logService;
    }
 
//    public void setIdentifyingCodeService(IdentifyingCodeService identifyingCodeService) {
//        this.identifyingCodeService = identifyingCodeService;
//    }
//
//    public void setIdentifyingCodeTimeWindowService(IdentifyingCodeTimeWindowService identifyingCodeTimeWindowService) {
//        this.identifyingCodeTimeWindowService = identifyingCodeTimeWindowService;
//    }
 
    public void setSysparaService(SysparaService sysparaService) {
        this.sysparaService = sysparaService;
    }
 
    public void setGoogleAuthService(GoogleAuthService googleAuthService) {
        this.googleAuthService = googleAuthService;
    }
    
}