From f43689a7e9dc42fbc2a1edbff7b14e427560006b Mon Sep 17 00:00:00 2001 From: ronger Date: Mon, 1 Feb 2021 18:12:13 +0800 Subject: [PATCH] :bug: https://github.com/rymcu/forest/issues/30 --- .../rymcu/forest/config/BaseShiroRealm.java | 19 ++-- .../forest/core/constant/ShiroConstants.java | 8 +- .../core/exception/TransactionException.java | 35 ++++++++ .../rymcu/forest/core/result/ResultCode.java | 24 ++--- .../forest/core/service/AbstractService.java | 9 +- .../rymcu/forest/core/service/Service.java | 89 ++++++++++++++++--- .../forest/enumerate/TransactionCode.java | 28 ++++++ ...{SponsorEnum.java => TransactionEnum.java} | 12 +-- .../service/impl/SponsorServiceImpl.java | 10 ++- 9 files changed, 190 insertions(+), 44 deletions(-) create mode 100644 src/main/java/com/rymcu/forest/core/exception/TransactionException.java create mode 100644 src/main/java/com/rymcu/forest/enumerate/TransactionCode.java rename src/main/java/com/rymcu/forest/enumerate/{SponsorEnum.java => TransactionEnum.java} (57%) diff --git a/src/main/java/com/rymcu/forest/config/BaseShiroRealm.java b/src/main/java/com/rymcu/forest/config/BaseShiroRealm.java index 46aa766..bdd6323 100644 --- a/src/main/java/com/rymcu/forest/config/BaseShiroRealm.java +++ b/src/main/java/com/rymcu/forest/config/BaseShiroRealm.java @@ -29,7 +29,7 @@ import java.util.List; * @author ronger * @since 2018/05/28 11:00 * 自定义权限匹配和账号密码匹配 - * */ + */ public class BaseShiroRealm extends AuthorizingRealm { @Resource private RoleService roleService; @@ -43,13 +43,13 @@ public class BaseShiroRealm extends AuthorizingRealm { @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); - Principal principal = (Principal)principals.getPrimaryPrincipal(); + Principal principal = (Principal) principals.getPrimaryPrincipal(); User user = new User(); user.setIdUser(principal.getId()); try { List roles = roleService.selectRoleByUser(user); for (Role role : roles) { - if(StringUtils.isNotBlank(role.getInputCode())){ + if (StringUtils.isNotBlank(role.getInputCode())) { authorizationInfo.addRole(role.getInputCode()); } } @@ -70,7 +70,7 @@ public class BaseShiroRealm extends AuthorizingRealm { /** * 认证回调函数, 登录时调用,主要是用来进行身份认证的,也就是说验证用户输入的账号和密码是否正确。 - * */ + */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) authcToken; @@ -90,10 +90,11 @@ public class BaseShiroRealm extends AuthorizingRealm { if (user == null) { return null; } - if (!"0".equals(user.getStatus())) { //账户冻结(是否允许登陆) + // 账户冻结(是否允许登陆) + if (!"0".equals(user.getStatus())) { throw new LockedAccountException(); } - byte[] salt = Encodes.decodeHex(user.getPassword().substring(0,16)); + byte[] salt = Encodes.decodeHex(user.getPassword().substring(0, 16)); return new SimpleAuthenticationInfo(new Principal(user, token.isMobileLogin()), user.getPassword().substring(16), ByteSource.Util.bytes(salt), getName()); } @@ -101,7 +102,7 @@ public class BaseShiroRealm extends AuthorizingRealm { /** * 授权用户信息 */ - public static class Principal implements Serializable { + public static class Principal implements Serializable { private static final long serialVersionUID = 1L; @@ -139,9 +140,9 @@ public class BaseShiroRealm extends AuthorizingRealm { * 获取SESSIONID */ public String getSessionid() { - try{ + try { return (String) Utils.getSession().getId(); - }catch (Exception e) { + } catch (Exception e) { return ""; } } diff --git a/src/main/java/com/rymcu/forest/core/constant/ShiroConstants.java b/src/main/java/com/rymcu/forest/core/constant/ShiroConstants.java index 8b7476d..1c5d116 100644 --- a/src/main/java/com/rymcu/forest/core/constant/ShiroConstants.java +++ b/src/main/java/com/rymcu/forest/core/constant/ShiroConstants.java @@ -2,10 +2,10 @@ package com.rymcu.forest.core.constant; /** * Shiro通用常量 - * + * + * @author ronger */ -public interface ShiroConstants -{ +public interface ShiroConstants { /** * 当前登录的用户 */ @@ -54,7 +54,7 @@ public interface ShiroConstants /** * 验证码 */ - public static final String CURRENT_VALIDATECODE = "validateCode"; + public static final String CURRENT_VALIDATE_CODE = "validateCode"; /** * 验证码错误 diff --git a/src/main/java/com/rymcu/forest/core/exception/TransactionException.java b/src/main/java/com/rymcu/forest/core/exception/TransactionException.java new file mode 100644 index 0000000..16e66b2 --- /dev/null +++ b/src/main/java/com/rymcu/forest/core/exception/TransactionException.java @@ -0,0 +1,35 @@ +package com.rymcu.forest.core.exception; + +import com.rymcu.forest.enumerate.TransactionCode; + +/** + * @author ronger + */ +public class TransactionException extends Exception { + + private int code; + + private String message; + + public TransactionException(TransactionCode transactionCode) { + super(transactionCode.getMessage()); + this.code = transactionCode.getCode(); + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + @Override + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/main/java/com/rymcu/forest/core/result/ResultCode.java b/src/main/java/com/rymcu/forest/core/result/ResultCode.java index c0f894e..40c43fe 100644 --- a/src/main/java/com/rymcu/forest/core/result/ResultCode.java +++ b/src/main/java/com/rymcu/forest/core/result/ResultCode.java @@ -2,18 +2,22 @@ package com.rymcu.forest.core.result; /** * 响应码枚举,参考HTTP状态码的语义 + * + * @author ronger */ public enum ResultCode { - SUCCESS(1, "SUCCESS"),//成功 - FAIL(400, "访问失败"),//失败 - UNAUTHORIZED(401, "签名错误"),//未认证(签名错误) - NOT_FOUND(404, "此接口不存在"),//接口不存在 - INTERNAL_SERVER_ERROR(500, "系统繁忙,请稍后再试"),//服务器内部错误 - INVALID_PARAM(10000, "参数错误"), - - - - ; + // 成功 + SUCCESS(1, "SUCCESS"), + // 失败 + FAIL(400, "访问失败"), + // 未认证(签名错误) + UNAUTHORIZED(401, "签名错误"), + // 接口不存在 + NOT_FOUND(404, "此接口不存在"), + // 服务器内部错误 + INTERNAL_SERVER_ERROR(500, "系统繁忙,请稍后再试"), + // 参数错误 + INVALID_PARAM(10000, "参数错误"); private int code; private String message; diff --git a/src/main/java/com/rymcu/forest/core/service/AbstractService.java b/src/main/java/com/rymcu/forest/core/service/AbstractService.java index a1e2eb2..e973449 100644 --- a/src/main/java/com/rymcu/forest/core/service/AbstractService.java +++ b/src/main/java/com/rymcu/forest/core/service/AbstractService.java @@ -7,19 +7,24 @@ import org.apache.ibatis.exceptions.TooManyResultsException; import org.springframework.beans.factory.annotation.Autowired; import tk.mybatis.mapper.entity.Condition; +import javax.annotation.Resource; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.util.List; /** * 基于通用MyBatis Mapper插件的Service接口的实现 + * + * @author ronger */ public abstract class AbstractService implements Service { @Autowired protected Mapper mapper; - - private Class modelClass; // 当前泛型真实类型的Class + /** + * 当前泛型真实类型的Class + */ + private Class modelClass; public AbstractService() { ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); diff --git a/src/main/java/com/rymcu/forest/core/service/Service.java b/src/main/java/com/rymcu/forest/core/service/Service.java index bf69a13..b755c14 100644 --- a/src/main/java/com/rymcu/forest/core/service/Service.java +++ b/src/main/java/com/rymcu/forest/core/service/Service.java @@ -8,16 +8,85 @@ import java.util.List; /** * Service 层 基础接口,其他Service 接口 请继承该接口 + * + * @author ronger */ public interface Service { - void save(T model);//持久化 - void save(List models);//批量持久化 - void deleteById(String id);//通过主鍵刪除 - void deleteByIds(String ids);//批量刪除 eg:ids -> “1,2,3,4” - void update(T model);//更新 - T findById(String id);//通过ID查找 - T findBy(String fieldName, Object value) throws TooManyResultsException, ServiceException; //通过Model中某个成员变量名称(非数据表中column的名称)查找,value需符合unique约束 - List findByIds(String ids);//通过多个ID查找//eg:ids -> “1,2,3,4” - List findByCondition(Condition condition);//根据条件查找 - List findAll();//获取所有 + /** + * 持久化 + * + * @param model + */ + void save(T model); + + /** + * 批量持久化 + * + * @param models + */ + void save(List models); + + /** + * 通过主鍵刪除 + * + * @param id + */ + void deleteById(String id); + + /** + * 批量刪除 eg:ids -> “1,2,3,4” + * + * @param ids + */ + void deleteByIds(String ids); + + /** + * 更新 + * + * @param model + */ + void update(T model); + + /** + * 通过ID查找 + * + * @param id + * @return + */ + T findById(String id); + + /** + * 通过Model中某个成员变量名称(非数据表中column的名称)查找,value需符合unique约束 + * + * @param fieldName + * @param value + * @return + * @throws TooManyResultsException + * @throws ServiceException + */ + T findBy(String fieldName, Object value) throws TooManyResultsException, ServiceException; + + + /** + * 通过多个ID查找//eg:ids -> “1,2,3,4” + * + * @param ids + * @return + */ + List findByIds(String ids); + + /** + * 根据条件查找 + * + * @param condition + * @return + */ + List findByCondition(Condition condition); + + /** + * 获取所有 + * + * @return + */ + List findAll(); } diff --git a/src/main/java/com/rymcu/forest/enumerate/TransactionCode.java b/src/main/java/com/rymcu/forest/enumerate/TransactionCode.java new file mode 100644 index 0000000..233331a --- /dev/null +++ b/src/main/java/com/rymcu/forest/enumerate/TransactionCode.java @@ -0,0 +1,28 @@ +package com.rymcu.forest.enumerate; + +/** + * @author ronger + */ + +public enum TransactionCode { + + InsufficientBalance(901, "余额不足"); + + private int code; + + private String message; + + TransactionCode(int code, String message) { + this.code = code; + this.message = message; + } + + + public String getMessage() { + return this.message; + } + + public int getCode() { + return this.code; + } +} diff --git a/src/main/java/com/rymcu/forest/enumerate/SponsorEnum.java b/src/main/java/com/rymcu/forest/enumerate/TransactionEnum.java similarity index 57% rename from src/main/java/com/rymcu/forest/enumerate/SponsorEnum.java rename to src/main/java/com/rymcu/forest/enumerate/TransactionEnum.java index 7efc1e7..2eb7202 100644 --- a/src/main/java/com/rymcu/forest/enumerate/SponsorEnum.java +++ b/src/main/java/com/rymcu/forest/enumerate/TransactionEnum.java @@ -4,14 +4,16 @@ package com.rymcu.forest.enumerate; * @author ronger */ -public enum SponsorEnum { - Article("0", 20); +public enum TransactionEnum { + ArticleSponsor("0", 20), + Answer("1", 30), + CorrectAnswer("2", 50); private String dataType; private Integer money; - SponsorEnum(String dataType, Integer money) { + TransactionEnum(String dataType, Integer money) { this.dataType = dataType; this.money = money; } @@ -24,7 +26,7 @@ public enum SponsorEnum { return this.money; } - public boolean isArticle() { - return Article.equals(this); + public boolean isArticleSponsor() { + return ArticleSponsor.equals(this); } } diff --git a/src/main/java/com/rymcu/forest/service/impl/SponsorServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/SponsorServiceImpl.java index 8b1e589..9401c38 100644 --- a/src/main/java/com/rymcu/forest/service/impl/SponsorServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/SponsorServiceImpl.java @@ -1,11 +1,13 @@ package com.rymcu.forest.service.impl; +import com.rymcu.forest.core.exception.TransactionException; import com.rymcu.forest.core.service.AbstractService; import com.rymcu.forest.dto.ArticleDTO; import com.rymcu.forest.entity.Sponsor; import com.rymcu.forest.entity.TransactionRecord; import com.rymcu.forest.entity.User; -import com.rymcu.forest.enumerate.SponsorEnum; +import com.rymcu.forest.enumerate.TransactionCode; +import com.rymcu.forest.enumerate.TransactionEnum; import com.rymcu.forest.mapper.SponsorMapper; import com.rymcu.forest.service.ArticleService; import com.rymcu.forest.service.SponsorService; @@ -39,7 +41,7 @@ public class SponsorServiceImpl extends AbstractService implements Spon map.put("success", false); map.put("message", "数据异常"); } else { - SponsorEnum result = Arrays.stream(SponsorEnum.values()).filter(sponsorEnum -> sponsorEnum.getDataType().equals(sponsor.getDataType())).findFirst().orElse(SponsorEnum.Article); + TransactionEnum result = TransactionEnum.valueOf(sponsor.getDataType()); BigDecimal money = BigDecimal.valueOf(result.getMoney()); sponsor.setSponsorshipMoney(money); User user = UserUtils.getCurrentUserByToken(); @@ -47,11 +49,11 @@ public class SponsorServiceImpl extends AbstractService implements Spon sponsor.setSponsorshipTime(new Date()); sponsorMapper.insertSelective(sponsor); // 赞赏金额划转 - if (result.isArticle()) { + if (result.isArticleSponsor()) { ArticleDTO articleDTO = articleService.findArticleDTOById(sponsor.getDataId(), 1); TransactionRecord transactionRecord = transactionRecordService.transferByUserId(articleDTO.getArticleAuthorId(), user.getIdUser(), money); if (Objects.isNull(transactionRecord.getIdTransactionRecord())) { - throw new Exception("余额不足"); + throw new TransactionException(TransactionCode.InsufficientBalance); } // 更新文章赞赏数 sponsorMapper.updateArticleSponsorCount(articleDTO.getIdArticle());