Authored by qinchao

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

  1 +package com.yohoufo.user.cache;
  2 +
  3 +import com.alibaba.fastjson.JSON;
  4 +import org.apache.commons.lang3.StringUtils;
  5 +
  6 +public class CacheHelper {
  7 +
  8 + /**
  9 + * 序列化value值
  10 + * @param value
  11 + * @return
  12 + */
  13 + public static <T> String value2String(T value) {
  14 + String v = null;
  15 + if (value == null) {
  16 + return null;
  17 + }
  18 + if (value instanceof String) {
  19 + v = (String) value;
  20 + } else {
  21 + v = JSON.toJSONString(value);
  22 + }
  23 + return v;
  24 + }
  25 +
  26 + /**
  27 + * 反序列化value值
  28 + *
  29 + * @param value
  30 + * @param clazz
  31 + * @return
  32 + */
  33 + @SuppressWarnings("unchecked")
  34 + public static <T> T string2Value(String value, Class<T> clazz) {
  35 + if (StringUtils.isEmpty(value)) {
  36 + return null;
  37 + }
  38 + if (clazz.getName().equalsIgnoreCase("java.lang.String")) {
  39 + return (T) value;
  40 + }
  41 + return (T) JSON.parseObject(value, clazz);
  42 + }
  43 +
  44 +
  45 +}
@@ -43,10 +43,40 @@ public class RedisValueCache { @@ -43,10 +43,40 @@ public class RedisValueCache {
43 } 43 }
44 44
45 45
  46 + /**
  47 + * 设置值
  48 + *
  49 + * @param cacheKey
  50 + * @param value
  51 + * @param timeout
  52 + * @param unit
  53 + * @param <T>
  54 + */
  55 + public <T> void set(RedisKeyBuilder cacheKey, T value, long timeout, TimeUnit unit) {
  56 + logger.debug("Enter set valueOperation redis value.key is {},value is {}", cacheKey, value);
  57 + try {
  58 + String v = CacheHelper.value2String(value);
  59 + yhValueOperations.set(cacheKey, v, timeout, unit);
  60 + } catch (Exception e) {
  61 + logger.warn("Redis exception. value redis set,key {},value is {}, error msg is {}", cacheKey, value, e);
  62 + }
  63 + }
46 64
47 -  
48 -  
49 -  
50 - 65 + /**
  66 + * 取值
  67 + *
  68 + * @param cacheKey
  69 + * @param clazz
  70 + */
  71 + public <T> T get(RedisKeyBuilder cacheKey, Class<T> clazz) {
  72 + logger.debug("Enter get valueOperation redis value.key is {}", cacheKey);
  73 + try {
  74 + String value = yhValueOperations.get(cacheKey);
  75 + return CacheHelper.string2Value(value, clazz);
  76 + } catch (Exception e) {
  77 + logger.warn("Redis exception. value redis get,key {}, error msg is {}", cacheKey, e);
  78 + }
  79 + return null;
  80 + }
51 81
52 } 82 }
@@ -48,6 +48,7 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService { @@ -48,6 +48,7 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService {
48 private final static DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); 48 private final static DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
49 49
50 private final String UFO_USER_AUTHORIZE_FAILED_PREX="ufo:user:authorizeFailed:"; 50 private final String UFO_USER_AUTHORIZE_FAILED_PREX="ufo:user:authorizeFailed:";
  51 + private final String UFO_USER_AUTHORIZE_INFO_PREX="ufo:user:authorizeInfo:";
51 //请求实名认证银联接口url 测试环境 52 //请求实名认证银联接口url 测试环境
52 private final String requestUrl="http://58.247.0.18:29015/v1/datacenter/smartverification/bankcard/verify"; 53 private final String requestUrl="http://58.247.0.18:29015/v1/datacenter/smartverification/bankcard/verify";
53 //请求实名认证银联接口url 生产环境 54 //请求实名认证银联接口url 生产环境
@@ -81,7 +82,7 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService { @@ -81,7 +82,7 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService {
81 AuthorizeResultRespVO result=new AuthorizeResultRespVO(); 82 AuthorizeResultRespVO result=new AuthorizeResultRespVO();
82 result.setUid(uid); 83 result.setUid(uid);
83 84
84 - UserAuthorizeInfo info= userAuthorizeInfoDao.selectValidAuthorizeInfoByUid(uid); 85 + UserAuthorizeInfo info= getValidAuthorizeInfoFromRedis(uid);
85 if(null!=info){ 86 if(null!=info){
86 result.setAuthorizeFlag(true); 87 result.setAuthorizeFlag(true);
87 result.setCardNo(info.getCardNo()); 88 result.setCardNo(info.getCardNo());
@@ -95,6 +96,27 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService { @@ -95,6 +96,27 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService {
95 return result; 96 return result;
96 } 97 }
97 98
  99 + private UserAuthorizeInfo getValidAuthorizeInfoFromRedis(int uid){
  100 + // 从redis缓存中获取
  101 + RedisKeyBuilder key=RedisKeyBuilder.newInstance().appendFixed(UFO_USER_AUTHORIZE_INFO_PREX).appendVar(uid);
  102 + UserAuthorizeInfo authorizeInfo = redisValueCache.get(key,UserAuthorizeInfo.class);
  103 + if(null != authorizeInfo){
  104 + return authorizeInfo;
  105 + }
  106 +
  107 + //如果不存在,则从数据库获取
  108 + authorizeInfo= userAuthorizeInfoDao.selectValidAuthorizeInfoByUid(uid);
  109 + if(authorizeInfo!=null){
  110 + //保存到redis
  111 + try{
  112 + redisValueCache.set(key, authorizeInfo,60*60*2L,TimeUnit.SECONDS);
  113 + }catch(Exception e){
  114 + logger.warn("set valid authorize info to redis error. uid={}", uid);
  115 + }
  116 + }
  117 + return authorizeInfo;
  118 + }
  119 +
98 /** 120 /**
99 * 实名身份认证 121 * 实名身份认证
100 */ 122 */
@@ -105,7 +127,7 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService { @@ -105,7 +127,7 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService {
105 String name=reqVO.getName(); 127 String name=reqVO.getName();
106 128
107 //检查是否已经实名认证,如果已经认证直接返回 129 //检查是否已经实名认证,如果已经认证直接返回
108 - if(null!=userAuthorizeInfoDao.selectValidAuthorizeInfoByUid(uid)){ 130 + if(null!=getValidAuthorizeInfoFromRedis(uid)){
109 return new ApiResponse(400,"已实名认证",null); 131 return new ApiResponse(400,"已实名认证",null);
110 } 132 }
111 133