修改Auth有效期方便测试,将用户未读红点添加至Redis中,方便别人调用

This commit is contained in:
wen 2021-12-30 14:00:55 +08:00
parent 02bc08b1e4
commit edbab6dbee
4 changed files with 142 additions and 34 deletions

View File

@ -27,7 +27,7 @@ public class TokenService {
@Autowired
private RedisService redisService;
private final static long EXPIRE_TIME = Constants.TOKEN_EXPIRE * 60;
private final static long EXPIRE_TIME = Constants.TOKEN_EXPIRE * 6000;
private final static String ACCESS_TOKEN = CacheConstants.LOGIN_TOKEN_KEY;

View File

@ -1,9 +1,11 @@
package com.xhpc.gateway.filter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhpc.common.core.constant.CacheConstants;
import com.xhpc.common.core.constant.Constants;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.utils.ServletUtils;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.gateway.config.properties.IgnoreWhiteProperties;
@ -14,6 +16,7 @@ import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
@ -22,6 +25,8 @@ import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import javax.annotation.Resource;
/**
* 网关鉴权
*
@ -38,8 +43,8 @@ public class AuthFilter implements GlobalFilter, Ordered {
@Autowired
private IgnoreWhiteProperties ignoreWhite;
// @Resource(name = "stringRedisTemplate")
// private ValueOperations<String, String> sops;
@Resource(name = "stringRedisTemplate")
private ValueOperations<String, String> sops;
@Autowired
private RedisService redisService;
@ -48,34 +53,34 @@ public class AuthFilter implements GlobalFilter, Ordered {
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String url = exchange.getRequest().getURI().getPath();
// // 跳过不需要验证的路径
// if (StringUtils.matches(url, ignoreWhite.getWhites())) {
// return chain.filter(exchange);
// }
// String token = getToken(exchange.getRequest());
// if (StringUtils.isBlank(token)) {
// return setUnauthorizedResponse(exchange, "令牌不能为空");
// }
// String userStr = sops.get(getTokenKey(token));
// if (StringUtils.isNull(userStr)) {
// userStr = redisService.getCacheObject(CacheConstants.LOGIN_TOKEN_KEY + token);
// if (StringUtils.isNull(userStr)) {
// return setUnauthorizedResponse(exchange, "登录状态已过期");
// }
// }
// JSONObject obj = JSONObject.parseObject(userStr);
// String userid = obj.getString("userid");
// String username = obj.getString("username");
// if (StringUtils.isBlank(userid) || StringUtils.isBlank(username)) {
// return setUnauthorizedResponse(exchange, "令牌验证失败");
// }
//
// // 设置过期时间
// redisService.expire(getTokenKey(token), EXPIRE_TIME);
// // 设置用户信息到请求
// ServerHttpRequest mutableReq = exchange.getRequest().mutate().header(CacheConstants.DETAILS_USER_ID, userid)
// .header(CacheConstants.DETAILS_USERNAME, ServletUtils.urlEncode(username)).build();
// ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build();
// 跳过不需要验证的路径
if (StringUtils.matches(url, ignoreWhite.getWhites())) {
return chain.filter(exchange);
}
String token = getToken(exchange.getRequest());
if (StringUtils.isBlank(token)) {
return setUnauthorizedResponse(exchange, "令牌不能为空");
}
String userStr = sops.get(getTokenKey(token));
if (StringUtils.isNull(userStr)) {
userStr = redisService.getCacheObject(CacheConstants.LOGIN_TOKEN_KEY + token);
if (StringUtils.isNull(userStr)) {
return setUnauthorizedResponse(exchange, "登录状态已过期");
}
}
JSONObject obj = JSONObject.parseObject(userStr);
String userid = obj.getString("userid");
String username = obj.getString("username");
if (StringUtils.isBlank(userid) || StringUtils.isBlank(username)) {
return setUnauthorizedResponse(exchange, "令牌验证失败");
}
// 设置过期时间
redisService.expire(getTokenKey(token), EXPIRE_TIME);
// 设置用户信息到请求
ServerHttpRequest mutableReq = exchange.getRequest().mutate().header(CacheConstants.DETAILS_USER_ID, userid)
.header(CacheConstants.DETAILS_USERNAME, ServletUtils.urlEncode(username)).build();
ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build();
return chain.filter(exchange);
}

View File

@ -75,7 +75,7 @@ public interface XhpcInvoiceMapper {
BigDecimal allNotInvoicedMoney();
/**
* 更新指定发票状态让其成为已开发票状态
* 更新指定发票状态让其成为已开发票状态并设置用户未阅读标签
*
* @param requestData 更新数据的来源
* @return Long 返回更新受影响的条数

View File

@ -1,10 +1,14 @@
package com.xhpc.invoice.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.xhpc.common.core.utils.DateUtils;
import com.xhpc.common.core.utils.bean.BeanUtils;
import com.xhpc.common.domain.XhpcChargingStation;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.invoice.constant.InvoiceMapHistoryOrderStatusConst;
import com.xhpc.invoice.domain.*;
import com.xhpc.invoice.mapper.*;
@ -48,6 +52,8 @@ public class XhpcInvoiceServiceImpl implements XhpcInvoiceService {
XhpcHistoryOrderMapper xhpcHistoryOrderMapper;
@Resource
Environment environment;
@Resource
RedisService redisService;
/**
* 通过requestData中的申请人申请人类型发票状态发票起始时间发票申请终止时间开票起始时间开票终点时间当前所在页数当前页所要显示几行来查询发票列表信息
@ -128,11 +134,13 @@ public class XhpcInvoiceServiceImpl implements XhpcInvoiceService {
electricInvoiceFile.delete();
}
}
//更新发票数据
//更新发票数据,并设置用户未阅读状态
Long successFlag = xhpcInvoiceMapper.invoiceToUser(requestData);
if (successFlag == 0) {
throw new RuntimeException("无法更新指定发票,传入的数据有问题");
}
//将该用户未阅读的数量放入到redis中
putNoReadInvoiceCountIntoRedis(requestData);
return Boolean.TRUE;
}
@ -146,6 +154,28 @@ public class XhpcInvoiceServiceImpl implements XhpcInvoiceService {
xhpcInvoiceMapper.failInvoiceToUser(requestData);
//解除锁定
xhpcInvoiceMapHistoryOrderMapper.unlockHistoryOrdersByInvoiceId(requestData.getInvoiceId());
//将该用户未阅读的数量放入到redis中
putNoReadInvoiceCountIntoRedis(requestData);
}
/**
* 将该用户没有阅读的发票的数量放入到Redis中保存供别人调用
*
* @author WH
* @date 2021/12/30 13:54
* @since version-1.0
*/
private void putNoReadInvoiceCountIntoRedis(InvoiceToUserRequest requestData) {
XhpcInvoice xhpcInvoice = xhpcInvoiceMapper.selectByPrimaryKey(requestData.getInvoiceId());
String redisKey = generateRedisKey(xhpcInvoice);
if (redisService.getCacheObject(redisKey) == null) {
redisService.setCacheObject(redisKey, 1);
} else {
Integer noReadCount = redisService.getCacheObject(redisKey);
noReadCount = noReadCount + 1;
redisService.setCacheObject(redisKey, noReadCount);
}
}
@Override
@ -261,11 +291,62 @@ public class XhpcInvoiceServiceImpl implements XhpcInvoiceService {
historyOrdersDTO.setTotalItems(historyOrdersList.size());
specificInvoicedResponse.setHistoryOrders(historyOrdersDTO);
//一旦调了详情接口就去掉该已开发票的未读状态
//一旦调了详情接口就去掉该已开发票的未读状态同时redis中的未读数量数据-1
xhpcInvoiceMapper.updateByInvoiceId(invoiceId);
reduceNoReadInvoiceCount(xhpcInvoice);
return specificInvoicedResponse;
}
/**
* 将用户未读的发票数量-1
*
* @param xhpcInvoice 用户查看的发票
* @author WH
* @date 2021/12/30 13:55
* @since version-1.0
*/
private void reduceNoReadInvoiceCount(XhpcInvoice xhpcInvoice) {
String redisKey = generateRedisKey(xhpcInvoice);
if (redisService.getCacheObject(redisKey).equals(1)) {
redisService.deleteObject(redisKey);
} else {
Integer noReadCount = redisService.getCacheObject(redisKey);
noReadCount = noReadCount - 1;
redisService.setCacheObject(redisKey, noReadCount);
}
}
/**
* 生成该用户在Redis中的唯一表示key
*
* @param xhpcInvoice 用户查看的发票
* @author WH
* @date 2021/12/30 13:52
* @since version-1.0
*/
private String generateRedisKey(XhpcInvoice xhpcInvoice) {
String redisKey = "global:invoice:";
switch (xhpcInvoice.getCreatorType()) {
case 0:
redisKey = redisKey + "C" + xhpcInvoice.getCreatorId();
break;
case 1:
redisKey = redisKey + "L" + xhpcInvoice.getCreatorId();
break;
case 2:
redisKey = redisKey + "ST" + xhpcInvoice.getCreatorId();
break;
case 3:
redisKey = redisKey + "BE" + xhpcInvoice.getCreatorId();
break;
default:
break;
}
return redisKey;
}
/**
* 根据前端传递过来的请求参数查询所需要的开发票历史记录
*
@ -396,6 +477,28 @@ public class XhpcInvoiceServiceImpl implements XhpcInvoiceService {
return xhpcInvoiceMapper.findNotReadCount(creatorId, creatorType);
}
/**
* 导出所有发票记录已开与未开到Excel中
*
* @param invoiceIds 发票id
* @author WH
* @date 2021/12/30 10:16
* @since version-1.0
*/
@Override
public void exportExcel(List<Integer> invoiceIds) {
Integer integer = invoiceIds.get(0);
//获取要导出的发票记录
XhpcInvoice xhpcInvoice = xhpcInvoiceMapper.selectByPrimaryKey(Long.valueOf(integer));
List<XhpcInvoice> rows = CollUtil.newArrayList(xhpcInvoice);
//写出记录到excel表中
ExcelWriter writer = ExcelUtil.getWriter("d:/writeTest.xlsx");
writer.write(rows);
writer.close();
//写入到Response中
}
/**
* 通过发票id查找对应的发票数据