This commit is contained in:
ronger 2021-02-01 18:12:13 +08:00
parent f6e00e61ce
commit f43689a7e9
9 changed files with 190 additions and 44 deletions

View File

@ -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<Role> 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 "";
}
}

View File

@ -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";
/**
* 验证码错误

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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<T> implements Service<T> {
@Autowired
protected Mapper<T> mapper;
private Class<T> modelClass; // 当前泛型真实类型的Class
/**
* 当前泛型真实类型的Class
*/
private Class<T> modelClass;
public AbstractService() {
ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();

View File

@ -8,16 +8,85 @@ import java.util.List;
/**
* Service 基础接口其他Service 接口 请继承该接口
*
* @author ronger
*/
public interface Service<T> {
void save(T model);//持久化
void save(List<T> models);//批量持久化
void deleteById(String id);//通过主鍵刪除
void deleteByIds(String ids);//批量刪除 egids -> 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<T> findByIds(String ids);//通过多个ID查找//egids -> 1,2,3,4
List<T> findByCondition(Condition condition);//根据条件查找
List<T> findAll();//获取所有
/**
* 持久化
*
* @param model
*/
void save(T model);
/**
* 批量持久化
*
* @param models
*/
void save(List<T> models);
/**
* 通过主鍵刪除
*
* @param id
*/
void deleteById(String id);
/**
* 批量刪除 egids -> 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查找//egids -> 1,2,3,4
*
* @param ids
* @return
*/
List<T> findByIds(String ids);
/**
* 根据条件查找
*
* @param condition
* @return
*/
List<T> findByCondition(Condition condition);
/**
* 获取所有
*
* @return
*/
List<T> findAll();
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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<Sponsor> 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<Sponsor> 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());