From edbab6dbee78504fbda91c669900a52c45a1ff1e Mon Sep 17 00:00:00 2001 From: wen <1455474577@qq.com> Date: Thu, 30 Dec 2021 14:00:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9Auth=E6=9C=89=E6=95=88?= =?UTF-8?q?=E6=9C=9F=E6=96=B9=E4=BE=BF=E6=B5=8B=E8=AF=95=EF=BC=8C=E5=B0=86?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=9C=AA=E8=AF=BB=E7=BA=A2=E7=82=B9=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E8=87=B3Redis=E4=B8=AD=EF=BC=8C=E6=96=B9=E4=BE=BF?= =?UTF-8?q?=E5=88=AB=E4=BA=BA=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/security/service/TokenService.java | 2 +- .../com/xhpc/gateway/filter/AuthFilter.java | 65 ++++++----- .../invoice/mapper/XhpcInvoiceMapper.java | 2 +- .../service/impl/XhpcInvoiceServiceImpl.java | 107 +++++++++++++++++- 4 files changed, 142 insertions(+), 34 deletions(-) diff --git a/ruoyi-common/ruoyi-common-security/src/main/java/com/xhpc/common/security/service/TokenService.java b/ruoyi-common/ruoyi-common-security/src/main/java/com/xhpc/common/security/service/TokenService.java index f13c4ebb..237db200 100644 --- a/ruoyi-common/ruoyi-common-security/src/main/java/com/xhpc/common/security/service/TokenService.java +++ b/ruoyi-common/ruoyi-common-security/src/main/java/com/xhpc/common/security/service/TokenService.java @@ -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; diff --git a/ruoyi-gateway/src/main/java/com/xhpc/gateway/filter/AuthFilter.java b/ruoyi-gateway/src/main/java/com/xhpc/gateway/filter/AuthFilter.java index 84508f74..69394c89 100644 --- a/ruoyi-gateway/src/main/java/com/xhpc/gateway/filter/AuthFilter.java +++ b/ruoyi-gateway/src/main/java/com/xhpc/gateway/filter/AuthFilter.java @@ -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 sops; + @Resource(name = "stringRedisTemplate") + private ValueOperations sops; @Autowired private RedisService redisService; @@ -48,34 +53,34 @@ public class AuthFilter implements GlobalFilter, Ordered { public Mono 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); } diff --git a/xhpc-modules/xhpc-invoice/src/main/java/com/xhpc/invoice/mapper/XhpcInvoiceMapper.java b/xhpc-modules/xhpc-invoice/src/main/java/com/xhpc/invoice/mapper/XhpcInvoiceMapper.java index fbd11b3f..4974e0b7 100644 --- a/xhpc-modules/xhpc-invoice/src/main/java/com/xhpc/invoice/mapper/XhpcInvoiceMapper.java +++ b/xhpc-modules/xhpc-invoice/src/main/java/com/xhpc/invoice/mapper/XhpcInvoiceMapper.java @@ -75,7 +75,7 @@ public interface XhpcInvoiceMapper { BigDecimal allNotInvoicedMoney(); /** - * 更新指定发票状态,让其成为已开发票状态 + * 更新指定发票状态,让其成为已开发票状态,并设置用户未阅读标签 * * @param requestData 更新数据的来源 * @return Long 返回更新受影响的条数 diff --git a/xhpc-modules/xhpc-invoice/src/main/java/com/xhpc/invoice/service/impl/XhpcInvoiceServiceImpl.java b/xhpc-modules/xhpc-invoice/src/main/java/com/xhpc/invoice/service/impl/XhpcInvoiceServiceImpl.java index 8ad50225..52e0cd11 100644 --- a/xhpc-modules/xhpc-invoice/src/main/java/com/xhpc/invoice/service/impl/XhpcInvoiceServiceImpl.java +++ b/xhpc-modules/xhpc-invoice/src/main/java/com/xhpc/invoice/service/impl/XhpcInvoiceServiceImpl.java @@ -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 invoiceIds) { + + Integer integer = invoiceIds.get(0); + //获取要导出的发票记录 + XhpcInvoice xhpcInvoice = xhpcInvoiceMapper.selectByPrimaryKey(Long.valueOf(integer)); + List rows = CollUtil.newArrayList(xhpcInvoice); + //写出记录到excel表中 + ExcelWriter writer = ExcelUtil.getWriter("d:/writeTest.xlsx"); + writer.write(rows); + writer.close(); + //写入到Response中 + } + /** * 通过发票id查找对应的发票数据