本项目实现了一个基于AOP的低耦合通用积分模块,支持论坛、商城、教育等多种业务场景的积分管理需求。通过注解驱动的方式,实现非侵入式积分功能集成。
@PointsEvent注解实现非侵入式积分功能执行database/points_table.sql文件创建积分模块所需表结构:
source database/points_table.sql
@Service
public class AuthService {
@PointsEvent(eventCode = "USER_REGISTER", sourceType = "AUTH")
public User register(RegisterRequest request) {
// 原有注册逻辑完全不变
User user = new User();
user.setUsername(request.getUsername());
user.setPassword(passwordUtils.encode(request.getPassword()));
return userService.createUser(user);
}
}
@Service
public class CommentService {
@PointsEvent(eventCode = "POST_COMMENT", sourceType = "COMMENT", sourceIdIndex = 0)
public Comment createComment(CreateCommentRequest request) {
// 原有评论逻辑完全不变
Comment comment = new Comment();
comment.setContent(request.getContent());
comment.setUserId(CurrentUserUtils.getCurrentUserId());
return commentMapper.insert(comment);
}
}
GET /api/points/records/my?page=1&size=10 Authorization: Bearer {token}
GET /api/points/summary/my Authorization: Bearer {token}
POST /api/points/demo/register Content-Type: application/json { "username": "testuser", "password": "password", "email": "test@example.com" }
@PointsEvent:标记需要触发积分事件的方法PointsAspect:拦截注解方法,自动处理积分逻辑PointsEventService:积分事件处理核心逻辑PointsRecordService:积分记录管理PointsRuleService:积分规则验证PointsSummaryService:用户积分汇总管理PointsEventConfig:积分事件配置PointsRecord:积分变动记录PointsRuleConfig:积分规则配置UserPointsSummary:用户积分汇总在application.yml中配置积分模块参数:
points:
async:
enabled: true
core-pool-size: 5
expiry:
check-enabled: true
check-cron: "0 0 2 * * ?"
rules:
daily-max-gain: 1000
single-max-gain: 500
积分事件配置表示例:
INSERT INTO points_event_config (event_code, event_name, points_value, event_type, description) VALUES
('USER_REGISTER', '用户注册', 100, 'GAIN', '新用户注册成功获得积分'),
('POST_ARTICLE', '发表文章', 20, 'GAIN', '发表文章获得积分'),
('REDEEM_COUPON', '兑换优惠券', -100, 'CONSUME', '使用积分兑换优惠券');
points_event_config表中添加事件配置@PointsEvent注解points_rule_config表中添加规则配置PointsRuleService中实现规则验证逻辑运行积分模块的单元测试:
mvn test -Dtest=PointsAspectTest
使用演示接口测试积分功能:
# 用户注册测试
curl -X POST http://localhost:8080/api/points/demo/register \
-H "Content-Type: application/json" \
-d '{"username":"testuser","password":"password"}'
# 查询积分信息
curl -X GET http://localhost:8080/api/points/demo/my-info \
-H "Authorization: Bearer {token}"
如有问题或建议,请联系开发团队或查看项目文档。