This commit is contained in:
sxu 2025-01-27 13:53:28 +08:00
parent a30ea14fc3
commit 3e69095724
4 changed files with 344 additions and 308 deletions

View File

@ -1,269 +1,269 @@
package net.xnzn.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* spring redis 工具类
*
**/
@Component
public class RedisCache
{
@Autowired
public RedisTemplate redisTemplate;
//添加分布式锁
public <T> Boolean setNxCacheObject(final String key, final T value,long lt,TimeUnit tu)
{
return redisTemplate.opsForValue().setIfAbsent(key,value,lt,tu);
}
/**
* 缓存基本的对象IntegerString实体类等
*
* @param key 缓存的键值
* @param value 缓存的值
*/
public <T> void setCacheObject(final String key, final T value)
{
redisTemplate.opsForValue().set(key, value);
}
/**
* 缓存基本的对象IntegerString实体类等
*
* @param key 缓存的键值
* @param value 缓存的值
* @param timeout 时间
* @param timeUnit 时间颗粒度
*/
public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
{
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
/**
* 设置有效时间
*
* @param key Redis键
* @param timeout 超时时间
* @return true=设置成功false=设置失败
*/
public boolean expire(final String key, final long timeout)
{
return expire(key, timeout, TimeUnit.SECONDS);
}
/**
* 设置有效时间
*
* @param key Redis键
* @param timeout 超时时间
* @param unit 时间单位
* @return true=设置成功false=设置失败
*/
public boolean expire(final String key, final long timeout, final TimeUnit unit)
{
return redisTemplate.expire(key, timeout, unit);
}
/**
* 获取有效时间
*
* @param key Redis键
* @return 有效时间
*/
public long getExpire(final String key)
{
return redisTemplate.getExpire(key);
}
/**
* 判断 key是否存在
*
* @param key
* @return true 存在 false不存在
*/
public Boolean hasKey(String key)
{
return redisTemplate.hasKey(key);
}
/**
* 获得缓存的基本对象
*
* @param key 缓存键值
* @return 缓存键值对应的数据
*/
public <T> T getCacheObject(final String key)
{
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
}
/**
* 删除单个对象
*
* @param key
*/
public boolean deleteObject(final String key)
{
return redisTemplate.delete(key);
}
/**
* 删除集合对象
*
* @param collection 多个对象
* @return
*/
public boolean deleteObject(final Collection collection)
{
return redisTemplate.delete(collection) > 0;
}
/**
* 缓存List数据
*
* @param key 缓存的键值
* @param dataList 待缓存的List数据
* @return 缓存的对象
*/
public <T> long setCacheList(final String key, final List<T> dataList)
{
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count;
}
/**
* 获得缓存的list对象
*
* @param key 缓存的键值
* @return 缓存键值对应的数据
*/
public <T> List<T> getCacheList(final String key)
{
return redisTemplate.opsForList().range(key, 0, -1);
}
/**
* 缓存Set
*
* @param key 缓存键值
* @param dataSet 缓存的数据
* @return 缓存数据的对象
*/
public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet)
{
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
Iterator<T> it = dataSet.iterator();
while (it.hasNext())
{
setOperation.add(it.next());
}
return setOperation;
}
/**
* 获得缓存的set
*
* @param key
* @return
*/
public <T> Set<T> getCacheSet(final String key)
{
return redisTemplate.opsForSet().members(key);
}
/**
* 缓存Map
*
* @param key
* @param dataMap
*/
public <T> void setCacheMap(final String key, final Map<String, T> dataMap)
{
if (dataMap != null) {
redisTemplate.opsForHash().putAll(key, dataMap);
}
}
/**
* 获得缓存的Map
*
* @param key
* @return
*/
public <T> Map<String, T> getCacheMap(final String key)
{
return redisTemplate.opsForHash().entries(key);
}
/**
* 往Hash中存入数据
*
* @param key Redis键
* @param hKey Hash键
* @param value
*/
public <T> void setCacheMapValue(final String key, final String hKey, final T value)
{
redisTemplate.opsForHash().put(key, hKey, value);
}
/**
* 获取Hash中的数据
*
* @param key Redis键
* @param hKey Hash键
* @return Hash中的对象
*/
public <T> T getCacheMapValue(final String key, final String hKey)
{
HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
return opsForHash.get(key, hKey);
}
/**
* 获取多个Hash中的数据
*
* @param key Redis键
* @param hKeys Hash键集合
* @return Hash对象集合
*/
public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
{
return redisTemplate.opsForHash().multiGet(key, hKeys);
}
/**
* 删除Hash中的某条数据
*
* @param key Redis键
* @param hKey Hash键
* @return 是否成功
*/
public boolean deleteCacheMapValue(final String key, final String hKey)
{
return redisTemplate.opsForHash().delete(key, hKey) > 0;
}
/**
* 获得缓存的基本对象列表
*
* @param pattern 字符串前缀
* @return 对象列表
*/
public Collection<String> keys(final String pattern)
{
return redisTemplate.keys(pattern);
}
}
//package net.xnzn.utils;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.BoundSetOperations;
//import org.springframework.data.redis.core.HashOperations;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.core.ValueOperations;
//import org.springframework.stereotype.Component;
//
//import java.util.*;
//import java.util.concurrent.TimeUnit;
//
///**
// * spring redis 工具类
// *
// **/
//@Component
//public class RedisCache
//{
// @Autowired
// public RedisTemplate redisTemplate;
//
// //添加分布式锁
// public <T> Boolean setNxCacheObject(final String key, final T value,long lt,TimeUnit tu)
// {
// return redisTemplate.opsForValue().setIfAbsent(key,value,lt,tu);
// }
//
// /**
// * 缓存基本的对象IntegerString实体类等
// *
// * @param key 缓存的键值
// * @param value 缓存的值
// */
// public <T> void setCacheObject(final String key, final T value)
// {
// redisTemplate.opsForValue().set(key, value);
// }
//
// /**
// * 缓存基本的对象IntegerString实体类等
// *
// * @param key 缓存的键值
// * @param value 缓存的值
// * @param timeout 时间
// * @param timeUnit 时间颗粒度
// */
// public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
// {
// redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
// }
//
// /**
// * 设置有效时间
// *
// * @param key Redis键
// * @param timeout 超时时间
// * @return true=设置成功false=设置失败
// */
// public boolean expire(final String key, final long timeout)
// {
// return expire(key, timeout, TimeUnit.SECONDS);
// }
//
// /**
// * 设置有效时间
// *
// * @param key Redis键
// * @param timeout 超时时间
// * @param unit 时间单位
// * @return true=设置成功false=设置失败
// */
// public boolean expire(final String key, final long timeout, final TimeUnit unit)
// {
// return redisTemplate.expire(key, timeout, unit);
// }
//
// /**
// * 获取有效时间
// *
// * @param key Redis键
// * @return 有效时间
// */
// public long getExpire(final String key)
// {
// return redisTemplate.getExpire(key);
// }
//
// /**
// * 判断 key是否存在
// *
// * @param key
// * @return true 存在 false不存在
// */
// public Boolean hasKey(String key)
// {
// return redisTemplate.hasKey(key);
// }
//
// /**
// * 获得缓存的基本对象
// *
// * @param key 缓存键值
// * @return 缓存键值对应的数据
// */
// public <T> T getCacheObject(final String key)
// {
// ValueOperations<String, T> operation = redisTemplate.opsForValue();
// return operation.get(key);
// }
//
// /**
// * 删除单个对象
// *
// * @param key
// */
// public boolean deleteObject(final String key)
// {
// return redisTemplate.delete(key);
// }
//
// /**
// * 删除集合对象
// *
// * @param collection 多个对象
// * @return
// */
// public boolean deleteObject(final Collection collection)
// {
// return redisTemplate.delete(collection) > 0;
// }
//
// /**
// * 缓存List数据
// *
// * @param key 缓存的键值
// * @param dataList 待缓存的List数据
// * @return 缓存的对象
// */
// public <T> long setCacheList(final String key, final List<T> dataList)
// {
// Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
// return count == null ? 0 : count;
// }
//
// /**
// * 获得缓存的list对象
// *
// * @param key 缓存的键值
// * @return 缓存键值对应的数据
// */
// public <T> List<T> getCacheList(final String key)
// {
// return redisTemplate.opsForList().range(key, 0, -1);
// }
//
// /**
// * 缓存Set
// *
// * @param key 缓存键值
// * @param dataSet 缓存的数据
// * @return 缓存数据的对象
// */
// public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet)
// {
// BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
// Iterator<T> it = dataSet.iterator();
// while (it.hasNext())
// {
// setOperation.add(it.next());
// }
// return setOperation;
// }
//
// /**
// * 获得缓存的set
// *
// * @param key
// * @return
// */
// public <T> Set<T> getCacheSet(final String key)
// {
// return redisTemplate.opsForSet().members(key);
// }
//
// /**
// * 缓存Map
// *
// * @param key
// * @param dataMap
// */
// public <T> void setCacheMap(final String key, final Map<String, T> dataMap)
// {
// if (dataMap != null) {
// redisTemplate.opsForHash().putAll(key, dataMap);
// }
// }
//
// /**
// * 获得缓存的Map
// *
// * @param key
// * @return
// */
// public <T> Map<String, T> getCacheMap(final String key)
// {
// return redisTemplate.opsForHash().entries(key);
// }
//
// /**
// * 往Hash中存入数据
// *
// * @param key Redis键
// * @param hKey Hash键
// * @param value
// */
// public <T> void setCacheMapValue(final String key, final String hKey, final T value)
// {
// redisTemplate.opsForHash().put(key, hKey, value);
// }
//
// /**
// * 获取Hash中的数据
// *
// * @param key Redis键
// * @param hKey Hash键
// * @return Hash中的对象
// */
// public <T> T getCacheMapValue(final String key, final String hKey)
// {
// HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
// return opsForHash.get(key, hKey);
// }
//
// /**
// * 获取多个Hash中的数据
// *
// * @param key Redis键
// * @param hKeys Hash键集合
// * @return Hash对象集合
// */
// public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
// {
// return redisTemplate.opsForHash().multiGet(key, hKeys);
// }
//
// /**
// * 删除Hash中的某条数据
// *
// * @param key Redis键
// * @param hKey Hash键
// * @return 是否成功
// */
// public boolean deleteCacheMapValue(final String key, final String hKey)
// {
// return redisTemplate.opsForHash().delete(key, hKey) > 0;
// }
//
// /**
// * 获得缓存的基本对象列表
// *
// * @param pattern 字符串前缀
// * @return 对象列表
// */
// public Collection<String> keys(final String pattern)
// {
// return redisTemplate.keys(pattern);
// }
//}

View File

@ -9,7 +9,7 @@ import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface CustInfoMapper extends BaseMapper<CustInfo> {
public interface CustInfoMapper { //extends BaseMapper<CustInfo> {
// @Select({"select cust_id, cust_name, cust_num, cust_photo_url, mobile ,pwd from cust_info ${ew.customSqlSegment}"})
// CustInfoAppIdLoginVO selectLoginInfo(@Param("ew") Wrapper<CustInfo> wrapper);

View File

@ -7,7 +7,14 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bonus.common.core.constant.SecurityConstants;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.JwtUtils;
import com.bonus.common.core.utils.ip.IpUtils;
import com.bonus.common.core.utils.uuid.IdUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.system.api.domain.SysUser;
import com.bonus.system.api.model.LoginUser;
import net.xnzn.core.common.encrypt.SM4EncryptUtils;
import net.xnzn.core.common.enums.DelFlagEnum;
import net.xnzn.core.common.utils.AesEncryptUtil;
@ -31,6 +38,12 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
public class CustInfoBusinessImpl implements CustInfoBusiness {
private static final Logger log = LoggerFactory.getLogger(CustInfoBusinessImpl.class);
@ -41,10 +54,10 @@ public class CustInfoBusinessImpl implements CustInfoBusiness {
@Autowired
private SmsCodeApi smsCodeApi;
@Autowired
@Autowired(required=true)
private CustInfoMapper custInfoMapper;
@Autowired
@Autowired(required=true)
private CustCasualMapper custCasualMapper;
public CustInfoAppIdLoginVO customLoginWithAppId(CustInfoAppIdLoginDTO content, Integer sourceType) {
@ -128,4 +141,39 @@ public class CustInfoBusinessImpl implements CustInfoBusiness {
return result;
}
/**
* 创建令牌
*/
// public Map<String, Object> createToken(LoginUser loginUser) {
// // 检查并删除已有的token
// delExistingToken(loginUser.getSysUser().getUserId());
// String token = IdUtils.fastUUID();
// Long userId = loginUser.getSysUser().getUserId();
// String userName = loginUser.getSysUser().getUserName();
// loginUser.setToken(token);
// loginUser.setUserid(userId);
// loginUser.setUsername(userName);
// loginUser.setIpaddr(IpUtils.getIpAddr());
// refreshToken(loginUser);
// // Jwt存储信息
// Map<String, Object> claimsMap = new HashMap<String, Object>(16);
// claimsMap.put(SecurityConstants.USER_KEY, token);
// claimsMap.put(SecurityConstants.DETAILS_USER_ID, userId);
// claimsMap.put(SecurityConstants.DETAILS_USERNAME, userName);
// String accessToken = JwtUtils.createToken(claimsMap);
// Map<String, Object> rspMap = new HashMap<String, Object>(16);
// rspMap.put("access_token", accessToken);
// rspMap.put("expires_in", EXPIRETIME);
// rspMap.put("isLogin", isLogin(String.valueOf(userId)));
// long tokenTime = getTokenTime();
// //对token进行存储
// redisService.setCacheObject(LOGIN_USER_KEY + userId, token, tokenTime, TimeUnit.MINUTES);
// SysUser sysUser = new SysUser();
// sysUser.setUserId(loginUser.getSysUser().getUserId());
// sysUser.setLoginDate(new Date());
// AjaxResult edit = remoteUserService.systemUpdateUser(sysUser, SecurityConstants.INNER);
// return rspMap;
// }
}

View File

@ -3,9 +3,9 @@ package net.xnzn.core.notice.v2.api;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.redis.service.RedisService;
import net.xnzn.core.common.constant.LeMqConstant;
import net.xnzn.core.merchant.dto.SmsCodeVerifyDTO;
import net.xnzn.utils.RedisCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -22,28 +22,28 @@ public class SmsCodeApi {
private static final Logger log = LoggerFactory.getLogger(SmsCodeApi.class);
@Autowired
private RedisCache redisCache;
public void sendSmsCodePost(String telephoneNumber) {
this.sendSmsCodePost(telephoneNumber, "code_");
}
public void sendSmsCodePost(String telephoneNumber, String cacheKey) {
String limitKey = "limit_" + telephoneNumber;
LocalDateTime now = LocalDateTime.now();
LocalDateTime endOfDay = now.with(LocalTime.MAX);
long expirTime = Math.max(1L, Duration.between(now, endOfDay).getSeconds());
String lastSendTimeKey = "last_send_time_" + telephoneNumber;
String lastSendTime = redisCache.getCacheObject(lastSendTimeKey);
if (lastSendTime != null) {
long lastSendTimestamp = Long.parseLong(lastSendTime);
long currentTimestamp = System.currentTimeMillis();
long timeElapsed = currentTimestamp - lastSendTimestamp;
if (timeElapsed < 60000L) {
throw new ServiceException("验证码重复");
}
}
private RedisService redisService;
// public void sendSmsCodePost(String telephoneNumber) {
// this.sendSmsCodePost(telephoneNumber, "code_");
// }
//
// public void sendSmsCodePost(String telephoneNumber, String cacheKey) {
// String limitKey = "limit_" + telephoneNumber;
// LocalDateTime now = LocalDateTime.now();
// LocalDateTime endOfDay = now.with(LocalTime.MAX);
// long expirTime = Math.max(1L, Duration.between(now, endOfDay).getSeconds());
// String lastSendTimeKey = "last_send_time_" + telephoneNumber;
// String lastSendTime = redisCache.getCacheObject(lastSendTimeKey);
// if (lastSendTime != null) {
// long lastSendTimestamp = Long.parseLong(lastSendTime);
// long currentTimestamp = System.currentTimeMillis();
// long timeElapsed = currentTimestamp - lastSendTimestamp;
// if (timeElapsed < 60000L) {
// throw new ServiceException("验证码重复");
// }
// }
//
// Integer times = RedisUtil.incr(limitKey, expirTime);
// if (times > 5) {
// throw new ServiceException("验证码时间超过限制");
@ -60,19 +60,7 @@ public class SmsCodeApi {
// //redisCache.setNxCacheObject(lastSendTimeKey, String.valueOf(System.currentTimeMillis())); //TODO
// redisCache.setNxCacheObject(lastSendTimeKey, String.valueOf(System.currentTimeMillis()), 300L, TimeUnit.SECONDS);
// }
int code = (int)((Math.random() * 9.0 + 1.0) * 100000.0);
String codeString = "" + code;
Map<String, String> maps = new HashMap();
maps.put("telephoneNumber", telephoneNumber);
maps.put("sendCode", codeString);
log.info("验证码发送code : {}", codeString);
//MqUtil.send(JSON.toJSONString(maps), LeMqConstant.Topic.NOTICE_VERIFICATION_CODE);
String key = cacheKey + telephoneNumber;
redisCache.setNxCacheObject(key, codeString, 300L, TimeUnit.SECONDS);
//redisCache.setNxCacheObject(lastSendTimeKey, String.valueOf(System.currentTimeMillis())); //TODO
redisCache.setNxCacheObject(lastSendTimeKey, String.valueOf(System.currentTimeMillis()), 300L, TimeUnit.SECONDS);
}
// }
public boolean verifySmsCode(SmsCodeVerifyDTO smsCodeVerifyDTO) {
return this.verifySmsCode(smsCodeVerifyDTO, "code_");
@ -80,7 +68,7 @@ public class SmsCodeApi {
public boolean verifySmsCode(SmsCodeVerifyDTO smsCodeVerifyDTO, String cacheKey) {
String key = cacheKey + smsCodeVerifyDTO.getTelephoneNumber();
String code = redisCache.getCacheObject(key);
String code = redisService.getCacheObject(key);
log.info("redis缓存验证码code : {}", code);
return ObjectUtil.isNotEmpty(code) && ObjectUtil.equal(code, smsCodeVerifyDTO.getCode());
}