Authored by qinchao

实名认证接口-开启图形验证码

package com.yohoufo.user.cache;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
public class CacheHelper {
/**
* 序列化value值
* @param value
* @return
*/
public static <T> String value2String(T value) {
String v = null;
if (value == null) {
return null;
}
if (value instanceof String) {
v = (String) value;
} else {
v = JSON.toJSONString(value);
}
return v;
}
/**
* 反序列化value值
*
* @param value
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T string2Value(String value, Class<T> clazz) {
if (StringUtils.isEmpty(value)) {
return null;
}
if (clazz.getName().equalsIgnoreCase("java.lang.String")) {
return (T) value;
}
return (T) JSON.parseObject(value, clazz);
}
}
... ...
... ... @@ -43,10 +43,40 @@ public class RedisValueCache {
}
/**
* 设置值
*
* @param cacheKey
* @param value
* @param timeout
* @param unit
* @param <T>
*/
public <T> void set(RedisKeyBuilder cacheKey, T value, long timeout, TimeUnit unit) {
logger.debug("Enter set valueOperation redis value.key is {},value is {}", cacheKey, value);
try {
String v = CacheHelper.value2String(value);
yhValueOperations.set(cacheKey, v, timeout, unit);
} catch (Exception e) {
logger.warn("Redis exception. value redis set,key {},value is {}, error msg is {}", cacheKey, value, e);
}
}
/**
* 取值
*
* @param cacheKey
* @param clazz
*/
public <T> T get(RedisKeyBuilder cacheKey, Class<T> clazz) {
logger.debug("Enter get valueOperation redis value.key is {}", cacheKey);
try {
String value = yhValueOperations.get(cacheKey);
return CacheHelper.string2Value(value, clazz);
} catch (Exception e) {
logger.warn("Redis exception. value redis get,key {}, error msg is {}", cacheKey, e);
}
return null;
}
}
... ...
... ... @@ -48,6 +48,7 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService {
private final static DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
private final String UFO_USER_AUTHORIZE_FAILED_PREX="ufo:user:authorizeFailed:";
private final String UFO_USER_AUTHORIZE_INFO_PREX="ufo:user:authorizeInfo:";
//请求实名认证银联接口url 测试环境
private final String requestUrl="http://58.247.0.18:29015/v1/datacenter/smartverification/bankcard/verify";
//请求实名认证银联接口url 生产环境
... ... @@ -81,7 +82,7 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService {
AuthorizeResultRespVO result=new AuthorizeResultRespVO();
result.setUid(uid);
UserAuthorizeInfo info= userAuthorizeInfoDao.selectValidAuthorizeInfoByUid(uid);
UserAuthorizeInfo info= getValidAuthorizeInfoFromRedis(uid);
if(null!=info){
result.setAuthorizeFlag(true);
result.setCardNo(info.getCardNo());
... ... @@ -95,6 +96,27 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService {
return result;
}
private UserAuthorizeInfo getValidAuthorizeInfoFromRedis(int uid){
// 从redis缓存中获取
RedisKeyBuilder key=RedisKeyBuilder.newInstance().appendFixed(UFO_USER_AUTHORIZE_INFO_PREX).appendVar(uid);
UserAuthorizeInfo authorizeInfo = redisValueCache.get(key,UserAuthorizeInfo.class);
if(null != authorizeInfo){
return authorizeInfo;
}
//如果不存在,则从数据库获取
authorizeInfo= userAuthorizeInfoDao.selectValidAuthorizeInfoByUid(uid);
if(authorizeInfo!=null){
//保存到redis
try{
redisValueCache.set(key, authorizeInfo,60*60*2L,TimeUnit.SECONDS);
}catch(Exception e){
logger.warn("set valid authorize info to redis error. uid={}", uid);
}
}
return authorizeInfo;
}
/**
* 实名身份认证
*/
... ... @@ -105,7 +127,7 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService {
String name=reqVO.getName();
//检查是否已经实名认证,如果已经认证直接返回
if(null!=userAuthorizeInfoDao.selectValidAuthorizeInfoByUid(uid)){
if(null!=getValidAuthorizeInfoFromRedis(uid)){
return new ApiResponse(400,"已实名认证",null);
}
... ...