diff --git a/kernel-d-wrapper/wrapper-api/src/main/java/cn/stylefeng/roses/kernel/wrapper/api/exception/enums/WrapperExceptionEnum.java b/kernel-d-wrapper/wrapper-api/src/main/java/cn/stylefeng/roses/kernel/wrapper/api/exception/enums/WrapperExceptionEnum.java index cdf811f19..11d4931c6 100644 --- a/kernel-d-wrapper/wrapper-api/src/main/java/cn/stylefeng/roses/kernel/wrapper/api/exception/enums/WrapperExceptionEnum.java +++ b/kernel-d-wrapper/wrapper-api/src/main/java/cn/stylefeng/roses/kernel/wrapper/api/exception/enums/WrapperExceptionEnum.java @@ -46,7 +46,7 @@ public enum WrapperExceptionEnum implements AbstractExceptionEnum { /** * 字段包装转化异常 */ - TRANSFER_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + WrapperConstants.WRAPPER_EXCEPTION_STEP_CODE + "02", "字段包装转化异常"); + TRANSFER_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + WrapperConstants.WRAPPER_EXCEPTION_STEP_CODE + "02", "字段包装转化异常,具体信息:{}"); /** * 错误编码 diff --git a/kernel-d-wrapper/wrapper-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/WrapperAop.java b/kernel-d-wrapper/wrapper-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/WrapperAop.java index 31831601e..4af62475b 100644 --- a/kernel-d-wrapper/wrapper-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/WrapperAop.java +++ b/kernel-d-wrapper/wrapper-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/WrapperAop.java @@ -213,7 +213,7 @@ public class WrapperAop { } } catch (Exception e) { log.error("原始对象包装过程,字段转化异常:{}", e.getMessage()); - throw new WrapperException(WrapperExceptionEnum.TRANSFER_ERROR); + throw new WrapperException(WrapperExceptionEnum.TRANSFER_ERROR, e.getMessage()); } return originMap; diff --git a/kernel-s-customer/customer-api/src/main/java/cn/stylefeng/roses/kernel/customer/api/CustomerApi.java b/kernel-s-customer/customer-api/src/main/java/cn/stylefeng/roses/kernel/customer/api/CustomerApi.java index a1c238504..6535fef82 100644 --- a/kernel-s-customer/customer-api/src/main/java/cn/stylefeng/roses/kernel/customer/api/CustomerApi.java +++ b/kernel-s-customer/customer-api/src/main/java/cn/stylefeng/roses/kernel/customer/api/CustomerApi.java @@ -24,6 +24,8 @@ */ package cn.stylefeng.roses.kernel.customer.api; +import cn.stylefeng.roses.kernel.customer.api.pojo.CustomerInfo; + /** * C端用户api * @@ -32,5 +34,12 @@ package cn.stylefeng.roses.kernel.customer.api; */ public interface CustomerApi { + /** + * 获取C端用户的详细信息 + * + * @author fengshuonan + * @date 2021/6/8 21:25 + */ + CustomerInfo getCustomerInfoById(Long customerId); } diff --git a/kernel-s-customer/customer-api/src/main/java/cn/stylefeng/roses/kernel/customer/api/constants/CustomerConstants.java b/kernel-s-customer/customer-api/src/main/java/cn/stylefeng/roses/kernel/customer/api/constants/CustomerConstants.java index 87ed304ec..55befa757 100644 --- a/kernel-s-customer/customer-api/src/main/java/cn/stylefeng/roses/kernel/customer/api/constants/CustomerConstants.java +++ b/kernel-s-customer/customer-api/src/main/java/cn/stylefeng/roses/kernel/customer/api/constants/CustomerConstants.java @@ -42,4 +42,9 @@ public interface CustomerConstants { */ String CUSTOMER_EXCEPTION_STEP_CODE = "31"; + /** + * C端用户的缓存 + */ + String CUSTOMER_CACHE_PREFIX = "customer:"; + } diff --git a/kernel-s-customer/customer-api/src/main/java/cn/stylefeng/roses/kernel/customer/api/expander/CustomerConfigExpander.java b/kernel-s-customer/customer-api/src/main/java/cn/stylefeng/roses/kernel/customer/api/expander/CustomerConfigExpander.java index 127f74e44..c987e15c4 100644 --- a/kernel-s-customer/customer-api/src/main/java/cn/stylefeng/roses/kernel/customer/api/expander/CustomerConfigExpander.java +++ b/kernel-s-customer/customer-api/src/main/java/cn/stylefeng/roses/kernel/customer/api/expander/CustomerConfigExpander.java @@ -51,4 +51,34 @@ public class CustomerConfigExpander { return ConfigContext.me().getSysConfigValueWithDefault("CUSTOMER_RESET_PWD_EMAIL_CONTENT", String.class, "您的验证码是【{}】,此验证码用于修改登录密码,请不要泄露给他人,如果不是您本人操作,请忽略此邮件。"); } + /** + * 存放用户头像的bucket的名称 + * + * @author fengshuonan + * @date 2021/6/7 15:42 + */ + public static String getCustomerBucket() { + return ConfigContext.me().getSysConfigValueWithDefault("CUSTOMER_FILE_BUCKET", String.class, "customer-bucket"); + } + + /** + * 存放用户头像的bucket的名称的过期时间 + * + * @author fengshuonan + * @date 2021/6/7 15:42 + */ + public static Long getCustomerBucketExpiredSeconds() { + return ConfigContext.me().getSysConfigValueWithDefault("CUSTOMER_FILE_BUCKET_EXPIRED_SECONDS", Long.class, 600L); + } + + /** + * 获取用户缓存的过期时间 + * + * @author fengshuonan + * @date 2021/6/7 15:42 + */ + public static Long getCustomerCacheExpiredSeconds() { + return ConfigContext.me().getSysConfigValueWithDefault("CUSTOMER_CACHE_EXPIRED_SECONDS", Long.class, 3600L); + } + } diff --git a/kernel-s-customer/customer-api/src/main/java/cn/stylefeng/roses/kernel/customer/api/pojo/CustomerInfo.java b/kernel-s-customer/customer-api/src/main/java/cn/stylefeng/roses/kernel/customer/api/pojo/CustomerInfo.java new file mode 100644 index 000000000..0a1bdc5f3 --- /dev/null +++ b/kernel-s-customer/customer-api/src/main/java/cn/stylefeng/roses/kernel/customer/api/pojo/CustomerInfo.java @@ -0,0 +1,54 @@ +package cn.stylefeng.roses.kernel.customer.api.pojo; + +import lombok.Data; + +/** + * 用户信息封装 + * + * @author fengshuonan + * @date 2021/6/8 21:23 + */ +@Data +public class CustomerInfo { + + /** + * 主键id + */ + private Long customerId; + + /** + * 账号 + */ + private String account; + + /** + * 昵称(显示名称) + */ + private String nickName; + + /** + * 邮箱 + */ + private String email; + + /** + * 手机 + */ + private String telephone; + + /** + * 用户头像(文件表id) + */ + private Long avatar; + + /** + * 用户头像的全部url + */ + private String avatarObjectUrl; + + /** + * 用户积分 + */ + private Integer score; + +} diff --git a/kernel-s-customer/customer-business/pom.xml b/kernel-s-customer/customer-business/pom.xml index b81196540..296b879eb 100644 --- a/kernel-s-customer/customer-business/pom.xml +++ b/kernel-s-customer/customer-business/pom.xml @@ -86,6 +86,34 @@ spring-boot-starter-web + + + + cn.stylefeng.roses + cache-api + ${roses.version} + + + cn.stylefeng.roses + cache-sdk-memory + ${roses.version} + true + + + cn.stylefeng.roses + cache-sdk-redis + ${roses.version} + true + + + + + + cn.stylefeng.roses + file-api + ${roses.version} + + diff --git a/kernel-s-customer/customer-business/src/main/java/cn/stylefeng/roses/kernel/customer/modular/cache/CustomerMemoryCache.java b/kernel-s-customer/customer-business/src/main/java/cn/stylefeng/roses/kernel/customer/modular/cache/CustomerMemoryCache.java new file mode 100644 index 000000000..ca1b726b1 --- /dev/null +++ b/kernel-s-customer/customer-business/src/main/java/cn/stylefeng/roses/kernel/customer/modular/cache/CustomerMemoryCache.java @@ -0,0 +1,49 @@ +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.customer.modular.cache; + +import cn.hutool.cache.impl.TimedCache; +import cn.stylefeng.roses.kernel.cache.memory.AbstractMemoryCacheOperator; +import cn.stylefeng.roses.kernel.customer.api.constants.CustomerConstants; +import cn.stylefeng.roses.kernel.customer.api.pojo.CustomerInfo; + +/** + * C端用户的缓存 + * + * @author fengshuonan + * @date 2021/2/28 10:23 + */ +public class CustomerMemoryCache extends AbstractMemoryCacheOperator { + + public CustomerMemoryCache(TimedCache timedCache) { + super(timedCache); + } + + @Override + public String getCommonKeyPrefix() { + return CustomerConstants.CUSTOMER_CACHE_PREFIX; + } + +} diff --git a/kernel-s-customer/customer-business/src/main/java/cn/stylefeng/roses/kernel/customer/modular/cache/CustomerRedisCache.java b/kernel-s-customer/customer-business/src/main/java/cn/stylefeng/roses/kernel/customer/modular/cache/CustomerRedisCache.java new file mode 100644 index 000000000..0046f902d --- /dev/null +++ b/kernel-s-customer/customer-business/src/main/java/cn/stylefeng/roses/kernel/customer/modular/cache/CustomerRedisCache.java @@ -0,0 +1,49 @@ +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.customer.modular.cache; + +import cn.stylefeng.roses.kernel.cache.redis.AbstractRedisCacheOperator; +import cn.stylefeng.roses.kernel.customer.api.constants.CustomerConstants; +import cn.stylefeng.roses.kernel.customer.api.pojo.CustomerInfo; +import org.springframework.data.redis.core.RedisTemplate; + +/** + * C端用户的缓存 + * + * @author fengshuonan + * @date 2021/2/28 10:23 + */ +public class CustomerRedisCache extends AbstractRedisCacheOperator { + + public CustomerRedisCache(RedisTemplate redisTemplate) { + super(redisTemplate); + } + + @Override + public String getCommonKeyPrefix() { + return CustomerConstants.CUSTOMER_CACHE_PREFIX; + } + +} diff --git a/kernel-s-customer/customer-business/src/main/java/cn/stylefeng/roses/kernel/customer/modular/service/CustomerService.java b/kernel-s-customer/customer-business/src/main/java/cn/stylefeng/roses/kernel/customer/modular/service/CustomerService.java index fe2ae0167..9aa3ab92a 100644 --- a/kernel-s-customer/customer-business/src/main/java/cn/stylefeng/roses/kernel/customer/modular/service/CustomerService.java +++ b/kernel-s-customer/customer-business/src/main/java/cn/stylefeng/roses/kernel/customer/modular/service/CustomerService.java @@ -2,6 +2,7 @@ package cn.stylefeng.roses.kernel.customer.modular.service; import cn.stylefeng.roses.kernel.auth.api.pojo.auth.LoginRequest; import cn.stylefeng.roses.kernel.auth.api.pojo.auth.LoginResponse; +import cn.stylefeng.roses.kernel.customer.api.CustomerApi; import cn.stylefeng.roses.kernel.customer.modular.entity.Customer; import cn.stylefeng.roses.kernel.customer.modular.request.CustomerRequest; import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult; @@ -15,7 +16,7 @@ import java.util.List; * @author fengshuonan * @date 2021/06/07 11:40 */ -public interface CustomerService extends IService { +public interface CustomerService extends IService, CustomerApi { /** * 注册用户 diff --git a/kernel-s-customer/customer-business/src/main/java/cn/stylefeng/roses/kernel/customer/modular/service/impl/CustomerServiceImpl.java b/kernel-s-customer/customer-business/src/main/java/cn/stylefeng/roses/kernel/customer/modular/service/impl/CustomerServiceImpl.java index dc47453ce..9aa54fc52 100644 --- a/kernel-s-customer/customer-business/src/main/java/cn/stylefeng/roses/kernel/customer/modular/service/impl/CustomerServiceImpl.java +++ b/kernel-s-customer/customer-business/src/main/java/cn/stylefeng/roses/kernel/customer/modular/service/impl/CustomerServiceImpl.java @@ -11,8 +11,11 @@ import cn.stylefeng.roses.kernel.auth.api.password.PasswordStoredEncryptApi; import cn.stylefeng.roses.kernel.auth.api.pojo.auth.LoginRequest; import cn.stylefeng.roses.kernel.auth.api.pojo.auth.LoginResponse; import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser; +import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; import cn.stylefeng.roses.kernel.customer.api.exception.CustomerException; import cn.stylefeng.roses.kernel.customer.api.exception.enums.CustomerExceptionEnum; +import cn.stylefeng.roses.kernel.customer.api.expander.CustomerConfigExpander; +import cn.stylefeng.roses.kernel.customer.api.pojo.CustomerInfo; import cn.stylefeng.roses.kernel.customer.modular.entity.Customer; import cn.stylefeng.roses.kernel.customer.modular.factory.CustomerFactory; import cn.stylefeng.roses.kernel.customer.modular.mapper.CustomerMapper; @@ -23,6 +26,7 @@ import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory; import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult; import cn.stylefeng.roses.kernel.email.api.MailSenderApi; import cn.stylefeng.roses.kernel.email.api.pojo.SendMailParam; +import cn.stylefeng.roses.kernel.file.api.FileOperatorApi; import cn.stylefeng.roses.kernel.jwt.api.context.JwtContext; import cn.stylefeng.roses.kernel.jwt.api.pojo.payload.DefaultJwtPayload; import cn.stylefeng.roses.kernel.log.api.LoginLogServiceApi; @@ -73,6 +77,12 @@ public class CustomerServiceImpl extends ServiceImpl i @Resource private LoginLogServiceApi loginLogServiceApi; + @Resource + private CacheOperatorApi customerInfoCacheOperatorApi; + + @Resource + private FileOperatorApi fileOperatorApi; + @Override @Transactional(rollbackFor = Exception.class) public void reg(CustomerRequest customerRequest) { @@ -250,6 +260,38 @@ public class CustomerServiceImpl extends ServiceImpl i return this.list(wrapper); } + @Override + public CustomerInfo getCustomerInfoById(Long customerId) { + + // 查询缓存中有没有用户信息 + String customerIdKey = String.valueOf(customerId); + CustomerInfo customerInfo = customerInfoCacheOperatorApi.get(customerIdKey); + if (customerInfo != null) { + return customerInfo; + } + + // 获取C端用户详情 + Customer customer = this.getById(customerId); + if (customer == null) { + throw new CustomerException(CustomerExceptionEnum.CANT_FIND_CUSTOMER, customerId); + } + + CustomerInfo result = new CustomerInfo(); + BeanUtil.copyProperties(customer, result); + + // 获取头像的url + String fileAuthUrl = fileOperatorApi.getFileAuthUrl( + CustomerConfigExpander.getCustomerBucket(), + customer.getAvatarObjectName(), + CustomerConfigExpander.getCustomerBucketExpiredSeconds()); + result.setAvatarObjectUrl(fileAuthUrl); + + // 放入缓存用户信息 + customerInfoCacheOperatorApi.put(customerIdKey, result, CustomerConfigExpander.getCustomerCacheExpiredSeconds()); + + return result; + } + /** * 获取信息 * @@ -319,4 +361,5 @@ public class CustomerServiceImpl extends ServiceImpl i } } + } \ No newline at end of file diff --git a/kernel-s-customer/customer-spring-boot-starter/pom.xml b/kernel-s-customer/customer-spring-boot-starter/pom.xml index 5566a10fb..0bb6e008e 100644 --- a/kernel-s-customer/customer-spring-boot-starter/pom.xml +++ b/kernel-s-customer/customer-spring-boot-starter/pom.xml @@ -24,6 +24,13 @@ ${roses.version} + + + cn.stylefeng.roses + cache-sdk-memory + ${roses.version} + + diff --git a/kernel-s-customer/customer-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/customer/starter/GunsCustomerAutoConfiguration.java b/kernel-s-customer/customer-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/customer/starter/GunsCustomerAutoConfiguration.java index 72ffda56e..69da9f1ba 100644 --- a/kernel-s-customer/customer-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/customer/starter/GunsCustomerAutoConfiguration.java +++ b/kernel-s-customer/customer-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/customer/starter/GunsCustomerAutoConfiguration.java @@ -24,6 +24,14 @@ */ package cn.stylefeng.roses.kernel.customer.starter; +import cn.hutool.cache.CacheUtil; +import cn.hutool.cache.impl.TimedCache; +import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; +import cn.stylefeng.roses.kernel.customer.api.expander.CustomerConfigExpander; +import cn.stylefeng.roses.kernel.customer.api.pojo.CustomerInfo; +import cn.stylefeng.roses.kernel.customer.modular.cache.CustomerMemoryCache; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** @@ -35,4 +43,17 @@ import org.springframework.context.annotation.Configuration; @Configuration public class GunsCustomerAutoConfiguration { + /** + * C端用户的缓存 + * + * @author fengshuonan + * @date 2021/6/8 22:41 + */ + @Bean + @ConditionalOnMissingBean(name = "customerInfoCacheOperatorApi") + public CacheOperatorApi customerInfoCacheOperatorApi() { + TimedCache customerInfoTimedCache = CacheUtil.newTimedCache(CustomerConfigExpander.getCustomerCacheExpiredSeconds() * 1000); + return new CustomerMemoryCache(customerInfoTimedCache); + } + }