@@ -32,4 +32,7 @@ public class Constant { | |||||
public static final String importMemPrev = "importmem:"; | public static final String importMemPrev = "importmem:"; | ||||
public static final String adminPage = "https://admin.malls.iformall.com"; | public static final String adminPage = "https://admin.malls.iformall.com"; | ||||
/// REDIS key Prev | |||||
public static final String tt_cuser_customer_service_url_key = "cuser:serviceurl:"; | |||||
} | } |
@@ -0,0 +1,165 @@ | |||||
package com.iformall.utils; | |||||
import com.alibaba.fastjson.JSON; | |||||
import com.alibaba.fastjson.JSONObject; | |||||
import com.alibaba.fastjson.TypeReference; | |||||
import org.springframework.data.redis.core.RedisTemplate; | |||||
import org.springframework.data.redis.core.ValueOperations; | |||||
import java.util.List; | |||||
import java.util.Map; | |||||
import java.util.Set; | |||||
import java.util.concurrent.TimeUnit; | |||||
public class RedisCacheUtils { | |||||
private static <T> T parseJson(String json,Class<T> clazz) { | |||||
return JSON.parseObject(json,clazz); | |||||
} | |||||
private static <T> List<T> parseListJson(String json,Class<T> clazz) { | |||||
return JSON.parseObject(json,new TypeReference<List<T>>(clazz) {}); | |||||
} | |||||
private static <T> Set<T> parseSetJson(String json,Class<T> clazz) { | |||||
return JSON.parseObject(json,new TypeReference<Set<T>>(clazz) {}); | |||||
} | |||||
public static void cache(RedisTemplate<String, Object> template,String key, Object value,long seconds) { | |||||
ValueOperations<String, Object> operations = template.opsForValue(); | |||||
if (seconds > 0) { | |||||
operations.set(key, value, seconds,TimeUnit.SECONDS); | |||||
}else { | |||||
operations.set(key, value); | |||||
} | |||||
} | |||||
public static <T> T getCacheObject(RedisTemplate<String, Object> template,String key,Class<T> clazz) { | |||||
if (template.hasKey(key)) { | |||||
ValueOperations<String, Object> operations = template.opsForValue(); | |||||
Object o = operations.get(key); | |||||
if (o instanceof String) { | |||||
return parseJson((String)o, clazz); | |||||
}else { | |||||
return parseJson(JSON.toJSONString(o), clazz); | |||||
} | |||||
} | |||||
return null; | |||||
} | |||||
public static <T> List<T> getCacheListObject(RedisTemplate<String, Object> template,String key,Class<T> clazz) { | |||||
if (template.hasKey(key)) { | |||||
ValueOperations<String, Object> operations = template.opsForValue(); | |||||
Object o = operations.get(key); | |||||
if (o instanceof String) { | |||||
return parseListJson((String)o, clazz); | |||||
}else { | |||||
return parseListJson(JSON.toJSONString(o), clazz); | |||||
} | |||||
} | |||||
return null; | |||||
} | |||||
public static <T> Set<T> getCacheSetObject(RedisTemplate<String, Object> template,String key,Class<T> clazz) { | |||||
if (template.hasKey(key)) { | |||||
ValueOperations<String, Object> operations = template.opsForValue(); | |||||
Object o = operations.get(key); | |||||
if (o instanceof String) { | |||||
return parseSetJson((String)o, clazz); | |||||
}else { | |||||
return parseSetJson(JSON.toJSONString(o), clazz); | |||||
} | |||||
} | |||||
return null; | |||||
} | |||||
public static Map getCacheMap(RedisTemplate<String, Object> template,String key) { | |||||
return getCacheObject(template,key,Map.class); | |||||
} | |||||
public static String getCacheString(RedisTemplate<String, Object> template,String key) { | |||||
if (template.hasKey(key)) { | |||||
ValueOperations<String, Object> operations = template.opsForValue(); | |||||
Object o = operations.get(key); | |||||
if( o instanceof String) { | |||||
return (String) o; | |||||
} | |||||
return JSON.toJSONString(o); | |||||
} | |||||
return null; | |||||
} | |||||
public static Long getCacheLong(RedisTemplate<String, Object> template,String key) { | |||||
if (template.hasKey(key)) { | |||||
ValueOperations<String, Object> operations = template.opsForValue(); | |||||
Object o = operations.get(key); | |||||
if (o instanceof String) { | |||||
Long.parseLong((String)o); | |||||
} | |||||
return Long.parseLong(o.toString()); | |||||
} | |||||
return null; | |||||
} | |||||
public static Integer getCacheInteger(RedisTemplate<String, Object> template,String key) { | |||||
if (template.hasKey(key)) { | |||||
ValueOperations<String, Object> operations = template.opsForValue(); | |||||
Object o = operations.get(key); | |||||
if (o instanceof String) { | |||||
return new Integer((String)o); | |||||
} | |||||
return new Integer(o.toString()); | |||||
} | |||||
return null; | |||||
} | |||||
public static <T> T getFieldObject(Object o,String property,Class<T> clazz) { | |||||
String json = JSON.toJSONString(o); | |||||
JSONObject jsonObject = JSON.parseObject(json); | |||||
Object p = jsonObject.get(property); | |||||
if (null != p ) { | |||||
if (p instanceof String) { | |||||
return parseJson((String)p, clazz); | |||||
} | |||||
return parseJson(JSON.toJSONString(p), clazz); | |||||
} | |||||
return null; | |||||
} | |||||
public static <T> List<T> getFieldListObject(Object o,String property,Class<T> clazz) { | |||||
String json = JSON.toJSONString(o); | |||||
JSONObject jsonObject = JSON.parseObject(json); | |||||
Object p = jsonObject.get(property); | |||||
if (null != p ) { | |||||
if (p instanceof String) { | |||||
return parseListJson((String)p, clazz); | |||||
} | |||||
return parseListJson(JSON.toJSONString(p), clazz); | |||||
} | |||||
return null; | |||||
} | |||||
public static <T> Set<T> getFieldSetObject(Object o,String property,Class<T> clazz) { | |||||
String json = JSON.toJSONString(o); | |||||
JSONObject jsonObject = JSON.parseObject(json); | |||||
Object p = jsonObject.get(property); | |||||
if (null != p ) { | |||||
if (p instanceof String) { | |||||
return parseSetJson((String)p, clazz); | |||||
} | |||||
return parseSetJson(JSON.toJSONString(p), clazz); | |||||
} | |||||
return null; | |||||
} | |||||
public static Map getFieldMapObject(Object o,String property) { | |||||
return getFieldObject(o,property,Map.class); | |||||
} | |||||
public static void removeCache(RedisTemplate<String, Object> template,String key) { | |||||
if (template.hasKey(key)) { | |||||
template.delete(key); | |||||
} | |||||
} | |||||
} |
@@ -1,5 +1,9 @@ | |||||
package com.iformall.config; | package com.iformall.config; | ||||
import com.fasterxml.jackson.annotation.JsonAutoDetect; | |||||
import com.fasterxml.jackson.annotation.PropertyAccessor; | |||||
import com.fasterxml.jackson.databind.DeserializationFeature; | |||||
import com.fasterxml.jackson.databind.ObjectMapper; | |||||
import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
import org.springframework.cache.CacheManager; | import org.springframework.cache.CacheManager; | ||||
@@ -10,6 +14,9 @@ import org.springframework.context.annotation.Configuration; | |||||
import org.springframework.data.redis.cache.RedisCacheConfiguration; | import org.springframework.data.redis.cache.RedisCacheConfiguration; | ||||
import org.springframework.data.redis.cache.RedisCacheManager; | import org.springframework.data.redis.cache.RedisCacheManager; | ||||
import org.springframework.data.redis.connection.RedisConnectionFactory; | import org.springframework.data.redis.connection.RedisConnectionFactory; | ||||
import org.springframework.data.redis.core.RedisTemplate; | |||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; | |||||
import org.springframework.data.redis.serializer.StringRedisSerializer; | |||||
import java.time.Duration; | import java.time.Duration; | ||||
import java.util.HashMap; | import java.util.HashMap; | ||||
@@ -69,4 +76,25 @@ public class RedisConfig extends CachingConfigurerSupport { | |||||
return cacheManager; | return cacheManager; | ||||
} | } | ||||
@Bean("objectCommonRedisTemplate") | |||||
public RedisTemplate<String, Object> getObjectValueOperations(RedisConnectionFactory connectionFactory) { | |||||
RedisTemplate<String, Object> template = new RedisTemplate<>(); | |||||
template.setConnectionFactory(connectionFactory); | |||||
Jackson2JsonRedisSerializer<Object> j = new Jackson2JsonRedisSerializer<Object>(Object.class); | |||||
ObjectMapper om = new ObjectMapper(); | |||||
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); | |||||
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | |||||
j.setObjectMapper(om); | |||||
// value值的序列化 | |||||
template.setValueSerializer(j); | |||||
template.setHashValueSerializer(j); | |||||
// key的序列化 | |||||
template.setKeySerializer(new StringRedisSerializer()); | |||||
template.setHashKeySerializer(new StringRedisSerializer()); | |||||
template.afterPropertiesSet(); | |||||
return template; | |||||
} | |||||
} | } |
@@ -1,6 +1,7 @@ | |||||
package com.iformall.controller; | package com.iformall.controller; | ||||
import com.alibaba.fastjson.JSON; | import com.alibaba.fastjson.JSON; | ||||
import com.alibaba.fastjson.JSONObject; | |||||
import com.github.pagehelper.PageInfo; | import com.github.pagehelper.PageInfo; | ||||
import com.iformall.common.ErrorCode; | import com.iformall.common.ErrorCode; | ||||
import com.iformall.common.ResultData; | import com.iformall.common.ResultData; | ||||
@@ -14,6 +15,8 @@ import com.iformall.service.TtMerchantPoiService; | |||||
import com.iformall.service.WxAppinfoService; | import com.iformall.service.WxAppinfoService; | ||||
import com.iformall.service.toutiao.FmTtOpenService; | import com.iformall.service.toutiao.FmTtOpenService; | ||||
import com.iformall.service.toutiao.api.TtOpenMaService; | import com.iformall.service.toutiao.api.TtOpenMaService; | ||||
import com.iformall.utils.Constant; | |||||
import com.iformall.utils.RedisCacheUtils; | |||||
import io.swagger.annotations.ApiImplicitParam; | import io.swagger.annotations.ApiImplicitParam; | ||||
import io.swagger.annotations.ApiImplicitParams; | import io.swagger.annotations.ApiImplicitParams; | ||||
import io.swagger.annotations.ApiOperation; | import io.swagger.annotations.ApiOperation; | ||||
@@ -21,6 +24,8 @@ import org.apache.commons.lang3.StringUtils; | |||||
import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||
import org.springframework.beans.factory.annotation.Qualifier; | |||||
import org.springframework.data.redis.core.RedisTemplate; | |||||
import org.springframework.web.bind.annotation.*; | import org.springframework.web.bind.annotation.*; | ||||
import org.springframework.web.multipart.MultipartFile; | import org.springframework.web.multipart.MultipartFile; | ||||
@@ -43,6 +48,10 @@ public class TtMerchantPoiController extends BaseController { | |||||
@Autowired | @Autowired | ||||
protected FmTtOpenService openService; | protected FmTtOpenService openService; | ||||
@Autowired | |||||
@Qualifier("objectCommonRedisTemplate") | |||||
RedisTemplate<String, Object> redisTemplate; | |||||
@ApiOperation("分页列表接口") | @ApiOperation("分页列表接口") | ||||
@GetMapping("list") | @GetMapping("list") | ||||
@ApiImplicitParams({ | @ApiImplicitParams({ | ||||
@@ -323,16 +332,31 @@ public class TtMerchantPoiController extends BaseController { | |||||
if(!type.equals("1128") && !type.equals("2329")){ | if(!type.equals("1128") && !type.equals("2329")){ | ||||
return new ResultData(ErrorCode.SYS_PARAMETER_ERROR); | return new ResultData(ErrorCode.SYS_PARAMETER_ERROR); | ||||
} | } | ||||
WxAppinfo appinfo = appinfoService.getByAppId(appid); | |||||
String key = Constant.tt_cuser_customer_service_url_key + "type:" | |||||
+ appid + ":" + openid; | |||||
String cacheString = RedisCacheUtils.getCacheString(redisTemplate, key); | |||||
if(appinfo == null){ | |||||
return new ResultData(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"找不到小程序信息"); | |||||
if(StringUtils.isNotBlank(cacheString)){ | |||||
return new ResultData(cacheString); | |||||
} | } | ||||
// WxAppinfo appinfo = appinfoService.getByAppId(appid); | |||||
// | |||||
// if(appinfo == null){ | |||||
// return new ResultData(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"找不到小程序信息"); | |||||
// } | |||||
try{ | try{ | ||||
TtOpenMaService openMaService = openService.getTtOpenComponentService().getTtMaServiceByAppid(appid); | TtOpenMaService openMaService = openService.getTtOpenComponentService().getTtMaServiceByAppid(appid); | ||||
String res = openMaService.queryCustomerServiceUrl(openid,type); | String res = openMaService.queryCustomerServiceUrl(openid,type); | ||||
return new ResultData(res); | |||||
JSONObject jsonObject = JSONObject.parseObject(res); | |||||
Integer err_no = jsonObject.getInteger("err_no"); | |||||
if(err_no.intValue() == 0){ | |||||
RedisCacheUtils.cache(redisTemplate,key,res,24*3600); | |||||
return new ResultData(res); | |||||
}else{ | |||||
String err_tips = jsonObject.getString("err_tips"); | |||||
return new ResultData(ErrorCode.SYS_SERVER_ERROR.getCode(),err_tips); | |||||
} | |||||
}catch(Exception e){ | }catch(Exception e){ | ||||
logger.error("获取客服链接 error"+ e.getMessage()); | logger.error("获取客服链接 error"+ e.getMessage()); | ||||
return new ResultData(ErrorCode.SYS_SERVER_ERROR.getCode(),"获取客服链接失败"); | return new ResultData(ErrorCode.SYS_SERVER_ERROR.getCode(),"获取客服链接失败"); | ||||