QRCodeServiceImpl.java
2.17 KB
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
package com.xkl.service;
import com.xkl.config.Constants;
import com.xkl.tools.DESTools;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* create by zhaoyue
* 2017-03-11
*/
@Service
@CommonsLog
public class QRCodeServiceImpl implements IQRCodeService, Constants {
private static final String REDIS_ACC_QR_PREFIX = "acc_qr-";
private static final String REDIS_QR_ACC_PREFIX = "qr_acc-";
private RedisTemplate<String, String> redis;
@Autowired
public void setRedis(RedisTemplate redis) {
this.redis = redis;
}
@Override
public String getAccountWithQRCode(String qrcode) {
String qrAcckey = genQrAccKey(qrcode);
String account = null;
if (redis.hasKey(qrAcckey)) {
account = redis.boundValueOps(qrAcckey).get();
}
return account;
}
@Override
public String getQRCodeWithAccount(String account) {
String accQrkey = genAccQrKey(account);
String qrCodeStr = "";
if (redis.hasKey(accQrkey)) {
qrCodeStr = redis.boundValueOps(accQrkey).get();
} else {
Date dt = new Date();
SimpleDateFormat matter1 = new SimpleDateFormat("yyyyMMdd");
String dateStr = matter1.format(dt);
try {
qrCodeStr = DESTools.encrypt(account + dateStr);
} catch (Exception e) {
e.printStackTrace();
}
String qrAcckey = genQrAccKey(qrCodeStr);
redis.boundValueOps(qrAcckey).set(account, Constants.QRCODE_EXPIRES_HOUR, TimeUnit.HOURS);
redis.boundValueOps(accQrkey).set(qrCodeStr, Constants.QRCODE_EXPIRES_HOUR, TimeUnit.HOURS);
}
return qrCodeStr;
}
private static String genAccQrKey(String account) {
return REDIS_ACC_QR_PREFIX + account;
}
private static String genQrAccKey(String account) {
return REDIS_QR_ACC_PREFIX + account;
}
}