| | |
| | | package com.yami.trading.admin.controller; |
| | | |
| | | import com.yami.trading.admin.model.LoginModel; |
| | | import com.yami.trading.admin.model.UpdateAddressModel; |
| | | import com.yami.trading.admin.model.UpdateUserAddressModel; |
| | | import com.yami.trading.bean.model.ChannelBlockchain; |
| | | import com.yami.trading.bean.model.User; |
| | | import com.yami.trading.common.constants.RedisKeys; |
| | | import com.yami.trading.common.domain.Result; |
| | | import com.yami.trading.common.exception.YamiShopBindException; |
| | | import com.yami.trading.common.util.GoogleAuthenticator; |
| | | import com.yami.trading.security.common.bo.UserInfoInTokenBO; |
| | | import com.yami.trading.security.common.enums.CryptoCurrencyEnum; |
| | | import com.yami.trading.security.common.enums.SysTypeEnum; |
| | | import com.yami.trading.security.common.util.LocalKeyStorageAESUtil; |
| | | import com.yami.trading.security.common.vo.TokenInfoVO; |
| | | import com.yami.trading.sys.model.SysUser; |
| | | import com.yami.trading.service.ChannelBlockchainService; |
| | | import com.yami.trading.service.exchange.PartyBlockchainService; |
| | | import com.yami.trading.service.user.UserService; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.validation.Valid; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * @program: trading-order-master |
| | | * @description: 充值地址 |
| | | * @create: 2025-08-07 14:44 |
| | | **/ |
| | | * 管理后台 - 财务 - 充值地址(ChannelBlockchain 表) |
| | | */ |
| | | @Slf4j |
| | | @RestController |
| | | @RequestMapping("address") |
| | | @Api(tags = "充值地址") |
| | | public class AddressController { |
| | | |
| | | /** |
| | | * 地址列表 |
| | | * @return |
| | | */ |
| | | @Autowired |
| | | RedisTemplate redisTemplate; |
| | | |
| | | @Autowired |
| | | private ChannelBlockchainService channelBlockchainService; |
| | | |
| | | @Autowired |
| | | private PartyBlockchainService partyBlockchainService; |
| | | |
| | | @Autowired |
| | | private UserService userService; |
| | | |
| | | @ApiOperation("充值地址列表") |
| | | @PostMapping("/list") |
| | | public Result<?> list() { |
| | | public Result<List<ChannelBlockchain>> list() { |
| | | List<ChannelBlockchain> list = channelBlockchainService.list(); |
| | | list.forEach(this::fillBlockchainNameAlias); |
| | | return Result.succeed(list); |
| | | } |
| | | |
| | | @ApiOperation("修改充值地址") |
| | | @PostMapping("/update") |
| | | public Result<ChannelBlockchain> update(@RequestBody UpdateAddressModel model) { |
| | | if (StringUtils.isNotBlank(model.getUuid())) { |
| | | ChannelBlockchain existing = channelBlockchainService.getById(model.getUuid()); |
| | | if (existing == null) { |
| | | throw new YamiShopBindException("充值地址记录不存在"); |
| | | } |
| | | if (StringUtils.isBlank(model.getAddress())) { |
| | | throw new YamiShopBindException("充值地址不能为空"); |
| | | } |
| | | existing.setAddress(model.getAddress().trim()); |
| | | if (StringUtils.isNotBlank(model.getCoin())) { |
| | | existing.setCoin(model.getCoin().trim().toLowerCase()); |
| | | } |
| | | if (StringUtils.isNotBlank(model.getChain())) { |
| | | existing.setBlockchainName(model.getChain().trim()); |
| | | } |
| | | channelBlockchainService.updateById(existing); |
| | | fillBlockchainNameAlias(existing); |
| | | return Result.succeed(existing); |
| | | } |
| | | ChannelBlockchain saved = channelBlockchainService.saveOrUpdateByCoinAndChain( |
| | | model.getCoin(), model.getChain(), model.getAddress()); |
| | | fillBlockchainNameAlias(saved); |
| | | return Result.succeed(saved); |
| | | } |
| | | |
| | | /** |
| | | * u盾地址列表(按用户维度,仍走 Redis) |
| | | */ |
| | | @GetMapping("/getUDList") |
| | | public Result<List<ChannelBlockchain>> getUDList(String partyId) { |
| | | if (partyId == null || partyId.isEmpty()) { |
| | | return Result.failed("请选择用户"); |
| | | } |
| | | List<CryptoCurrencyEnum> currencyEnums = CryptoCurrencyEnum.getAll(); |
| | | List<ChannelBlockchain> data = new ArrayList<>(); |
| | | currencyEnums.forEach(f->{ |
| | | currencyEnums.forEach(currencyEnum -> { |
| | | try { |
| | | String address = LocalKeyStorageAESUtil.loadAndDecrypt(f.getName()); |
| | | String address = (String) redisTemplate.opsForValue() |
| | | .get(RedisKeys.BLOCKCHAIN_ADDRESS + partyId + currencyEnum.getName()); |
| | | ChannelBlockchain blockchain = new ChannelBlockchain(); |
| | | blockchain.setBlockchain_name(f.getChain()); |
| | | blockchain.setBlockchain_name(currencyEnum.getChain()); |
| | | blockchain.setBlockchainName(currencyEnum.getChain()); |
| | | blockchain.setAddress(address); |
| | | blockchain.setCoin(f.getCoin()); |
| | | blockchain.setCoin(currencyEnum.getCoin()); |
| | | blockchain.setAuto(false); |
| | | blockchain.setImg(null); |
| | | data.add(blockchain); |
| | | } catch (Exception e) { |
| | | e.getMessage(); |
| | | log.error(e.getMessage()); |
| | | } |
| | | }); |
| | | return Result.succeed(data); |
| | | } |
| | | |
| | | @PostMapping("/update") |
| | | public Result<?> list(@RequestBody UpdateAddressModel model) { |
| | | String name = model.getCoin()+"_"+model.getChain(); |
| | | try { |
| | | if(model.getCoin().equals("eth") || model.getCoin().equals("btc")){ |
| | | name = model.getCoin(); |
| | | } |
| | | return LocalKeyStorageAESUtil.encryptAndStore(model.getPassword(),model.getAddress(),name); |
| | | }catch (Exception e){ |
| | | e.getMessage(); |
| | | @GetMapping("/updateUserAddress") |
| | | public Result<?> updateUserAddress(UpdateUserAddressModel model) { |
| | | if (StringUtils.isBlank(model.getUserCode())) { |
| | | throw new YamiShopBindException("用户编码不能为空"); |
| | | } |
| | | return Result.succeed(); |
| | | User user = userService.findUserByUserCode(model.getUserCode()); |
| | | if (user == null) { |
| | | throw new YamiShopBindException("用户不存在"); |
| | | } |
| | | String userName = user.getUserName(); |
| | | String partyId = user.getUserId(); |
| | | saveUserAddress(userName, partyId, "usdt", "TRC20", model.getUsdtTrc20()); |
| | | saveUserAddress(userName, partyId, "usdt", "ERC20", model.getUsdtErc20()); |
| | | saveUserAddress(userName, partyId, "btc", "BTC", model.getBtc()); |
| | | saveUserAddress(userName, partyId, "eth", "ETH", model.getEth()); |
| | | saveUserAddress(userName, partyId, "usdc", "ERC20", model.getUsdcErc20()); |
| | | saveUserAddress(userName, partyId, "usdc", "TRC20", model.getUsdcTrc20()); |
| | | return Result.succeed(null); |
| | | } |
| | | |
| | | private void saveUserAddress(String userName, String partyId, String coin, String chain, String address) { |
| | | if (StringUtils.isBlank(address)) { |
| | | return; |
| | | } |
| | | ChannelBlockchain template = findTemplate(coin, chain); |
| | | String chainName = template != null ? template.getBlockchainName() : chain.toLowerCase(); |
| | | String qrImage = template != null ? template.getImg() : null; |
| | | partyBlockchainService.saveOrUpdateAddress(userName, coin, chainName, address.trim(), qrImage); |
| | | CryptoCurrencyEnum currencyEnum = findCurrencyEnum(coin, chain); |
| | | if (currencyEnum != null) { |
| | | redisTemplate.opsForValue().set(RedisKeys.BLOCKCHAIN_ADDRESS + partyId + currencyEnum.getName(), address.trim()); |
| | | } |
| | | } |
| | | |
| | | private CryptoCurrencyEnum findCurrencyEnum(String coin, String chain) { |
| | | for (CryptoCurrencyEnum currencyEnum : CryptoCurrencyEnum.getAll()) { |
| | | if (currencyEnum.getCoin().equalsIgnoreCase(coin) && currencyEnum.getChain().equalsIgnoreCase(chain)) { |
| | | return currencyEnum; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private ChannelBlockchain findTemplate(String coin, String chain) { |
| | | List<ChannelBlockchain> list = channelBlockchainService.findByCoin(coin.toLowerCase()); |
| | | return list.stream() |
| | | .filter(item -> chain.equalsIgnoreCase(item.getBlockchainName())) |
| | | .findFirst() |
| | | .orElse(null); |
| | | } |
| | | |
| | | private void fillBlockchainNameAlias(ChannelBlockchain item) { |
| | | if (item != null && StringUtils.isNotBlank(item.getBlockchainName())) { |
| | | item.setBlockchain_name(item.getBlockchainName()); |
| | | } |
| | | } |
| | | } |