1
zyy
2 days ago a8e2dbcd82859a3f972192c60b95cba4defe1738
trading-order-admin/src/main/java/com/yami/trading/api/controller/ApiUserController.java
@@ -9,6 +9,7 @@
import com.yami.trading.bean.model.RealNameAuthRecord;
import com.yami.trading.bean.model.User;
import com.yami.trading.bean.model.UserRecom;
import com.yami.trading.bean.model.UserSimRelation;
import com.yami.trading.bean.model.UserSafewordApply;
import com.yami.trading.bean.syspara.domain.Syspara;
import com.yami.trading.common.constants.Constants;
@@ -41,6 +42,8 @@
import com.yami.trading.service.user.UserRecomService;
import com.yami.trading.service.user.UserSafewordApplyService;
import com.yami.trading.service.user.UserService;
import com.yami.trading.service.user.UserSimRelationService;
import com.yami.trading.service.WalletService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
@@ -94,6 +97,10 @@
    @Autowired
    TokenStore tokenStore;
    @Autowired
    UserSimRelationService userSimRelationService;
    @Autowired
    WalletService walletService;
    @Autowired
    LogService logService;
    @Autowired
    QRGenerateService qrGenerateService;
@@ -136,7 +143,12 @@
        userInfoInToken.setEnabled(secUser.getStatus() == 1);
        secUser.setUserLastip(IPHelper.getIpAddr());
        secUser.setUserLasttime(now);
        // 登录时清除主账户与模拟账户的旧 token(若有关联)
        tokenStore.deleteAllToken(String.valueOf(SysTypeEnum.ORDINARY.value()), String.valueOf(secUser.getUserId()));
        String simUserId = userSimRelationService.getSimUserId(secUser.getUserId());
        if (simUserId != null) {
            tokenStore.deleteAllToken(String.valueOf(SysTypeEnum.ORDINARY.value()), simUserId);
        }
        // 存储token返回vo
        TokenInfoVO tokenInfoVO = tokenStore.storeAndGetVo(userInfoInToken);
@@ -146,6 +158,9 @@
        data.put("token", tokenInfoVO.getAccessToken());
        data.put("username", secUser.getUserName());
        data.put("usercode", secUser.getUserCode());
        data.put("accountType", secUser.getAccountType() != null ? secUser.getAccountType() : 0);
        data.put("mainUserId", userSimRelationService.getMainUserId(secUser.getUserId()));
        data.put("simUserId", simUserId);
        Log log = new Log();
        log.setCategory(Constants.LOG_CATEGORY_SECURITY);
        log.setLog("用户登录,ip[" + IPHelper.getIpAddr() + "]");
@@ -158,6 +173,83 @@
        userService.updateById(secUser);
        return Result.succeed(data);
    }
    @GetMapping("switchAccount")
    @ApiOperation("切换主账户/模拟账户")
    public Result switchAccount() {
        String currentUserId = SecurityUtils.getUser().getUserId();
        User currentUser = userService.getById(currentUserId);
        if (currentUser == null) {
            throw new YamiShopBindException("用户不存在");
        }
        Integer accountType = currentUser.getAccountType() != null ? currentUser.getAccountType() : 0;
        String targetUserId;
        Integer targetAccountType;
        if (accountType == 1) {
            // 当前是模拟账户,切换到主账户
            UserSimRelation relation = userSimRelationService.findBySimUserId(currentUserId);
            if (relation == null) {
                throw new YamiShopBindException("未找到关联的主账户");
            }
            targetUserId = relation.getMainUserId();
            targetAccountType = 0;
        } else {
            // 当前是主账户,切换到模拟账户:没有则先创建,再切换
            String simId = userSimRelationService.getSimUserId(currentUserId);
            if (simId == null) {
                userService.createSimAccountIfAbsent(currentUserId);
                simId = userSimRelationService.getSimUserId(currentUserId);
            }
            if (simId == null) {
                throw new YamiShopBindException("创建模拟账户失败");
            }
            targetUserId = simId;
            targetAccountType = 1;
        }
        User targetUser = userService.getById(targetUserId);
        if (targetUser == null || targetUser.getStatus() != 1) {
            throw new YamiShopBindException("目标账户不可用");
        }
        tokenStore.deleteAllToken(String.valueOf(SysTypeEnum.ORDINARY.value()), currentUserId);
        tokenStore.deleteAllToken(String.valueOf(SysTypeEnum.ORDINARY.value()), targetUserId);
        UserInfoInTokenBO userInfoInToken = new UserInfoInTokenBO();
        userInfoInToken.setUserId(targetUserId);
        userInfoInToken.setSysType(SysTypeEnum.ORDINARY.value());
        userInfoInToken.setEnabled(targetUser.getStatus() == 1);
        TokenInfoVO tokenInfoVO = tokenStore.storeAndGetVo(userInfoInToken);
        tokenInfoVO.setToken(tokenInfoVO.getAccessToken());
        userService.online(targetUserId);
        Map<String, Object> data = new HashMap<>();
        data.put("token", tokenInfoVO.getAccessToken());
        data.put("userId", targetUserId);
        data.put("accountType", targetAccountType);
        data.put("username", targetUser.getUserName());
        data.put("usercode", targetUser.getUserCode());
        String mainId = userSimRelationService.getMainUserId(targetUserId);
        data.put("mainUserId", mainId);
        data.put("simUserId", targetAccountType == 0 ? userSimRelationService.getSimUserId(targetUserId) : targetUserId);
        return Result.succeed(data);
    }
    @PostMapping("resetSimFunds")
    @ApiOperation("重置模拟账户资金(仅模拟账户可用)")
    public Result resetSimFunds() {
        String userId = SecurityUtils.getUser().getUserId();
        User user = userService.getById(userId);
        if (user == null || user.getAccountType() == null || user.getAccountType() != 1) {
            throw new YamiShopBindException("仅模拟账户可重置资金");
        }
        double amount = 100000;
        Syspara virtualGift = sysparaService.find("virtual_register_gift_coin");
        if (virtualGift != null) {
            amount = virtualGift.getDouble();
        }
        walletService.resetSimWallet(userId, amount);
        Map<String, Object> data = new HashMap<>();
        data.put("message", "重置成功");
        data.put("balance", amount);
        return Result.succeed(data);
    }
@@ -200,12 +292,12 @@
        if (!StringUtils.isNullOrEmpty(error)) {
            throw new YamiShopBindException(error);
        }
        if (StringUtils.isEmptyString(safeword)) {
            throw new YamiShopBindException("资金密码不能为空");
        }
        if (safeword.length() != 6 || !Strings.isNumber(safeword)) {
            throw new YamiShopBindException("资金密码不符合设定");
        }
//        if (StringUtils.isEmptyString(safeword)) {
//            throw new YamiShopBindException("资金密码不能为空");
//        }
//        if (safeword.length() != 6 || !Strings.isNumber(safeword)) {
//            throw new YamiShopBindException("资金密码不符合设定");
//        }
        userService.saveRegister(username, password, usercode, safeword, verifcode, type);
        User secUser = userService.findByUserName(username);
        Log log = new Log();
@@ -483,6 +575,7 @@
        // 如:级别11表示:新注册的前端显示为VIP1;
        map.put("user_level", (int) (party.getUserLevel() % 10));
        map.put("user_level_custom", (int) Math.floor(party.getUserLevel() / 10));
        map.put("credit_score", party.getCreditScore() != null ? party.getCreditScore() : 100);
        map.put("username", party.getUserName());
        map.put("userrole", party.getRoleName());
        map.put("usercode", party.getUserCode());
@@ -498,7 +591,7 @@
        map.put("lastloginip", party.getUserLastip());
        // 实名认证通过返回真实姓名
        if (party.isRealNameAuthority()) {
            map.put("name", kyc.getName());
            map.put("name", kyc != null ? kyc.getName() : party.getUserName());
        }
        if (null != kyc) {
            map.put("nationality", kyc.getNationality());