From 49c3e34b2388d31612ed586aeea3190ab444b18b Mon Sep 17 00:00:00 2001 From: Loki <654612@qq.com> Date: Mon, 29 Aug 2022 19:13:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=A7=9F=E6=88=B7=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E5=88=87=E9=9D=A2=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/context/TenantContextHolder.java | 12 + .../service/auth/AdminAuthServiceImpl.java | 9 +- yudao-ui-admin | 2 +- .../auth/rest/LetterAppAuthController.java | 21 +- .../app/modules/services/AppAuthService.java | 2 +- .../services/CreatShareProductService.java | 2 +- .../rest/controller/WxMaUserController.java | 8 +- .../co/yixiang/aspect/TenantInitAspect.java | 55 ++- .../listener/RedisKeyInitialization.java | 97 ---- .../impl/YxStoreCombinationServiceImpl.java | 2 +- .../impl/YxStoreSeckillServiceImpl.java | 2 +- .../customer/rest/QrCodeController.java | 2 +- .../java/co/yixiang/utils/RedisUtils.java | 449 ++---------------- .../java/co/yixiang/utils/ShopKeyUtils.java | 55 +-- 14 files changed, 156 insertions(+), 562 deletions(-) delete mode 100644 zsw-bxg/src/main/java/co/yixiang/listener/RedisKeyInitialization.java diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/context/TenantContextHolder.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/context/TenantContextHolder.java index a42acc41..0fd2afb0 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/context/TenantContextHolder.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/context/TenantContextHolder.java @@ -2,6 +2,8 @@ package cn.iocoder.yudao.framework.tenant.core.context; import com.alibaba.ttl.TransmittableThreadLocal; +import java.util.function.Supplier; + /** * 多租户上下文 Holder * @@ -58,6 +60,16 @@ public class TenantContextHolder { return Boolean.TRUE.equals(IGNORE.get()); } + public static T apply(Long tenantId, Supplier func){ + Long oldTenant = TenantContextHolder.getTenantId(); + try { + TenantContextHolder.setTenantId(tenantId); + return func.get(); + }finally { + TenantContextHolder.setTenantId(oldTenant); + } + } + public static void clear() { TENANT_ID.remove(); IGNORE.remove(); diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java index ae244697..44961a23 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.system.service.auth; +import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; @@ -13,6 +14,7 @@ import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.AuthSocialBi import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.AuthSocialLogin2ReqVO; import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.AuthSocialLoginReqVO; import cn.iocoder.yudao.module.system.convert.auth.AuthConvert; +import cn.iocoder.yudao.module.system.dal.dataobject.CpUser.CpUserDO; import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum; import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum; @@ -113,8 +115,11 @@ public class AdminAuthServiceImpl implements AdminAuthService { // 使用账号密码,进行登录 LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword()); - //将cpUserId存入 key为 系统用户id value为 cpUserId - redisTemplate.opsForValue().set("CpUserId::" + loginUser.getId(), cpUserService.getByUserId(loginUser.getUsername()).getId().toString()); + CpUserDO wxUser = cpUserService.getByUserId(loginUser.getUsername()); + if (ObjectUtil.isNotEmpty(wxUser)){ + //将cpUserId存入 key为 系统用户id value为 cpUserId + redisTemplate.opsForValue().set("CpUserId::" + loginUser.getId(), wxUser.getId().toString()); + } // 缓存登陆用户到 Redis 中,返回 sessionId 编号 return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_USERNAME, userIp, userAgent); diff --git a/yudao-ui-admin b/yudao-ui-admin index beb44f8a..e2285feb 160000 --- a/yudao-ui-admin +++ b/yudao-ui-admin @@ -1 +1 @@ -Subproject commit beb44f8aa54a4e20a48985d771a0353641df4787 +Subproject commit e2285febb83a51499ae0ae28fd89b8f301913c51 diff --git a/zsw-bxg/src/main/java/co/yixiang/app/modules/auth/rest/LetterAppAuthController.java b/zsw-bxg/src/main/java/co/yixiang/app/modules/auth/rest/LetterAppAuthController.java index f2751bcd..355dbcaf 100644 --- a/zsw-bxg/src/main/java/co/yixiang/app/modules/auth/rest/LetterAppAuthController.java +++ b/zsw-bxg/src/main/java/co/yixiang/app/modules/auth/rest/LetterAppAuthController.java @@ -106,7 +106,7 @@ public class LetterAppAuthController { @ApiOperation(value = "小程序获取用户信息", notes = "小程序获取用户信息") public ApiResult loginAuth(@Validated @RequestBody LoginParam loginParam) { Long uid = LocalUser.getUidByToken(); - String sessionKey = redisUtil.get(ShopConstants.ZSW_MINI_SESSION_KET + uid).toString(); + String sessionKey = redisUtil.getY(ShopConstants.ZSW_MINI_SESSION_KET + uid); YxUser yxUser = authService.loginAuth(loginParam, uid, sessionKey); return ApiResult.ok(yxUser).setMsg("获取成功"); @@ -181,11 +181,10 @@ public class LetterAppAuthController { @ApiOperation("H5验证码登录授权") @PostMapping(value = "/login/mobile") public ApiResult> loginVerify(@Validated @RequestBody LoginVerifyParam loginVerifyParam, HttpServletRequest request) { - Object codeObj = redisUtil.get("code_" + loginVerifyParam.getAccount()); - if(codeObj == null){ + String code = redisUtil.getY("code_" + loginVerifyParam.getAccount()); + if(code == null){ throw new YshopException("请先获取验证码"); } - String code = codeObj.toString(); if (!StrUtil.equals(code, loginVerifyParam.getCaptcha())) { throw new YshopException("验证码错误"); } @@ -221,11 +220,10 @@ public class LetterAppAuthController { @ApiOperation("修改密码") @PostMapping(value = "/register/reset") public ApiResult updatePassword(@Validated @RequestBody UpdatePasswordParam updatePasswordParam, HttpServletRequest request) { - Object codeObj = redisUtil.get("code_" + updatePasswordParam.getAccount()); - if(codeObj == null){ + String code = redisUtil.getY("code_" + updatePasswordParam.getAccount()); + if(code == null){ throw new YshopException("请先获取验证码"); } - String code = codeObj.toString(); if (!StrUtil.equals(code, updatePasswordParam.getCaptcha())) { throw new YshopException("验证码错误"); } @@ -252,11 +250,10 @@ public class LetterAppAuthController { @PostMapping("/register") @ApiOperation(value = "H5/APP注册新用户", notes = "H5/APP注册新用户") public ApiResult register(@Validated @RequestBody RegParam param) { - Object codeObj = redisUtil.get("code_" + param.getAccount()); - if(codeObj == null){ + String code = redisUtil.getY("code_" + param.getAccount()); + if(code == null){ return ApiResult.fail("请先获取验证码"); } - String code = codeObj.toString(); if (!StrUtil.equals(code, param.getCaptcha())) { return ApiResult.fail("验证码错误"); } @@ -283,8 +280,8 @@ public class LetterAppAuthController { return ApiResult.fail("账号不存在"); } String codeKey = "code_" + param.getPhone(); - if (ObjectUtil.isNotNull(redisUtil.get(codeKey))) { - return ApiResult.fail("10分钟内有效:" + redisUtil.get(codeKey).toString()); + if (ObjectUtil.isNotNull(redisUtil.getY(codeKey))) { + return ApiResult.fail("10分钟内有效:" + redisUtil.getY(codeKey)); } String code = RandomUtil.randomNumbers(ShopConstants.ZSW_SMS_SIZE); diff --git a/zsw-bxg/src/main/java/co/yixiang/app/modules/services/AppAuthService.java b/zsw-bxg/src/main/java/co/yixiang/app/modules/services/AppAuthService.java index 575eb386..0f9668d0 100644 --- a/zsw-bxg/src/main/java/co/yixiang/app/modules/services/AppAuthService.java +++ b/zsw-bxg/src/main/java/co/yixiang/app/modules/services/AppAuthService.java @@ -328,7 +328,7 @@ public class AppAuthService { * @param request / */ public void save(YxUser yxUser, String token, HttpServletRequest request) { - String job = "yshop开发工程师"; + String job = "开发工程师"; String ip = StringUtils.getIp(request); String browser = StringUtils.getBrowser(request); String address = StringUtils.getCityInfo(ip); diff --git a/zsw-bxg/src/main/java/co/yixiang/app/modules/services/CreatShareProductService.java b/zsw-bxg/src/main/java/co/yixiang/app/modules/services/CreatShareProductService.java index c44014a9..4b1d29c2 100644 --- a/zsw-bxg/src/main/java/co/yixiang/app/modules/services/CreatShareProductService.java +++ b/zsw-bxg/src/main/java/co/yixiang/app/modules/services/CreatShareProductService.java @@ -87,7 +87,7 @@ public class CreatShareProductService { public YxStoreOrderQueryVo handleQrcode(YxStoreOrderQueryVo storeOrder,String path){ //门店 if(OrderInfoEnum.SHIPPIING_TYPE_2.getValue().equals(storeOrder.getShippingType())){ - String mapKey = redisUtils.get(SystemConfigConstants.TENGXUN_MAP_KEY).toString(); + String mapKey = redisUtils.getY(SystemConfigConstants.TENGXUN_MAP_KEY); if(StrUtil.isBlank(mapKey)) { throw new YshopException("请配置腾讯地图key"); } diff --git a/zsw-bxg/src/main/java/co/yixiang/app/modules/wechat/rest/controller/WxMaUserController.java b/zsw-bxg/src/main/java/co/yixiang/app/modules/wechat/rest/controller/WxMaUserController.java index 8f82d3a9..93770922 100644 --- a/zsw-bxg/src/main/java/co/yixiang/app/modules/wechat/rest/controller/WxMaUserController.java +++ b/zsw-bxg/src/main/java/co/yixiang/app/modules/wechat/rest/controller/WxMaUserController.java @@ -54,12 +54,10 @@ public class WxMaUserController { @PostMapping("/binding") @ApiOperation(value = "公众号绑定手机号", notes = "公众号绑定手机号") public ApiResult verify(@Validated @RequestBody BindPhoneParam param) { - Object codeObj = redisUtils.get("code_" + param.getPhone()); - if(codeObj == null){ + String code = redisUtils.getY("code_" + param.getPhone()); + if(code == null){ return ApiResult.fail("请先获取验证码"); } - String code = codeObj.toString(); - if (!StrUtil.equals(code, param.getCaptcha())) { return ApiResult.fail("验证码错误"); } @@ -94,7 +92,7 @@ public class WxMaUserController { WxMaService wxMaService = WxMaConfiguration.getWxMaService(); String phone = ""; try { - String sessionKey = redisUtils.get(ShopConstants.ZSW_MINI_SESSION_KET + user.getUid()).toString(); + String sessionKey = redisUtils.getY(ShopConstants.ZSW_MINI_SESSION_KET + user.getUid()); WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService() .getPhoneNoInfo(sessionKey, param.getEncryptedData(), param.getIv()); phone = phoneNoInfo.getPhoneNumber(); diff --git a/zsw-bxg/src/main/java/co/yixiang/aspect/TenantInitAspect.java b/zsw-bxg/src/main/java/co/yixiang/aspect/TenantInitAspect.java index 7e8bd52a..f8144c28 100644 --- a/zsw-bxg/src/main/java/co/yixiang/aspect/TenantInitAspect.java +++ b/zsw-bxg/src/main/java/co/yixiang/aspect/TenantInitAspect.java @@ -1,13 +1,28 @@ package co.yixiang.aspect; +import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO; +import co.yixiang.modules.canvas.domain.StoreCanvas; +import co.yixiang.modules.canvas.service.StoreCanvasService; +import co.yixiang.modules.shop.domain.YxSystemConfig; +import co.yixiang.modules.shop.domain.YxSystemGroupData; +import co.yixiang.modules.shop.service.YxSystemConfigService; +import co.yixiang.modules.shop.service.YxSystemGroupDataService; +import co.yixiang.tools.domain.QiniuConfig; +import co.yixiang.tools.service.QiniuConfigService; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; +import java.util.Date; +import java.util.List; + /** * 如果有新租户需要创建 ,则需要调用这个方法执行一些操作,初始化一些数据 * 如动态图设计。前端可以不用,后端这个功能依然保持,七牛初始化配置。 @@ -16,14 +31,46 @@ import org.springframework.stereotype.Component; @Aspect @Component @Slf4j +@Order public class TenantInitAspect { + @Autowired + private StoreCanvasService storeCanvasService; + @Autowired + private YxSystemGroupDataService systemGroupDataService; + @Autowired + private QiniuConfigService qiniuConfigService; + @Autowired + private YxSystemConfigService systemConfigService; - @Pointcut("execution(* cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper.insert(..))") + @Pointcut("execution(* cn.iocoder.yudao.module.system.service.tenant.TenantService.createTenant(..))") public void tenantInit(){} - @AfterReturning(value = "tenantInit()",returning = "tenant") - public void afterTenantInit(JoinPoint point, TenantDO tenant){ - log.info("新租户被加入,准备初始化...{}",tenant); + @AfterReturning(value = "tenantInit()",returning = "tenantId") + public void afterTenantInit(JoinPoint point, Long tenantId){ + log.info("新租户被加入,准备初始化...{}",tenantId); + // 动画 + StoreCanvas temp = TenantContextHolder.apply(0L,()-> storeCanvasService.getById(1)); + temp.setCanvasId(null); + temp.setCreateTime(new Date()); + TenantContextHolder.apply(tenantId,()->storeCanvasService.save(temp)); + //数据设置 + List list = TenantContextHolder.apply(0L, () -> systemGroupDataService.list()); + list.forEach(yxSystemGroupData -> { + yxSystemGroupData.setCreateTime(new Date()); + yxSystemGroupData.setId(null); + }); + TenantContextHolder.apply(tenantId,()->systemGroupDataService.saveBatch(list)); + //七牛设置 + QiniuConfig qiniuConfig = TenantContextHolder.apply(0L, () -> qiniuConfigService.getById(1)); + qiniuConfig.setId(null); + TenantContextHolder.apply(tenantId,()->qiniuConfigService.save(qiniuConfig)); + //系统设置 + List configs = TenantContextHolder.apply(0L,()->systemConfigService.list()); + configs.forEach(config->{ + config.setId(null); + }); + TenantContextHolder.apply(tenantId,()->systemConfigService.saveBatch(configs)); + } } diff --git a/zsw-bxg/src/main/java/co/yixiang/listener/RedisKeyInitialization.java b/zsw-bxg/src/main/java/co/yixiang/listener/RedisKeyInitialization.java deleted file mode 100644 index b87ec6d1..00000000 --- a/zsw-bxg/src/main/java/co/yixiang/listener/RedisKeyInitialization.java +++ /dev/null @@ -1,97 +0,0 @@ -package co.yixiang.listener; - -import cn.hutool.core.util.ObjectUtil; -import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; -import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO; -import cn.iocoder.yudao.module.system.service.tenant.TenantService; -import cn.iocoder.yudao.module.system.service.tenant.TenantServiceImpl; -import co.yixiang.modules.shop.domain.YxSystemConfig; -import co.yixiang.modules.shop.domain.YxSystemGroupData; -import co.yixiang.modules.shop.service.YxSystemConfigService; -import co.yixiang.modules.shop.service.YxSystemGroupDataService; -import co.yixiang.modules.shop.service.dto.YxSystemGroupDataQueryCriteria; -import co.yixiang.utils.RedisUtils; -import com.google.common.collect.Lists; -import jodd.util.MapEntry; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.CommandLineRunner; -import org.springframework.stereotype.Component; - -import javax.annotation.Resource; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -@Slf4j -@Component -public class RedisKeyInitialization implements CommandLineRunner { - - @Resource - private TenantServiceImpl tenantService; - - @Autowired - private RedisUtils redisUtils; - - @Autowired - private YxSystemConfigService systemConfigService; - - @Autowired - private YxSystemGroupDataService systemGroupDataService; - - @Override - public void run(String... args) throws Exception { - - Map tenantCache = tenantService.getTenantCache(); - TenantContextHolder.setTenantId(0L); - - // 配置模版 - List configTemplateList = systemConfigService.queryAll(null); - // 商城数据 - List groupDataTemplateList = systemGroupDataService.queryAll(null); - - for ( Map.Entry entry : tenantCache.entrySet()){ - TenantDO tenant = entry.getValue(); - TenantContextHolder.setTenantId(tenant.getId()); - - // 处理系统设置 - List configs = systemConfigService.queryAll(null); - List configsName = configs.stream() - .map(YxSystemConfig::getMenuName) - .collect(Collectors.toList()); - - // 准备配置模版 将已经有的剔除 没有的准备插入数据库 - List rsT = ObjectUtil.clone(configTemplateList); - if (ObjectUtil.isNotEmpty(configsName)) { - rsT.removeIf(r -> configsName.contains(r.getMenuName())); - } - // 清除id - rsT.forEach(r-> r.setId(null)); - systemConfigService.saveBatch(rsT); - - // 处理系统配置 - List groupDatas = systemGroupDataService.queryAll( - new YxSystemGroupDataQueryCriteria().setStatus(1)); - List groupDatasNames = groupDatas.stream() - .map(YxSystemGroupData::getGroupName) - .collect(Collectors.toList()); - - List rsD = ObjectUtil.clone(groupDataTemplateList); - if (ObjectUtil.isNotEmpty(groupDatasNames)) { - rsD.removeIf(r -> groupDatasNames.contains(r.getGroupName())); - } - rsD.forEach(r-> r.setId(null)); - systemGroupDataService.saveBatch(rsD); - - // 重新读取 - configs = systemConfigService.queryAll(null); - - for (YxSystemConfig config: configs) { - redisUtils.set(config.getMenuName(),config.getValue()); - } - - log.info("租户:{} 配置和缓存处理完成。。。",tenant.getName()); - } - } -} diff --git a/zsw-bxg/src/main/java/co/yixiang/modules/activity/service/impl/YxStoreCombinationServiceImpl.java b/zsw-bxg/src/main/java/co/yixiang/modules/activity/service/impl/YxStoreCombinationServiceImpl.java index f2531f16..29ba6b4e 100644 --- a/zsw-bxg/src/main/java/co/yixiang/modules/activity/service/impl/YxStoreCombinationServiceImpl.java +++ b/zsw-bxg/src/main/java/co/yixiang/modules/activity/service/impl/YxStoreCombinationServiceImpl.java @@ -140,7 +140,7 @@ public class YxStoreCombinationServiceImpl extends BaseServiceImpl returnMap = yxStoreProductAttrService.getProductAttrDetail(storeSeckill.getProductId()); //获取运费模板名称 - String storeFreePostage = redisUtils.get("store_free_postage").toString(); + String storeFreePostage = redisUtils.getY("store_free_postage"); String tempName = ""; if(StrUtil.isBlank(storeFreePostage) || !NumberUtil.isNumber(storeFreePostage) diff --git a/zsw-bxg/src/main/java/co/yixiang/modules/customer/rest/QrCodeController.java b/zsw-bxg/src/main/java/co/yixiang/modules/customer/rest/QrCodeController.java index 7b1110d1..a260b41d 100644 --- a/zsw-bxg/src/main/java/co/yixiang/modules/customer/rest/QrCodeController.java +++ b/zsw-bxg/src/main/java/co/yixiang/modules/customer/rest/QrCodeController.java @@ -103,7 +103,7 @@ public class QrCodeController { @ResponseBody @GetMapping("/getOpenId") public ResponseEntity userInfo(HttpServletRequest request, @RequestParam("key") String key) { - String openId = redisUtils.get("qrCode:" + key).toString(); + String openId = redisUtils.getY("qrCode:" + key); if (openId != null) { String[] str = openId.split(":"); JSONObject json = new JSONObject(); diff --git a/zsw-bxg/src/main/java/co/yixiang/utils/RedisUtils.java b/zsw-bxg/src/main/java/co/yixiang/utils/RedisUtils.java index 9db7b98f..c09152f8 100644 --- a/zsw-bxg/src/main/java/co/yixiang/utils/RedisUtils.java +++ b/zsw-bxg/src/main/java/co/yixiang/utils/RedisUtils.java @@ -8,6 +8,9 @@ */ package co.yixiang.utils; +import cn.hutool.core.util.ObjectUtil; +import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; +import co.yixiang.modules.shop.service.YxSystemConfigService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisConnectionFactory; @@ -26,10 +29,14 @@ import java.util.concurrent.TimeUnit; @SuppressWarnings({"unchecked","all"}) public class RedisUtils { - + @Autowired + private YxSystemConfigService systemConfigService; @Resource private RedisTemplate redisTemplate; + private String buildCacheKey(String key){ + return TenantContextHolder.getTenantId() + ":" + key; + } // =============================commonold============================ @@ -39,6 +46,7 @@ public class RedisUtils { * @param time 时间(秒) */ public boolean expire(String key, long time) { + key = buildCacheKey(key); try { if (time > 0) { redisTemplate.expire(key, time, TimeUnit.SECONDS); @@ -56,6 +64,7 @@ public class RedisUtils { * @return 时间(秒) 返回0代表为永久有效 */ public long getExpire(String key) { + key = buildCacheKey(key); return redisTemplate.getExpire(key, TimeUnit.SECONDS); } @@ -65,6 +74,7 @@ public class RedisUtils { * @return / */ public List scan(String pattern) { + pattern = buildCacheKey(pattern); ScanOptions options = ScanOptions.scanOptions().match(pattern).build(); RedisConnectionFactory factory = redisTemplate.getConnectionFactory(); RedisConnection rc = Objects.requireNonNull(factory).getConnection(); @@ -89,6 +99,7 @@ public class RedisUtils { * @return / */ public List findKeysForPage(String patternKey, int page, int size) { + patternKey = buildCacheKey(patternKey); ScanOptions options = ScanOptions.scanOptions().match(patternKey).build(); RedisConnectionFactory factory = redisTemplate.getConnectionFactory(); RedisConnection rc = Objects.requireNonNull(factory).getConnection(); @@ -124,6 +135,7 @@ public class RedisUtils { * @return true 存在 false不存在 */ public boolean hasKey(String key) { + key = buildCacheKey(key); try { return redisTemplate.hasKey(key); } catch (Exception e) { @@ -137,6 +149,9 @@ public class RedisUtils { * @param key 可以传一个值 或多个 */ public void del(String... key) { + Arrays.stream(key).forEach(s -> { + buildCacheKey(s); + }); if (key != null && key.length > 0) { if (key.length == 1) { redisTemplate.delete(key[0]); @@ -154,22 +169,33 @@ public class RedisUtils { * @return 值 */ public Object get(String key) { - return key == null ? null : redisTemplate.opsForValue().get(key); + if (ObjectUtil.isEmpty(key)){ + return null; + } + String tarKey = buildCacheKey(key); + Object rs = redisTemplate.opsForValue().get(tarKey); + if (ObjectUtil.isEmpty(rs)){ + rs = systemConfigService.getData(key); + redisTemplate.opsForValue().set(tarKey,rs); + } + return rs; } public String getY(String key){ - return key == null || !redisTemplate.hasKey(key) ? "" : redisTemplate.opsForValue().get(key).toString(); + key = buildCacheKey(key); + if (ObjectUtil.isEmpty(key)){ + return null; + } + String tarKey = buildCacheKey(key); + Object rs = redisTemplate.opsForValue().get(tarKey); + if (ObjectUtil.isEmpty(rs)){ + rs = systemConfigService.getData(key); + redisTemplate.opsForValue().set(tarKey,rs); + } + return rs.toString(); } - /** - * 批量获取 - * @param keys - * @return - */ - public List multiGet(List keys) { - Object obj = redisTemplate.opsForValue().multiGet(keys); - return null; - } + /** * 普通缓存放入 @@ -178,8 +204,9 @@ public class RedisUtils { * @return true成功 false失败 */ public boolean set(String key, Object value) { + key = buildCacheKey(key); try { - redisTemplate.opsForValue().set(key, value); + redisTemplate.opsForValue().set(key,value); return true; } catch (Exception e) { e.printStackTrace(); @@ -195,6 +222,7 @@ public class RedisUtils { * @return true成功 false 失败 */ public boolean set(String key, Object value, long time) { + key = buildCacheKey(key); try { if (time > 0) { redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); @@ -217,6 +245,7 @@ public class RedisUtils { * @return true成功 false 失败 */ public boolean set(String key, String value, long time, TimeUnit timeUnit) { + key = buildCacheKey(key); try { if (time > 0) { redisTemplate.opsForValue().set(key, value, time, timeUnit); @@ -229,398 +258,4 @@ public class RedisUtils { return false; } } - - // ================================Map================================= - - /** - * HashGet - * @param key 键 不能为null - * @param item 项 不能为null - * @return 值 - */ - public Object hget(String key, String item) { - return redisTemplate.opsForHash().get(key, item); - } - - /** - * 获取hashKey对应的所有键值 - * @param key 键 - * @return 对应的多个键值 - */ - public Map hmget(String key) { - return redisTemplate.opsForHash().entries(key); - - } - - /** - * HashSet - * @param key 键 - * @param map 对应多个键值 - * @return true 成功 false 失败 - */ - public boolean hmset(String key, Map map) { - try { - redisTemplate.opsForHash().putAll(key, map); - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - /** - * HashSet 并设置时间 - * @param key 键 - * @param map 对应多个键值 - * @param time 时间(秒) - * @return true成功 false失败 - */ - public boolean hmset(String key, Map map, long time) { - try { - redisTemplate.opsForHash().putAll(key, map); - if (time > 0) { - expire(key, time); - } - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - /** - * 向一张hash表中放入数据,如果不存在将创建 - * - * @param key 键 - * @param item 项 - * @param value 值 - * @return true 成功 false失败 - */ - public boolean hset(String key, String item, Object value) { - try { - redisTemplate.opsForHash().put(key, item, value); - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - /** - * 向一张hash表中放入数据,如果不存在将创建 - * - * @param key 键 - * @param item 项 - * @param value 值 - * @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间 - * @return true 成功 false失败 - */ - public boolean hset(String key, String item, Object value, long time) { - try { - redisTemplate.opsForHash().put(key, item, value); - if (time > 0) { - expire(key, time); - } - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - /** - * 删除hash表中的值 - * - * @param key 键 不能为null - * @param item 项 可以使多个 不能为null - */ - public void hdel(String key, Object... item) { - redisTemplate.opsForHash().delete(key, item); - } - - /** - * 判断hash表中是否有该项的值 - * - * @param key 键 不能为null - * @param item 项 不能为null - * @return true 存在 false不存在 - */ - public boolean hHasKey(String key, String item) { - return redisTemplate.opsForHash().hasKey(key, item); - } - - /** - * hash递增 如果不存在,就会创建一个 并把新增后的值返回 - * - * @param key 键 - * @param item 项 - * @param by 要增加几(大于0) - * @return - */ - public double hincr(String key, String item, double by) { - return redisTemplate.opsForHash().increment(key, item, by); - } - - /** - * hash递减 - * - * @param key 键 - * @param item 项 - * @param by 要减少记(小于0) - * @return - */ - public double hdecr(String key, String item, double by) { - return redisTemplate.opsForHash().increment(key, item, -by); - } - - // ============================set============================= - - /** - * 根据key获取Set中的所有值 - * - * @param key 键 - * @return - */ - public Set sGet(String key) { - try { - return redisTemplate.opsForSet().members(key); - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - - /** - * 根据value从一个set中查询,是否存在 - * - * @param key 键 - * @param value 值 - * @return true 存在 false不存在 - */ - public boolean sHasKey(String key, Object value) { - try { - return redisTemplate.opsForSet().isMember(key, value); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - /** - * 将数据放入set缓存 - * - * @param key 键 - * @param values 值 可以是多个 - * @return 成功个数 - */ - public long sSet(String key, Object... values) { - try { - return redisTemplate.opsForSet().add(key, values); - } catch (Exception e) { - e.printStackTrace(); - return 0; - } - } - - /** - * 将set数据放入缓存 - * @param key 键 - * @param time 时间(秒) - * @param values 值 可以是多个 - * @return 成功个数 - */ - public long sSetAndTime(String key, long time, Object... values) { - try { - Long count = redisTemplate.opsForSet().add(key, values); - if (time > 0) { - expire(key, time); - } - return count; - } catch (Exception e) { - e.printStackTrace(); - return 0; - } - } - - /** - * 获取set缓存的长度 - * @param key 键 - * @return - */ - public long sGetSetSize(String key) { - try { - return redisTemplate.opsForSet().size(key); - } catch (Exception e) { - e.printStackTrace(); - return 0; - } - } - - /** - * 移除值为value的 - * @param key 键 - * @param values 值 可以是多个 - * @return 移除的个数 - */ - public long setRemove(String key, Object... values) { - try { - Long count = redisTemplate.opsForSet().remove(key, values); - return count; - } catch (Exception e) { - e.printStackTrace(); - return 0; - } - } - - // ===============================list================================= - - /** - * 获取list缓存的内容 - * @param key 键 - * @param start 开始 - * @param end 结束 0 到 -1代表所有值 - * @return - */ - public List lGet(String key, long start, long end) { - try { - return redisTemplate.opsForList().range(key, start, end); - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - - /** - * 获取list缓存的长度 - * @param key 键 - * @return - */ - public long lGetListSize(String key) { - try { - return redisTemplate.opsForList().size(key); - } catch (Exception e) { - e.printStackTrace(); - return 0; - } - } - - /** - * 通过索引 获取list中的值 - * @param key 键 - * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推 - * @return - */ - public Object lGetIndex(String key, long index) { - try { - return redisTemplate.opsForList().index(key, index); - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - - /** - * 将list放入缓存 - * @param key 键 - * @param value 值 - * @return - */ - public boolean lSet(String key, Object value) { - try { - redisTemplate.opsForList().rightPush(key, value); - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - /** - * 将list放入缓存 - * @param key 键 - * @param value 值 - * @param time 时间(秒) - * @return - */ - public boolean lSet(String key, Object value, long time) { - try { - redisTemplate.opsForList().rightPush(key, value); - if (time > 0) { - expire(key, time); - } - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - /** - * 将list放入缓存 - * @param key 键 - * @param value 值 - * @return - */ - public boolean lSet(String key, List value) { - try { - redisTemplate.opsForList().rightPushAll(key, value); - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - /** - * 将list放入缓存 - * @param key 键 - * @param value 值 - * @param time 时间(秒) - * @return - */ - public boolean lSet(String key, List value, long time) { - try { - redisTemplate.opsForList().rightPushAll(key, value); - if (time > 0) { - expire(key, time); - } - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - /** - * 根据索引修改list中的某条数据 - * @param key 键 - * @param index 索引 - * @param value 值 - * @return / - */ - public boolean lUpdateIndex(String key, long index, Object value) { - try { - redisTemplate.opsForList().set(key, index, value); - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - /** - * 移除N个值为value - * @param key 键 - * @param count 移除多少个 - * @param value 值 - * @return 移除的个数 - */ - public long lRemove(String key, long count, Object value) { - try { - return redisTemplate.opsForList().remove(key, count, value); - } catch (Exception e) { - e.printStackTrace(); - return 0; - } - } } diff --git a/zsw-bxg/src/main/java/co/yixiang/utils/ShopKeyUtils.java b/zsw-bxg/src/main/java/co/yixiang/utils/ShopKeyUtils.java index 9e1f764f..c6b99a52 100644 --- a/zsw-bxg/src/main/java/co/yixiang/utils/ShopKeyUtils.java +++ b/zsw-bxg/src/main/java/co/yixiang/utils/ShopKeyUtils.java @@ -13,8 +13,7 @@ public class ShopKeyUtils { *扩展值,默认为空, 把这个值追加到所有key值上 */ private static String getExtendValue(){ - String extendValue= ""; - return extendValue; + return ""; } //*********************************begin yx_system_config 通用值 ***************************************************** @@ -23,22 +22,19 @@ public class ShopKeyUtils { * api_url */ public static String getApiUrl(){ - String apiUrl= SystemConfigConstants.API_URL; - return apiUrl; + return SystemConfigConstants.API_URL; } /** * site_url */ public static String getSiteUrl(){ - String siteUrl= SystemConfigConstants.SITE_URL; - return siteUrl; + return SystemConfigConstants.SITE_URL; } /** * 腾讯mapkey tengxun_map_key */ public static String getTengXunMapKey(){ - String tengxunMapKey= SystemConfigConstants.TENGXUN_MAP_KEY; - return tengxunMapKey; + return SystemConfigConstants.TENGXUN_MAP_KEY; } //*********************************begin yx_system_config 业务字段 ***************************************************** @@ -89,13 +85,6 @@ public class ShopKeyUtils { String wechatEncodingAESKey= SystemConfigConstants.WECHAT_ENCODINGAESKEY; return wechatEncodingAESKey+getExtendValue(); } - /** - * 微信支付service - */ - public static String getYshopWeiXinPayService(){ - String yshopWeiXinPayService= TenantContextHolder.getTenantId().toString() + ":"+ShopConstants.ZSW_WEIXIN_PAY_SERVICE; - return yshopWeiXinPayService+getExtendValue(); - } /** * 商户号 */ @@ -117,20 +106,6 @@ public class ShopKeyUtils { String wxPayKeyPath= SystemConfigConstants.WXPAY_KEYPATH; return wxPayKeyPath+getExtendValue(); } - /** - * 微信支付小程序service - */ - public static String getYshopWeiXinMiniPayService(){ - String yshopWeiXinMiniPayService= TenantContextHolder.getTenantId().toString() + ":" + ShopConstants.ZSW_WEIXIN_MINI_PAY_SERVICE; - return yshopWeiXinMiniPayService+getExtendValue(); - } - /** - * 微信支付app service - */ - public static String getYshopWeiXinAppPayService(){ - String yshopWeiXinAppPayService= TenantContextHolder.getTenantId().toString() + ":" + ShopConstants.ZSW_WEIXIN_APP_PAY_SERVICE; - return yshopWeiXinAppPayService+getExtendValue(); - } /** * 微信小程序id */ @@ -178,4 +153,26 @@ public class ShopKeyUtils { return yshopWeiXinMaSevice+getExtendValue(); } + /** + * 微信支付小程序service + */ + public static String getYshopWeiXinMiniPayService(){ + String yshopWeiXinMiniPayService= TenantContextHolder.getTenantId().toString() + ":" + ShopConstants.ZSW_WEIXIN_MINI_PAY_SERVICE; + return yshopWeiXinMiniPayService+getExtendValue(); + } + /** + * 微信支付app service + */ + public static String getYshopWeiXinAppPayService(){ + String yshopWeiXinAppPayService= TenantContextHolder.getTenantId().toString() + ":" + ShopConstants.ZSW_WEIXIN_APP_PAY_SERVICE; + return yshopWeiXinAppPayService+getExtendValue(); + } + /** + * 微信支付service + */ + public static String getYshopWeiXinPayService(){ + String yshopWeiXinPayService= TenantContextHolder.getTenantId().toString() + ":"+ShopConstants.ZSW_WEIXIN_PAY_SERVICE; + return yshopWeiXinPayService+getExtendValue(); + } + }