添加了手机号短信验证码注册的代码模块
This commit is contained in:
parent
dc9c235745
commit
1fdbfb97b1
22
pom.xml
22
pom.xml
@ -32,6 +32,7 @@
|
||||
<httpclient.version>4.5.2</httpclient.version>
|
||||
<poi.version>5.0.0</poi.version>
|
||||
<poi-ooxml.version>5.0.0</poi-ooxml.version>
|
||||
<dysmsapi20170525.version>2.0.4</dysmsapi20170525.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@ -39,6 +40,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
@ -193,6 +198,23 @@
|
||||
<version>${poi-ooxml.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--阿里云短信服务-->
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>aliyun-java-sdk-core</artifactId>
|
||||
<version>4.0.6</version>
|
||||
</dependency>
|
||||
<!--<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>dysmsapi20170525</artifactId>
|
||||
<version>${dysmsapi20170525.version}</version>
|
||||
</dependency>-->
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
|
||||
<version>1.1.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@ -0,0 +1,30 @@
|
||||
package com.xkrs.common.config;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Redis 缓存自动配置
|
||||
* @author tajochen
|
||||
*/
|
||||
@Configuration
|
||||
@AutoConfigureAfter(RedisAutoConfiguration.class)
|
||||
public class RedisCacheAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
|
||||
RedisTemplate<String, Serializable> template = new RedisTemplate<>();
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
|
||||
template.setConnectionFactory(redisConnectionFactory);
|
||||
return template;
|
||||
}
|
||||
}
|
161
src/main/java/com/xkrs/common/config/RedisConfig.java
Normal file
161
src/main/java/com/xkrs/common/config/RedisConfig.java
Normal file
@ -0,0 +1,161 @@
|
||||
package com.xkrs.common.config;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.interceptor.CacheErrorHandler;
|
||||
import org.springframework.cache.interceptor.CacheResolver;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
||||
import org.springframework.data.redis.cache.RedisCacheManager;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.RedisSerializationContext;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* redis配置
|
||||
* @author tajochen
|
||||
*/
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public class RedisConfig extends CachingConfigurerSupport {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class);
|
||||
|
||||
@Resource
|
||||
private LettuceConnectionFactory lettuceConnectionFactory;
|
||||
|
||||
private Duration timeToLive = Duration.ofSeconds(3600*6);
|
||||
|
||||
@Bean
|
||||
@Override
|
||||
public KeyGenerator keyGenerator() {
|
||||
|
||||
// 设置自动key的生成规则,配置spring boot的注解,进行方法级别的缓存
|
||||
return (target, method, params) -> {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(target.getClass().getName());
|
||||
sb.append(":");
|
||||
sb.append(method.getName());
|
||||
for (Object obj : params) {
|
||||
sb.append(":" + String.valueOf(obj));
|
||||
}
|
||||
String rsToUse = String.valueOf(sb);
|
||||
// logger.info("自动生成Redis Key -> [{}]", rsToUse);
|
||||
return rsToUse;
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Override
|
||||
public CacheManager cacheManager() {
|
||||
// 关键点,spring cache的注解使用的序列化都从这来,没有这个配置的话使用的jdk自己的序列化
|
||||
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
|
||||
//key序列化方式
|
||||
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))
|
||||
//value序列化方式
|
||||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()))
|
||||
.disableCachingNullValues()
|
||||
.entryTtl(timeToLive);
|
||||
|
||||
RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder
|
||||
.fromConnectionFactory(lettuceConnectionFactory)
|
||||
.cacheDefaults(config)
|
||||
.transactionAware();
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* RedisTemplate配置 在单独使用redisTemplate的时候 重新定义序列化方式
|
||||
*/
|
||||
@Bean
|
||||
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
|
||||
// 设置序列化
|
||||
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
// 配置null值序列化成空字符串
|
||||
om.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
|
||||
@Override
|
||||
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
||||
jsonGenerator.writeString("");
|
||||
}
|
||||
});
|
||||
// 解决jackson无法反序列化LocalDateTime的问题,引入jsr310标准
|
||||
JavaTimeModule javaTimeModule = new JavaTimeModule();
|
||||
om.registerModule(javaTimeModule);
|
||||
om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
// 其他设置
|
||||
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance ,
|
||||
ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
|
||||
jackson2JsonRedisSerializer.setObjectMapper(om);
|
||||
// 配置redisTemplate
|
||||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
|
||||
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
|
||||
RedisSerializer<?> stringSerializer = new StringRedisSerializer();
|
||||
redisTemplate.setKeySerializer(stringSerializer);
|
||||
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
|
||||
redisTemplate.setHashKeySerializer(stringSerializer);
|
||||
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
|
||||
redisTemplate.afterPropertiesSet();
|
||||
return redisTemplate;
|
||||
}
|
||||
|
||||
private RedisSerializer<String> keySerializer() {
|
||||
return new StringRedisSerializer();
|
||||
}
|
||||
|
||||
private RedisSerializer<Object> valueSerializer() {
|
||||
// 设置序列化
|
||||
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
|
||||
Object.class);
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
// 解决jackson无法反序列化LocalDateTime的问题,引入jsr310标准
|
||||
JavaTimeModule javaTimeModule = new JavaTimeModule();
|
||||
om.registerModule(javaTimeModule);
|
||||
om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
// 其他设置
|
||||
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance ,
|
||||
ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
|
||||
jackson2JsonRedisSerializer.setObjectMapper(om);
|
||||
return jackson2JsonRedisSerializer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CacheResolver cacheResolver() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Bean
|
||||
public CacheErrorHandler errorHandler() {
|
||||
// 异常处理,当Redis发生异常时,打印日志,但是程序正常走
|
||||
logger.info("初始化 -> [{}]", "Redis CacheErrorHandler");
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -53,6 +53,7 @@ class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
.antMatchers(HttpMethod.GET,"/weather/cityName").permitAll()
|
||||
.antMatchers(HttpMethod.GET,"/selectFirePoint").permitAll()
|
||||
.antMatchers(HttpMethod.POST,"/updateTypeByFireCode").permitAll()
|
||||
.antMatchers(HttpMethod.GET,"/api/user/verificationCode").permitAll()
|
||||
// 所有其它请求需要身份认证
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.xkrs.controller;
|
||||
|
||||
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
|
||||
import com.aliyuncs.exceptions.ClientException;
|
||||
import com.xkrs.common.encapsulation.PromptMessageEnum;
|
||||
import com.xkrs.common.tool.TokenUtil;
|
||||
import com.xkrs.dao.SysUserDao;
|
||||
@ -8,7 +10,9 @@ import com.xkrs.model.qo.SysUserQo;
|
||||
import com.xkrs.model.validation.SysUserQoInsert;
|
||||
import com.xkrs.model.validation.SysUserQoUpdate;
|
||||
import com.xkrs.model.vo.SysUserVo;
|
||||
import com.xkrs.service.RedisService;
|
||||
import com.xkrs.service.SysUserService;
|
||||
import com.xkrs.utils.RandomUtil;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.BindingResult;
|
||||
@ -20,9 +24,11 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationErrorList;
|
||||
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
|
||||
import static com.xkrs.utils.AliyunSmsUtils.sendSms;
|
||||
|
||||
/**
|
||||
* 系统用户Controller
|
||||
@ -38,6 +44,9 @@ public class SysUserController {
|
||||
@Resource
|
||||
private SysUserDao sysUserDao;
|
||||
|
||||
@Resource
|
||||
private RedisService redisService;
|
||||
|
||||
/**
|
||||
* 登录用户Token验证
|
||||
* @return
|
||||
@ -74,8 +83,7 @@ public class SysUserController {
|
||||
return outputEncapsulationObject(PromptMessageEnum.PARAM_ILLEGAL,"该账号已经注册,请勿重复注册",locale);
|
||||
}
|
||||
// 添加新用户
|
||||
sysUserService.addUser(userQo);
|
||||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"OK",locale);
|
||||
return sysUserService.addUser(userQo);
|
||||
}
|
||||
|
||||
|
||||
@ -240,4 +248,14 @@ public class SysUserController {
|
||||
return sysUserService.adminUpdatePassword(userId,newPassword,confirmPassword);
|
||||
}
|
||||
|
||||
@GetMapping("/verificationCode")
|
||||
public String verificationCode(@RequestParam("phone") String phone) throws ClientException {
|
||||
// 获取区域信息
|
||||
Locale locale = LocaleContextHolder.getLocale();
|
||||
String optCode = String.valueOf(RandomUtil.returnCode());
|
||||
redisService.set(phone,optCode,10, TimeUnit.MINUTES);
|
||||
SendSmsResponse response =sendSms(phone,optCode);
|
||||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"",locale);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -56,6 +56,8 @@ public class SysUserQo {
|
||||
|
||||
private Integer authorityId;
|
||||
|
||||
private String verificationCode;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
@ -135,4 +137,12 @@ public class SysUserQo {
|
||||
public void setAuthorityId(Integer authorityId) {
|
||||
this.authorityId = authorityId;
|
||||
}
|
||||
|
||||
public String getVerificationCode() {
|
||||
return verificationCode;
|
||||
}
|
||||
|
||||
public void setVerificationCode(String verificationCode) {
|
||||
this.verificationCode = verificationCode;
|
||||
}
|
||||
}
|
||||
|
993
src/main/java/com/xkrs/service/RedisService.java
Normal file
993
src/main/java/com/xkrs/service/RedisService.java
Normal file
@ -0,0 +1,993 @@
|
||||
package com.xkrs.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @ClassName: RedisService
|
||||
*/
|
||||
@Service
|
||||
public class RedisService {
|
||||
|
||||
@Resource
|
||||
private RedisTemplate<String,Object> redisTemplate;
|
||||
/** -------------------key相关操作--------------------- */
|
||||
|
||||
/**
|
||||
* 是否存在key
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @return java.lang.Boolean
|
||||
* @throws
|
||||
*/
|
||||
public Boolean hasKey(String key) {
|
||||
if (null==key){
|
||||
return false;
|
||||
}
|
||||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除key
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @return Boolean 成功返回true 失败返回false
|
||||
* @throws
|
||||
*/
|
||||
public Boolean delete(String key) {
|
||||
if (null==key){
|
||||
return false;
|
||||
}
|
||||
return redisTemplate.delete(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除key
|
||||
* @Author: 小霍
|
||||
* @CreateDate: 2019/8/27 20:27
|
||||
* @UpdateUser:
|
||||
* @UpdateDate: 2019/8/27 20:27
|
||||
* @Version: 0.0.1
|
||||
* @param keys
|
||||
* @return Long 返回成功删除key的数量
|
||||
* @throws
|
||||
*/
|
||||
public Long delete(Collection<String> keys) {
|
||||
return redisTemplate.delete(keys);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置过期时间
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param timeout
|
||||
* @param unit
|
||||
* @return java.lang.Boolean
|
||||
* @throws
|
||||
*/
|
||||
public Boolean expire(String key, long timeout, TimeUnit unit) {
|
||||
if (null==key||null==unit){
|
||||
return false;
|
||||
}
|
||||
return redisTemplate.expire(key, timeout, unit);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找匹配的key
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param pattern
|
||||
* @return java.util.Set<java.lang.String>
|
||||
* @throws
|
||||
*/
|
||||
public Set<String> keys(String pattern) {
|
||||
if (null==pattern){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.keys(pattern);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 移除 key 的过期时间,key 将持久保持
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @return java.lang.Boolean
|
||||
* @throws
|
||||
*/
|
||||
public Boolean persist(String key) {
|
||||
if (null==key){
|
||||
return false;
|
||||
}
|
||||
return redisTemplate.persist(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 key 的剩余的过期时间
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param unit
|
||||
* @return java.lang.Long 当 key 不存在时,返回 -2 。当 key 存在但没有设置剩余生存时间时,返回 -1 。否则,以秒为单位,返回 key的剩余生存时间
|
||||
* @throws
|
||||
*/
|
||||
public Long getExpire(String key, TimeUnit unit) {
|
||||
if(null==key||null==unit){
|
||||
// throw new BusinessException(4001004,"key or TomeUnit 不能为空");
|
||||
}
|
||||
return redisTemplate.getExpire(key, unit);
|
||||
}
|
||||
|
||||
//*************String相关数据类型***************************
|
||||
/**
|
||||
* 设置指定 key 的值
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param value
|
||||
* @return void
|
||||
* @throws
|
||||
*/
|
||||
public void set(String key, Object value) {
|
||||
|
||||
if(null==key||null==value){
|
||||
return;
|
||||
}
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
}
|
||||
/**
|
||||
* 设置key 的值 并设置过期时间
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param value
|
||||
* @param time
|
||||
* @param unit
|
||||
* @return void
|
||||
* @throws
|
||||
*/
|
||||
public void set(String key,Object value,long time,TimeUnit unit){
|
||||
|
||||
if(null==key||null==value||null==unit){
|
||||
return;
|
||||
}
|
||||
redisTemplate.opsForValue().set(key,value,time,unit);
|
||||
}
|
||||
/**
|
||||
* 设置key 的值 并设置过期时间
|
||||
* key存在 不做操作返回false
|
||||
* key不存在设置值返回true
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param value
|
||||
* @param time
|
||||
* @param unit
|
||||
* @return java.lang.Boolean
|
||||
* @throws
|
||||
*/
|
||||
public Boolean setifAbsen(String key,Object value,long time,TimeUnit unit){
|
||||
|
||||
if(null==key||null==value||null==unit){
|
||||
// throw new BusinessException(4001004,"kkey、value、unit都不能为空");
|
||||
}
|
||||
return redisTemplate.opsForValue().setIfAbsent(key,value,time,unit);
|
||||
}
|
||||
/**
|
||||
* 获取指定Key的Value。如果与该Key关联的Value不是string类型,Redis将抛出异常,
|
||||
* 因为GET命令只能用于获取string Value,如果该Key不存在,返回null
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @return java.lang.Object
|
||||
* @throws
|
||||
*/
|
||||
public Object get(String key){
|
||||
|
||||
if(null==key){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
/**
|
||||
* 很明显先get再set就说先获取key值对应的value然后再set 新的value 值。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param value
|
||||
* @return java.lang.Object
|
||||
* @throws
|
||||
*/
|
||||
public Object getSet(String key,Object value){
|
||||
|
||||
if(null==key){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForValue().getAndSet(key,value);
|
||||
}
|
||||
/**
|
||||
* 通过批量的key获取批量的value
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param keys
|
||||
* @return java.util.List<java.lang.Object>
|
||||
* @throws
|
||||
*/
|
||||
public List<Object> mget(Collection<String> keys){
|
||||
|
||||
if(null==keys){
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return redisTemplate.opsForValue().multiGet(keys);
|
||||
}
|
||||
/**
|
||||
* 将指定Key的Value原子性的增加increment。如果该Key不存在,其初始值为0,在incrby之后其值为increment。
|
||||
* 如果Value的值不能转换为整型值,如Hi,该操作将执行失败并抛出相应异常。操作成功则返回增加后的value值。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param increment
|
||||
* @return long
|
||||
* @throws
|
||||
*/
|
||||
public long incrby(String key,long increment){
|
||||
if(null==key){
|
||||
// throw new BusinessException(4001004,"key不能为空");
|
||||
}
|
||||
return redisTemplate.opsForValue().increment(key,increment);
|
||||
}
|
||||
/**
|
||||
*
|
||||
* 将指定Key的Value原子性的减少decrement。如果该Key不存在,其初始值为0,
|
||||
* 在decrby之后其值为-decrement。如果Value的值不能转换为整型值,
|
||||
* 如Hi,该操作将执行失败并抛出相应的异常。操作成功则返回减少后的value值。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param decrement
|
||||
* @return java.lang.Long
|
||||
* @throws
|
||||
*/
|
||||
public Long decrby(String key,long decrement){
|
||||
if(null==key){
|
||||
// throw new BusinessException(4001004,"key不能为空");
|
||||
}
|
||||
return redisTemplate.opsForValue().decrement(key,decrement);
|
||||
}
|
||||
/**
|
||||
* 如果该Key已经存在,APPEND命令将参数Value的数据追加到已存在Value的末尾。如果该Key不存在,
|
||||
* APPEND命令将会创建一个新的Key/Value。返回追加后Value的字符串长度。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param value
|
||||
* @return java.lang.Integer
|
||||
* @throws
|
||||
*/
|
||||
public Integer append(String key,String value){
|
||||
if(key==null){
|
||||
// throw new BusinessException(4001004,"key不能为空");
|
||||
}
|
||||
return redisTemplate.opsForValue().append(key,value);
|
||||
}
|
||||
//******************hash数据类型*********************
|
||||
/**
|
||||
* 通过key 和 field 获取指定的 value
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param field
|
||||
* @return java.lang.Object
|
||||
* @throws
|
||||
*/
|
||||
public Object hget(String key, Object field) {
|
||||
if(null==key||null==field){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForHash().get(key,field);
|
||||
}
|
||||
|
||||
/**
|
||||
* 为指定的Key设定Field/Value对,如果Key不存在,该命令将创建新Key以用于存储参数中的Field/Value对,
|
||||
* 如果参数中的Field在该Key中已经存在,则用新值覆盖其原有值。
|
||||
* 返回1表示新的Field被设置了新值,0表示Field已经存在,用新值覆盖原有值。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param field
|
||||
* @param value
|
||||
* @return
|
||||
* @throws
|
||||
*/
|
||||
public void hset(String key, Object field, Object value) {
|
||||
if(null==key||null==field){
|
||||
return;
|
||||
}
|
||||
redisTemplate.opsForHash().put(key,field,value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断指定Key中的指定Field是否存在,返回true表示存在,false表示参数中的Field或Key不存在。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param field
|
||||
* @return java.lang.Boolean
|
||||
* @throws
|
||||
*/
|
||||
public Boolean hexists(String key, Object field) {
|
||||
if(null==key||null==field){
|
||||
return false;
|
||||
}
|
||||
return redisTemplate.opsForHash().hasKey(key,field);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从指定Key的Hashes Value中删除参数中指定的多个字段,如果不存在的字段将被忽略,
|
||||
* 返回实际删除的Field数量。如果Key不存在,则将其视为空Hashes,并返回0。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param fields
|
||||
* @return java.lang.Long
|
||||
* @throws
|
||||
*/
|
||||
public Long hdel(String key, Object... fields) {
|
||||
if(null==key||null==fields||fields.length==0){
|
||||
return 0L;
|
||||
}
|
||||
return redisTemplate.opsForHash().delete(key,fields);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 通过key获取所有的field和value
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @return java.util.Map<java.lang.Object,java.lang.Object>
|
||||
* @throws
|
||||
*/
|
||||
public Map<Object, Object> hgetall(String key) {
|
||||
if(key==null){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForHash().entries(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 逐对依次设置参数中给出的Field/Value对。如果其中某个Field已经存在,则用新值覆盖原有值。
|
||||
* 如果Key不存在,则创建新Key,同时设定参数中的Field/Value。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param hash
|
||||
* @return
|
||||
* @throws
|
||||
*/
|
||||
public void hmset(String key, Map<String, Object> hash) {
|
||||
|
||||
if(null==key||null==hash){
|
||||
return;
|
||||
}
|
||||
redisTemplate.opsForHash().putAll(key,hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取和参数中指定Fields关联的一组Values,其返回顺序等同于Fields的请求顺序。
|
||||
* 如果请求的Field不存在,其值对应的value为null。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param fields
|
||||
* @return java.util.List<java.lang.Object>
|
||||
* @throws
|
||||
*/
|
||||
public List<Object> hmget(String key, Collection<Object> fields) {
|
||||
|
||||
if(null==key||null==fields){
|
||||
return null;
|
||||
}
|
||||
|
||||
return redisTemplate.opsForHash().multiGet(key,fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对应key的字段自增相应的值
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param field
|
||||
* @param increment
|
||||
* @return java.lang.Long
|
||||
* @throws
|
||||
*/
|
||||
public Long hIncrBy(String key,Object field,long increment){
|
||||
if (null==key||null==field){
|
||||
// throw new BusinessException(4001004,"key or field 不能为空");
|
||||
}
|
||||
return redisTemplate.opsForHash().increment(key,field,increment);
|
||||
|
||||
}
|
||||
//***************List数据类型***************
|
||||
/**
|
||||
* 向列表左边添加元素。如果该Key不存在,该命令将在插入之前创建一个与该Key关联的空链表,之后再将数据从链表的头部插入。
|
||||
* 如果该键的Value不是链表类型,该命令将将会抛出相关异常。操作成功则返回插入后链表中元素的数量。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param strs 可以使一个string 也可以使string数组
|
||||
* @return java.lang.Long 返回操作的value个数
|
||||
* @throws
|
||||
*/
|
||||
public Long lpush(String key, Object... strs) {
|
||||
if(null==key){
|
||||
return 0L;
|
||||
}
|
||||
return redisTemplate.opsForList().leftPushAll(key,strs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 向列表右边添加元素。如果该Key不存在,该命令将在插入之前创建一个与该Key关联的空链表,之后再将数据从链表的尾部插入。
|
||||
* 如果该键的Value不是链表类型,该命令将将会抛出相关异常。操作成功则返回插入后链表中元素的数量。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param strs 可以使一个string 也可以使string数组
|
||||
* @return java.lang.Long 返回操作的value个数
|
||||
* @throws
|
||||
*/
|
||||
public Long rpush(String key, Object... strs) {
|
||||
if(null==key){
|
||||
return 0L;
|
||||
}
|
||||
return redisTemplate.opsForList().rightPushAll(key,strs);
|
||||
}
|
||||
/**
|
||||
* 返回并弹出指定Key关联的链表中的第一个元素,即头部元素。如果该Key不存在,
|
||||
* 返回nil。LPOP命令执行两步操作:第一步是将列表左边的元素从列表中移除,第二步是返回被移除的元素值。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @return java.lang.Object
|
||||
* @throws
|
||||
*/
|
||||
public Object lpop(String key) {
|
||||
if(null==key){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForList().leftPop(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回并弹出指定Key关联的链表中的最后一个元素,即头部元素。如果该Key不存在,返回nil。
|
||||
* RPOP命令执行两步操作:第一步是将列表右边的元素从列表中移除,第二步是返回被移除的元素值。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @return java.lang.Object
|
||||
* @throws
|
||||
*/
|
||||
public Object rpop(String key) {
|
||||
if(null==key){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForList().rightPop(key);
|
||||
}
|
||||
|
||||
/**
|
||||
*该命令的参数start和end都是0-based。即0表示链表头部(leftmost)的第一个元素。
|
||||
* 其中start的值也可以为负值,-1将表示链表中的最后一个元素,即尾部元素,-2表示倒数第二个并以此类推。
|
||||
* 该命令在获取元素时,start和end位置上的元素也会被取出。如果start的值大于链表中元素的数量,
|
||||
* 空链表将会被返回。如果end的值大于元素的数量,该命令则获取从start(包括start)开始,链表中剩余的所有元素。
|
||||
* 注:Redis的列表起始索引为0。显然,LRANGE numbers 0 -1 可以获取列表中的所有元素。返回指定范围内元素的列表。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param start
|
||||
* @param end
|
||||
* @return java.util.List<java.lang.Object>
|
||||
* @throws
|
||||
*/
|
||||
public List<Object> lrange(String key, long start, long end) {
|
||||
if(null==key){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForList().range(key,start,end);
|
||||
}
|
||||
|
||||
/**
|
||||
* 让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。
|
||||
* 下标 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。
|
||||
* 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param start
|
||||
* @param end
|
||||
* @return
|
||||
* @throws
|
||||
*/
|
||||
public void ltrim(String key, long start, long end) {
|
||||
if(null==key){
|
||||
return;
|
||||
}
|
||||
redisTemplate.opsForList().trim(key,start,end);
|
||||
}
|
||||
|
||||
/**
|
||||
* 该命令将返回链表中指定位置(index)的元素,index是0-based,表示从头部位置开始第index的元素,
|
||||
* 如果index为-1,表示尾部元素。如果与该Key关联的不是链表,该命令将返回相关的错误信息。 如果超出index返回这返回nil。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param index
|
||||
* @return java.lang.Object
|
||||
* @throws
|
||||
*/
|
||||
public Object lindex(String key, long index) {
|
||||
if(null==key){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForList().index(key,index);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回指定Key关联的链表中元素的数量,如果该Key不存在,则返回0。如果与该Key关联的Value的类型不是链表,则抛出相关的异常。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @return java.lang.Long
|
||||
* @throws
|
||||
*/
|
||||
public Long llen(String key) {
|
||||
|
||||
if(null==key){
|
||||
return 0L;
|
||||
}
|
||||
return redisTemplate.opsForList().size(key);
|
||||
}
|
||||
//***************Set数据类型*************
|
||||
/**
|
||||
* 如果在插入的过程用,参数中有的成员在Set中已经存在,该成员将被忽略,而其它成员仍将会被正常插入。
|
||||
* 如果执行该命令之前,该Key并不存在,该命令将会创建一个新的Set,此后再将参数中的成员陆续插入。返回实际插入的成员数量。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param members 可以是一个String 也可以是一个String数组
|
||||
* @return java.lang.Long 添加成功的个数
|
||||
* @throws
|
||||
*/
|
||||
public Long sadd(String key, Object... members) {
|
||||
if (null==key){
|
||||
return 0L;
|
||||
}
|
||||
return redisTemplate.opsForSet().add(key, members);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回Set中成员的数量,如果该Key并不存在,返回0。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @return java.lang.Long
|
||||
* @throws
|
||||
*/
|
||||
public Long scard(String key) {
|
||||
if (null==key){
|
||||
return 0L;
|
||||
}
|
||||
return redisTemplate.opsForSet().size(key);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断参数中指定成员是否已经存在于与Key相关联的Set集合中。返回true表示已经存在,false表示不存在,或该Key本身并不存在。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param member
|
||||
* @return java.lang.Boolean
|
||||
* @throws
|
||||
*/
|
||||
public Boolean sismember(String key, Object member) {
|
||||
if (null==key){
|
||||
return false;
|
||||
}
|
||||
return redisTemplate.opsForSet().isMember(key,member);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 和SPOP一样,随机的返回Set中的一个成员,不同的是该命令并不会删除返回的成员。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @return java.lang.String
|
||||
* @throws
|
||||
*/
|
||||
public Object srandmember(String key) {
|
||||
if (null==key){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForSet().randomMember(key);
|
||||
|
||||
}
|
||||
/**
|
||||
* 和SPOP一样,随机的返回Set中的一个成员,不同的是该命令并不会删除返回的成员。
|
||||
* 还可以传递count参数来一次随机获得多个元素,根据count的正负不同,具体表现也不同。
|
||||
* 当count 为正数时,SRANDMEMBER 会随机从集合里获得count个不重复的元素。
|
||||
* 如果count的值大于集合中的元素个数,则SRANDMEMBER 会返回集合中的全部元素。
|
||||
* 当count为负数时,SRANDMEMBER 会随机从集合里获得|count|个的元素,如果|count|大与集合中的元素,
|
||||
* 就会返回全部元素不够的以重复元素补齐,如果key不存在则返回nil。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param count
|
||||
* @return java.util.List<java.lang.String>
|
||||
* @throws
|
||||
*/
|
||||
public List<Object> srandmember(String key,int count) {
|
||||
if(null==key){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForSet().randomMembers(key,count);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过key随机删除一个set中的value并返回该值
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @return java.lang.String
|
||||
* @throws
|
||||
*/
|
||||
public Object spop(String key) {
|
||||
if (null==key){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForSet().pop(key);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过key获取set中所有的value
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @return java.util.Set<java.lang.String>
|
||||
* @throws
|
||||
*/
|
||||
public Set<Object> smembers(String key) {
|
||||
if (null==key){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForSet().members(key);
|
||||
|
||||
}
|
||||
/**
|
||||
* 从与Key关联的Set中删除参数中指定的成员,不存在的参数成员将被忽略,
|
||||
* 如果该Key并不存在,将视为空Set处理。返回从Set中实际移除的成员数量,如果没有则返回0。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param members
|
||||
* @return java.lang.Long
|
||||
* @throws
|
||||
*/
|
||||
public Long srem(String key, Object... members) {
|
||||
if (null==key){
|
||||
return 0L;
|
||||
}
|
||||
return redisTemplate.opsForSet().remove(key,members);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 将元素value从一个集合移到另一个集合
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param srckey
|
||||
* @param dstkey
|
||||
* @param member
|
||||
* @return java.lang.Long
|
||||
* @throws
|
||||
*/
|
||||
public Boolean smove(String srckey, String dstkey, Object member) {
|
||||
if (null==srckey||null==dstkey){
|
||||
return false;
|
||||
}
|
||||
return redisTemplate.opsForSet().move(srckey,member,dstkey);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取两个集合的并集
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param otherKeys
|
||||
* @return java.util.Set<java.lang.Object> 返回两个集合合并值
|
||||
* @throws
|
||||
*/
|
||||
public Set<Object> sUnion(String key, String otherKeys) {
|
||||
if (null==key||otherKeys==null){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForSet().union(key, otherKeys);
|
||||
}
|
||||
//**********Sorted Set 数据类型********************
|
||||
/**
|
||||
*添加参数中指定的所有成员及其分数到指定key的Sorted Set中,在该命令中我们可以指定多组score/member作为参数。
|
||||
* 如果在添加时参数中的某一成员已经存在,该命令将更新此成员的分数为新值,同时再将该成员基于新值重新排序。
|
||||
* 如果键不存在,该命令将为该键创建一个新的Sorted Set Value,并将score/member对插入其中。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param score
|
||||
* @param member
|
||||
* @return java.lang.Long
|
||||
* @throws
|
||||
*/
|
||||
public Boolean zadd(String key, double score, Object member) {
|
||||
if (null==key){
|
||||
return false;
|
||||
}
|
||||
return redisTemplate.opsForZSet().add(key,member,score);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 该命令将移除参数中指定的成员,其中不存在的成员将被忽略。
|
||||
* 如果与该Key关联的Value不是Sorted Set,相应的错误信息将被返回。 如果操作成功则返回实际被删除的成员数量。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param members 可以使一个string 也可以是一个string数组
|
||||
* @return java.lang.Long
|
||||
* @throws
|
||||
*/
|
||||
public Long zrem(String key, Object... members) {
|
||||
if(null==key||null==members){
|
||||
return 0L;
|
||||
}
|
||||
return redisTemplate.opsForZSet().remove(key,members);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回Sorted Set中的成员数量,如果该Key不存在,返回0。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @return java.lang.Long
|
||||
* @throws
|
||||
*/
|
||||
public Long zcard(String key) {
|
||||
if (null==key){
|
||||
return 0L;
|
||||
}
|
||||
return redisTemplate.opsForZSet().size(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 该命令将为指定Key中的指定成员增加指定的分数。如果成员不存在,该命令将添加该成员并假设其初始分数为0,
|
||||
* 此后再将其分数加上increment。如果Key不存在,该命令将创建该Key及其关联的Sorted Set,
|
||||
* 并包含参数指定的成员,其分数为increment参数。如果与该Key关联的不是Sorted Set类型,
|
||||
* 相关的错误信息将被返回。如果不报错则以串形式表示的新分数。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param score
|
||||
* @param member
|
||||
* @return java.lang.Double
|
||||
* @throws
|
||||
*/
|
||||
public Double zincrby(String key, double score, Object member) {
|
||||
if (null==key){
|
||||
// throw new BusinessException(4001004,"key 不能为空");
|
||||
}
|
||||
return redisTemplate.opsForZSet().incrementScore(key,member,score);
|
||||
}
|
||||
|
||||
/**
|
||||
* 该命令用于获取分数(score)在min和max之间的成员数量。
|
||||
* (min=<score<=max)如果加上了“(”着表明是开区间例如zcount key (min max 则 表示(min<score=<max)
|
||||
* 同理zcount key min (max 则表明(min=<score<max) 返回指定返回数量。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param min
|
||||
* @param max
|
||||
* @return java.lang.Long
|
||||
* @throws
|
||||
*/
|
||||
public Long zcount(String key, double min, double max) {
|
||||
if (null==key){
|
||||
return 0L;
|
||||
}
|
||||
return redisTemplate.opsForZSet().count(key, min, max);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorted Set中的成员都是按照分数从低到高的顺序存储,该命令将返回参数中指定成员的位置值,
|
||||
* 其中0表示第一个成员,它是Sorted Set中分数最低的成员。 如果该成员存在,则返回它的位置索引值。否则返回nil。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param member
|
||||
* @return java.lang.Long
|
||||
* @throws
|
||||
*/
|
||||
public Long zrank(String key, Object member) {
|
||||
if (null==key){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForZSet().rank(key,member);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果该成员存在,以字符串的形式返回其分数,否则返回null
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param member
|
||||
* @return java.lang.Double
|
||||
* @throws
|
||||
*/
|
||||
public Double zscore(String key, Object member) {
|
||||
if (null==key){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForZSet().score(key,member);
|
||||
}
|
||||
|
||||
/**
|
||||
* 该命令返回顺序在参数start和stop指定范围内的成员,这里start和stop参数都是0-based,即0表示第一个成员,-1表示最后一个成员。如果start大于该Sorted
|
||||
* Set中的最大索引值,或start > stop,此时一个空集合将被返回。如果stop大于最大索引值,
|
||||
* 该命令将返回从start到集合的最后一个成员。如果命令中带有可选参数WITHSCORES选项,
|
||||
* 该命令在返回的结果中将包含每个成员的分数值,如value1,score1,value2,score2...。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param min
|
||||
* @param max
|
||||
* @return java.util.Set<java.lang.String> 指定区间内的有序集成员的列表。
|
||||
* @throws
|
||||
*/
|
||||
public Set<Object> zrange(String key, long min, long max) {
|
||||
if (null==key){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForZSet().range(key, min, max);
|
||||
|
||||
}
|
||||
/**
|
||||
* 该命令的功能和ZRANGE基本相同,唯一的差别在于该命令是通过反向排序获取指定位置的成员,
|
||||
* 即从高到低的顺序。如果成员具有相同的分数,则按降序字典顺序排序。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param start
|
||||
* @param end
|
||||
* @return java.util.Set<java.lang.String>
|
||||
* @throws
|
||||
*/
|
||||
public Set<Object> zReverseRange(String key, long start, long end) {
|
||||
if (null==key){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForZSet().reverseRange(key, start, end);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 该命令将返回分数在min和max之间的所有成员,即满足表达式min <= score <= max的成员,
|
||||
* 其中返回的成员是按照其分数从低到高的顺序返回,如果成员具有相同的分数,
|
||||
* 则按成员的字典顺序返回。可选参数LIMIT用于限制返回成员的数量范围。
|
||||
* 可选参数offset表示从符合条件的第offset个成员开始返回,同时返回count个成员。
|
||||
* 可选参数WITHSCORES的含义参照ZRANGE中该选项的说明。*最后需要说明的是参数中min和max的规则可参照命令ZCOUNT。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param max
|
||||
* @param min
|
||||
* @return java.util.Set<java.lang.String>
|
||||
* @throws
|
||||
*/
|
||||
public Set<Object> zrangebyscore(String key, double min, double max) {
|
||||
if (null==key){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForZSet().rangeByScore(key, min, max);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 该命令除了排序方式是基于从高到低的分数排序之外,其它功能和参数含义均与ZRANGEBYSCORE相同。
|
||||
* 需要注意的是该命令中的min和max参数的顺序和ZRANGEBYSCORE命令是相反的。
|
||||
* @Author: 小霍
|
||||
* @UpdateUser:
|
||||
* @Version: 0.0.1
|
||||
* @param key
|
||||
* @param max
|
||||
* @param min
|
||||
* @return java.util.Set<java.lang.String>
|
||||
* @throws
|
||||
*/
|
||||
public Set<Object> zrevrangeByScore(String key, double min, double max) {
|
||||
if (null==key){
|
||||
return null;
|
||||
}
|
||||
return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max);
|
||||
}
|
||||
}
|
@ -29,7 +29,7 @@ public interface SysUserService {
|
||||
* 保存用户
|
||||
* @param sysUserQo
|
||||
*/
|
||||
void addUser(SysUserQo sysUserQo);
|
||||
String addUser(SysUserQo sysUserQo);
|
||||
|
||||
/**
|
||||
* 获取系统用户实体根据用户名
|
||||
|
@ -119,8 +119,9 @@ public class FirePointServiceImpl implements FirePointService {
|
||||
return firePointDao.selectTodayFirePoint(addTime,countyCode.substring(0,2));
|
||||
}else if(countyCode.substring(4).equals("00") && !countyCode.substring(2).equals("0000")){
|
||||
return firePointDao.selectTodayFirePoint(addTime,countyCode.substring(0,4));
|
||||
}else {
|
||||
return firePointDao.selectTodayFirePointOne(addTime,countyCode);
|
||||
}
|
||||
return firePointDao.selectTodayFirePointOne(addTime,countyCode);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -11,6 +11,7 @@ import com.xkrs.model.entity.SysRoleEntity;
|
||||
import com.xkrs.model.entity.SysUserEntity;
|
||||
import com.xkrs.model.qo.SysUserQo;
|
||||
import com.xkrs.model.vo.SysUserVo;
|
||||
import com.xkrs.service.RedisService;
|
||||
import com.xkrs.service.SysUserService;
|
||||
import com.xkrs.utils.DateTimeUtil;
|
||||
import org.slf4j.Logger;
|
||||
@ -49,6 +50,9 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
@Resource
|
||||
private RelRoleAuthorityDao relRoleAuthorityDao;
|
||||
|
||||
@Resource
|
||||
private RedisService redisService;
|
||||
|
||||
/**
|
||||
* 检查用户名是否存在
|
||||
* @param userName
|
||||
@ -66,10 +70,15 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
*/
|
||||
@Transactional(rollbackFor=Exception.class)
|
||||
@Override
|
||||
public void addUser(SysUserQo sysUserQo) {
|
||||
public String addUser(SysUserQo sysUserQo) {
|
||||
// 获取区域信息
|
||||
Locale locale = LocaleContextHolder.getLocale();
|
||||
String salt = KeyGenerators.string().generateKey();
|
||||
SysUserEntity sysUserEntity = new SysUserEntity();
|
||||
sysUserEntity.setUserName(sysUserQo.getUserName());
|
||||
if(!redisService.get(sysUserQo.getUserName()).equals(sysUserQo.getVerificationCode())){
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG,"验证码错误,请重新输入!",locale);
|
||||
}
|
||||
sysUserEntity.setReallyName(sysUserQo.getReallyName());
|
||||
sysUserEntity.setSalt(salt);
|
||||
sysUserEntity.setPassword(encry256(sysUserQo.getPassword() + salt));
|
||||
@ -96,6 +105,7 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
relUserRoleDao.save(relUserRoleEntity);
|
||||
|
||||
relRoleAuthorityDao.save(relRoleAuthorityEntity);
|
||||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"注册成功!",locale);
|
||||
|
||||
//sysRoleDao.insertRelUserRole(sysUserEntity.getUserName(),"role_general_user");
|
||||
}
|
||||
|
68
src/main/java/com/xkrs/utils/AliyunSmsUtils.java
Normal file
68
src/main/java/com/xkrs/utils/AliyunSmsUtils.java
Normal file
@ -0,0 +1,68 @@
|
||||
package com.xkrs.utils;
|
||||
|
||||
import com.aliyuncs.DefaultAcsClient;
|
||||
import com.aliyuncs.IAcsClient;
|
||||
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
|
||||
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
|
||||
import com.aliyuncs.exceptions.ClientException;
|
||||
import com.aliyuncs.profile.DefaultProfile;
|
||||
import com.aliyuncs.profile.IClientProfile;
|
||||
import com.xkrs.service.impl.FirePointServiceImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Xin
|
||||
*/
|
||||
public class AliyunSmsUtils {
|
||||
public static Logger log = LoggerFactory.getLogger(AliyunSmsUtils.class);
|
||||
/**
|
||||
* 产品名称:云通信短信API产品,开发者无需替换
|
||||
*/
|
||||
static final String product = "Dysmsapi";
|
||||
/**
|
||||
* 产品域名,开发者无需替换
|
||||
*/
|
||||
static final String domain = "dysmsapi.aliyuncs.com";
|
||||
/**
|
||||
* TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
|
||||
* TODO 修改成自己的
|
||||
*/
|
||||
static final String accessKeyId = "LTAI5tMSjLfu8Xu2w6WeguFF";
|
||||
/**
|
||||
* TODO 修改成自己的
|
||||
*/
|
||||
static final String accessKeySecret = "hvqM5zpi72hvX4VXM71wq6AE0XYtDI";
|
||||
|
||||
public static SendSmsResponse sendSms(String telephone, String code) throws ClientException {
|
||||
//可自助调整超时时间
|
||||
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
|
||||
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
|
||||
//初始化acsClient,暂不支持region化
|
||||
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
|
||||
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
|
||||
IAcsClient acsClient = new DefaultAcsClient(profile);
|
||||
//组装请求对象-具体描述见控制台-文档部分内容
|
||||
SendSmsRequest request = new SendSmsRequest();
|
||||
//必填:待发送手机号
|
||||
request.setPhoneNumbers(telephone);
|
||||
//必填:短信签名-可在短信控制台中找到
|
||||
request.setSignName("青岛星科瑞升");
|
||||
//必填:短信模板-可在短信控制台中找到
|
||||
request.setTemplateCode("SMS_219570204");
|
||||
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
|
||||
//如果为发送验证码 无需修改
|
||||
request.setTemplateParam("{\"code\":\"" + code + "\"}");
|
||||
//选填-上行短信扩展码(无特殊需求用户请忽略此字段)
|
||||
//request.setSmsUpExtendCode("90997");
|
||||
//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
|
||||
//hint 此处可能会抛出异常,注意catch
|
||||
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
|
||||
if(sendSmsResponse.getCode()!= null && sendSmsResponse.getCode().equals("OK")){
|
||||
log.info("------------>短信发送成功!");
|
||||
}else {
|
||||
log.info("------------>短信发送失败!");
|
||||
}
|
||||
return sendSmsResponse;
|
||||
}
|
||||
}
|
@ -59,4 +59,13 @@ public class RandomUtil {
|
||||
return uuid;
|
||||
// return UUID.randomUUID().toString().replace("-", "").toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机生成六位数
|
||||
* @return
|
||||
*/
|
||||
public static int returnCode() {
|
||||
Random rand = new Random();
|
||||
return rand.nextInt(899999) + 100000;
|
||||
}
|
||||
}
|
||||
|
@ -33,6 +33,19 @@ spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
|
||||
#spring.jpa.properties.hibernate.dialect = org.hibernate.spatial.dialect.postgis.PostgisDialect
|
||||
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
|
||||
|
||||
## Redis配置
|
||||
spring.cache.type = redis
|
||||
spring.redis.database = 8
|
||||
spring.redis.host = localhost
|
||||
spring.redis.port = 6379
|
||||
#spring.redis.password=sdust2020
|
||||
spring.redis.timeout = 10000
|
||||
spring.redis.lettuce.pool.max-active = 100
|
||||
spring.redis.lettuce.pool.max-wait = 10000
|
||||
spring.redis.lettuce.pool.max-idle = 100
|
||||
spring.redis.lettuce.pool.min-idle = 1
|
||||
spring.redis.lettuce.shutdown-timeout = 0
|
||||
|
||||
## Devtools配置
|
||||
spring.devtools.livereload.enabled = true
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user