WalletService.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Module\App\Service;
  3. use App\Module\Fund\Enums\FUND_TYPE;
  4. use App\Module\Fund\Service\User;
  5. use App\Module\Ulogic\Model\WalletAddress;
  6. class WalletService
  7. {
  8. // 账户统计
  9. public static function stats($userId)
  10. {
  11. $fundIds = [FUND_TYPE::FUND1->value(), FUND_TYPE::USD->value(), FUND_TYPE::BNB->value()];
  12. $data = User::getAllAccount($userId, $fundIds);
  13. $data = array_column($data, 'balance', 'fund_id');
  14. $address = WalletAddress::getInfoByCondition('user_id', $userId);
  15. // 老用户没有默认地址,需要创建
  16. if (!$address) {
  17. // 生成uraus地址
  18. $addStr = WalletAddress::createAddress();
  19. $walletAddress = '0xuraus' . $addStr;
  20. WalletAddress::createRow($userId, $walletAddress);
  21. $address = WalletAddress::getInfoByCondition('user_id', $userId);
  22. }
  23. $result = [
  24. 'uraus' => $data[FUND_TYPE::FUND1->value()] ?? 0,
  25. 'usdt' => $data[FUND_TYPE::USD->value()] ?? 0,
  26. 'bnb' => $data[FUND_TYPE::BNB->value()] ?? 0,
  27. 'address' => $address->address
  28. ];
  29. return $result;
  30. }
  31. /**
  32. * @param $userId
  33. * @param $type
  34. * @return array|false
  35. * @throws \Exception
  36. * 资金详情
  37. */
  38. public static function walletDetail($userId, $type)
  39. {
  40. // proto枚举
  41. $map = [
  42. 1 => FUND_TYPE::FUND1->value(),
  43. 2 => FUND_TYPE::USD->value(),
  44. 3 => FUND_TYPE::BNB->value()
  45. ];
  46. $fundId = $map[$type];
  47. $fundData = User::get_account($userId, $fundId, true);
  48. $address = WalletAddress::getInfoByCondition('user_id', $userId);
  49. return [
  50. 'type' => $type,
  51. 'balance' => $fundData->balance,
  52. 'address' => $address->address,
  53. ];
  54. }
  55. }