fix登录,积分
This commit is contained in:
@@ -25,6 +25,9 @@ public class OpsController {
|
|||||||
*/
|
*/
|
||||||
@PostMapping("/points/add")
|
@PostMapping("/points/add")
|
||||||
public R<Void> addPoints(@RequestBody PointsDTO dto) {
|
public R<Void> addPoints(@RequestBody PointsDTO dto) {
|
||||||
|
if (!UserContext.isOps()) {
|
||||||
|
return R.fail("无权限:仅运营可操作");
|
||||||
|
}
|
||||||
Long operatorId = UserContext.getUserId();
|
Long operatorId = UserContext.getUserId();
|
||||||
pointsService.addPoints(dto.getUserId(), dto.getAmount(),
|
pointsService.addPoints(dto.getUserId(), dto.getAmount(),
|
||||||
IPointsService.TYPE_OPS_ADD, null, operatorId, dto.getRemark());
|
IPointsService.TYPE_OPS_ADD, null, operatorId, dto.getRemark());
|
||||||
@@ -36,17 +39,17 @@ public class OpsController {
|
|||||||
* 减积分
|
* 减积分
|
||||||
*/
|
*/
|
||||||
@PostMapping("/points/sub")
|
@PostMapping("/points/sub")
|
||||||
public R<Void> subPoints(@RequestBody PointsDTO dto) {
|
public R subPoints(@RequestBody PointsDTO dto) {
|
||||||
Long operatorId = UserContext.getUserId();
|
if (!UserContext.isOps()) {
|
||||||
boolean success = pointsService.subPoints(dto.getUserId(), dto.getAmount(),
|
return R.fail("无权限:仅运营可操作");
|
||||||
IPointsService.TYPE_OPS_SUB, null, operatorId, dto.getRemark());
|
|
||||||
if (success) {
|
|
||||||
return R.ok();
|
|
||||||
}
|
}
|
||||||
return R.fail("余额不足或用户不存在");
|
Long operatorId = UserContext.getUserId();
|
||||||
|
pointsService.subPoints(dto.getUserId(), dto.getAmount(),
|
||||||
|
IPointsService.TYPE_OPS_SUB, null, operatorId, dto.getRemark());
|
||||||
|
return R.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 拉入群
|
* 拉入群
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -7,7 +7,9 @@ import com.tailbet.model.vo.LoginUserVo;
|
|||||||
import com.tailbet.model.vo.LoginVo;
|
import com.tailbet.model.vo.LoginVo;
|
||||||
import com.tailbet.model.vo.R;
|
import com.tailbet.model.vo.R;
|
||||||
import com.tailbet.model.vo.UserContext;
|
import com.tailbet.model.vo.UserContext;
|
||||||
|
import com.tailbet.openim.OpenIMClient;
|
||||||
import com.tailbet.service.IUserService;
|
import com.tailbet.service.IUserService;
|
||||||
|
import com.tailbet.util.JwtUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@@ -20,6 +22,8 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
public class UserController {
|
public class UserController {
|
||||||
|
|
||||||
private final IUserService userService;
|
private final IUserService userService;
|
||||||
|
private final JwtUtil jwtUtil;
|
||||||
|
private final OpenIMClient openIMClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注册
|
* 注册
|
||||||
@@ -36,9 +40,13 @@ public class UserController {
|
|||||||
*/
|
*/
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
public R<LoginVo> login(@RequestBody LoginDTO dto) {
|
public R<LoginVo> login(@RequestBody LoginDTO dto) {
|
||||||
String token = userService.login(dto.getUsername(), dto.getPassword());
|
Long userId = userService.login(dto.getUsername(), dto.getPassword());
|
||||||
|
String token = jwtUtil.generateToken(userId, dto.getUsername());
|
||||||
|
String openIMToken = openIMClient.getUserToken(String.valueOf(userId));
|
||||||
|
|
||||||
LoginVo vo = new LoginVo();
|
LoginVo vo = new LoginVo();
|
||||||
vo.setToken(token);
|
vo.setToken(token);
|
||||||
|
vo.setOpenIMToken(openIMToken);
|
||||||
return R.ok("登录成功", vo);
|
return R.ok("登录成功", vo);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,4 +11,9 @@ public class LoginVo {
|
|||||||
* token
|
* token
|
||||||
*/
|
*/
|
||||||
private String token;
|
private String token;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OpenIM Token
|
||||||
|
*/
|
||||||
|
private String openIMToken;
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.tailbet.openim;
|
package com.tailbet.openim;
|
||||||
|
|
||||||
|
import cn.hutool.json.JSON;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
import com.tailbet.config.OpenIMConfig;
|
import com.tailbet.config.OpenIMConfig;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -149,4 +151,34 @@ public class OpenIMClient {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户Token
|
||||||
|
*/
|
||||||
|
public String getUserToken(String userId) {
|
||||||
|
String url = config.getApiUrl() + "/auth/user_token";
|
||||||
|
|
||||||
|
Map<String, Object> body = new HashMap<>();
|
||||||
|
body.put("userID", userId);
|
||||||
|
body.put("expireTimeSeconds", 604800); // 7天有效期
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||||
|
headers.set("token", config.getAdminToken());
|
||||||
|
headers.set("operationID", userId + System.currentTimeMillis());
|
||||||
|
|
||||||
|
HttpEntity<Map<String, Object>> request = new HttpEntity<>(body, headers);
|
||||||
|
|
||||||
|
try {
|
||||||
|
ResponseEntity<Map> response = restTemplate.postForEntity(url, request, Map.class);
|
||||||
|
log.info("getUserToken:{}", JSONUtil.toJsonStr(response.getBody()));
|
||||||
|
if (response.getBody() != null && response.getBody().get("data") != null) {
|
||||||
|
Map<String, Object> data = (Map<String, Object>) response.getBody().get("data");
|
||||||
|
return (String) data.get("token");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("获取OpenIM用户Token失败: userId={}, error={}", userId, e.getMessage());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ public interface IPointsService {
|
|||||||
/**
|
/**
|
||||||
* 扣除积分
|
* 扣除积分
|
||||||
*/
|
*/
|
||||||
boolean subPoints(Long userId, Long amount, String type, String bizNo, Long operatorId, String remark);
|
void subPoints(Long userId, Long amount, String type, String bizNo, Long operatorId, String remark);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取用户积分
|
* 获取用户积分
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ public interface IUserService extends IService<User> {
|
|||||||
/**
|
/**
|
||||||
* 用户登录
|
* 用户登录
|
||||||
*/
|
*/
|
||||||
String login(String username, String password);
|
Long login(String username, String password);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前登录用户
|
* 获取当前登录用户
|
||||||
|
|||||||
@@ -95,11 +95,9 @@ public class BetServiceImpl implements IBetService {
|
|||||||
BigDecimal odds = getOdds(playType);
|
BigDecimal odds = getOdds(playType);
|
||||||
|
|
||||||
// 扣减积分
|
// 扣减积分
|
||||||
boolean success = pointsService.subPoints(userId, betAmount.longValue(),
|
pointsService.subPoints(userId, betAmount.longValue(),
|
||||||
IPointsService.TYPE_BET, null, null, "游戏下注");
|
IPointsService.TYPE_BET, null, null, "游戏下注");
|
||||||
if (!success) {
|
|
||||||
throw new RuntimeException("余额不足");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建注单
|
// 创建注单
|
||||||
BetOrder bet = new BetOrder();
|
BetOrder bet = new BetOrder();
|
||||||
|
|||||||
@@ -62,13 +62,13 @@ public class PointsServiceImpl implements IPointsService {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public boolean subPoints(Long userId, Long amount, String type, String bizNo, Long operatorId, String remark) {
|
public void subPoints(Long userId, Long amount, String type, String bizNo, Long operatorId, String remark) {
|
||||||
User user = userMapper.selectById(userId);
|
User user = userMapper.selectById(userId);
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
throw new RuntimeException("用户不存在");
|
throw new RuntimeException("用户不存在");
|
||||||
}
|
}
|
||||||
if (user.getPoints() < amount) {
|
if (user.getPoints() < amount) {
|
||||||
return false;
|
throw new RuntimeException("余额不足");
|
||||||
}
|
}
|
||||||
|
|
||||||
Long before = user.getPoints();
|
Long before = user.getPoints();
|
||||||
@@ -76,7 +76,7 @@ public class PointsServiceImpl implements IPointsService {
|
|||||||
// 原子扣除积分(SQL层保证余额充足才扣)
|
// 原子扣除积分(SQL层保证余额充足才扣)
|
||||||
int rows = userMapper.subPoints(userId, amount);
|
int rows = userMapper.subPoints(userId, amount);
|
||||||
if (rows == 0) {
|
if (rows == 0) {
|
||||||
return false;
|
throw new RuntimeException("积分扣除失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
Long after = before - amount;
|
Long after = before - amount;
|
||||||
@@ -93,7 +93,6 @@ public class PointsServiceImpl implements IPointsService {
|
|||||||
flow.setRemark(remark);
|
flow.setRemark(remark);
|
||||||
pointsFlowMapper.insert(flow);
|
pointsFlowMapper.insert(flow);
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -61,11 +61,9 @@ public class RedPacketServiceImpl implements IRedPacketService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 扣积分
|
// 扣积分
|
||||||
boolean success = pointsService.subPoints(userId, totalAmount.longValue(),
|
pointsService.subPoints(userId, totalAmount.longValue(),
|
||||||
IPointsService.TYPE_RP_SEND, null, null, "发送红包");
|
IPointsService.TYPE_RP_SEND, null, null, "发送红包");
|
||||||
if (!success) {
|
|
||||||
throw new RuntimeException("余额不足");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建红包
|
// 创建红包
|
||||||
RedPacket rp = new RedPacket();
|
RedPacket rp = new RedPacket();
|
||||||
@@ -275,7 +273,8 @@ public class RedPacketServiceImpl implements IRedPacketService {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
List<BigDecimal> amounts = objectMapper.readValue(rp.getShareAmounts(),
|
List<BigDecimal> amounts = objectMapper.readValue(rp.getShareAmounts(),
|
||||||
new TypeReference<List<BigDecimal>>() {});
|
new TypeReference<List<BigDecimal>>() {
|
||||||
|
});
|
||||||
if (amounts.isEmpty()) {
|
if (amounts.isEmpty()) {
|
||||||
return calculateReceiveAmount(rp);
|
return calculateReceiveAmount(rp);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|||||||
* 用户登录
|
* 用户登录
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String login(String username, String password) {
|
public Long login(String username, String password) {
|
||||||
User user = getByUsername(username);
|
User user = getByUsername(username);
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
throw new RuntimeException("用户不存在");
|
throw new RuntimeException("用户不存在");
|
||||||
@@ -86,7 +86,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|||||||
if (!passwordUtil.matches(password, user.getPassword())) {
|
if (!passwordUtil.matches(password, user.getPassword())) {
|
||||||
throw new RuntimeException("密码错误");
|
throw new RuntimeException("密码错误");
|
||||||
}
|
}
|
||||||
return jwtUtil.generateToken(user.getId(), user.getUsername());
|
return user.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user